Skip to content

Commit

Permalink
Allow chains to be broken over multiple lines
Browse files Browse the repository at this point in the history
For some reason this breaks arithmetic with operators starting the following
lines, and I have no idea why.

This feels like whack-a-mole, and I don"t like it!
  • Loading branch information
irh committed Mar 4, 2024
1 parent c714f92 commit 41d551a
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 34 deletions.
14 changes: 9 additions & 5 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,17 @@ module.exports = grammar({
$.null,
),

chain: $ => prec.left(PREC.chain, seq(
chain: $ => prec.right(PREC.chain, seq(
field('start', $._term),
choice(
seq(
repeat1(choice(
field('lookup', seq(
'.', choice($.identifier, $.string),
)),
seq(
repeat($._indented_line),
field('lookup', seq(
'.', choice($.identifier, $.string),
)),
),
field('call', $.tuple),
field('index', $._index),
)),
Expand All @@ -199,7 +202,8 @@ module.exports = grammar({
']'
),

call: $ => prec.right(seq(
call: $ => prec.right(PREC.chain, seq(
repeat($._indented_line),
$._expression,
repeat(seq(
repeat($._indented_line),
Expand Down
4 changes: 2 additions & 2 deletions src/grammar.json
Git LFS file not shown
4 changes: 2 additions & 2 deletions src/parser.c
Git LFS file not shown
18 changes: 17 additions & 1 deletion test/corpus/chains.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ f g 1, 2
)

===========================================================================================
dots_and_calls
dots_and_calls_single_line
===========================================================================================

a.b()
Expand Down Expand Up @@ -98,3 +98,19 @@ a.b().c()
)
)

===========================================================================================
dots_and_calls_multi_line
===========================================================================================

foo
.bar()

---

(module
(chain
start: (identifier)
lookup: (identifier)
call: (tuple)
)
)
24 changes: 0 additions & 24 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -152,30 +152,6 @@ binary_op_multi_line_post
)


===========================================================================================
binary_op_multi_line_pre
===========================================================================================

1
+ 2
* 3
+ 4

---

(module
(binary_op
(binary_op
(binary_op
(number)
(number)
)
(number)
)
(number)
)
)

===========================================================================================
assignment
===========================================================================================
Expand Down
23 changes: 23 additions & 0 deletions test/failing/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,26 @@ from foo import a, b, c
)
)

===========================================================================================
binary_op_multi_line_pre
===========================================================================================

1
+ 2
* 3
+ 4

---

(module
(binary_op
(binary_op
(binary_op
(number)
(number)
)
(number)
)
(number)
)
)

0 comments on commit 41d551a

Please sign in to comment.