-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexical.jl
More file actions
329 lines (261 loc) · 10.5 KB
/
Copy pathLexical.jl
File metadata and controls
329 lines (261 loc) · 10.5 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
module Lexical
# REFERENCES
#
# [1] ISO 8879:1986 Information processing -- Text and office systems -- Standard Generalized Markup Language (SGML)
# https://www.iso.org/standard/16387.html
# [2] Extensible Markup Language (XML) 1.0 (Fifth Edition),
# https://www.w3.org/TR/xml/
# ----------------------------------------
# EXPORTED INTERFACE
# ----------------------------------------
export tokens
export location_of
# ----------------------------------------
# TOKEN TYPES
# ----------------------------------------
@enum TokenType begin
# These token names and values come from the SGML specification. See [1].
#
mdo # markup delimiter open ... <!
mdc # markup delimiter close ... > ... this one will never show up ... we'll use tagc instead.
dso # declaration subset open ... [
dsc # declaration subset close ... ]
msc # marked section close ... ]]
com # comment ... --
rni # reserved name indicator ... #
lit # literal ... "
lita # alternative literal ... '
grpo # group open ... (
grpc # group close ... )
and # and connector ... & ... this is not actually used in XML, and will never show up because ero will be found instead.
or # or connector ... |
seq # sequence connector ... ,
opt # optional occurrence indicator ... ?
rep # zero-or-more occurrence indicator ... *
plus # one-or-more occurrence indicator ... +
minus # exclusion/omission flag ... - ... this is not actually used in XML.
cro # character reference open ... &#
ero # entity reference open ... &
pero # parameter entity reference open ... %
refc # reference close ... ;
pio # processing instruction open ... <?
pic # processing instruction close ... > ... this is actually ?> in XML ... but see below in TwoCharacterTokens.
stago # start tag open ... <
etago # end tag open ... </
tagc # tag close ... >
net # null end tag ... /
vi # value indicator ... =
# The rest of these don't represent traditional SGML tokens.
#
ws # XML white-space ... see [2], § 2.3
eoi # end of input
text # anything else, except ...
illegal # this is not actually used in XML.
end
# ----------------------------------------
# TYPE DECLARATIONS
# ----------------------------------------
struct Location
identification ::String
line_number ::Int64
end
mutable struct State
io ::Union{IOStream, IOBuffer}
line_number ::Int64
last_match ::Union{Nothing, Array{Char, 1}}
length_of ::Int64
end
struct Token
token_type ::TokenType
value ::String
location ::Location
end
# ----------------------------------------
# CONSTANTS
# ----------------------------------------
# In XML, pic should be ?>, as compared to > in SGML. However, we can't actually tokenize ?> as a single token, because
# then element declarations that happen to be optional (e.g., <!ELEMENT a (b)?>) would be tokenized incorrectly (unless
# there happens to be space between the two characters) and then we'd have to bend over backwards in the element
# declaration parser. What kills me is that according to [1], § 1.1, "It shall be easy to write programs which process
# XML documents.". If they'd left pic as >, like in SGML, it would've been easier to parse: you'd end a PI by looking
# for tagc. Otherwise as it stands you'd need a context-sensitive lexical layer to distinguish the token ?> from the two
# tokens ? and >. And in the same section, "XML shall be compatible with SGML." ... well it isn't: PIs in SGML by
# default end with a tagc (i.e., >), not with the sequence ?>. Anyway, that's my tirade for today.
#
const TwoCharacterTokens = Dict([ '<', '!' ] => mdo,
[ ']', ']' ] => msc,
[ '-', '-' ] => com,
[ '&', '#' ] => cro,
[ '<', '?' ] => pio,
[ '<', '/' ] => etago)
const OneCharacterTokens = Dict('>' => mdc, # Again, this will never show up ... we'll always emit tagc instead.
'[' => dso,
']' => dsc,
# '#' => rni, ... Including this as a token in XML makes parsing reserved names a pain.
'\"' => lit,
'\'' => lita,
'(' => grpo,
')' => grpc,
'&' => and, # Again, this will never show up ... we'll always emit ero instead.
'|' => or,
',' => seq,
'?' => opt,
'*' => rep,
'+' => plus,
# '-' => minus, ... This is not a token in XML, and including it causes too many headaches.
'&' => ero,
'%' => pero,
';' => refc,
'<' => stago,
'>' => tagc,
'/' => net,
'=' => vi)
const IllegalCharacters = Set(vcat('\u00':'\u08', '\u0e':'\u1f', '\ud800':'\udfff', [ '\u0b', '\u0c', '\ufffe', '\uffff' ]))
const TokenStarts = Set([ '\"', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '/', ';', '<', '=', '>', '?', '[', ']', '|' ])
const WhiteSpaces = Set([ '\u20', '\u09', '\u0a', '\u0d' ])
# ----------------------------------------
# FUNCTIONS
# ----------------------------------------
State(io) = State(io, -1, nothing, -1)
Token(token_type::TokenType, token_value::String, state::State) = Token(token_type, token_value, location_of(state))
location_of(token::Token) = token.location
function consume_last_match(state::State)
skip(state.io, state.length_of)
return state.last_match
end
function consume_until(state::State, end_markers::Set{Char})
consumed = Array{Char, 1}()
start = position(state.io)
bytes_read = 0
while !eof(state.io)
c = read(state.io, Char)
if c ∈ end_markers
if c == '-'
# Be careful here: '-' is a token in SGML (minus), but in XML we need to look ahead to see if it is
# followed by another '-'. If so, then "--" is a token (com), but only if it, in turn, is followed by
# '>' (mdc/tagc). Otherwise it is part of the current token (text).
#
# Ultimately, it might be easier to handle this in an other layer, by consuming a sequence of tokens and
# grouping then. But this will do for now.
#
if eof(state.io)
bytes_read = position(state.io) - start
push!(consumed, c)
break
end
current_position = position(state.io)
if read(state.io, Char) == '-' && !eof(state.io) && read(state.io, Char) ∈ [ '-', '>' ]
break
else
# Keep going.
#
seek(state.io, current_position)
end
else
break
end
end
bytes_read = position(state.io) - start
push!(consumed, c)
end
return ( bytes_read, consumed )
end
function consume_while(state::State, set::Set{Char})
consumed = Array{Char, 1}()
start = position(state.io)
bytes_read = 0
while !eof(state.io)
c = read(state.io, Char)
if c ∉ set
break
end
bytes_read = position(state.io) - start
push!(consumed, c)
end
return ( bytes_read, consumed )
end
function location_of(state::State)
name_of(io::IOStream) = io.name
name_of(io) = "a buffer"
return Location(name_of(state.io), -1)
end
function is_delimiter_one(state::State)::Bool
start = mark(state.io)
token_value = read(state.io, Char)
current = position(state.io)
reset(state.io)
if haskey(OneCharacterTokens, token_value)
state.last_match = [ token_value ]
state.length_of = current - start
return true
else
return false
end
end
function is_delimiter_two(state::State)::Bool
start = mark(state.io)
first = read(state.io, Char)
if eof(state.io)
reset(state.io)
return false
else
second = read(state.io, Char)
current = position(state.io)
reset(state.io)
if haskey(TwoCharacterTokens, [ first, second ])
state.last_match = [ first, second ]
state.length_of = current - start
return true
else
return false
end
end
end
function is_text(state::State)::Bool
start = mark(state.io)
( bytes_read, token_value ) = consume_until(state, ∪(TokenStarts, WhiteSpaces, IllegalCharacters))
reset(state.io)
if length(token_value) > 0
state.last_match = token_value
state.length_of = bytes_read
return true
else
return false
end
end
function is_white_space(state::State)::Bool
start = mark(state.io)
( bytes_read, token_value ) = consume_while(state, WhiteSpaces)
reset(state.io)
if length(token_value) > 0
state.last_match = token_value
state.length_of = bytes_read
return true
else
return false
end
end
function tokens(state::State)
function next(state::State)::Union{Token, Nothing}
if is_delimiter_two(state)
token_value = consume_last_match(state)
return Token(TwoCharacterTokens[token_value], String(token_value), state)
elseif is_delimiter_one(state)
token_value = consume_last_match(state)
return Token(OneCharacterTokens[token_value[1]], String(token_value), state)
elseif is_text(state)
return Token(text, String(consume_last_match(state)), state)
elseif is_white_space(state)
return Token(ws, String(consume_last_match(state)), state)
else
return Token(illegal, string(read(state.io, Char)), state)
end
end
function tokenized(channel::Channel)
while !eof(state.io)
put!(channel, next(state))
end
end
return Channel(tokenized; ctype = Token, csize = 1)
end
end