forked from artagnon/rhine-ml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.mll
40 lines (36 loc) · 1.2 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
{
open Parser
exception SyntaxError of string
}
let digit = ['0'-'9']
let characters = ['a'-'z' 'A'-'Z']
let symbol_chars = ['a'-'z' 'A'-'Z' '?' '-' '+' '*' '/' '<' '>' '=' '.' '%' '^']
let e = ['E''e']['-''+']?['0'-'9']+
rule token = parse
| [' ' '\t' '\r' '\n'] { token lexbuf }
| ";;" { comment lexbuf }
| '`'+ as s { SQUOTE(String.length s) }
| '~'+ as s { UNQUOTE(String.length s) }
| '(' { LPAREN }
| ')' { RPAREN }
| '[' { LSQBR }
| ']' { RSQBR }
| "nil" { NIL }
| "true" { TRUE }
| "false" { FALSE }
| '&' { REST_ARGS }
| (['0'-'9']*)'.'['0'-'9']+e? as s { DOUBLE(float_of_string s) }
| (['0'-'9']+)'.'['0'-'9']*e? as s { DOUBLE(float_of_string s) }
| (['0'-'9']+)e as s { DOUBLE(float_of_string s) }
| ['-''+']?digit+ as s { INTEGER(int_of_string s) }
| (symbol_chars (digit|symbol_chars)*) as s { SYMBOL(s) }
| '\''[^'\'']'\'' as s { CHAR(s.[1]) }
| '"' { let b = Buffer.create 1024 in read_string b lexbuf }
| eof { EOF }
and comment = parse
'\n' { token lexbuf }
| _ { comment lexbuf }
and read_string b = parse
"\\\"" { Buffer.add_string b "\""; read_string b lexbuf }
| '"' { STRING(Buffer.contents b) }
| [^'"'] { Buffer.add_string b (Lexing.lexeme lexbuf); read_string b lexbuf }