-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLexer.mll
90 lines (69 loc) · 2.74 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
(* Copyright Per Lindgren 2018, see the file "LICENSE" *)
(* for the full license governing this code. *)
(* cimp/Lexer.mll *)
{
open Parser
open Lexing
open Common
open Error
open State__State
open Env
exception SyntaxError of string
(* helpers *)
let badd buf lexbuf = Buffer.add_string buf (Lexing.lexeme lexbuf)
}
(* regular expressions (regexps) *)
let white = [' ' '\t']+
let newline = '\r' | '\n' | "\r\n"
let id = ['A'-'Z' 'a'-'z' '_']['0'-'9' 'A'-'Z' 'a'-'z' '_']*
let digits = ['0'-'9']+
let stringval = '"' [^'"']* '"'
(* lexing rules *)
rule lex = parse
| "IF" { IF }
| "THEN" { THEN }
| "ELSE" { ELSE }
| "END" { END }
| "WHILE" { WHILE }
| "DO" { DO }
| "DONE" { DONE }
| "TRUE" { TRUE }
| "FALSE" { FALSE }
| "&&" { AND }
| "NOT" { NOT }
| "=" { BEQ }
| "<=" { BLE }
| ">=" { BGE }
| '<' { BL }
| '>' { BG }
| "SINT" { SINT }
| "UINT32" { UINT32 }
| "as" { AS }
| ';' { SC }
| ':' { C }
| ":=" { ASSIGN }
| '+' { PLUS }
| "+u" { PLUSU }
| '-' { MINUS }
| "-u" { MINUSU }
| stringval as s { STRINGVAL s }
| digits as i { INTVAL (int_of_string i) } (* literals/values *)
| id as s { ID (add_id s) }
| white { lex lexbuf } (* white space *)
| newline { next_line lexbuf; lex lexbuf } (* new line *)
| "//" { set_info lexbuf; comment lexbuf } (* single line comment *)
| "(*" { set_info lexbuf; comments 0 lexbuf } (* nested comment *)
| '(' { LP } (* must come after comment *)
| ')' { RP }
| eof { EOF }
| _ { raise (SyntaxError("Unknown Symbol.")) }
and comment = parse
| newline { next_line lexbuf; lex lexbuf }
| eof { EOF } (* // at last line *)
| _ { comment lexbuf; }
and comments level = parse
| "*)" { if level = 0 then lex lexbuf else comments (level-1) lexbuf }
| "(*" { comments (level+1) lexbuf }
| newline { next_line lexbuf; comments level lexbuf }
| _ { comments level lexbuf }
| eof { bol lexbuf; raise (SyntaxError("Comment not closed.")) }