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,6 +199,8 @@ class Statement extends ParseNode:
while parser.next_symbol_is([Constants.TokenType.TagMarker]):
parser.expect_symbol([Constants.TokenType.TagMarker])
if parser.next_symbol_is([Constants.TokenType.Identifier]):
var tag = parser.expect_symbol([Constants.TokenType.Identifier]).value
tags.append(tag)
@ -414,6 +416,7 @@ class ShortCutOption extends ParseNode:
func _init(index, parent, parser).(parent, parser):
parser.expect_symbol([Constants.TokenType.ShortcutOption])
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
@ -427,12 +430,12 @@ class ShortCutOption extends ParseNode:
condition = ExpressionNode.parse(self, parser)
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])
if parser.next_token_is([Constants.TokenType.Identifier]):
var tag = parser.expect_symbol([Constants.TokenType.Identifier]).value
tags.append(tag)
self.tags = tags
# parse remaining statements
@ -508,8 +511,8 @@ class OptionStatement extends ParseNode:
# NOTE: if there is a | get the next string
if parser.next_symbol_is([Constants.TokenType.OptionDelimit]):
parser.expect_symbol([Constants.TokenType.OptionDelimit])
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)
label = strings[0] if strings.size() > 1 else ''
@ -524,7 +527,7 @@ class OptionStatement extends ParseNode:
return tab(indent_level, 'Option: -> %s' % destination)
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:
var clauses = []#
@ -944,8 +947,12 @@ class Assignment extends ParseNode:
func _init(parent, parser).(parent, parser):
parser.expect_symbol([Constants.TokenType.BeginCommand])
parser.expect_symbol([Constants.TokenType.Set])
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)
parser.expect_symbol([Constants.TokenType.EndCommand])