Skip to content

Commit 2c46e10

Browse files
committed
Feature: for token
1 parent ae36010 commit 2c46e10

File tree

2 files changed

+18
-3
lines changed

2 files changed

+18
-3
lines changed

nml/parser.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
disable_item,
3030
error,
3131
font,
32+
for_,
3233
general,
3334
grf,
3435
item,
@@ -47,6 +48,7 @@
4748
)
4849

4950

51+
5052
class NMLParser:
5153
"""
5254
@ivar lexer: Scanner providing tokens.
@@ -145,7 +147,8 @@ def p_main_block(self, t):
145147
| engine_override
146148
| sort_vehicles
147149
| basecost
148-
| constant"""
150+
| constant
151+
| for"""
149152
t[0] = t[1]
150153

151154
#
@@ -389,7 +392,9 @@ def p_property_assignment(self, t):
389392
"""property_assignment : ID COLON expression SEMICOLON
390393
| ID COLON expression UNIT SEMICOLON
391394
| NUMBER COLON expression SEMICOLON
392-
| NUMBER COLON expression UNIT SEMICOLON"""
395+
| NUMBER COLON expression UNIT SEMICOLON
396+
| ID COLON for SEMICOLON
397+
| NUMBER COLON for SEMICOLON"""
393398
name = t[1]
394399
unit_value = None if len(t) == 5 else unit.get_unit(t[4])
395400
t[0] = item.Property(name, t[3], unit_value, t.lineno(1))
@@ -835,3 +840,11 @@ def p_tilelayout_item_prop(self, t):
835840
def p_constant(self, t):
836841
"constant : CONST expression EQ expression SEMICOLON"
837842
t[0] = constant.Constant(t[2], t[4])
843+
844+
def p_for(self, t):
845+
"""for : FOR ID LPAREN id_list RPAREN LBRACE layout_sprite_list RBRACE
846+
| LBRACKET non_empty_expression_list FOR ID IN expression RBRACKET"""
847+
if t[1] == '[' :
848+
t[0] = for_.For(" ", t[3], t[5])
849+
else:
850+
t[0] = for_.For(t[2], t[4], t[7])

nml/tokens.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@
6161
"recolour_sprite": "RECOLOUR_SPRITE",
6262
"engine_override": "ENGINE_OVERRIDE",
6363
"sort": "SORT_VEHICLES",
64-
"const": "CONST"
64+
"const": "CONST",
65+
"for": "FOR",
66+
"in": "IN",
6567
}
6668
# fmt: on
6769

0 commit comments

Comments
 (0)