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