Skip to content

Commit

Permalink
Change syntax of aggregates
Browse files Browse the repository at this point in the history
Use square brackets `[]` instead of parentheses `()` for aggregates.

Ref. #432
  • Loading branch information
treiher committed Sep 9, 2020
1 parent 28b0635 commit 24d43b0
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion rflx/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ def __init__(self, *elements: Expr, location: Location = None) -> None:
self.elements = list(elements)

def _update_str(self) -> None:
self._str = intern("(" + ", ".join(map(str, self.elements)) + ")")
self._str = intern("[" + ", ".join(map(str, self.elements)) + "]")

def __neg__(self) -> Expr:
raise NotImplementedError
Expand Down
4 changes: 2 additions & 2 deletions rflx/parser/grammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,10 @@ def expression(restricted: bool = False) -> Token:
designator |= Keyword("Head") | Keyword("Opaque") | Keyword("Present") | Keyword("Valid")

array_aggregate = (
Literal("(").setParseAction(lambda s, l, t: l)
Literal("[").setParseAction(lambda s, l, t: l)
+ numeric_literal()
+ (comma() - numeric_literal()) * (0,)
+ Literal(")").setParseAction(lambda s, l, t: l)
+ Literal("]").setParseAction(lambda s, l, t: l)
)
array_aggregate.setParseAction(parse_array_aggregate)

Expand Down
4 changes: 2 additions & 2 deletions specs/tls_handshake.rflx
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,10 @@ package TLS_Handshake is
Extensions_Length : Server_Hello_Extensions_Length
then Extensions
with Length => Extensions_Length * 8
if Random /= (16#CF#, 16#21#, 16#AD#, 16#74#, 16#E5#, 16#9A#, 16#61#, 16#11#, 16#BE#, 16#1D#, 16#8C#, 16#02#, 16#1E#, 16#65#, 16#B8#, 16#91#, 16#C2#, 16#A2#, 16#11#, 16#16#, 16#7A#, 16#BB#, 16#8C#, 16#5E#, 16#07#, 16#9E#, 16#09#, 16#E2#, 16#C8#, 16#A8#, 16#33#, 16#9C#)
if Random /= [16#CF#, 16#21#, 16#AD#, 16#74#, 16#E5#, 16#9A#, 16#61#, 16#11#, 16#BE#, 16#1D#, 16#8C#, 16#02#, 16#1E#, 16#65#, 16#B8#, 16#91#, 16#C2#, 16#A2#, 16#11#, 16#16#, 16#7A#, 16#BB#, 16#8C#, 16#5E#, 16#07#, 16#9E#, 16#09#, 16#E2#, 16#C8#, 16#A8#, 16#33#, 16#9C#]
then HRR_Extensions
with Length => Extensions_Length * 8
if Random = (16#CF#, 16#21#, 16#AD#, 16#74#, 16#E5#, 16#9A#, 16#61#, 16#11#, 16#BE#, 16#1D#, 16#8C#, 16#02#, 16#1E#, 16#65#, 16#B8#, 16#91#, 16#C2#, 16#A2#, 16#11#, 16#16#, 16#7A#, 16#BB#, 16#8C#, 16#5E#, 16#07#, 16#9E#, 16#09#, 16#E2#, 16#C8#, 16#A8#, 16#33#, 16#9C#);
if Random = [16#CF#, 16#21#, 16#AD#, 16#74#, 16#E5#, 16#9A#, 16#61#, 16#11#, 16#BE#, 16#1D#, 16#8C#, 16#02#, 16#1E#, 16#65#, 16#B8#, 16#91#, 16#C2#, 16#A2#, 16#11#, 16#16#, 16#7A#, 16#BB#, 16#8C#, 16#5E#, 16#07#, 16#9E#, 16#09#, 16#E2#, 16#C8#, 16#A8#, 16#33#, 16#9C#];
Extensions : SH_Extensions
then null;
HRR_Extensions : HRR_Extensions;
Expand Down
4 changes: 2 additions & 2 deletions tests/test_expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,8 +678,8 @@ def test_aggregate_simplified() -> None:


def test_aggregate_str() -> None:
assert str(Aggregate(Number(1))) == "(1)"
assert str(Aggregate(Number(1), Number(2))) == "(1, 2)"
assert str(Aggregate(Number(1))) == "[1]"
assert str(Aggregate(Number(1), Number(2))) == "[1, 2]"


def test_aggregate_precedence() -> None:
Expand Down
8 changes: 4 additions & 4 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@ def test_comparison_big_integers(condition: str) -> None:
"condition",
[
'A = "Foo Bar"',
"A /= (0, 1, 2, 3, 4, 5, 6)",
'A = "Foo" & (0) & "Bar"',
"A /= [0, 1, 2, 3, 4, 5, 6]",
'A = "Foo" & [0] & "Bar"',
'"Foo Bar" /= A',
"(0, 1, 2, 3, 4, 5, 6) = A",
'"Foo" & (0) & "Bar" /= A',
"[0, 1, 2, 3, 4, 5, 6] = A",
'"Foo" & [0] & "Bar" /= A',
],
)
def test_comparison_opaque(condition: str) -> None:
Expand Down
10 changes: 5 additions & 5 deletions tests/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def test_grammar_expression_suffix(string: str, expected: Expr) -> None:
Sub(Variable("A"), Sub(Mul(Variable("B"), Pow(Number(2), Number(3))), Number(1))),
),
(
"A + B * -8",
"A + B * (-8)",
Add(Variable("A"), Mul(Variable("B"), Number(-8))),
),
],
Expand Down Expand Up @@ -339,10 +339,10 @@ def test_grammar_boolean_expression_error(string: str, error: Expr) -> None:
[
("42", expr.Number(42)),
('"Foo Bar"', expr.String("Foo Bar")),
("(1)", expr.Aggregate(expr.Number(1))),
("(1, 2)", expr.Aggregate(expr.Number(1), expr.Number(2))),
("[1]", expr.Aggregate(expr.Number(1))),
("[1, 2]", expr.Aggregate(expr.Number(1), expr.Number(2))),
(
'(137) & "PNG" & (13, 10, 26, 10)',
'[137] & "PNG" & [13, 10, 26, 10]',
expr.Aggregate(
Number(137),
Number(80),
Expand Down Expand Up @@ -898,7 +898,7 @@ def test_grammar_unexpected_exception(monkeypatch: Any) -> None:

def test_grammar_expression_aggregate_no_number() -> None:
with pytest.raises(ParseFatalException, match=r"^Expected Number"):
grammar.expression().parseString("(1, Foo)")
grammar.expression().parseString("[1, Foo]")


def test_grammar_unexpected_suffix() -> None:
Expand Down

0 comments on commit 24d43b0

Please sign in to comment.