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