-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmik_lexer.mll
More file actions
143 lines (134 loc) · 3.37 KB
/
mik_lexer.mll
File metadata and controls
143 lines (134 loc) · 3.37 KB
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
{
open Mik_parser
exception Error of string
let new_line lexbuf =
Lexing.new_line lexbuf
let predefined_classes = [
("lower", {|[a-z]|});
("upper", {|[A-Z]|});
("alpha", {|[A-Za-z]|});
("digit", {|[0-9]|});
("alnum", {|[0-9A-Za-z]|});
("punct", {|[!-/:-@[-`{-~]|});
("graph", {|[!-~]|});
("print", {|[ -~]|});
("blank", {|[\t ]|});
("cntrl", "[\x00-\x1f\x7f]");
("xdigit", {|[0-9A-Fa-f]]|});
("space", {|[\t-\r ]|});
(* ("word", {|[0-9A-Za-z_]|}); *)
("eos", {|$|});
("eol", {|$|[\n]|});
("bnd", {|\b|});
("bos", {|^|});
("bol", {|^|[\n]|});
("any", {|[\s\S]|});
("notnl", {|[^\n]|});
]
let escape_char = function
| 'n' -> '\n'
| 't' -> '\t'
| 'r' -> '\r'
| 'b' -> '\b'
| '\\' -> '\\'
| '\'' -> '\''
| '"' -> '"'
| c -> c
let escape_special = function
| '(' -> {|\(|}
| ')' -> {|\)|}
| '[' -> {|\[|}
| ']' -> {|\]|}
| '{' -> {|\{|}
| '}' -> {|\}|}
| '.' -> {|\.|}
| '*' -> {|\*|}
| '+' -> {|\+|}
| '?' -> {|\?|}
| '^' -> {|\^|}
| '$' -> {|\$|}
| '|' -> {|\||}
| c -> String.make 1 c
let needs_escape = function
| '(' | ')' | '[' | ']' | '{' | '}' | '.' | '*' | '+' | '?' | '^' | '$' | '|' -> true
| _ -> false
}
let whitespace = [' ' '\t' '\r']
let lowercase = ['a'-'z']
let uppercase = ['A'-'Z']
let alpha = lowercase | uppercase
let digit = ['0'-'9']
let ident = (lowercase | '_') (alpha | digit | '_' | '\'')*
let module_name = uppercase (alpha | digit | '_' | '\'')*
let module_ident = module_name ('.' (module_name | ident))*
rule token = parse
| [' ' '\t' '\r']+ { token lexbuf }
| '\n' { new_line lexbuf; token lexbuf }
| '/' { SLASH }
| '(' { LPAREN }
| ')' { RPAREN }
| '[' { LBRACKET }
| ']' { RBRACKET }
| '^' { CARET }
| '{' { LBRACE }
| '}' { RBRACE }
| '-' { DASH }
| '|' { BAR }
| '*' { STAR }
| '+' { PLUS }
| '?' { QUESTION }
| '_' { UNDERSCORE }
| ':' { COLON }
| '=' { EQUAL }
| '~' { TILDE }
| "as" { AS }
| ">>>" { PIPE }
| "int" { INT_CONVERTER }
| "float" { FLOAT_CONVERTER }
| "$" { PREDEFINED_CLASS "$" }
| digit+ as n { INT n }
| module_ident as id { MOD_IDENT id }
| ident as id {
match List.assoc_opt id predefined_classes with
| Some pcre_class -> PREDEFINED_CLASS pcre_class
| None -> IDENT id
}
| '\'' { char_literal (Buffer.create 16) lexbuf }
| "\"\"" { EMPTY_STR }
| '"' { string_literal (Buffer.create 16) lexbuf }
| eof { EOF }
| _ as c { raise (Error ("Unexpected character: " ^ String.make 1 c)) }
and char_literal buf = parse
| '\\' '\\' {
Buffer.add_string buf "\\\\";
char_literal buf lexbuf
}
| '\\' (_ as c) {
Buffer.add_char buf (escape_char c);
char_literal buf lexbuf
}
| '\'' { CHAR_LITERAL (Buffer.contents buf) }
| _ as c {
begin if needs_escape c then
Buffer.add_string buf (escape_special c)
else
Buffer.add_char buf c
end;
char_literal buf lexbuf
}
| eof { raise (Error "Unterminated character literal") }
and string_literal buf = parse
| '\\' (_ as c) {
Buffer.add_char buf (escape_char c);
string_literal buf lexbuf
}
| '"' { STRING_LITERAL (Buffer.contents buf) }
| _ as c {
begin if needs_escape c then
Buffer.add_string buf (escape_special c)
else
Buffer.add_char buf c
end;
string_literal buf lexbuf
}
| eof { raise (Error "Unterminated string literal") }