Rename "tiger" dir to "compiler"
[tiger.ml.git] / compiler / src / lib / tiger / tiger_lexer.mll
1 {
2 open Tiger_parser
3
4 let comment_level = ref 0
5 let string_buf = Buffer.create 100
6 }
7
8 let alpha = ['a'-'z' 'A'-'Z']
9 let num = ['0'-'9']
10 let newline = '\n' | '\r' | "\n\r"
11
12 rule token = parse
13 | eof {EOF}
14
15 (* Track line number *)
16 | newline {
17 Lexing.new_line lexbuf;
18 token lexbuf
19 }
20
21 (* Comment *)
22 | "/*" {
23 incr comment_level;
24 comment lexbuf
25 }
26
27 | ":=" {ASSIGN}
28 | "<=" {LE}
29 | ">=" {GE}
30 | "<>" {NEQ}
31 | '&' {AND}
32 | '(' {LPAREN}
33 | ')' {RPAREN}
34 | '*' {TIMES}
35 | '+' {PLUS}
36 | '-' {MINUS}
37 | '/' {DIVIDE}
38 | ',' {COMMA}
39 | '.' {DOT}
40 | ':' {COLON}
41 | ';' {SEMICOLON}
42 | '>' {GT}
43 | '<' {LT}
44 | '=' {EQ}
45 | '[' {LBRACK}
46 | ']' {RBRACK}
47 | '{' {LBRACE}
48 | '}' {RBRACE}
49 | '|' {OR}
50
51 (* String literal *)
52 | '"' {
53 string_literal lexbuf
54 }
55
56 (* Drop whitespace *)
57 | [' ' '\t'] {
58 token lexbuf
59 }
60
61 | (num+ as int) {
62 INT (int_of_string int)
63 }
64
65 | (alpha (alpha | num | '_')* as id) {
66 match id with
67 | "array" -> ARRAY
68 | "break" -> BREAK
69 | "do" -> DO
70 | "else" -> ELSE
71 | "end" -> END
72 | "for" -> FOR
73 | "function" -> FUNCTION
74 | "if" -> IF
75 | "in" -> IN
76 | "let" -> LET
77 | "nil" -> NIL
78 | "of" -> OF
79 | "then" -> THEN
80 | "to" -> TO
81 | "type" -> TYPE
82 | "var" -> VAR
83 | "while" -> WHILE
84 | _ -> (ID id)
85 }
86 and string_literal = parse
87 (* Keep escaped quote marks as part of the string literal *)
88 | '\\' '"' {
89 Buffer.add_char string_buf '"';
90 string_literal lexbuf
91 }
92
93 | '"' {
94 let string = Buffer.contents string_buf in
95 Buffer.reset string_buf;
96 STRING string
97 }
98
99
100 | (_ as c) {
101 Buffer.add_char string_buf c;
102 string_literal lexbuf
103 }
104 and comment = parse
105 (* TODO: Error: unterminated comment? or we don't care? *)
106 | eof {EOF}
107
108 (* Track line number *)
109 | newline {
110 Lexing.new_line lexbuf;
111 comment lexbuf
112 }
113
114 | "/*" {
115 incr comment_level;
116 comment lexbuf
117 }
118
119 | "*/" {
120 decr comment_level;
121 match !comment_level with
122 | 0 -> token lexbuf
123 | n when n > 0 -> comment lexbuf
124 | _ -> assert false
125 }
126
127 (* Drop comment contents *)
128 | _ {
129 comment lexbuf
130 }
This page took 0.049424 seconds and 4 git commands to generate.