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