Parse non-strings in lists in maps (#39786)

* Parse non-strings in lists in maps

* Fix 'listening' var
This commit is contained in:
Tad Hardesty
2018-08-24 12:39:05 +03:00
committed by vuonojenmustaturska
parent 4a10d41ba7
commit 5ffd85bf91
2 changed files with 54 additions and 34 deletions
+42 -32
View File
@@ -409,57 +409,67 @@
//build a list from variables in text form (e.g {var1="derp"; var2; var3=7} => list(var1="derp", var2, var3=7))
//return the filled list
/datum/parsed_map/proc/readlist(text as text, delimiter=",")
var/list/to_return = list()
. = list()
if (!text)
return
var/position
var/old_position = 1
do
//find next delimiter that is not within "..."
while(position != 0)
// find next delimiter that is not within "..."
position = find_next_delimiter_position(text,old_position,delimiter)
//check if this is a simple variable (as in list(var1, var2)) or an associative one (as in list(var1="foo",var2=7))
// check if this is a simple variable (as in list(var1, var2)) or an associative one (as in list(var1="foo",var2=7))
var/equal_position = findtext(text,"=",old_position, position)
var/trim_left = trim_text(copytext(text,old_position,(equal_position ? equal_position : position)),1)//the name of the variable, must trim quotes to build a BYOND compliant associatives list
var/trim_left = trim_text(copytext(text,old_position,(equal_position ? equal_position : position)))
var/left_constant = delimiter == ";" ? trim_left : parse_constant(trim_left)
old_position = position + 1
if(equal_position)//associative var, so do the association
var/trim_right = trim_text(copytext(text,equal_position+1,position))//the content of the variable
if(equal_position && !isnum(left_constant))
// Associative var, so do the association.
// Note that numbers cannot be keys - the RHS is dropped if so.
var/trim_right = trim_text(copytext(text,equal_position+1,position))
var/right_constant = parse_constant(trim_right)
.[left_constant] = right_constant
//Check for string
if(findtext(trim_right,"\"",1,2))
trim_right = copytext(trim_right,2,findtext(trim_right,"\"",3,0))
else // simple var
. += list(left_constant)
//Check for number
else if(isnum(text2num(trim_right)))
trim_right = text2num(trim_right)
/datum/parsed_map/proc/parse_constant(text)
// number
var/num = text2num(text)
if(isnum(num))
return num
//Check for null
else if(trim_right == "null")
trim_right = null
// string
if(findtext(text,"\"",1,2))
return copytext(text,2,findtext(text,"\"",3,0))
//Check for list
else if(copytext(trim_right,1,5) == "list")
trim_right = readlist(copytext(trim_right,6,length(trim_right)))
// list
if(copytext(text,1,6) == "list(")
return readlist(copytext(text,6,length(text)))
//Check for file
else if(copytext(trim_right,1,2) == "'")
trim_right = file(copytext(trim_right,2,length(trim_right)))
// typepath
var/path = text2path(text)
if(ispath(path))
return path
//Check for path
else if(ispath(text2path(trim_right)))
trim_right = text2path(trim_right)
// file
if(copytext(text,1,2) == "'")
return file(copytext(text,2,length(text)))
to_return[trim_left] = trim_right
// null
if(text == "null")
return null
else//simple var
to_return[trim_left] = null
// not parsed:
// - pops: /obj{name="foo"}
// - new(), newlist(), icon(), matrix(), sound()
while(position != 0)
return to_return
// fallback: string
return text
/datum/parsed_map/Destroy()
..()