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