🐐 Arrays with newlines
This commit is contained in:
@@ -273,14 +273,14 @@ class Parser():
|
|||||||
|
|
||||||
@self.pg.production('array_items : ')
|
@self.pg.production('array_items : ')
|
||||||
@self.pg.production('array_items : expression')
|
@self.pg.production('array_items : expression')
|
||||||
@self.pg.production('array_items : expression SYMBOL_COMMA array_items')
|
@self.pg.production('array_items : expression SYMBOL_COMMA opt_newline array_items')
|
||||||
def array_items(calls):
|
def array_items(calls):
|
||||||
if len(calls) == 0:
|
if len(calls) == 0:
|
||||||
return []
|
return []
|
||||||
elif len(calls) == 1:
|
elif len(calls) == 1:
|
||||||
return [calls[0]]
|
return [calls[0]]
|
||||||
else:
|
else:
|
||||||
return [calls[0]] + calls[2]
|
return [calls[0]] + calls[3]
|
||||||
|
|
||||||
@self.pg.production('expression : id')
|
@self.pg.production('expression : id')
|
||||||
def expression_id(tokens):
|
def expression_id(tokens):
|
||||||
|
|||||||
30
tests.py
30
tests.py
@@ -1798,6 +1798,36 @@ class TestArraySlice(unittest.TestCase):
|
|||||||
run_test(self, source, nodes, value)
|
run_test(self, source, nodes, value)
|
||||||
|
|
||||||
|
|
||||||
|
# --- Multiline arrays ---
|
||||||
|
|
||||||
|
multiline_array_tests = [
|
||||||
|
# newlines after commas
|
||||||
|
("[I,\nII,\nIII]",
|
||||||
|
Program([], [ExpressionStatement(DataArray([Numeral("I"), Numeral("II"), Numeral("III")]))]),
|
||||||
|
ValList([ValInt(1), ValInt(2), ValInt(3)])),
|
||||||
|
# single newline after comma
|
||||||
|
("[I, II,\nIII]",
|
||||||
|
Program([], [ExpressionStatement(DataArray([Numeral("I"), Numeral("II"), Numeral("III")]))]),
|
||||||
|
ValList([ValInt(1), ValInt(2), ValInt(3)])),
|
||||||
|
# empty array still works
|
||||||
|
("[]",
|
||||||
|
Program([], [ExpressionStatement(DataArray([]))]),
|
||||||
|
ValList([])),
|
||||||
|
# nested arrays with newlines
|
||||||
|
("[I,\n[II, III],\nIV]",
|
||||||
|
Program([], [ExpressionStatement(DataArray([
|
||||||
|
Numeral("I"),
|
||||||
|
DataArray([Numeral("II"), Numeral("III")]),
|
||||||
|
Numeral("IV")]))]),
|
||||||
|
ValList([ValInt(1), ValList([ValInt(2), ValInt(3)]), ValInt(4)])),
|
||||||
|
]
|
||||||
|
|
||||||
|
class TestMultilineArray(unittest.TestCase):
|
||||||
|
@parameterized.expand(multiline_array_tests)
|
||||||
|
def test_multiline_array(self, source, nodes, value):
|
||||||
|
run_test(self, source, nodes, value)
|
||||||
|
|
||||||
|
|
||||||
# --- String indexing ---
|
# --- String indexing ---
|
||||||
|
|
||||||
string_index_tests = [
|
string_index_tests = [
|
||||||
|
|||||||
Reference in New Issue
Block a user