-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.sml
More file actions
297 lines (233 loc) · 9.4 KB
/
token.sml
File metadata and controls
297 lines (233 loc) · 9.4 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
structure token :> token =
struct
datatype token = Key of string | Id of string;
(*new stuff *)
exception TER of string
fun is_char(l,i,u) = l <= i andalso i <= u;
fun is_symbol c =
let val cl = List.map ord [#"=",#"<",#">",#"-",#":",#"*",#"(",#")"]
in mem c cl
end
fun is_letter_or_digit c =
is_char(ord #"A",c,ord #"Z") orelse is_char(ord #"a",c,ord #"z") orelse is_char(ord #"0",c,ord #"9") orelse
is_char(913,c,937) orelse is_char(945,c,969) orelse is_char(8320,c,8329) orelse
c = 0x00B9 orelse c = 0x00B2 orelse c = 0x00B3 orelse is_char(0x2074,c,0x2079) orelse
c = ord #"'" orelse c = ord #"_";
fun token_of a = if mem a ["ar","ob","o","!","?","==>","<=>",":","->","(*","*)","=="] then (Key a) else (Id a);
fun char_of a = case a of Key a0 => a0 | Id a0 => a0
fun getN s n =
if n <= 0 then ([], s)
else case UTF8.getChar s of
NONE => ([], s)
| SOME ((cs,_), s') =>
let val (css, s'') = getN s' (Int.-(n ,1))
in
(cs::css, s'')
end
(*
(* abcde*) P(a) <=> Q(c)
PQ(A)
"==="
*)(*string list * string -> token * string*)
fun scan_symbol s =
let
val (l1,s1) = getN s 1
val (l2,s2) = getN s 2
val (l3,s3) = getN s 3
val syml =
["=","<",">","-",":","*","(",")"]
in
if l3 = ["=","=",">"] then (Key "==>",s3) else
if l3 = ["<","=",">"] then (Key "<=>",s3) else
if l2 = ["(","*"] then (Key "(*",s2) else
if l2 = ["*",")"] then (Key "*)",s2) else
if l2 = ["-",">"] then (Key "->",s2) else
if l2 = ["=","="] then (Key "==",s2) else
if mem (List.hd l1) syml then (Key $ List.hd l1,s1) else
(Id "",s)
end
(* lex "P (* a*) + Q";
val it = [Id "P", Key "+", Id "Q"]: token list
lex "P (* a*) * Q";
val it = [Id "P", Key "*", Id "Q"]: token list
old scan symbol can do
*)
(*
fun scan_symbol (front, s) =
case UTF8.getChar s of
NONE =>
(token_of (String.concat $ rev front),s)
| SOME ((s0,i),rest) =>
if is_symbol i then
scan_symbol (s0 :: front,rest)
else
(token_of (String.concat $ rev front),s)
*)
fun scan_ident (front, s) =
case UTF8.getChar s of
NONE =>
(token_of (String.concat $ rev front),s)
| SOME ((s0,i),rest) =>
if is_letter_or_digit i then
scan_ident (s0 :: front,rest)
else
(token_of (String.concat $ rev front),s)
fun scan (front_toks, "") cd =
if cd = 0 then rev front_toks (*end of char list*)
else raise TER "end of comment not found"
(*long infix operators*)
| scan (front_toks,s) 0 =
let val (tok,rest) = scan_symbol s
in
case tok of
Key "==>" =>
scan(Key"==>" :: front_toks,rest) 0
| Key "<=>" =>
scan(Key"<=>" :: front_toks,rest) 0
| Key "->" =>
scan(Key"->" :: front_toks,rest) 0
| Key "==" =>
scan(Key"==" :: front_toks,rest) 0
| Key ":" =>
scan(Key":" :: front_toks,rest) 0
| Key "(*" =>
scan(front_toks,rest) 1
| _ =>
case UTF8.getChar s of
NONE => raise TER "unexpected eng of string"
| SOME ((s0,i),rest) =>
if is_letter_or_digit i then
scannext(front_toks,
scan_ident([s0],rest)) 0
else
if mem s0 [" ","\n","\t"] then
scan (front_toks,rest) 0
else
scan (Key s0 :: front_toks,rest) 0
end
| scan (front_toks,s) cd =
if cd < 0 then raise TER "unexpected comment depth"
else
let val (tok,rest) = scan_symbol s
in if tok = Key "(*" then
scan (front_toks, rest) (cd + 1) else
if tok = Key "*)" then
if cd > 0 then
scan (front_toks, rest) (Int.-(cd,1)) else
raise TER "beginning of comment not found"
else
case UTF8.getChar s of
SOME(_,rest) => scan (front_toks, rest) cd
| _ => raise TER "end of comment not found'"
end
and scannext (front_toks, (tok, cs)) n = scan (tok::front_toks, cs) n;
fun enclose a = "(" ^ a ^ ")";
fun tokentoString tok =
case tok of
Key s => "Key" ^ enclose s
| Id s => "Id" ^ enclose s
fun lex s = scan ([],s) 0
(*
fun token_of a = if mem a ["ar","ob","o","!","?"] then (Key a) else (Id a);
fun is_char(l,c,u) = ord l <= ord c andalso ord c <= ord u;
fun is_letter_or_digit c =
is_char(#"A",c,#"Z") orelse is_char(#"a",c,#"z") orelse is_char(#"0",c,#"9") orelse c = #"'";
fun scan_ident (front, c::cs) =
if is_letter_or_digit c
then scan_ident (c::front, cs)
else (token_of (implode(rev front)), c::cs)
| scan_ident (front, []) = (token_of (implode(rev front)), []);
(*
exception TER of string
fun lex s = scan ([],s) 0
fun scan (front_toks, "") cd =
if cd = 0 then rev front_toks (*end of char list*)
else raise TER "end of comment not found"
(*long infix operators*)
| scan(front_toks, str) cd =
if cd = 0 then
case getChar str of
SOME ((str0,i0),rest0) =>
(case getChar str0 of
SOME ((str1,i1),rest1) =>
case )
let val ((str0,i0),rest0) = getChar str
val ((str1,i1),rest1) = getChar str0
val ((str2,i2),rest2) = getChar str1
in
if str0 = "=" andalso
str1 = "=" andalso str2 = ">"
then scan (Key "==>" :: front_toks,rest2) 0 (*else
if str0 = "<" andalso
str1 = "=" andalso str2 = ">"
then scan (Key "<=>" :: front_toks,rest2) 0 else
if str0 = "-" andalso str1 = ">"
then scan (Key "->" :: front_toks,rest1) 0 else
if str0 = ":"
then scan (Key ":" :: front_toks,rest0) 0 else
if str0 = " " orelse
str0 = "\t" orelse str0 = "\n"
then scan (front_toks,rest0) 0 else
if is_letter_or_digit i0 then
scannext(front_toks, scan_ident([str0],rest0)) 0
*) else scan (Key(str0) :: front_toks,rest0)
end
else raise TER "unexpected comment depth"
(#"=")::(#"=")::(#">")::cs => scan (Key"==>" ::front_toks, cs) 0
| (#"<")::(#"=")::(#">")::cs => scan (Key"<=>" ::front_toks, cs) 0
| (#"-")::(#">")::cs => scan (Key"->" ::front_toks, cs) 0
| (#":")::cs => scan (Key":" ::front_toks, cs) 0
| (#" ")::cs => scan (front_toks, cs) 0
| (#"\t")::cs => scan (front_toks, cs) 0
| (#"\n")::cs => scan (front_toks, cs) 0
| c:: cs => if is_letter_or_digit c then scannext(front_toks, scan_ident([c], cs)) 0
else scan (Key(str c)::front_toks, cs) 0
else if cd < 0 then raise TER "unexpected comment depth" else scan (front_toks, cs) cd
| scan (front_toks, (#"(")::(#"*")::cs) cd = scan (front_toks,cs) (cd+1)
| scan (front_toks, (#"*")::(#")")::cs) cd =
if cd > 0 then scan (front_toks,cs) (cd-1) else raise TER "beginning of comment not found"
| scan (front_toks,c::cs) cd =
if cd = 0 then
case (c::cs) of
(#"=")::(#"=")::(#">")::cs => scan (Key"==>" ::front_toks, cs) 0
| (#"<")::(#"=")::(#">")::cs => scan (Key"<=>" ::front_toks, cs) 0
| (#"-")::(#">")::cs => scan (Key"->" ::front_toks, cs) 0
| (#":")::cs => scan (Key":" ::front_toks, cs) 0
| (#" ")::cs => scan (front_toks, cs) 0
| (#"\t")::cs => scan (front_toks, cs) 0
| (#"\n")::cs => scan (front_toks, cs) 0
| c:: cs => if is_letter_or_digit c then scannext(front_toks, scan_ident([c], cs)) 0
else scan (Key(str c)::front_toks, cs) 0
else if cd < 0 then raise TER "unexpected comment depth" else scan (front_toks, cs) cd
and scannext (front_toks, (tok, cs)) n = scan (tok::front_toks, cs) n;
fun string_of_char (# a) = a
*)
fun scan (front_toks, []) cd =
if cd = 0 then rev front_toks (*end of char list*)
else raise TER "end of comment not found"
(*long infix operators*)
| scan (front_toks, (#"(")::(#"*")::cs) cd = scan (front_toks,cs) (cd+1)
| scan (front_toks, (#"*")::(#")")::cs) cd =
if cd > 0 then scan (front_toks,cs) (cd-1) else raise TER "beginning of comment not found"
| scan (front_toks,c::cs) cd =
if cd = 0 then
case (c::cs) of
(#"=")::(#"=")::(#">")::cs => scan (Key"==>" ::front_toks, cs) 0
| (#"<")::(#"=")::(#">")::cs => scan (Key"<=>" ::front_toks, cs) 0
| (#"-")::(#">")::cs => scan (Key"->" ::front_toks, cs) 0
| (#":")::cs => scan (Key":" ::front_toks, cs) 0
| (#" ")::cs => scan (front_toks, cs) 0
| (#"\t")::cs => scan (front_toks, cs) 0
| (#"\n")::cs => scan (front_toks, cs) 0
| c:: cs => if is_letter_or_digit c then scannext(front_toks, scan_ident([c], cs)) 0
else scan (Key(str c)::front_toks, cs) 0
else if cd < 0 then raise TER "unexpected comment depth" else scan (front_toks, cs) cd
and scannext (front_toks, (tok, cs)) n = scan (tok::front_toks, cs) n;
fun enclose a = "(" ^ a ^ ")";
fun tokentoString tok =
case tok of
Key s => "Key" ^ enclose s
| Id s => "Id" ^ enclose s
fun lex s = scan ([],explode s) 0
*)
end