-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocamlipat.ml
160 lines (150 loc) · 3.55 KB
/
ocamlipat.ml
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
open Parser
let special_case = function
"_" -> ".*"
| x -> Str.quote x
(* Read a search pattern using the OCaml lexer *)
let string_of_token = function
| AMPERAMPER -> "&&"
| AMPERSAND -> "&"
| AND -> "and"
| AS -> "as"
| ASSERT -> "assert"
| BACKQUOTE -> "`"
| BANG -> "!"
| BAR -> "|"
| BARBAR -> "||"
| BARRBRACKET -> "|]"
| BEGIN -> "begin"
| CHAR c -> Printf.sprintf "%C" c
| CLASS -> "class"
| COLON -> ":"
| COLONCOLON -> "::"
| COLONEQUAL -> ":="
| COLONGREATER -> ":>"
| COMMA -> ","
| CONSTRAINT -> "constraint"
| DO -> "do"
| DONE -> "done"
| DOT -> "."
| DOTOP _ -> "."
| DOTDOT -> ".."
| DOWNTO -> "downto"
| ELSE -> "else"
| END -> "end"
| EOF -> ""
| EQUAL -> "="
| EXCEPTION -> "exception"
| EXTERNAL -> "external"
| FALSE -> "false"
| FLOAT (s, _) -> Printf.sprintf "%s" s (*FIXME: Check this. In general, floats will be a bit of a problem... *)
| FOR -> "for"
| FUN -> "fun"
| FUNCTION -> "function"
| FUNCTOR -> "functor"
| GREATER -> ">"
| GREATERRBRACE -> ">}"
| GREATERRBRACKET -> ">]"
| IF -> "if"
| IN -> "in"
| INCLUDE -> "include"
| INFIXOP0 s -> s
| INFIXOP1 s -> s
| INFIXOP2 s -> s
| INFIXOP3 s -> s
| INFIXOP4 s -> s
| INHERIT -> "inherit"
| INITIALIZER -> "initializer"
| INT (s, _) -> Printf.sprintf "%s" s (* FIXME: modifiers *)
| LABEL s -> s (* FIXME: Check *)
| LAZY -> "lazy"
| LBRACE -> "{"
| LBRACELESS -> "{<"
| LBRACKET -> "["
| LBRACKETBAR -> "[|"
| LBRACKETLESS -> "[<"
| LBRACKETGREATER -> "[>"
| LBRACKETPERCENT -> "[%"
| LBRACKETPERCENTPERCENT -> "[%%"
| LESS -> "<"
| LESSMINUS -> "<-"
| LET -> "let"
| LIDENT s -> s
| LPAREN -> "("
| LBRACKETAT -> "[@"
| LBRACKETATAT -> "[@@"
| LBRACKETATATAT -> "[@@@"
| MATCH -> "match"
| METHOD -> "method"
| MINUS -> "-"
| MINUSDOT -> "-."
| MINUSGREATER -> "->"
| MODULE -> "module"
| MUTABLE -> "mutable"
| NEW -> "new"
| NONREC -> "nonrec"
| OBJECT -> "object"
| OF -> "of"
| OPEN -> "open"
| OPTLABEL s -> s (* FIXME: check *)
| OR -> "or"
| PERCENT -> "%"
| PLUS -> "+"
| PLUSDOT -> "+."
| PLUSEQ -> "+="
| PREFIXOP s -> s
| PRIVATE -> "private"
| QUESTION -> "?"
| QUOTE -> "\""
| RBRACE -> "}"
| RBRACKET -> "]"
| REC -> "rec"
| RPAREN -> ")"
| SEMI -> ";"
| SEMISEMI -> ";;"
| HASH -> "#"
| HASHOP s -> s
| SIG -> "sig"
| STAR -> "*"
| STRING (s, _) -> s
| STRUCT -> "struct"
| THEN -> "then"
| TILDE -> "~"
| TO -> "to"
| TRUE -> "true"
| TRY -> "try"
| TYPE -> "type"
| UIDENT s -> s
| UNDERSCORE -> "_"
| VAL -> "val"
| VIRTUAL -> "virtual"
| WHEN -> "when"
| WHILE -> "while"
| WITH -> "with"
| COMMENT (s, _) -> s
| DOCSTRING _ -> "FIXMEDOCSTRING"
| EOL -> ""
let tokens_of_lexbuf l =
let toks = ref [] in
try
while true do
let tok = Lexer.token l in
if tok = EOF then raise Exit else toks := tok :: !toks;
done;
[]
with
Exit -> List.rev !toks
let whitespace_of_items items =
"\\(" ^ List.fold_left ( ^ ) "" (List.map (fun x -> x ^ "\\|") items) ^ "\\)*"
let classic_whitespace_items = [" "; "\\t"; "\\n"; "\\f"; "\\t"]
let paren_items = ["("; ")"; "begin"; "end"]
let whitespace =
whitespace_of_items
((*(if !noparens then paren_items else []) @*) classic_whitespace_items)
let regexp_of_lexbuf l =
let terms =
let tokens = List.map string_of_token (tokens_of_lexbuf l) in
List.map special_case tokens
in
String.concat whitespace terms
let regexp_of_string s =
regexp_of_lexbuf (Lexing.from_string s)