I am writing a small scripting engine (as part of a game engine), part of which requires the parsing of a string to get a certain object. I already have regexes to identify what the string in question is (string, variable, map or array/list), but string concatenation is where I'm stuck. It's not required, I guess i could write a concat()
, but I would rather the parse
method recognize string + string
.
This would be simple if all arguments were strings or variables (e.g. 'i like ' + 'pie'
or variable + 'string'
or variable + variable
), however I want to provide data types like lists/arrays and maps. Ideally, the parse method would be able to split any of the following lines by ignoring certain plus signs:
print parse("{var='value ' + ''}")
print parse("['value', x, x + y] + ' a string'")
print parse("function('arg ', 'another arg ' + 'concatenated')")
In the first print
, the +
is ignored completely because it is between unquoted curly braces
In the second print, the first +
is ignored because it is between unquoted square brackets, but the second is not.
In the third print, similar to the first, the +
is ignored because it is inside unquoted parentheses
EDIT: In case I wasn't really clear, the regex is only being used to split the text into a string array
I know that this answer is somewhat similar, and is a great start. But I've been unable to alter it to also ignore +
s within (unquoted) parentheses and (()
) brackets ([]
or {}
)
In case it is important, as I know some flavors are different in each language, I am using Java.