Commit | Line | Data |
---|---|---|
34e49164 | 1 | (* |
951c7801 C |
2 | * Copyright 2005-2009, Ecole des Mines de Nantes, University of Copenhagen |
3 | * Yoann Padioleau, Julia Lawall, Rene Rydhof Hansen, Henrik Stuart, Gilles Muller, Nicolas Palix | |
4 | * This file is part of Coccinelle. | |
5 | * | |
6 | * Coccinelle is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, according to version 2 of the License. | |
9 | * | |
10 | * Coccinelle is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
13 | * GNU General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU General Public License | |
16 | * along with Coccinelle. If not, see <http://www.gnu.org/licenses/>. | |
17 | * | |
18 | * The authors reserve the right to distribute this or future versions of | |
19 | * Coccinelle under other licenses. | |
20 | *) | |
34e49164 C |
21 | |
22 | ||
23 | { | |
24 | open Parser_cocci_menhir | |
25 | module D = Data | |
26 | module Ast = Ast_cocci | |
27 | module Ast0 = Ast0_cocci | |
28 | module P = Parse_aux | |
29 | exception Lexical of string | |
30 | let tok = Lexing.lexeme | |
31 | ||
32 | let line = ref 1 | |
33 | let logical_line = ref 0 | |
34 | ||
35 | (* ---------------------------------------------------------------------- *) | |
36 | (* control codes *) | |
37 | ||
38 | (* Defined in data.ml | |
39 | type line_type = MINUS | OPTMINUS | UNIQUEMINUS | PLUS | CONTEXT | UNIQUE | OPT | |
40 | *) | |
41 | ||
42 | let current_line_type = ref (D.CONTEXT,!line,!logical_line) | |
43 | ||
44 | let prev_plus = ref false | |
45 | let line_start = ref 0 (* offset of the beginning of the line *) | |
46 | let get_current_line_type lexbuf = | |
47 | let (c,l,ll) = !current_line_type in | |
48 | let lex_start = Lexing.lexeme_start lexbuf in | |
49 | let preceeding_spaces = | |
50 | if !line_start < 0 then 0 else lex_start - !line_start in | |
708f4980 | 51 | (*line_start := -1;*) |
951c7801 | 52 | prev_plus := (c = D.PLUS) or (c = D.PLUSPLUS); |
34e49164 C |
53 | (c,l,ll,lex_start,preceeding_spaces,[],[],Ast0.NoMetaPos) |
54 | let current_line_started = ref false | |
55 | let col_zero = ref true | |
56 | ||
57 | let reset_line lexbuf = | |
58 | line := !line + 1; | |
59 | current_line_type := (D.CONTEXT,!line,!logical_line); | |
60 | current_line_started := false; | |
61 | col_zero := true; | |
62 | line_start := Lexing.lexeme_start lexbuf + 1 | |
63 | ||
64 | let started_line = ref (-1) | |
65 | ||
66 | let start_line seen_char = | |
67 | current_line_started := true; | |
68 | col_zero := false; | |
69 | (if seen_char && not(!line = !started_line) | |
70 | then | |
71 | begin | |
72 | started_line := !line; | |
73 | logical_line := !logical_line + 1 | |
74 | end) | |
75 | ||
76 | let pass_zero _ = col_zero := false | |
77 | ||
78 | let lexerr s1 s2 = raise (Lexical (Printf.sprintf "%s%s" s1 s2)) | |
79 | ||
80 | let add_current_line_type x = | |
81 | match (x,!current_line_type) with | |
82 | (D.MINUS,(D.CONTEXT,ln,lln)) -> | |
83 | current_line_type := (D.MINUS,ln,lln) | |
84 | | (D.MINUS,(D.UNIQUE,ln,lln)) -> | |
85 | current_line_type := (D.UNIQUEMINUS,ln,lln) | |
86 | | (D.MINUS,(D.OPT,ln,lln)) -> | |
87 | current_line_type := (D.OPTMINUS,ln,lln) | |
88 | | (D.PLUS,(D.CONTEXT,ln,lln)) -> | |
89 | current_line_type := (D.PLUS,ln,lln) | |
951c7801 C |
90 | | (D.PLUSPLUS,(D.CONTEXT,ln,lln)) -> |
91 | current_line_type := (D.PLUSPLUS,ln,lln) | |
34e49164 C |
92 | | (D.UNIQUE,(D.CONTEXT,ln,lln)) -> |
93 | current_line_type := (D.UNIQUE,ln,lln) | |
94 | | (D.OPT,(D.CONTEXT,ln,lln)) -> | |
95 | current_line_type := (D.OPT,ln,lln) | |
96 | | _ -> lexerr "invalid control character combination" "" | |
97 | ||
98 | let check_minus_context_linetype s = | |
99 | match !current_line_type with | |
951c7801 | 100 | (D.PLUS,_,_) | (D.PLUSPLUS,_,_) -> lexerr "invalid in a + context: " s |
34e49164 C |
101 | | _ -> () |
102 | ||
103 | let check_context_linetype s = | |
104 | match !current_line_type with | |
105 | (D.CONTEXT,_,_) -> () | |
106 | | _ -> lexerr "invalid in a nonempty context: " s | |
107 | ||
108 | let check_plus_linetype s = | |
109 | match !current_line_type with | |
951c7801 | 110 | (D.PLUS,_,_) | (D.PLUSPLUS,_,_) -> () |
34e49164 C |
111 | | _ -> lexerr "invalid in a non + context: " s |
112 | ||
113 | let check_arity_context_linetype s = | |
114 | match !current_line_type with | |
951c7801 C |
115 | (D.CONTEXT,_,_) | (D.PLUS,_,_) | (D.PLUSPLUS,_,_) |
116 | | (D.UNIQUE,_,_) | (D.OPT,_,_) -> () | |
34e49164 C |
117 | | _ -> lexerr "invalid in a nonempty context: " s |
118 | ||
119 | let process_include start finish str = | |
120 | (match !current_line_type with | |
951c7801 | 121 | (D.PLUS,_,_) | (D.PLUSPLUS,_,_) -> |
34e49164 C |
122 | (try |
123 | let _ = Str.search_forward (Str.regexp "\\.\\.\\.") str start in | |
124 | lexerr "... not allowed in + include" "" | |
125 | with Not_found -> ()) | |
126 | | _ -> ()); | |
127 | String.sub str (start + 1) (finish - start - 1) | |
128 | ||
129 | (* ---------------------------------------------------------------------- *) | |
130 | type pm = PATCH | MATCH | UNKNOWN | |
131 | ||
132 | let pm = ref UNKNOWN | |
133 | ||
134 | let patch_or_match = function | |
135 | PATCH -> | |
136 | (match !pm with | |
137 | MATCH -> lexerr "- or + not allowed in the first column for a match" "" | |
138 | | PATCH -> () | |
139 | | UNKNOWN -> Flag.sgrep_mode2 := false; pm := PATCH) | |
140 | | MATCH -> | |
141 | (match !pm with | |
142 | PATCH -> lexerr "* not allowed in the first column for a patch" "" | |
143 | | MATCH -> () | |
144 | | UNKNOWN -> Flag.sgrep_mode2 := true; pm := MATCH) | |
145 | | _ -> failwith "unexpected argument" | |
146 | ||
147 | (* ---------------------------------------------------------------------- *) | |
148 | (* identifiers, including metavariables *) | |
149 | ||
150 | let metavariables = (Hashtbl.create(100) : (string, D.clt -> token) Hashtbl.t) | |
151 | ||
152 | let all_metavariables = | |
153 | (Hashtbl.create(100) : (string,(string * (D.clt -> token)) list) Hashtbl.t) | |
154 | ||
155 | let type_names = (Hashtbl.create(100) : (string, D.clt -> token) Hashtbl.t) | |
156 | ||
157 | let declarer_names = (Hashtbl.create(100) : (string, D.clt -> token) Hashtbl.t) | |
158 | ||
159 | let iterator_names = (Hashtbl.create(100) : (string, D.clt -> token) Hashtbl.t) | |
160 | ||
161 | let rule_names = (Hashtbl.create(100) : (string, unit) Hashtbl.t) | |
162 | ||
163 | let check_var s linetype = | |
164 | let fail _ = | |
165 | if (!Data.in_prolog || !Data.in_rule_name) && | |
166 | Str.string_match (Str.regexp "<.*>") s 0 | |
167 | then TPathIsoFile s | |
168 | else | |
169 | try (Hashtbl.find metavariables s) linetype | |
170 | with Not_found -> | |
171 | (try (Hashtbl.find type_names s) linetype | |
172 | with Not_found -> | |
173 | (try (Hashtbl.find declarer_names s) linetype | |
faf9a90c | 174 | with Not_found -> |
34e49164 C |
175 | (try (Hashtbl.find iterator_names s) linetype |
176 | with Not_found -> TIdent (s,linetype)))) in | |
177 | if !Data.in_meta or !Data.in_rule_name | |
178 | then (try Hashtbl.find rule_names s; TRuleName s with Not_found -> fail()) | |
179 | else fail() | |
180 | ||
181 | let id_tokens lexbuf = | |
182 | let s = tok lexbuf in | |
183 | let linetype = get_current_line_type lexbuf in | |
184 | let in_rule_name = !Data.in_rule_name in | |
978fd7e5 | 185 | let in_meta = !Data.in_meta && not !Data.saw_struct in |
34e49164 C |
186 | let in_iso = !Data.in_iso in |
187 | let in_prolog = !Data.in_prolog in | |
188 | match s with | |
189 | "identifier" when in_meta -> check_arity_context_linetype s; TIdentifier | |
190 | | "type" when in_meta -> check_arity_context_linetype s; TType | |
191 | | "parameter" when in_meta -> check_arity_context_linetype s; TParameter | |
192 | | "constant" when in_meta -> check_arity_context_linetype s; TConstant | |
faf9a90c C |
193 | | "generated" when in_rule_name && not (!Flag.make_hrule = None) -> |
194 | check_arity_context_linetype s; TGenerated | |
34e49164 C |
195 | | "expression" when in_meta || in_rule_name -> |
196 | check_arity_context_linetype s; TExpression | |
113803cf C |
197 | | "initialiser" when in_meta || in_rule_name -> |
198 | check_arity_context_linetype s; TInitialiser | |
199 | | "initializer" when in_meta || in_rule_name -> | |
200 | check_arity_context_linetype s; TInitialiser | |
34e49164 C |
201 | | "idexpression" when in_meta -> |
202 | check_arity_context_linetype s; TIdExpression | |
203 | | "statement" when in_meta -> check_arity_context_linetype s; TStatement | |
204 | | "function" when in_meta -> check_arity_context_linetype s; TFunction | |
205 | | "local" when in_meta -> check_arity_context_linetype s; TLocal | |
206 | | "list" when in_meta -> check_arity_context_linetype s; Tlist | |
207 | | "fresh" when in_meta -> check_arity_context_linetype s; TFresh | |
208 | | "typedef" when in_meta -> check_arity_context_linetype s; TTypedef | |
209 | | "declarer" when in_meta -> check_arity_context_linetype s; TDeclarer | |
210 | | "iterator" when in_meta -> check_arity_context_linetype s; TIterator | |
211 | | "name" when in_meta -> check_arity_context_linetype s; TName | |
212 | | "position" when in_meta -> check_arity_context_linetype s; TPosition | |
213 | | "any" when in_meta -> check_arity_context_linetype s; TPosAny | |
214 | | "pure" when in_meta && in_iso -> | |
215 | check_arity_context_linetype s; TPure | |
216 | | "context" when in_meta && in_iso -> | |
217 | check_arity_context_linetype s; TContext | |
218 | | "error" when in_meta -> check_arity_context_linetype s; TError | |
219 | | "words" when in_meta -> check_context_linetype s; TWords | |
220 | ||
221 | | "using" when in_rule_name || in_prolog -> check_context_linetype s; TUsing | |
951c7801 | 222 | | "virtual" when in_prolog -> check_context_linetype s; TVirtual |
34e49164 C |
223 | | "disable" when in_rule_name -> check_context_linetype s; TDisable |
224 | | "extends" when in_rule_name -> check_context_linetype s; TExtends | |
225 | | "depends" when in_rule_name -> check_context_linetype s; TDepends | |
226 | | "on" when in_rule_name -> check_context_linetype s; TOn | |
227 | | "ever" when in_rule_name -> check_context_linetype s; TEver | |
228 | | "never" when in_rule_name -> check_context_linetype s; TNever | |
978fd7e5 | 229 | (* exists and forall for when are reparsed in parse_cocci.ml *) |
34e49164 C |
230 | | "exists" when in_rule_name -> check_context_linetype s; TExists |
231 | | "forall" when in_rule_name -> check_context_linetype s; TForall | |
b1b2de81 C |
232 | | "script" when in_rule_name -> check_context_linetype s; TScript |
233 | | "initialize" when in_rule_name -> check_context_linetype s; TInitialize | |
234 | | "finalize" when in_rule_name -> check_context_linetype s; TFinalize | |
34e49164 C |
235 | |
236 | | "char" -> Tchar linetype | |
237 | | "short" -> Tshort linetype | |
238 | | "int" -> Tint linetype | |
239 | | "double" -> Tdouble linetype | |
240 | | "float" -> Tfloat linetype | |
241 | | "long" -> Tlong linetype | |
242 | | "void" -> Tvoid linetype | |
978fd7e5 C |
243 | (* in_meta is only for the first keyword; drop it now to allow any type |
244 | name *) | |
245 | | "struct" -> Data.saw_struct := true; Tstruct linetype | |
246 | | "union" -> Data.saw_struct := true; Tunion linetype | |
247 | | "enum" -> Data.saw_struct := true; Tenum linetype | |
34e49164 C |
248 | | "unsigned" -> Tunsigned linetype |
249 | | "signed" -> Tsigned linetype | |
faf9a90c | 250 | |
34e49164 C |
251 | | "auto" -> Tauto linetype |
252 | | "register" -> Tregister linetype | |
253 | | "extern" -> Textern linetype | |
254 | | "static" -> Tstatic linetype | |
255 | | "inline" -> Tinline linetype | |
256 | | "typedef" -> Ttypedef linetype | |
257 | ||
258 | | "const" -> Tconst linetype | |
259 | | "volatile" -> Tvolatile linetype | |
260 | ||
261 | | "if" -> TIf linetype | |
262 | | "else" -> TElse linetype | |
263 | | "while" -> TWhile linetype | |
264 | | "do" -> TDo linetype | |
265 | | "for" -> TFor linetype | |
266 | | "switch" -> TSwitch linetype | |
267 | | "case" -> TCase linetype | |
268 | | "default" -> TDefault linetype | |
269 | | "return" -> TReturn linetype | |
270 | | "break" -> TBreak linetype | |
271 | | "continue" -> TContinue linetype | |
272 | | "goto" -> TGoto linetype | |
273 | ||
274 | | "sizeof" -> TSizeof linetype | |
275 | ||
276 | | "Expression" -> TIsoExpression | |
277 | | "ArgExpression" -> TIsoArgExpression | |
278 | | "TestExpression" -> TIsoTestExpression | |
279 | | "Statement" -> TIsoStatement | |
280 | | "Declaration" -> TIsoDeclaration | |
281 | | "Type" -> TIsoType | |
282 | | "TopLevel" -> TIsoTopLevel | |
283 | ||
284 | | s -> check_var s linetype | |
285 | ||
286 | let mkassign op lexbuf = | |
287 | TAssign (Ast.OpAssign op, (get_current_line_type lexbuf)) | |
288 | ||
289 | let init _ = | |
290 | line := 1; | |
291 | logical_line := 0; | |
292 | prev_plus := false; | |
293 | line_start := 0; | |
294 | current_line_started := false; | |
295 | col_zero := true; | |
296 | pm := UNKNOWN; | |
297 | Data.in_rule_name := false; | |
298 | Data.in_meta := false; | |
299 | Data.in_prolog := false; | |
978fd7e5 | 300 | Data.saw_struct := false; |
34e49164 C |
301 | Data.inheritable_positions := []; |
302 | Hashtbl.clear all_metavariables; | |
303 | Hashtbl.clear Data.all_metadecls; | |
304 | Hashtbl.clear metavariables; | |
305 | Hashtbl.clear type_names; | |
306 | Hashtbl.clear rule_names; | |
708f4980 C |
307 | Hashtbl.clear iterator_names; |
308 | Hashtbl.clear declarer_names; | |
34e49164 C |
309 | let get_name (_,x) = x in |
310 | Data.add_id_meta := | |
311 | (fun name constraints pure -> | |
312 | let fn clt = TMetaId(name,constraints,pure,clt) in | |
313 | Hashtbl.replace metavariables (get_name name) fn); | |
b1b2de81 C |
314 | Data.add_fresh_id_meta := |
315 | (fun name -> | |
951c7801 | 316 | let fn clt = TMetaId(name,Ast.IdNoConstraint,Ast0.Impure,clt) in |
b1b2de81 | 317 | Hashtbl.replace metavariables (get_name name) fn); |
34e49164 C |
318 | Data.add_type_meta := |
319 | (fun name pure -> | |
320 | let fn clt = TMetaType(name,pure,clt) in | |
321 | Hashtbl.replace metavariables (get_name name) fn); | |
113803cf C |
322 | Data.add_init_meta := |
323 | (fun name pure -> | |
324 | let fn clt = TMetaInit(name,pure,clt) in | |
325 | Hashtbl.replace metavariables (get_name name) fn); | |
34e49164 C |
326 | Data.add_param_meta := |
327 | (function name -> function pure -> | |
328 | let fn clt = TMetaParam(name,pure,clt) in | |
329 | Hashtbl.replace metavariables (get_name name) fn); | |
330 | Data.add_paramlist_meta := | |
331 | (function name -> function lenname -> function pure -> | |
332 | let fn clt = TMetaParamList(name,lenname,pure,clt) in | |
333 | Hashtbl.replace metavariables (get_name name) fn); | |
334 | Data.add_const_meta := | |
335 | (fun tyopt name constraints pure -> | |
336 | let fn clt = TMetaConst(name,constraints,pure,tyopt,clt) in | |
337 | Hashtbl.replace metavariables (get_name name) fn); | |
338 | Data.add_err_meta := | |
339 | (fun name constraints pure -> | |
340 | let fn clt = TMetaErr(name,constraints,pure,clt) in | |
341 | Hashtbl.replace metavariables (get_name name) fn); | |
342 | Data.add_exp_meta := | |
343 | (fun tyopt name constraints pure -> | |
344 | let fn clt = TMetaExp(name,constraints,pure,tyopt,clt) in | |
345 | Hashtbl.replace metavariables (get_name name) fn); | |
346 | Data.add_idexp_meta := | |
347 | (fun tyopt name constraints pure -> | |
348 | let fn clt = TMetaIdExp(name,constraints,pure,tyopt,clt) in | |
349 | Hashtbl.replace metavariables (get_name name) fn); | |
350 | Data.add_local_idexp_meta := | |
351 | (fun tyopt name constraints pure -> | |
352 | let fn clt = TMetaLocalIdExp(name,constraints,pure,tyopt,clt) in | |
353 | Hashtbl.replace metavariables (get_name name) fn); | |
354 | Data.add_explist_meta := | |
355 | (function name -> function lenname -> function pure -> | |
356 | let fn clt = TMetaExpList(name,lenname,pure,clt) in | |
357 | Hashtbl.replace metavariables (get_name name) fn); | |
358 | Data.add_stm_meta := | |
359 | (function name -> function pure -> | |
360 | let fn clt = TMetaStm(name,pure,clt) in | |
361 | Hashtbl.replace metavariables (get_name name) fn); | |
362 | Data.add_stmlist_meta := | |
363 | (function name -> function pure -> | |
364 | let fn clt = TMetaStmList(name,pure,clt) in | |
365 | Hashtbl.replace metavariables (get_name name) fn); | |
366 | Data.add_func_meta := | |
367 | (fun name constraints pure -> | |
368 | let fn clt = TMetaFunc(name,constraints,pure,clt) in | |
369 | Hashtbl.replace metavariables (get_name name) fn); | |
370 | Data.add_local_func_meta := | |
371 | (fun name constraints pure -> | |
372 | let fn clt = TMetaLocalFunc(name,constraints,pure,clt) in | |
373 | Hashtbl.replace metavariables (get_name name) fn); | |
374 | Data.add_iterator_meta := | |
375 | (fun name constraints pure -> | |
376 | let fn clt = TMetaIterator(name,constraints,pure,clt) in | |
377 | Hashtbl.replace metavariables (get_name name) fn); | |
378 | Data.add_declarer_meta := | |
379 | (fun name constraints pure -> | |
380 | let fn clt = TMetaDeclarer(name,constraints,pure,clt) in | |
381 | Hashtbl.replace metavariables (get_name name) fn); | |
382 | Data.add_pos_meta := | |
383 | (fun name constraints any -> | |
384 | let fn ((d,ln,_,_,_,_,_,_) as clt) = | |
385 | (if d = Data.PLUS | |
386 | then | |
387 | failwith | |
388 | (Printf.sprintf "%d: positions only allowed in minus code" ln)); | |
389 | TMetaPos(name,constraints,any,clt) in | |
390 | Hashtbl.replace metavariables (get_name name) fn); | |
391 | Data.add_type_name := | |
392 | (function name -> | |
393 | let fn clt = TTypeId(name,clt) in | |
394 | Hashtbl.replace type_names name fn); | |
395 | Data.add_declarer_name := | |
396 | (function name -> | |
397 | let fn clt = TDeclarerId(name,clt) in | |
398 | Hashtbl.replace declarer_names name fn); | |
399 | Data.add_iterator_name := | |
400 | (function name -> | |
401 | let fn clt = TIteratorId(name,clt) in | |
402 | Hashtbl.replace iterator_names name fn); | |
403 | Data.init_rule := (function _ -> Hashtbl.clear metavariables); | |
404 | Data.install_bindings := | |
405 | (function parent -> | |
406 | List.iter (function (name,fn) -> Hashtbl.add metavariables name fn) | |
407 | (Hashtbl.find all_metavariables parent)) | |
408 | ||
409 | let drop_spaces s = | |
410 | let len = String.length s in | |
411 | let rec loop n = | |
412 | if n = len | |
413 | then n | |
414 | else | |
415 | if List.mem (String.get s n) [' ';'\t'] | |
416 | then loop (n+1) | |
417 | else n in | |
418 | let start = loop 0 in | |
419 | String.sub s start (len - start) | |
420 | } | |
421 | ||
422 | (* ---------------------------------------------------------------------- *) | |
423 | (* tokens *) | |
424 | ||
425 | let letter = ['A'-'Z' 'a'-'z' '_'] | |
426 | let digit = ['0'-'9'] | |
427 | ||
428 | let dec = ['0'-'9'] | |
429 | let oct = ['0'-'7'] | |
430 | let hex = ['0'-'9' 'a'-'f' 'A'-'F'] | |
431 | ||
432 | let decimal = ('0' | (['1'-'9'] dec*)) | |
433 | let octal = ['0'] oct+ | |
faf9a90c | 434 | let hexa = ("0x" |"0X") hex+ |
34e49164 C |
435 | |
436 | let pent = dec+ | |
437 | let pfract = dec+ | |
438 | let sign = ['-' '+'] | |
439 | let exp = ['e''E'] sign? dec+ | |
440 | let real = pent exp | ((pent? '.' pfract | pent '.' pfract? ) exp?) | |
441 | ||
442 | ||
443 | rule token = parse | |
444 | | [' ' '\t' ]+ { start_line false; token lexbuf } | |
445 | | ['\n' '\r' '\011' '\012'] { reset_line lexbuf; token lexbuf } | |
446 | ||
447 | | "//" [^ '\n']* { start_line false; token lexbuf } | |
448 | ||
449 | | "@@" { start_line true; TArobArob } | |
450 | | "@" { pass_zero(); | |
451 | if !Data.in_rule_name or not !current_line_started | |
452 | then (start_line true; TArob) | |
453 | else (check_minus_context_linetype "@"; TPArob) } | |
454 | ||
951c7801 C |
455 | | "~=" { start_line true; TTildeEq (get_current_line_type lexbuf) } |
456 | | "!~=" { start_line true; TTildeExclEq (get_current_line_type lexbuf) } | |
34e49164 C |
457 | | "WHEN" | "when" |
458 | { start_line true; check_minus_context_linetype (tok lexbuf); | |
459 | TWhen (get_current_line_type lexbuf) } | |
460 | ||
461 | | "..." | |
462 | { start_line true; check_minus_context_linetype (tok lexbuf); | |
463 | TEllipsis (get_current_line_type lexbuf) } | |
464 | (* | |
465 | | "ooo" | |
466 | { start_line true; check_minus_context_linetype (tok lexbuf); | |
467 | TCircles (get_current_line_type lexbuf) } | |
468 | ||
469 | | "***" | |
470 | { start_line true; check_minus_context_linetype (tok lexbuf); | |
471 | TStars (get_current_line_type lexbuf) } | |
472 | *) | |
473 | | "<..." { start_line true; check_context_linetype (tok lexbuf); | |
474 | TOEllipsis (get_current_line_type lexbuf) } | |
475 | | "...>" { start_line true; check_context_linetype (tok lexbuf); | |
476 | TCEllipsis (get_current_line_type lexbuf) } | |
477 | | "<+..." { start_line true; check_context_linetype (tok lexbuf); | |
478 | TPOEllipsis (get_current_line_type lexbuf) } | |
479 | | "...+>" { start_line true; check_context_linetype (tok lexbuf); | |
480 | TPCEllipsis (get_current_line_type lexbuf) } | |
481 | (* | |
482 | | "<ooo" { start_line true; check_context_linetype (tok lexbuf); | |
483 | TOCircles (get_current_line_type lexbuf) } | |
484 | | "ooo>" { start_line true; check_context_linetype (tok lexbuf); | |
485 | TCCircles (get_current_line_type lexbuf) } | |
486 | ||
487 | | "<***" { start_line true; check_context_linetype (tok lexbuf); | |
488 | TOStars (get_current_line_type lexbuf) } | |
489 | | "***>" { start_line true; check_context_linetype (tok lexbuf); | |
490 | TCStars (get_current_line_type lexbuf) } | |
491 | *) | |
492 | | "-" { pass_zero(); | |
493 | if !current_line_started | |
494 | then (start_line true; TMinus (get_current_line_type lexbuf)) | |
495 | else (patch_or_match PATCH; | |
496 | add_current_line_type D.MINUS; token lexbuf) } | |
497 | | "+" { pass_zero(); | |
498 | if !current_line_started | |
499 | then (start_line true; TPlus (get_current_line_type lexbuf)) | |
500 | else if !Data.in_meta | |
501 | then TPlus0 | |
502 | else (patch_or_match PATCH; | |
503 | add_current_line_type D.PLUS; token lexbuf) } | |
504 | | "?" { pass_zero(); | |
505 | if !current_line_started | |
506 | then (start_line true; TWhy (get_current_line_type lexbuf)) | |
507 | else if !Data.in_meta | |
508 | then TWhy0 | |
509 | else (add_current_line_type D.OPT; token lexbuf) } | |
510 | | "!" { pass_zero(); | |
511 | if !current_line_started | |
512 | then (start_line true; TBang (get_current_line_type lexbuf)) | |
513 | else if !Data.in_meta | |
514 | then TBang0 | |
515 | else (add_current_line_type D.UNIQUE; token lexbuf) } | |
516 | | "(" { if not !col_zero | |
517 | then (start_line true; TOPar (get_current_line_type lexbuf)) | |
518 | else | |
519 | (start_line true; check_context_linetype (tok lexbuf); | |
520 | TOPar0 (get_current_line_type lexbuf))} | |
521 | | "\\(" { start_line true; TOPar0 (get_current_line_type lexbuf) } | |
522 | | "|" { if not (!col_zero) | |
523 | then (start_line true; TOr(get_current_line_type lexbuf)) | |
524 | else (start_line true; | |
525 | check_context_linetype (tok lexbuf); | |
526 | TMid0 (get_current_line_type lexbuf))} | |
527 | | "\\|" { start_line true; TMid0 (get_current_line_type lexbuf) } | |
528 | | ")" { if not !col_zero | |
529 | then (start_line true; TCPar (get_current_line_type lexbuf)) | |
530 | else | |
531 | (start_line true; check_context_linetype (tok lexbuf); | |
532 | TCPar0 (get_current_line_type lexbuf))} | |
533 | | "\\)" { start_line true; TCPar0 (get_current_line_type lexbuf) } | |
534 | ||
535 | | '[' { start_line true; TOCro (get_current_line_type lexbuf) } | |
536 | | ']' { start_line true; TCCro (get_current_line_type lexbuf) } | |
537 | | '{' { start_line true; TOBrace (get_current_line_type lexbuf) } | |
538 | | '}' { start_line true; TCBrace (get_current_line_type lexbuf) } | |
539 | ||
540 | | "->" { start_line true; TPtrOp (get_current_line_type lexbuf) } | |
541 | | '.' { start_line true; TDot (get_current_line_type lexbuf) } | |
542 | | ',' { start_line true; TComma (get_current_line_type lexbuf) } | |
543 | | ";" { start_line true; | |
544 | if !Data.in_meta | |
545 | then TMPtVirg (* works better with tokens_all *) | |
546 | else TPtVirg (get_current_line_type lexbuf) } | |
547 | ||
faf9a90c | 548 | |
34e49164 C |
549 | | '*' { pass_zero(); |
550 | if !current_line_started | |
551 | then | |
552 | (start_line true; TMul (get_current_line_type lexbuf)) | |
553 | else | |
554 | (patch_or_match MATCH; | |
555 | add_current_line_type D.MINUS; token lexbuf) } | |
556 | | '/' { start_line true; | |
faf9a90c | 557 | TDmOp (Ast.Div,get_current_line_type lexbuf) } |
34e49164 | 558 | | '%' { start_line true; |
faf9a90c C |
559 | TDmOp (Ast.Mod,get_current_line_type lexbuf) } |
560 | | '~' { start_line true; TTilde (get_current_line_type lexbuf) } | |
561 | ||
951c7801 C |
562 | | "++" { pass_zero(); |
563 | if !current_line_started | |
564 | then | |
565 | (start_line true; TInc (get_current_line_type lexbuf)) | |
566 | else (patch_or_match PATCH; | |
567 | add_current_line_type D.PLUSPLUS; token lexbuf) } | |
34e49164 | 568 | | "--" { start_line true; TDec (get_current_line_type lexbuf) } |
faf9a90c C |
569 | |
570 | | "=" { start_line true; TEq (get_current_line_type lexbuf) } | |
571 | ||
34e49164 C |
572 | | "-=" { start_line true; mkassign Ast.Minus lexbuf } |
573 | | "+=" { start_line true; mkassign Ast.Plus lexbuf } | |
faf9a90c | 574 | |
34e49164 C |
575 | | "*=" { start_line true; mkassign Ast.Mul lexbuf } |
576 | | "/=" { start_line true; mkassign Ast.Div lexbuf } | |
577 | | "%=" { start_line true; mkassign Ast.Mod lexbuf } | |
faf9a90c | 578 | |
34e49164 C |
579 | | "&=" { start_line true; mkassign Ast.And lexbuf } |
580 | | "|=" { start_line true; mkassign Ast.Or lexbuf } | |
581 | | "^=" { start_line true; mkassign Ast.Xor lexbuf } | |
faf9a90c | 582 | |
34e49164 C |
583 | | "<<=" { start_line true; mkassign Ast.DecLeft lexbuf } |
584 | | ">>=" { start_line true; mkassign Ast.DecRight lexbuf } | |
585 | ||
586 | | ":" { start_line true; TDotDot (get_current_line_type lexbuf) } | |
faf9a90c | 587 | |
951c7801 C |
588 | | "==" { start_line true; TEqEq (get_current_line_type lexbuf) } |
589 | | "!=" { start_line true; TNotEq (get_current_line_type lexbuf) } | |
34e49164 C |
590 | | ">=" { start_line true; |
591 | TLogOp(Ast.SupEq,get_current_line_type lexbuf) } | |
592 | | "<=" { start_line true; | |
593 | TLogOp(Ast.InfEq,get_current_line_type lexbuf) } | |
594 | | "<" { start_line true; | |
faf9a90c | 595 | TLogOp(Ast.Inf,get_current_line_type lexbuf) } |
34e49164 C |
596 | | ">" { start_line true; |
597 | TLogOp(Ast.Sup,get_current_line_type lexbuf) } | |
faf9a90c C |
598 | |
599 | | "&&" { start_line true; TAndLog (get_current_line_type lexbuf) } | |
34e49164 | 600 | | "||" { start_line true; TOrLog (get_current_line_type lexbuf) } |
faf9a90c | 601 | |
34e49164 C |
602 | | ">>" { start_line true; |
603 | TShOp(Ast.DecRight,get_current_line_type lexbuf) } | |
604 | | "<<" { start_line true; | |
605 | TShOp(Ast.DecLeft,get_current_line_type lexbuf) } | |
faf9a90c | 606 | |
34e49164 C |
607 | | "&" { start_line true; TAnd (get_current_line_type lexbuf) } |
608 | | "^" { start_line true; TXor(get_current_line_type lexbuf) } | |
609 | ||
978fd7e5 | 610 | | "##" { start_line true; TCppConcatOp } |
708f4980 | 611 | | (( ("#" [' ' '\t']* "define" [' ' '\t']+)) as def) |
faf9a90c | 612 | ( (letter (letter |digit)*) as ident) |
34e49164 C |
613 | { start_line true; |
614 | let (arity,line,lline,offset,col,strbef,straft,pos) as lt = | |
615 | get_current_line_type lexbuf in | |
708f4980 | 616 | let off = String.length def in |
34e49164 C |
617 | (* -1 in the code below because the ident is not at the line start *) |
618 | TDefine | |
619 | (lt, | |
620 | check_var ident | |
708f4980 C |
621 | (arity,line,lline,offset+off,col+off,[],[],Ast0.NoMetaPos)) } |
622 | | (( ("#" [' ' '\t']* "define" [' ' '\t']+)) as def) | |
faf9a90c | 623 | ( (letter (letter | digit)*) as ident) |
34e49164 C |
624 | '(' |
625 | { start_line true; | |
626 | let (arity,line,lline,offset,col,strbef,straft,pos) as lt = | |
627 | get_current_line_type lexbuf in | |
708f4980 | 628 | let off = String.length def in |
34e49164 C |
629 | TDefineParam |
630 | (lt, | |
631 | check_var ident | |
632 | (* why pos here but not above? *) | |
708f4980 C |
633 | (arity,line,lline,offset+off,col+off,strbef,straft,pos), |
634 | offset + off + (String.length ident), | |
635 | col + off + (String.length ident)) } | |
34e49164 C |
636 | | "#" [' ' '\t']* "include" [' ' '\t']* '"' [^ '"']+ '"' |
637 | { TIncludeL | |
638 | (let str = tok lexbuf in | |
639 | let start = String.index str '"' in | |
640 | let finish = String.rindex str '"' in | |
641 | start_line true; | |
642 | (process_include start finish str,get_current_line_type lexbuf)) } | |
643 | | "#" [' ' '\t']* "include" [' ' '\t']* '<' [^ '>']+ '>' | |
644 | { TIncludeNL | |
645 | (let str = tok lexbuf in | |
646 | let start = String.index str '<' in | |
647 | let finish = String.rindex str '>' in | |
648 | start_line true; | |
649 | (process_include start finish str,get_current_line_type lexbuf)) } | |
650 | | "#" [' ' '\t']* "if" [^'\n']* | |
651 | | "#" [' ' '\t']* "ifdef" [^'\n']* | |
652 | | "#" [' ' '\t']* "ifndef" [^'\n']* | |
653 | | "#" [' ' '\t']* "else" [^'\n']* | |
654 | | "#" [' ' '\t']* "elif" [^'\n']* | |
655 | | "#" [' ' '\t']* "endif" [^'\n']* | |
656 | | "#" [' ' '\t']* "error" [^'\n']* | |
657 | { start_line true; check_plus_linetype (tok lexbuf); | |
0708f913 C |
658 | TPragma (tok lexbuf, get_current_line_type lexbuf) } |
659 | | "/*" | |
660 | { start_line true; check_plus_linetype (tok lexbuf); | |
661 | (* second argument to TPragma is not quite right, because | |
662 | it represents only the first token of the comemnt, but that | |
663 | should be good enough *) | |
664 | TPragma ("/*"^(comment lexbuf), get_current_line_type lexbuf) } | |
34e49164 C |
665 | | "---" [^'\n']* |
666 | { (if !current_line_started | |
667 | then lexerr "--- must be at the beginning of the line" ""); | |
668 | start_line true; | |
669 | TMinusFile | |
670 | (let str = tok lexbuf in | |
671 | (drop_spaces(String.sub str 3 (String.length str - 3)), | |
672 | (get_current_line_type lexbuf))) } | |
673 | | "+++" [^'\n']* | |
674 | { (if !current_line_started | |
675 | then lexerr "+++ must be at the beginning of the line" ""); | |
676 | start_line true; | |
677 | TPlusFile | |
678 | (let str = tok lexbuf in | |
679 | (drop_spaces(String.sub str 3 (String.length str - 3)), | |
680 | (get_current_line_type lexbuf))) } | |
681 | ||
682 | | letter (letter | digit)* | |
faf9a90c | 683 | { start_line true; id_tokens lexbuf } |
34e49164 C |
684 | |
685 | | "'" { start_line true; | |
686 | TChar(char lexbuf,get_current_line_type lexbuf) } | |
687 | | '"' { start_line true; | |
688 | TString(string lexbuf,(get_current_line_type lexbuf)) } | |
689 | | (real as x) { start_line true; | |
690 | TFloat(x,(get_current_line_type lexbuf)) } | |
faf9a90c C |
691 | | ((( decimal | hexa | octal) |
692 | ( ['u' 'U'] | |
693 | | ['l' 'L'] | |
34e49164 C |
694 | | (['l' 'L'] ['u' 'U']) |
695 | | (['u' 'U'] ['l' 'L']) | |
696 | | (['u' 'U'] ['l' 'L'] ['l' 'L']) | |
697 | | (['l' 'L'] ['l' 'L']) | |
698 | )? | |
699 | ) as x) { start_line true; TInt(x,(get_current_line_type lexbuf)) } | |
700 | ||
701 | | "<=>" { TIso } | |
702 | | "=>" { TRightIso } | |
703 | ||
704 | | eof { EOF } | |
705 | ||
706 | | _ { lexerr "unrecognised symbol, in token rule: " (tok lexbuf) } | |
707 | ||
708 | ||
709 | and char = parse | |
710 | | (_ as x) "'" { String.make 1 x } | |
711 | | (("\\" (oct | oct oct | oct oct oct)) as x "'") { x } | |
712 | | (("\\x" (hex | hex hex)) as x "'") { x } | |
713 | | (("\\" (_ as v)) as x "'") | |
714 | { (match v with | |
715 | | 'n' -> () | 't' -> () | 'v' -> () | 'b' -> () | |
716 | | 'r' -> () | 'f' -> () | 'a' -> () | |
717 | | '\\' -> () | '?' -> () | '\'' -> () | '"' -> () | |
718 | | 'e' -> () | |
719 | | _ -> lexerr "unrecognised symbol: " (tok lexbuf) | |
720 | ); | |
721 | x | |
faf9a90c | 722 | } |
34e49164 C |
723 | | _ { lexerr "unrecognised symbol: " (tok lexbuf) } |
724 | ||
725 | and string = parse | |
726 | | '"' { "" } | |
727 | | (_ as x) { Common.string_of_char x ^ string lexbuf } | |
728 | | ("\\" (oct | oct oct | oct oct oct)) as x { x ^ string lexbuf } | |
729 | | ("\\x" (hex | hex hex)) as x { x ^ string lexbuf } | |
faf9a90c C |
730 | | ("\\" (_ as v)) as x |
731 | { | |
34e49164 | 732 | (match v with |
faf9a90c | 733 | | 'n' -> () | 't' -> () | 'v' -> () | 'b' -> () | 'r' -> () |
34e49164 C |
734 | | 'f' -> () | 'a' -> () |
735 | | '\\' -> () | '?' -> () | '\'' -> () | '"' -> () | |
736 | | 'e' -> () | |
faf9a90c | 737 | | '\n' -> () |
34e49164 C |
738 | | _ -> lexerr "unrecognised symbol:" (tok lexbuf) |
739 | ); | |
740 | x ^ string lexbuf | |
741 | } | |
742 | | _ { lexerr "unrecognised symbol: " (tok lexbuf) } | |
0708f913 C |
743 | |
744 | and comment = parse | |
745 | | "*/" { start_line true; tok lexbuf } | |
746 | | ['\n' '\r' '\011' '\012'] | |
747 | { reset_line lexbuf; let s = tok lexbuf in s ^ comment lexbuf } | |
748 | | "+" { pass_zero(); | |
749 | if !current_line_started | |
750 | then (start_line true; let s = tok lexbuf in s^(comment lexbuf)) | |
751 | else comment lexbuf } | |
752 | (* noteopti: *) | |
753 | | [^ '*'] { start_line true; let s = tok lexbuf in s ^ comment lexbuf } | |
754 | | [ '*'] { start_line true; let s = tok lexbuf in s ^ comment lexbuf } | |
951c7801 | 755 | | _ |
0708f913 C |
756 | { start_line true; let s = tok lexbuf in |
757 | Common.pr2 ("LEXER: unrecognised symbol in comment:"^s); | |
758 | s ^ comment lexbuf | |
759 | } | |
760 |