-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrammar.js
99 lines (78 loc) · 1.84 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
const PREC = {
comment: 1,
}
module.exports = grammar({
name: 'wvlet',
extras: $ => [$.comment, /\s/],
rules: {
source_file: $ => choice($.flowr_expr),
flowr_expr: $ => seq(
optional($.for_expr),
optional($.where_expr),
$.return_expr
),
for_expr: $ => seq(
'for',
sep1(',', $.for_item)
),
for_item: $ => seq(
$.identifier,
'in',
$.table_expr
),
table_expr: $ => seq(
$.identifier
),
where_expr: $ => seq(
'where',
$._expression
),
return_expr: $ => seq(
'return',
sep1(',', $.return_item)
),
return_item: $ => choice(
$._expression
),
_expression: $ => choice(
$.qname,
$.identifier,
$.number,
$._conditional_expression
),
_conditional_expression: $ => choice(
$.comparison_expression
),
comparison_expression: $ => prec.left(1,
seq(
$._expression,
$.comparisonOperator,
$._expression
)
),
comparisonOperator: $ => choice(
'=',
'<',
'>',
'<=',
'>=',
'!='
),
qname : $ => seq($._identifier, repeat1(seq('.', $._identifier))),
named_identifier: $ => seq(
$.identifier,
':',
$._expression
),
// An identifier is a sequence of one or more letters, numbers, or underscores.
identifier: $ => /[a-zA-Z_][a-zA-Z0-9_]*/,
_identifier: $ => $.identifier,
// Number strings are sequences of digits, possibly separated by underscores.
number: $ => /\d[\d_]*/,
comment: $ => seq(token("//"), choice($._comment_text)),
_comment_text: $ => token(prec(PREC.comment, /.*/)),
}
});
function sep1(delimiter, rule) {
return seq(rule, repeat(seq(delimiter, rule)));
}