-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathParse.fs
218 lines (189 loc) · 4.57 KB
/
Parse.fs
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
######################################################
##
## Parse:
##
## A lexicon for parsing input from stdin or from
## pre-existing strings. Includes an error handler
## intended for reading from a file via stdin.
##
## Parse requires routines from Print.fs and
## String.fs in order to function.
##
## John Earnest
##
######################################################
:const qs 16 # input queue size
:const pad-size 32 # identifier input buffer size
: revector 1 + ! ; ( 'new-word 'word -- )
: inc dup @ 1 + swap ! ; ( addr -- )
: dec dup @ 1 - swap ! ; ( addr -- )
######################################################
##
## Error Handler and Input Subsystem:
##
## By default, the parser will read from stdin.
## using '>read' will redirect the input system
## to read from a string.
##
######################################################
:data line 1
:data char 1
: advance ( char -- )
10 = if
line inc
1 char !
else
char inc
then
;
:vector fail ( string -- )
type
" (line " type line @ .num
", char " type char @ .num
")" typeln
halt
;
:vector read CO @ ; ( -- char )
:var text-src
: read-text ( -- char )
text-src @ @ dup if
text-src inc
else
drop -1 # EOF
then
;
:proto clear-q
: >read ( string -- )
text-src !
clear-q
' read-text ' read revector
;
######################################################
##
## Input Buffering:
##
## This buffer provides a fixed-size lookahead
## window on the input stream, which is necessary
## for some high-level parsing words. xq can fetch
## an arbitrary index into the lookahead buffer.
## Take care when using words other than 'xq',
## 'pull', 'skip', 'curr' and 'getc' explicitly that
## you advance the input buffer as necessary.
##
######################################################
:array q qs 0
:var a # head
:var b # tail
:var s # size
: q+ dup @ 1 + qs mod swap ! ; ( addr -- )
: >q a @ q + ! a q+ s inc ; ( char -- )
: q> b @ q + @ b q+ s dec dup advance ; ( -- char )
: clear-q 0 a ! 0 b ! 0 s ! ; ( -- )
: xq ( index -- char )
s @ over <= if
dup s @ - for
read >q
next
then
b @ + qs mod q + @
;
: pull s @ 1 < if read >q then ; ( -- )
: skip pull q> drop ; ( -- )
: curr pull b @ q + @ ; ( -- char )
: getc pull q> ; ( -- char )
######################################################
##
## Basic Parsing Words:
##
## A series of handy predicates which operate on
## the head of the input stream.
##
######################################################
: space? curr white? ; ( -- flag )
: name? curr letter? ; ( -- flag )
: numeral? curr digit? ; ( -- flag )
: eof? curr -1 = ; ( -- flag )
: newline? curr 10 = ; ( -- flag )
######################################################
##
## High-Level Parsing Words:
##
## trim - advance past any whitespace.
## starts? - return true if input begins with a string.
## match? - like starts? but accept the string on a match.
## expect - like match? but errors on failure.
## number> - read an unsigned integer.
## signed? - return true if input begins with a signed integer.
## signed> - read a signed integer.
## input> - read into a specified buffer as long as pred is satisfied.
## accept> - read into pad as long as a pred is satisfied and trim.
## token> - accept> (letter)(letter|digit)*.
## line> - read into a specified buffer until a newline.
##
######################################################
: trim ( -- )
space? if
loop skip space? while
then
;
: starts? ( string -- flag )
dup 0 loop
( string char* queue-index )
over @ -if
2drop drop true exit
then
over @ over xq != if
2drop drop false exit
then
1 + swap 1 + swap
again
;
: match? ( string -- flag )
dup starts? if
size
1 - for skip next
trim true
else
drop false
then
;
: expect ( string -- )
dup match? -if "Expected '" type type "'." fail then
drop
;
: number> ( -- n )
numeral? -if "Numeral expected." fail then
0 loop
10 * getc to-num +
numeral?
while trim
;
: signed?
numeral? if true exit then
"-" starts? 1 xq digit? and
;
: signed> ( -- n )
"-" match? if -1 else 1 then
number> *
;
: input> ( addr len pred -- )
>r loop
dup 1 > -if break then
i exec -if break then
over getc swap !
1 - swap 1 + swap
again
r> 2drop
0 swap !
;
:array pad pad-size 0
: accept> ( pred -- string )
>r pad pad-size r> input> trim pad
;
: token> ( -- string )
name? -if "Name expected." fail then
{ name? numeral? or } accept>
;
: line> ( addr len -- )
{ newline? not } input> skip
;