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