File: pp.y

package info (click to toggle)
codelite 17.0.0%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 136,204 kB
  • sloc: cpp: 491,547; ansic: 280,393; php: 10,259; sh: 8,930; lisp: 7,664; vhdl: 6,518; python: 6,020; lex: 4,920; yacc: 3,123; perl: 2,385; javascript: 1,715; cs: 1,193; xml: 1,110; makefile: 804; cobol: 741; sql: 709; ruby: 620; f90: 566; ada: 534; asm: 464; fortran: 350; objc: 289; tcl: 258; java: 157; erlang: 61; pascal: 51; ml: 49; awk: 44; haskell: 36
file content (177 lines) | stat: -rw-r--r-- 4,893 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
%{
// Copyright Eran Ifrah(c)
%}

%{
/*************** Includes and Defines *****************************/
#include <wx/string.h>
#include "pptable.h"

#define YYSTYPE wxString
#define YYDEBUG 0        /* get the pretty debugging code to compile*/

#ifdef yylex
#undef yylex
#define yylex pp_lex
#endif

extern void pp_error(char *st);
extern int pp_lex();
extern wxString g_definition;
extern int in_if_1;
extern bool g_forCC;
extern wxString g_filename;

// Static
static std::vector<wxString> g_tmpMacros;

/*************** Standard ytab.c continues here *********************/
%}


/* This group is used by the C/C++ language parser */
%token  PP_DEFINE PP_IF         PP_IFDEF               PP_DEFINED 
%token  PP_UNDEF  PP_ELSE       PP_ELIF                PP_ENDIF
%token  PP_POUND  PP_IDENTIFIER PP_COMPLEX_REPLACEMENT PP_IFNDEF
%token  PP_ZERO    PP_CPLUSPLUS   PP_INCLUDE              PP_DEFINED
%token  PP_AND     PP_OR          PP_EQUAL                PP_NEQUAL
%token  PP_INT     PP_LOWERTHAN   PP_GREATERTHAN

%start   translation_unit

%%

translation_unit :      /*empty*/
                | translation_unit the_macros_rule
                ;
				
/** proxy rule so we can perform our cleanup **/
the_macros_rule: {g_tmpMacros.clear();}  macros
                ;

/**
 * We currently support 3 types of macros:
 * 1) Simple define macros (e.g. #define VALUE 1)
 * 2) Function like macros (e.g. #define FOO(x) printf("%s\n", x);)
 * 3) Conditional macros:
 *   3.1) #if (condition)
 *   3.2) #ifdef (NAME)
 *   3.3) #if defined(COND) && defiend(COND2)
 */
macros:   define_simple_macros
        | define_func_like_macros
        | if_cplusplus
		| ifdef_simple_macro
		| if_macros
		| elif_macros
		| end_if
        | error {
            //wxPrintf(wxT("CodeLite: syntax error, unexpected token '%s' found\n"), pp_lval.c_str());
        }
        ;

end_if : PP_ENDIF
	   ;
	   
if_cplusplus : PP_IF PP_CPLUSPLUS
        {
            if(in_if_1 == 0)
                in_if_1 = 1;
        }
        ;
        
define_simple_macros: PP_DEFINE PP_IDENTIFIER PP_COMPLEX_REPLACEMENT 
        {
            PPToken token;
			token.fileName = g_filename;
            token.name     = $2;
            token.flags    = (PPToken::IsValid | PPToken::IsOverridable);
            
            if(in_if_1) {
                // the token was found in a '#if 1' block - dont allow overriding it
                token.flags &= ~(PPToken::IsOverridable);
            }
            
            token.replacement = g_definition;
			
			if(g_forCC) {
				
				if(!token.replacement.empty()) {
					wxChar ch = token.replacement.at(0);
					if( !(ch >= (int)wxT('0') && ch <= (int)wxT('9')) )
						PPTable::Instance()->Add( token );
				}
				
			} else {
				PPTable::Instance()->Add( token );
			}
		}
        ;
        
define_func_like_macros: PP_DEFINE PP_IDENTIFIER '(' args_list ')' PP_COMPLEX_REPLACEMENT
        {
            PPToken token;
			token.fileName    = g_filename;
            token.name        = $2;
            token.replacement = g_definition;
            token.flags       = (PPToken::IsFunctionLike | PPToken::IsValid | PPToken::IsOverridable);
            if(in_if_1) {
                // the token was found in a '#if 1' block - dont allow overriding it
                token.flags &= ~(PPToken::IsOverridable);
            }
            token.processArgs( $4 );
            PPTable::Instance()->Add( token ); 
        }
        ;

args_list: /* empty */
        | PP_IDENTIFIER                 { $$ = $1;           }
        | args_list ',' PP_IDENTIFIER   { $$ = $1 + $2 + $3; }
        ;

ifdef_simple_macro: PP_IFDEF PP_IDENTIFIER
		{
			PPTable::Instance()->AddUsed($2);
		}
		;

if_macros: PP_IF if_condition
        ;

elif_macros: PP_ELIF if_condition
		;

if_condition: generic_condition
		{
			for(size_t i=0; i<g_tmpMacros.size(); i++) {
				//wxPrintf(wxT("Collected: %s\n"), g_tmpMacros[i].c_str());
				PPTable::Instance()->AddUsed(g_tmpMacros[i]);
			}
		}
        ;

 
generic_condition: generic_condition_base
				 | generic_condition logical_operator generic_condition_base
				 ;
				 
generic_condition_base: /*empty*/ 
						| '(' PP_IDENTIFIER ')'                      {g_tmpMacros.push_back($2);}
						| PP_IDENTIFIER                              {g_tmpMacros.push_back($1);}
						| PP_IDENTIFIER test_operator PP_INT         {g_tmpMacros.push_back($1);}
						| '(' PP_IDENTIFIER test_operator PP_INT ')' {g_tmpMacros.push_back($2);}
						| PP_DEFINED '(' PP_IDENTIFIER ')'           {g_tmpMacros.push_back($3);}
						;
				 
logical_operator: PP_AND 
			    | PP_OR
				;
		
test_operator: PP_EQUAL 
			 | PP_NEQUAL
			 | PP_LOWERTHAN
			 | PP_GREATERTHAN
			 ;

%%