-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.js
68 lines (56 loc) · 1.52 KB
/
grammar.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/**
* @file Elsa grammar for tree-sitter
* @author Miles Glapa-Grossklag <[email protected]>
* @author Amaan Qureshi <[email protected]>
* @license MIT
* @see {@link https://github.com/ucsd-progsys/elsa|official source}
* @see {@link https://github.com/ucsd-progsys/elsa#semantics-of-elsa-programs|official syntax spec}
*/
/* eslint-disable arrow-parens */
/* eslint-disable camelcase */
/* eslint-disable-next-line spaced-comment */
/// <reference types="tree-sitter-cli/dsl" />
// @ts-check
module.exports = grammar({
name: 'elsa',
extras: $ => [
$.comment,
/\s/,
],
rules: {
source_file: $ => seq(repeat($.definition), repeat($.reduction)),
definition: $ => seq(
'let',
alias($.identifier, $.function),
'=',
$.term,
),
reduction: $ =>
seq(
'eval',
alias($.identifier, $.method),
':',
$.term,
repeat(seq($.step, $.term)),
),
abstraction: $ => seq(
'\\',
field('parameters', repeat1(alias($.identifier, $.parameter))),
'->',
field('body', $.term),
),
application: $ => prec.left(1,
seq(
field('abstraction', $.term),
field('argument', $.term),
),
),
term: $ => choice(
seq('(', $.term, ')'),
choice($.identifier, $.abstraction, $.application),
),
step: _ => choice('=a>', '=b>', '=d>', '=*>', '=~>'),
identifier: _ => token(/[A-Za-z_]+[A-Za-z_0-9]*/),
comment: _ => token(seq('--', /(\\(.|\r?\n)|[^\\\n])*/)),
},
});