-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLexer.mll
187 lines (167 loc) · 5.24 KB
/
Lexer.mll
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
(*
* Copyright (c) 2015 Stefan Krah. All rights reserved.
*
* This file is distributed under the terms of the Q Public License
* version 1.0.
*)
{
open Lexing
module P = Parser
let implode = Util.implode
let explode = Util.explode
let create_hashtable = Util.create_hashtable
exception Error of string
module StringFilter :
sig
val unescape : string -> string
end =
struct
let dtochr (x, y, z) =
let s = implode [x; y; z] in
try let r = (int_of_string s) in
if r > 127 then raise (Error "character code greater than 127")
else Char.chr r
with Failure s -> raise (Error s)
let rec filter lst =
match lst with
[] -> []
| '\\' :: ys -> (match ys with
'n' :: xs -> '\n' :: (filter xs)
| 't' :: xs -> '\t' :: (filter xs)
| '\'' :: xs -> '\'' :: (filter xs)
| '\\' :: xs -> '\\' :: (filter xs)
| ' ' :: xs -> (filter_skip xs)
| '\n' :: xs -> (filter_skip xs)
| '\t' :: xs -> (filter_skip xs)
| '\012' :: xs -> (filter_skip xs)
| '^' :: x :: xs -> (Char.chr(Char.code x - 64)) :: (filter xs)
| x :: y :: z :: xs -> dtochr(x, y, z) :: (filter xs)
| _ -> raise (Error "invalid escape"))
| y :: ys -> y :: (filter ys)
and filter_skip lst =
match lst with
' ' :: xs -> (filter_skip xs)
| '\n' :: xs -> (filter_skip xs)
| '\t' :: xs -> (filter_skip xs)
| '\012' :: xs -> (filter_skip xs)
| '\\' :: xs -> (filter xs)
| _ -> raise (Error "invalid string continuation")
let unescape s =
let n = String.length s in
if n <= 2 then ""
else let s' = String.sub s 1 (n - 2) in
let lst = (explode s') in
implode (filter lst)
end
let keyword_table =
create_hashtable 101 [
"and", P.AND;
"external", P.EXTERNAL;
"do", P.DO;
"done", P.DONE;
"else", P.ELSE;
"false", P.FALSE;
"fun", P.FUN;
"for", P.FOR;
"if", P.IF;
"in", P.IN;
"let", P.LET;
"mutable", P.MUTABLE;
"nil", P.NIL;
"rec", P.REC;
"then", P.THEN;
"true", P.TRUE;
"to", P.TO;
"type", P.TYPE;
"while", P.WHILE
]
}
let id = ['A'-'Z' 'a'-'z' '_'] ['A'-'Z' 'a'-'z' '0'-'9' '_']*
let pri = ['\032'-'\033' '\035'-'\091' '\093'-'\126']
let str = '"'
('\\' ['n' 't' '"' '\\']
| '\\' '^' ['@' 'A'-'Z' '\\' '[' '^' ']' '_' ]
| '\\' ['0'-'9'] ['0'-'9'] ['0'-'9'] | ['a'-'z']
| pri
| '\\' ['\\' '\n' '\t' '\012']* '\\')*
'"'
let digit = ['0'-'9']
let integer = (digit)*
let intpart = (digit)+
let fraction = '.' (digit)+
let exponent = ('e' | 'E') ('+' | '-')? (digit)+
let pointfloat = (intpart)? (fraction) | intpart '.'
let exponentfloat = (intpart | pointfloat) exponent
let floatnumber = pointfloat | exponentfloat
rule token state = parse
| "(*"
{ let module LexState = (val state : LexState.S) in
let open LexState in
assert (open_comments () = 0);
incr_open_comments ();
comment state lexbuf }
| "->" { P.RARROW }
| "<-" { P.LARROW }
| ":=" { P.ASSIGN }
| ":" { P.COLON }
| "==" { P.EQEQUAL }
| "=" { P.EQUAL }
| "<>" { P.NOTEQUAL }
| "<=" { P.LESSEQUAL }
| "<" { P.LESS }
| ">=" { P.GREATEREQUAL }
| ">" { P.GREATER }
| "||" { P.DOUBLEBAR }
| "&&" { P.DOUBLEAMPER }
| ";;" { P.SEMISEMI }
| "**" { P.DOUBLESTAR }
| "/." { P.SLASHDOT }
| "*." { P.STARDOT }
| "-." { P.MINUSDOT }
| "+." { P.PLUSDOT }
| "/" { P.SLASH }
| "*" { P.STAR }
| "-" { P.MINUS }
| "+" { P.PLUS }
| "(" { P.LPAR }
| ")" { P.RPAR }
| "{" { P.LBRACE }
| "}" { P.RBRACE }
| "[" { P.LBRACK }
| "]" { P.RBRACK }
| "." { P.DOT }
| "," { P.COMMA }
| ";" { P.SEMI }
| "!" { P.BANG }
| "'" { P.QUOTE }
| "_" { P.UNDERSCORE }
| str
{ let s = Lexing.lexeme lexbuf in
P.STRING(StringFilter.unescape s) }
| id
{ let s = Lexing.lexeme lexbuf in
try Hashtbl.find keyword_table s
with Not_found -> P.ID s }
| integer { let s = Lexing.lexeme lexbuf in P.INT s }
| floatnumber { let s = Lexing.lexeme lexbuf in P.FLOAT s }
| "\n" { new_line lexbuf; token state lexbuf }
| [' ' '\r' '\t']+ { token state lexbuf }
| eof { P.EOF }
| _ { let s = Lexing.lexeme lexbuf in
raise (Error (Printf.sprintf "invalid character: %s" s)) }
and comment state = parse
| "(*"
{ let module LexState = (val state : LexState.S) in
let open LexState in
assert (open_comments () > 0);
incr_open_comments ();
comment state lexbuf }
| "*)"
{ let module LexState = (val state : LexState.S) in
let open LexState in
assert (open_comments () > 0);
decr_open_comments ();
if open_comments () = 0 then token state lexbuf
else comment state lexbuf }
| "\n" { new_line lexbuf; comment state lexbuf }
| _ { comment state lexbuf }