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