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