-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrammar.js
195 lines (161 loc) · 4.64 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const WHITESPACE_CHAR =
/[\f\n\r\t, \u000B\u001C\u001D\u001E\u001F\u2028\u2029\u1680\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2008\u2009\u200a\u205f\u3000]/;
const WHITESPACE =
token(repeat1(WHITESPACE_CHAR));
const COMMENT =
token(choice(seq(choice('#;', ';'), repeat(/./)),
token(/#\|(\n|.)*\|#/)));
const DELIMITER =
choice(WHITESPACE, '|', '(', ')', '"', ';')
const DIRECTIVE =
token(choice(/#!(?:\n|.)*!#/, /#!.*\n/))
const OPEN_BRACKET =
choice('(', '[', '{')
const CLOSE_BRACKET =
choice(')', ']', '}')
const BOOLEAN =
choice(/#t(rue)?/, /#f(alse)?/)
const ANY_CHAR =
/./;
const ALPHA =
/[a-zA-Z]/
const IDENTIFIER_CHAR =
/[!$%&*+\-.\/:<=>?@^_~a-zA-Z0-9]/
const IDENTIFIER_START =
choice(/[!$%&*+\-.\/:<=>?@^_~]/, ALPHA)
const IDENTIFIER_LITERAL =
token(seq('|', repeat1(/[^|]/), '|'))
const IDENTIFIER =
token(seq(IDENTIFIER_START, repeat(IDENTIFIER_CHAR)))
const BIN_DIGIT = /[01]/;
const OCT_DIGIT = /[0-7]/;
const DEC_DIGIT = /[0-9]/;
const HEX_DIGIT = /[0-9a-fA-F]/;
const SIGN = /[+\-]/;
const INFNAN = seq(SIGN, /(inf|nan)\.0/);
function digit(radix) {
switch(radix) {
case 2:
return BIN_DIGIT;
break;
case 8:
return OCT_DIGIT;
break;
case 10:
return DEC_DIGIT;
break;
case 16:
return HEX_DIGIT;
}
}
const SUFFIX = optional(seq('e', optional(SIGN), repeat1(digit(10))));
function radix(radix) {
switch(radix) {
case 2:
return '#b';
break;
case 8:
return '#o';
break;
case 10:
return optional('#d');
break;
case 16:
return '#x';
break;
}
}
function prefix(r) {
return choice(seq(radix(r), optional(/#[ie]/)),
seq(optional(/#[ie]/), radix(r)));
}
function uinteger(radix) {
return repeat1(digit(radix));
}
function decimal() {
return choice(
seq(uinteger(10), SUFFIX),
seq('.', repeat1(digit(10)), SUFFIX),
seq(repeat1(digit(10)), '.', repeat(digit(10)), SUFFIX)
);
}
function ureal(radix) {
return choice(
uinteger(radix),
seq(uinteger(radix), '/', uinteger(radix)),
decimal()
);
}
function real(radix) {
return choice(seq(optional(/[+\-]/), ureal(radix)), INFNAN);
}
function complex(radix) {
return choice(
real(radix),
seq(real(radix), '@', real(radix)),
seq(real(radix), choice('+', '-'), ureal(radix), 'i'),
seq(real(radix), choice('+i', '-i')),
seq(real(radix), INFNAN, 'i'),
seq(choice('+', '-'), ureal(radix), 'i'),
seq(INFNAN, 'i'),
seq(choice('+', '-'), 'i'));
}
function num(radix) {
return token(seq(prefix(radix), complex(radix)));
}
function number() {
return choice(num(2), num(8), num(10), num(16));
}
const NAMED_CHAR =
choice('alarm',
'backspace',
'delete',
'escape',
'newline',
'null',
'return',
'space',
'tab');
const CHARACTER =
seq('#', /\\[^\f\n\r\t ()]+/)
const MNEMONIC_ESCAPE =
choice('\\a', '\\b', '\\t', '\\n', '\\r')
const STRING_ELEMENT =
choice(/[^\\"]/,
MNEMONIC_ESCAPE,
'\\"',
'\\\\',
seq('\\', repeat(/[ \t]/), '\n', repeat(/[ \t]/)),
seq('\\x', HEX_DIGIT, ';'))
const STRING =
token(seq('"', repeat(STRING_ELEMENT), '"'))
const BYTE = /[0-9]{1,3}/
const BYTEVECTOR =
token(seq('#u8(', repeat(choice(BYTE, WHITESPACE)), ')'))
module.exports = grammar({
name: 'scheme',
extras: $ => [/\s/, $.comment, $.directive],
conflicts: $ =>
[],
rules: {
source: $ => repeat($._sexp),
_sexp: $ => choice($._atom, $.vector, $.list, $.abbreviation),
_atom: $ => choice($.boolean, $.number, $.character, $.string,
$.bytevector, $.symbol),
boolean: $ => BOOLEAN,
number: $ => number(),
character: $ => CHARACTER,
string: $ => STRING,
bytevector: $ => BYTEVECTOR,
symbol: $ => choice(IDENTIFIER, IDENTIFIER_LITERAL),
vector: $ =>
seq(field('open', '#('), repeat(choice($._sexp, WHITESPACE)),
field('close', ')')),
list: $ =>
seq(field('open', OPEN_BRACKET), repeat(choice($._sexp,
WHITESPACE)), field('close', CLOSE_BRACKET)),
abbreviation: $ => seq(choice("'", '`', ',', ',@'), $._sexp),
comment: $ => COMMENT,
directive: $ => DIRECTIVE,
}
});