feat: made parsing code more defensive

This commit is contained in:
Bram Dingelstad 2021-12-09 10:39:18 +01:00
parent 1e1bb8936f
commit 247f6b54c2

View file

@ -199,8 +199,10 @@ class Statement extends ParseNode:
while parser.next_symbol_is([Constants.TokenType.TagMarker]): while parser.next_symbol_is([Constants.TokenType.TagMarker]):
parser.expect_symbol([Constants.TokenType.TagMarker]) parser.expect_symbol([Constants.TokenType.TagMarker])
var tag = parser.expect_symbol([Constants.TokenType.Identifier]).value
tags.append(tag) if parser.next_symbol_is([Constants.TokenType.Identifier]):
var tag = parser.expect_symbol([Constants.TokenType.Identifier]).value
tags.append(tag)
if tags.size() > 0: if tags.size() > 0:
self.tags = tags self.tags = tags
@ -414,7 +416,8 @@ class ShortCutOption extends ParseNode:
func _init(index, parent, parser).(parent, parser): func _init(index, parent, parser).(parent, parser):
parser.expect_symbol([Constants.TokenType.ShortcutOption]) parser.expect_symbol([Constants.TokenType.ShortcutOption])
label = parser.expect_symbol([Constants.TokenType.Text]).value if parser.next_symbol_is([Constants.TokenType.Text]):
label = parser.expect_symbol([Constants.TokenType.Text]).value
# FIXME: Parse the conditional << if $x >> when it exists # FIXME: Parse the conditional << if $x >> when it exists
var tags = [] var tags = []
@ -427,11 +430,11 @@ class ShortCutOption extends ParseNode:
condition = ExpressionNode.parse(self, parser) condition = ExpressionNode.parse(self, parser)
parser.expect_symbol([Constants.TokenType.EndCommand]) parser.expect_symbol([Constants.TokenType.EndCommand])
elif parser.next_symbol_is([Constants.TokenType.TagMarker]): elif parser.next_symbols_are([Constants.TokenType.TagMarker, Constants.TokenType.Identifier]):
parser.expect_symbol([Constants.TokenType.TagMarker]) parser.expect_symbol([Constants.TokenType.TagMarker])
var tag = parser.expect_symbol([Constants.TokenType.Identifier]).value if parser.next_token_is([Constants.TokenType.Identifier]):
tags.append(tag) var tag = parser.expect_symbol([Constants.TokenType.Identifier]).value
tags.append(tag)
self.tags = tags self.tags = tags
# parse remaining statements # parse remaining statements
@ -508,9 +511,9 @@ class OptionStatement extends ParseNode:
# NOTE: if there is a | get the next string # NOTE: if there is a | get the next string
if parser.next_symbol_is([Constants.TokenType.OptionDelimit]): if parser.next_symbol_is([Constants.TokenType.OptionDelimit]):
parser.expect_symbol([Constants.TokenType.OptionDelimit]) parser.expect_symbol([Constants.TokenType.OptionDelimit])
var t = parser.expect_symbol([Constants.TokenType.Text, Constants.TokenType.Identifier]) if parser.next_symbol_is([Constants.TokenType.Text, Constants.TokenType.Identifier]):
var t = parser.expect_symbol([Constants.TokenType.Text, Constants.TokenType.Identifier])
strings.append(t.value as String) strings.append(t.value as String)
label = strings[0] if strings.size() > 1 else '' label = strings[0] if strings.size() > 1 else ''
destination = strings[1] if strings.size() > 1 else strings[0] destination = strings[1] if strings.size() > 1 else strings[0]
@ -524,7 +527,7 @@ class OptionStatement extends ParseNode:
return tab(indent_level, 'Option: -> %s' % destination) return tab(indent_level, 'Option: -> %s' % destination)
static func can_parse(parser): static func can_parse(parser):
return parser.next_symbol_is([Constants.TokenType.OptionStart]) return parser.next_symbols_are([Constants.TokenType.OptionStart, Constants.TokenType.Text])
class IfStatement extends ParseNode: class IfStatement extends ParseNode:
var clauses = []# var clauses = []#
@ -944,8 +947,12 @@ class Assignment extends ParseNode:
func _init(parent, parser).(parent, parser): func _init(parent, parser).(parent, parser):
parser.expect_symbol([Constants.TokenType.BeginCommand]) parser.expect_symbol([Constants.TokenType.BeginCommand])
parser.expect_symbol([Constants.TokenType.Set]) parser.expect_symbol([Constants.TokenType.Set])
destination = parser.expect_symbol([Constants.TokenType.Variable]).value
operation = parser.expect_symbol(Assignment.valid_ops()).type if parser.next_symbol_is([Constants.TokenType.Variable]):
destination = parser.expect_symbol([Constants.TokenType.Variable]).value
if parser.next_symbol_is(Assignment.valid_ops()):
operation = parser.expect_symbol(Assignment.valid_ops()).type
value = ExpressionNode.parse(self, parser) value = ExpressionNode.parse(self, parser)
parser.expect_symbol([Constants.TokenType.EndCommand]) parser.expect_symbol([Constants.TokenType.EndCommand])