forked from esumii/min-caml
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexer.mll
100 lines (98 loc) · 1.83 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
{
(* lexerが利用する変数、関数、型などの定義 *)
open Parser
open Type
}
(* 正規表現の略記 *)
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 }
| "true"
{ BOOL(true) }
| "false"
{ BOOL(false) }
| "not"
{ NOT }
| digit+ (* 整数を字句解析するルール (caml2html: lexer_int) *)
{ INT(int_of_string (Lexing.lexeme lexbuf)) }
| digit+ ('.' digit*)? (['e' 'E'] ['+' '-']? digit+)?
{ FLOAT(float_of_string (Lexing.lexeme lexbuf)) }
| '-' (* -.より後回しにしなくても良い? 最長一致? *)
{ MINUS }
| '+' (* +.より後回しにしなくても良い? 最長一致? *)
{ PLUS }
| "-."
{ 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 }
| ','
{ COMMA }
| '_'
{ IDENT(Id.gentmp Type.Unit) }
| "Array.create" | "Array.make" (* [XX] ad hoc *)
{ ARRAY_CREATE }
| '.'
{ DOT }
| "<-"
{ LESS_MINUS }
| ';'
{ SEMICOLON }
| eof
{ EOF }
| 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 }