Skip to content

Commit

Permalink
Hopefully improve expression/variable regex
Browse files Browse the repository at this point in the history
Previously the variable regex was having trouble with nested expressions, e.g. {Autoload.dictionary[{Player.Name}]}, would get incorrectly split and thus was unable to parse.
  • Loading branch information
Jowan-Spooner committed Jan 18, 2025
1 parent 201f2b8 commit 24d9326
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,10 @@ func color_region(dict:Dictionary, color:Color, line:String, start:String, end:S
end = "\\"+end

if end.is_empty():
region_regex.compile("(?<!\\\\)"+start+".*")
region_regex.compile(r"(?<!\\)"+start+".*")
else:
region_regex.compile("(?<!\\\\)"+start+"((?!"+end+").)*"+end)
r"(?<!\\){([^{}]|({[^}]*}))*}"
region_regex.compile(r"(?<!\\)"+start+"([^"+start+end+"]|("+start+"[^"+end+"]*"+end+"))*"+end)
if to <= from:
to = len(line)-1
for region in region_regex.search_all(line.substr(from, to-from+2)):
Expand Down
4 changes: 2 additions & 2 deletions addons/dialogic/Modules/Text/subsystem_text.gd
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,10 @@ func post_install() -> void:

## Applies modifiers, effects and coloring to the text
func parse_text(text:String, type:int=TextTypes.DIALOG_TEXT, variables := true, glossary := true, modifiers:= true, effects:= true, color_names:= true) -> String:
if modifiers:
text = parse_text_modifiers(text, type)
if variables and dialogic.has_subsystem('VAR'):
text = dialogic.VAR.parse_variables(text)
if modifiers:
text = parse_text_modifiers(text, type)
if effects:
text = parse_text_effects(text)
if color_names:
Expand Down
2 changes: 1 addition & 1 deletion addons/dialogic/Modules/Variable/subsystem_variables.gd
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func parse_variables(text:String) -> String:

# Trying to extract the curly brackets from the text
var regex := RegEx.new()
regex.compile("(?<!\\\\)\\{(?<variable>([^{}]|\\{.*\\})*)\\}")
regex.compile(r"(?<!\\)\{(?<variable>([^{}]|\{[^}]*\})*)\}")

var parsed := text.replace('\\{', '{')
for result in regex.search_all(text):
Expand Down

0 comments on commit 24d9326

Please sign in to comment.