-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.y
299 lines (271 loc) · 4.86 KB
/
parse.y
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/*
* Droscheme - a Scheme implementation
* Copyright © 2012 Andrew Robbins, Daniel Connelly
*
* This program is free software: it is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. You can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License (LGPLv3): <http://www.gnu.org/licenses/>.
*/
/* This file is a parser description for the Scheme language
* as understood by Droscheme. In particular, we recognize
* multiple revisions of the standard, most notably:
*
* - R6RS bytevectors #vu8(...)
* - R7RS/SRFI-4 bytevectors #u8(...)
* - R6RS numbers (+inf.0, -inf.0, ...|23)
* - R7RS numbers (subset of R6RS numbers)
*/
/* Implementation Details:
*
* There are two fundamental types that are used to pass
* information between the lexer, the parser, and the rest
* of the program.
*
* - yySymType - every production has access to this type, but
* is it only visible inside this file?
*
* - Lexer - this is the only thing that is available
* from outside this file, so we have an
* lval field in here to communicate.
*/
%{
package droscheme
import (
"fmt"
"strings"
)
var parseValue Any // returned value from yyParse
var parseErr error // return value for parsing errors
%}
// expands to yySymType
%union {
datum Any;
token int; // this is for token identifiers
}
%type <datum> datum datums2 datums1 datums0 simpledatum compounddatum
%type <datum> keyword symbol list vector u8vector abbreviation
// BEGIN tokens
%token <datum> ID
%token <datum> BOOL
%token <datum> NUMBER
%token <datum> CHAR
%token <datum> STRING
%token <datum> LABEL
%token <token> VECTORPAREN /* "#(" */
%token <token> U8VECTORPAREN /* "#u8(" */
%token <token> QUOTE /* "'" */
%token <token> QQUOTE /* "`" */
%token <token> UNQUOTE /* "," */
%token <token> UNQUOTES /* ",@" */
%token <token> SYNTAX /* "#'" */
%token <token> QSYNTAX /* "#`" */
%token <token> UNSYNTAX /* "#," */
%token <token> UNSYNTAXS /* "#,@" */
%token <token> ELLIPSIS /* "..." */
%token <token> KEYWORD /* "#:" */
%token <token> COMMENT /* "#;" */
%start datum
// END tokens
%%
// BEGIN grammar
// R7RS 7.1.2. External representations
datum:
simpledatum
{
$$ = $1
parseValue = $$
}
| compounddatum
{
$$ = $1
parseValue = $$
}
| keyword datum
{
$$ = NewKeyword($1, $2)
parseValue = $$
}
| comment datum
{
$$ = $2
parseValue = $$
}
| LABEL '=' datum
{
$$ = NewLabel($1, $3)
}
| LABEL '#'
{
$$ = $1
}
keyword:
KEYWORD datum
{
$$ = $2
}
comment:
COMMENT datum
{
}
symbol:
ID
{
$$ = $1
}
simpledatum:
BOOL
{
$$ = $1
}
| NUMBER
{
$$ = $1
}
| CHAR
{
$$ = $1
}
| STRING
{
$$ = $1
}
| symbol
{
$$ = $1
}
| u8vector
{
$$ = $1
}
compounddatum:
list
{
$$ = $1
}
| vector
{
$$ = $1
}
list:
'(' datums0 ')'
{
$$ = $2
}
| '[' datums0 ']'
{
$$ = $2
}
| '(' datums2 ')'
{
$$ = $2
}
| '[' datums2 ']'
{
$$ = $2
}
| abbreviation
{
$$ = $1
}
datums2:
datums1 '.' datum
{
$$ = listR($1, $3)
}
| datums1 '.' datum '.' datum
{
if (ToFixnum(Dlength(list1($1))) != 1) {
panic("double-dotted-lists must have 3 elements")
}
$$ = list3($3, unlist1($1), $5)
}
datums1:
datum
{
$$ = list1($1)
}
| datum datums1
{
$$ = list1R($1, $2)
}
| datum comment
{
$$ = list1($1)
}
datums0:
datums1
{
$$ = $1
}
| /*empty*/
{
$$ = list0()
}
abbreviation:
QUOTE datum
{
$$ = list2(SSymbol{"quote"}, $2)
}
| QQUOTE datum
{
$$ = list2(SSymbol{"quasiquote"}, $2)
}
| UNQUOTE datum
{
$$ = list2(SSymbol{"unquote"}, $2)
}
| UNQUOTES datum
{
$$ = list2(SSymbol{"unquote-splicing"}, $2)
}
| SYNTAX datum
{
$$ = list2(SSymbol{"syntax"}, $2)
}
| QSYNTAX datum
{
$$ = list2(SSymbol{"quasisyntax"}, $2)
}
| UNSYNTAX datum
{
$$ = list2(SSymbol{"unsyntax"}, $2)
}
| UNSYNTAXS datum
{
$$ = list2(SSymbol{"unsyntax-splicing"}, $2)
}
vector:
VECTORPAREN datums0 ')'
{
$$ = Dvector($2)
}
u8vector:
U8VECTORPAREN datums0 ')'
{
$$ = DbytevectorZKu8($2)
}
// END grammar
%%
// BEGIN functions
func (lex *Lexer) Lex(lval *yySymType) int {
tok := lex.nextToken()
lval.datum = tok.datum
lval.token = tok.token
return lval.token
}
func (lex *Lexer) Error(e string) {
parseErr = fmt.Errorf("Syntax error at position %d in line %s: %s", lex.pos, lex.input, e)
}
func ReadString(input string) (Any, error) {
input = strings.TrimSpace(input)
if input == "" {
return nil, nil
}
lex := newLexer(input)
yyParse(lex)
err := parseErr
parseErr = nil
return parseValue, err
}
// END functions