Skip to content

Commit

Permalink
Improve export parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
irh committed Mar 4, 2024
1 parent c37f5f2 commit 3154d98
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 30 deletions.
67 changes: 46 additions & 21 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,27 @@ const PREC = {
map_block: 1,
block: 2,
comma: 3,
range: 4,
if: 5,
not: 6,
pipe: 7,
assign: 9,
or: 10,
and: 11,
equality: 12,
comparison: 13,
add: 14,
multiply: 15,
unary: 16,
negate: 17,
debug: 18,
call: 19,
keyword: 20,
meta: 22,
import: 23,
chain: 24,
try: 24,
debug: 4,
call: 5,
keyword: 6,
range: 7,
if: 8,
not: 9,
pipe: 10,
assign: 12,
or: 14,
and: 15,
equality: 16,
comparison: 17,
add: 18,
multiply: 19,
unary: 20,
negate: 21,
meta: 26,
import: 27,
chain: 28,
try: 29,
export: 30,
};

const id = /[\p{XID_Start}_][\p{XID_Continue}]*/;
Expand Down Expand Up @@ -314,12 +315,36 @@ module.exports = grammar({
negate: $ => prec(PREC.negate, (seq('-', $._expression))),

debug: $ => keyword_expression($, 'debug'),
export: $ => keyword_expression($, 'export'),
not: $ => keyword_expression($, 'not'),
return: $ => keyword_expression($, 'return'),
yield: $ => keyword_expression($, 'yield'),
throw: $ => keyword_expression($, 'throw'),

export: $ => prec.right(PREC.export, seq(
'export',
choice(
seq(
choice(
$.identifier,
$.meta,
),
optional(
seq(
repeat($._indented_line),
'=',
repeat($._indented_line),
$._expression,
),
),
),
seq(
repeat($._indented_line),
$.map,
),
$.map_block,
),
)),

number: _ => token(
choice(
// Ints
Expand Down
4 changes: 2 additions & 2 deletions src/grammar.json
Git LFS file not shown
4 changes: 2 additions & 2 deletions src/node-types.json
Git LFS file not shown
4 changes: 2 additions & 2 deletions src/parser.c
Git LFS file not shown
48 changes: 45 additions & 3 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ yield
)

===========================================================================================
export
export_identifier
===========================================================================================

export foo
Expand All @@ -236,12 +236,54 @@ export foo = 99

(module
(export (identifier))
(assign
(export (identifier))
(export
(identifier)
(number)
)
)

===========================================================================================
export_map_inline
===========================================================================================

export { foo, bar }

---

(module
(export
(map
(entry_inline key: (identifier))
(entry_inline key: (identifier))
)
)
)

===========================================================================================
export_map_block
===========================================================================================

export
foo: 42
'bar': 99

---

(module
(export
(map_block
(entry_block
key: (identifier)
value: (number)
)
(entry_block
key: (string)
value: (number)
)
)
)
)

===========================================================================================
import
===========================================================================================
Expand Down

0 comments on commit 3154d98

Please sign in to comment.