Fix void expressions in FOR loops.

As an enhancement over LSL, we trigger a Type Mismatch when there are void expressions in list constructors, because in LSL, while accepted, they trigger an ugly runtime exception.

This works fine, but then expression lists, where this are checked, are not exclusive of list constructor; they are used in other places. One of these places is the initializer and the iterator of FOR loops. As a consequence, we didn't allow void functions in the FOR initializer or iterator.

Fix by adding another possible value to the parameter 'expected_types' in Parse_expression_list. False means don't allow void either (what Null did before); Null now means allow anything. All callers to Parse_expression_list are changed accordingly. Added corresponding regression test.
This commit is contained in:
Sei Lisa 2016-07-10 01:29:11 +02:00
parent e60457f00e
commit 37483a72cb
2 changed files with 22 additions and 17 deletions

View file

@ -872,7 +872,7 @@ class parser(object):
if tok0 == '[': if tok0 == '[':
self.NextToken() self.NextToken()
val = self.Parse_optional_expression_list() val = self.Parse_optional_expression_list(False)
self.expect(']') self.expect(']')
self.NextToken() self.NextToken()
return {'nt':'LIST', 't':'list', 'ch':val} return {'nt':'LIST', 't':'list', 'ch':val}
@ -926,7 +926,7 @@ class parser(object):
self.NextToken() self.NextToken()
if typ != 'list': if typ != 'list':
raise EParseTypeMismatch(self) raise EParseTypeMismatch(self)
idxexpr = self.Parse_optional_expression_list() idxexpr = self.Parse_optional_expression_list(False)
self.expect(']') self.expect(']')
self.NextToken() self.NextToken()
if self.tok[0] != '=' or not AllowAssignment: if self.tok[0] != '=' or not AllowAssignment:
@ -1464,24 +1464,28 @@ list lazy_list_set(list L, integer i, list v)
optional_expression_list: LAMBDA | expression_list optional_expression_list: LAMBDA | expression_list
expression_list: expression | expression_list ',' expression expression_list: expression | expression_list ',' expression
""" """
# This is a maze of which we get out with a dirty hack. # Recursive descendent parsers are nice, but not exempt of problems.
# optional_expression_list is used by FOR statements (closed by ';' or ')'), # We need to accept empty lists. This is a maze of which we get out
# list constants (closed by ']') and function arguments (closed by ')'). # with a dirty hack. Rather than attempt to parse as an expression and
# If it's not the right token, we'll err anyway, in Parse_expression or # backtrack in case of error, we check the next token to see if it
# upon return. # is one that closes the expression list.
# optional_expression_list is used by FOR loops (closed by ';' or ')'),
# list constants and lazy lists (closed by ']') and function arguments
# (closed by ')'). If it's not the right token, we'll err anyway upon
# return.
ret = [] ret = []
idx = 0 idx = 0
if self.tok[0] not in (']', ')', ';'): if self.tok[0] not in (']', ')', ';'):
while True: while True:
expr = self.Parse_expression() expr = self.Parse_expression()
if expected_types is not None: if False is not expected_types is not None:
if idx >= len(expected_types): if idx >= len(expected_types):
raise EParseFunctionMismatch(self) raise EParseFunctionMismatch(self)
try: try:
expr = self.autocastcheck(expr, expected_types[idx]); expr = self.autocastcheck(expr, expected_types[idx]);
except EParseTypeMismatch: except EParseTypeMismatch:
raise EParseFunctionMismatch(self) raise EParseFunctionMismatch(self)
else: elif expected_types is False: # don't accept void expressions
if expr['t'] not in self.types: if expr['t'] not in self.types:
raise EParseTypeMismatch(self) raise EParseTypeMismatch(self)
idx += 1 idx += 1
@ -1489,7 +1493,7 @@ list lazy_list_set(list L, integer i, list v)
if self.tok[0] != ',': if self.tok[0] != ',':
break break
self.NextToken() self.NextToken()
if expected_types is not None and idx != len(expected_types): if False is not expected_types is not None and idx != len(expected_types):
raise EParseFunctionMismatch(self) raise EParseFunctionMismatch(self)
return ret return ret

View file

@ -228,13 +228,14 @@ class Test02_Parser(UnitTestCase):
self.parser.parse('default{timer(){vector v;v.x=0;}}') self.parser.parse('default{timer(){vector v;v.x=0;}}')
# Check for exceptions only # Check for exceptions only
p = self.parser.parse('default{timer(){jump x;while(1)@x;}}') self.parser.parse('default{timer(){jump x;while(1)@x;}}')
p = self.parser.parse('default{timer(){jump x;do@x;while(1);}}') self.parser.parse('default{timer(){jump x;do@x;while(1);}}')
p = self.parser.parse('default{timer(){jump x;for(;1;)@x;}}') self.parser.parse('default{timer(){jump x;for(;1;)@x;}}')
p = self.parser.parse('default{timer(){jump x;while(1)@x;}}', ('breakcont',)) self.parser.parse('default{timer(){jump x;while(1)@x;}}', ('breakcont',))
p = self.parser.parse('default{timer(){jump x;do@x;while(1);}}', ('breakcont',)) self.parser.parse('default{timer(){jump x;do@x;while(1);}}', ('breakcont',))
p = self.parser.parse('default{timer(){jump x;for(;1;)@x;}}', ('breakcont',)) self.parser.parse('default{timer(){jump x;for(;1;)@x;}}', ('breakcont',))
self.outscript.output(p)
self.parser.parse('default{timer(){for(llDie();1;llDie());}}')
self.assertRaises(EParseUndefined, self.parser.parse, self.assertRaises(EParseUndefined, self.parser.parse,
'default{timer(){jump x;while(1){@x;}}}') 'default{timer(){jump x;while(1){@x;}}}')