-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.mll
83 lines (80 loc) · 1.95 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
{
open Parser
}
let space = [' ' '\t' '\n' '\r']
let digit = ['0'-'9']
let lower = ['a'-'z']
let upper = ['A'-'Z']
rule token = parse
| space+ { token lexbuf }
| "(*" { comment lexbuf; token lexbuf }
| '(' { LPAREN }
| ')' { RPAREN }
| '[' { LBRACK }
| ']' { RBRACK }
| "::" { CONS }
| '@' { AT }
| "as" { AS }
| "begin" { BEGIN }
| "end" { END }
| "match" { MATCH }
| "with" { WITH }
| "when" { WHEN }
| "->" { ARROW }
| "|" { BAR }
| "type" { TYPE }
| "of" { OF }
| ";;" { SEMISEMI }
| "true" { BOOL(true) }
| "false" { BOOL(false) }
| "not" { NOT }
| digit+ { INT(int_of_string (Lexing.lexeme lexbuf)) }
| digit+ ('.' digit*)? (['e' 'E'] ['+' '-']? digit+)?
{ FLOAT(float_of_string (Lexing.lexeme lexbuf)) }
| '-' { MINUS }
| '+' { PLUS }
| '*' { AST }
| "-." { MINUS_DOT }
| "+." { PLUS_DOT }
| "*." { AST_DOT }
| "/." { SLASH_DOT }
| '=' { EQUAL }
| "<>" { LESS_GREATER }
| "<=" { LESS_EQUAL }
| ">=" { GREATER_EQUAL }
| '<' { LESS }
| '>' { GREATER }
| "if" { IF }
| "then" { THEN }
| "else" { ELSE }
| "let" { LET }
| "in" { IN }
| "rec" { REC }
| "mutable" { MUTABLE }
| "open" { OPEN }
| '{' { LBRACE }
| '}' { RBRACE }
| ':' { COLON }
| ',' { COMMA }
| '_' { IDENT(Syntax.gentmp ()) }
| '.' { DOT }
| "<-" { LESS_MINUS }
| ":=" { COLON_EQUAL }
| '!' { EXCLAM }
| ';' { SEMICOLON }
| eof { EOF }
| '"' ([^ '"' '\\'] | '\\' _)* '"' { let s = Lexing.lexeme lexbuf in STRING(String.sub s 1 ((String.length s)-2))}
| upper (digit|lower|upper|'_')* '.' lower (digit|lower|upper|'_')* { IDENT(Lexing.lexeme lexbuf) }
| upper (digit|lower|upper|'_')* { CIDENT(Lexing.lexeme lexbuf) }
| lower (digit|lower|upper|'_')* { IDENT(Lexing.lexeme lexbuf) }
| _ {
failwith
(Printf.sprintf "unknown token %s near characters %d-%d"
(Lexing.lexeme lexbuf)
(Lexing.lexeme_start lexbuf)
(Lexing.lexeme_end lexbuf)) }
and comment = parse
| "*)" { () }
| "(*" { comment lexbuf; comment lexbuf }
| eof { Format.eprintf "warning: unterminated comment@." }
| _ { comment lexbuf }