Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.

Commit d1994de

Browse files
authored
[#43] Namespaces (#44)
1 parent 511ffe7 commit d1994de

File tree

2 files changed

+72
-3
lines changed

2 files changed

+72
-3
lines changed

grammar.js

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ const PREC = Object.freeze({
1717
PATTERN: 14,
1818
APPLICATION: 15,
1919
PIPELINE: 16,
20+
ACCESS: 17
2021
});
2122

2223
module.exports = grammar({
@@ -98,6 +99,7 @@ module.exports = grammar({
9899
$.prefix_application,
99100
$.infix_application,
100101
$.pipeline,
102+
$.access,
101103
alias($.simple_assignment, $.assignment),
102104
$.return,
103105
alias($.simple_if, $.if),
@@ -112,11 +114,12 @@ module.exports = grammar({
112114
alias($.compound_abstraction, $.abstraction),
113115
alias($.compound_assignment, $.assignment),
114116
alias($.compound_if, $.if),
115-
alias($.compound_case, $.case)
117+
$.case,
118+
$.module
116119
),
117120

118121
block: $ => choice(
119-
seq(optional('then'), $._simple_expression, $._newline),
122+
seq($._simple_expression, $._newline),
120123
seq($._newline, $._indent, repeat1($._expression), $._dedent)
121124
),
122125

@@ -265,6 +268,15 @@ module.exports = grammar({
265268
field('right', $._simple_expression)
266269
)),
267270

271+
access: $ => prec.left(PREC.ACCESS, seq(
272+
field('left', $._simple_expression),
273+
'->',
274+
choice(
275+
seq('[', field('right', $._simple_expression), ']'),
276+
field('right', alias($.identifier, $.shorthand_access_identifier))
277+
)
278+
)),
279+
268280
simple_assignment: $ => seq(
269281
field('left', choice(
270282
alias($.identifier, $.identifier_pattern),
@@ -304,17 +316,19 @@ module.exports = grammar({
304316
compound_if: $ => prec.right(seq(
305317
'if',
306318
field('condition', $._simple_expression),
319+
optional('then'),
307320
field('consequence', $.block),
308321
field('alternatives', alias(repeat($.else_if_clause), $.else_if_clauses)),
309322
optional(seq('else', field('alternative', $.block)))
310323
)),
311324
else_if_clause: $ => seq(
312325
'else if',
313326
field('condition', $._simple_expression),
327+
optional('then'),
314328
field('consequence', $.block)
315329
),
316330

317-
compound_case: $ => seq(
331+
case: $ => seq(
318332
'case',
319333
field('value', $._simple_expression),
320334
field('branches', alias(repeat1($.when_clause), $.when_clauses)),
@@ -323,10 +337,18 @@ module.exports = grammar({
323337
when_clause: $ => seq(
324338
'when',
325339
field('values', $.expression_list),
340+
optional('then'),
326341
field('consequence', $.block)
327342
),
328343
expression_list: $ => commaSep1($._simple_expression),
329344

345+
module: $ => seq(
346+
'module',
347+
field('name', $.identifier),
348+
optional('where'),
349+
field('body', $.block)
350+
),
351+
330352
map: $ => seq(
331353
'{',
332354
commaSep(choice(

test/corpus/expressions.txt

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,29 @@ a.b.c(1)
369369
(argument
370370
value: (number)))))
371371

372+
==================
373+
access
374+
==================
375+
376+
a->b(1)
377+
a.b->[c]
378+
379+
---
380+
381+
(program
382+
(application
383+
abstraction: (access
384+
left: (identifier)
385+
right: (shorthand_access_identifier))
386+
arguments: (arguments
387+
(argument
388+
value: (number))))
389+
(pipeline
390+
left: (identifier)
391+
right: (access
392+
left: (identifier)
393+
right: (identifier))))
394+
372395
==================
373396
assignment
374397
==================
@@ -514,3 +537,27 @@ else 1
514537
(number))))
515538
default: (block
516539
(number))))
540+
541+
==================
542+
module
543+
==================
544+
545+
module a
546+
a := 1
547+
module a where a := 1
548+
549+
---
550+
551+
(program
552+
(module
553+
name: (identifier)
554+
body: (block
555+
(assignment
556+
left: (identifier_pattern)
557+
right: (number))))
558+
(module
559+
name: (identifier)
560+
body: (block
561+
(assignment
562+
left: (identifier_pattern)
563+
right: (number)))))

0 commit comments

Comments
 (0)