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

Commit

Permalink
[#43] Namespaces (#44)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhue authored Mar 19, 2020
1 parent 511ffe7 commit d1994de
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
28 changes: 25 additions & 3 deletions grammar.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const PREC = Object.freeze({
PATTERN: 14,
APPLICATION: 15,
PIPELINE: 16,
ACCESS: 17
});

module.exports = grammar({
Expand Down Expand Up @@ -98,6 +99,7 @@ module.exports = grammar({
$.prefix_application,
$.infix_application,
$.pipeline,
$.access,
alias($.simple_assignment, $.assignment),
$.return,
alias($.simple_if, $.if),
Expand All @@ -112,11 +114,12 @@ module.exports = grammar({
alias($.compound_abstraction, $.abstraction),
alias($.compound_assignment, $.assignment),
alias($.compound_if, $.if),
alias($.compound_case, $.case)
$.case,
$.module
),

block: $ => choice(
seq(optional('then'), $._simple_expression, $._newline),
seq($._simple_expression, $._newline),
seq($._newline, $._indent, repeat1($._expression), $._dedent)
),

Expand Down Expand Up @@ -265,6 +268,15 @@ module.exports = grammar({
field('right', $._simple_expression)
)),

access: $ => prec.left(PREC.ACCESS, seq(
field('left', $._simple_expression),
'->',
choice(
seq('[', field('right', $._simple_expression), ']'),
field('right', alias($.identifier, $.shorthand_access_identifier))
)
)),

simple_assignment: $ => seq(
field('left', choice(
alias($.identifier, $.identifier_pattern),
Expand Down Expand Up @@ -304,17 +316,19 @@ module.exports = grammar({
compound_if: $ => prec.right(seq(
'if',
field('condition', $._simple_expression),
optional('then'),
field('consequence', $.block),
field('alternatives', alias(repeat($.else_if_clause), $.else_if_clauses)),
optional(seq('else', field('alternative', $.block)))
)),
else_if_clause: $ => seq(
'else if',
field('condition', $._simple_expression),
optional('then'),
field('consequence', $.block)
),

compound_case: $ => seq(
case: $ => seq(
'case',
field('value', $._simple_expression),
field('branches', alias(repeat1($.when_clause), $.when_clauses)),
Expand All @@ -323,10 +337,18 @@ module.exports = grammar({
when_clause: $ => seq(
'when',
field('values', $.expression_list),
optional('then'),
field('consequence', $.block)
),
expression_list: $ => commaSep1($._simple_expression),

module: $ => seq(
'module',
field('name', $.identifier),
optional('where'),
field('body', $.block)
),

map: $ => seq(
'{',
commaSep(choice(
Expand Down
47 changes: 47 additions & 0 deletions test/corpus/expressions.txt
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,29 @@ a.b.c(1)
(argument
value: (number)))))

==================
access
==================

a->b(1)
a.b->[c]

---

(program
(application
abstraction: (access
left: (identifier)
right: (shorthand_access_identifier))
arguments: (arguments
(argument
value: (number))))
(pipeline
left: (identifier)
right: (access
left: (identifier)
right: (identifier))))

==================
assignment
==================
Expand Down Expand Up @@ -514,3 +537,27 @@ else 1
(number))))
default: (block
(number))))

==================
module
==================

module a
a := 1
module a where a := 1

---

(program
(module
name: (identifier)
body: (block
(assignment
left: (identifier_pattern)
right: (number))))
(module
name: (identifier)
body: (block
(assignment
left: (identifier_pattern)
right: (number)))))

0 comments on commit d1994de

Please sign in to comment.