-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.mll
executable file
·95 lines (93 loc) · 1.7 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
{
open Parser
exception LexError of string
let line_no = ref 1
let end_of_previousline = ref 0
}
let space = [' ' '\t' '\r']
let newline = ['\n']
let digit = ['0'-'9']
let lower = ['a'-'z']
let upper = ['A'-'Z']
rule token = parse
| space+
{ token lexbuf }
| newline
{ end_of_previousline := (Lexing.lexeme_end lexbuf);
line_no := !line_no+1;
token lexbuf}
| "/*"
{ comment lexbuf;
token lexbuf }
| "_case" {CASE}
| "_fun" {FUN}
| "_dcons" {DP}
| digit digit*
{let s = Lexing.lexeme lexbuf in
INT(int_of_string s)}
| lower (digit|lower|upper|'_')*
{ let s = Lexing.lexeme lexbuf in
NAME(s)}
| upper (digit|lower|upper|'_')*
{ let s = Lexing.lexeme lexbuf in
NONTERM(s)}
| "%BEGING"
{BEGING}
| "%ENDG"
{ENDG}
| "%BEGINA"
{BEGINA}
| "%ENDA"
{ENDA}
| "%BEGINATA"
{BEGINATA}
| "%ENDATA"
{ENDATA}
| "%BEGINR"
{BEGINR}
| "%ENDR"
{ENDR}
| ","
{COMMA}
| "->"
{ARROW}
| "="
{ARROW}
| "("
{LPAR}
| ")"
{RPAR}
| "<"
{LBRA}
| ">"
{RBRA}
| "/\\"
{AND}
| "\\/"
{OR}
| "."
{PERIOD}
| "%BEGINML" [^ '%']* "%ENDML"
{let s = Lexing.lexeme lexbuf in
let s' = String.sub s 8 (String.length s - 14) in
ML(s')}
| eof
{ EOF }
| _
{ Format.eprintf "unknown token %s in line %d, column %d-%d @."
(Lexing.lexeme lexbuf)
(!line_no)
((Lexing.lexeme_start lexbuf)- (!end_of_previousline))
((Lexing.lexeme_end lexbuf)-(!end_of_previousline));
failwith "lex error" }
and comment = parse
| "*/"
{ () }
| "/*"
{ comment lexbuf;
comment lexbuf }
| eof
{ print_string "Lex error: unterminated comment\n";
failwith "unterminated comment" }
| _
{ comment lexbuf }