7b41698d02497ad9de8ad4fc424adc8d6927fccb
[bpt/coccinelle.git] / parsing_c / parsing_hacks.mli
1 open Common
2
3 (* This module tries to detect some cpp idioms so that we can parse as-is
4 * files by adjusting or commenting some tokens. Parsing hack style.
5 * Sometime we use some indentation information,
6 * sometimes we do some kind of lalr(k) by finding patterns. Often try to
7 * work on better token representation, like ifdef-paren-ized, brace-ized,
8 * paren-ized, so can do easier pattern matching to more easily match
9 * complex cpp idiom pattern. Also try to get context info such as
10 * whether the token is in a initializer as some common patterns have different
11 * use depending on context.
12 *
13 *
14 * Example of cpp idioms:
15 * - if 0 for commenting stuff (not always code, sometimes just real comments)
16 * - ifdef old version
17 * - ifdef funheader
18 * - ifdef statements, ifdef expression, ifdef-mid
19 * - macro toplevel (with or without ptvirg)
20 * - macro foreach
21 * - macro higher order
22 * - macro declare
23 * - macro debug
24 * - macro no ptvirg
25 * - macro string, and macro function string taking param and ##
26 * - macro attribute
27 *
28 * Cf the TMacroXxx in parser_c.mly and MacroXxx in ast_c.ml
29 *
30 * Also try infer typedef.
31 *
32 * Also do other stuff involving cpp like expanding some macros,
33 * or try parse well define body by finding the end of define virtual
34 * end-of-line token.
35 *)
36
37 (* corresponds to what is in the yacfe configuration file (e.g. standard.h) *)
38 type define_def = string * define_param * define_body
39 and define_param =
40 | NoParam
41 | Params of string list
42 and define_body =
43 | DefineBody of Parser_c.token list
44 | DefineHint of parsinghack_hint
45
46 (* strongly corresponds to the TMacroXxx in the grammar and lexer and the
47 * MacroXxx in the ast.
48 *)
49 and parsinghack_hint =
50 | HintIterator
51 | HintDeclarator
52 | HintMacroString
53 | HintMacroStatement
54 | HintAttribute
55 | HintMacroIdentBuilder
56
57 val regexp_macro: Str.regexp
58 val regexp_annot: Str.regexp
59 val regexp_declare: Str.regexp
60 val regexp_foreach: Str.regexp
61 val regexp_typedef: Str.regexp
62
63 val _defs : (string, define_def) Hashtbl.t ref
64
65 (* can reset it *)
66 val ifdef_paren_cnt: int ref
67
68 val fix_tokens_define : Parser_c.token list -> Parser_c.token list
69 val extract_cpp_define : Parser_c.token list -> (string, define_def) assoc
70
71
72 val fix_tokens_cpp : Parser_c.token list -> Parser_c.token list
73
74 (* next stream tokens -> passed stream tokens -> final next token *)
75 val lookahead :
76 pass:int ->
77 Parser_c.token list -> Parser_c.token list -> Parser_c.token
78
79