1-- Copyright (C) 2005 Roberto Raggi <roberto@kdevelop.org> 2-- Copyright (C) 2009 Jonathan Schmidt-Dominé <devel@the-user.org> 3-- 4-- This library is free software; you can redistribute it and/or 5-- modify it under the terms of the GNU Library General Public 6-- License as published by the Free Software Foundation; either 7-- version 2 of the License, or (at your option) any later version. 8-- 9-- This library is distributed in the hope that it will be useful, 10-- but WITHOUT ANY WARRANTY; without even the implied warranty of 11-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12-- Library General Public License for more details. 13-- 14-- You should have received a copy of the GNU Library General Public License 15-- along with this library; see the file COPYING.LIB. If not, write to 16-- the Free Software Foundation, Inc., 59 Temple Place - Suite 330, 17-- Boston, MA 02111-1307, USA. 18 19[: 20#include <QString> 21#include <QDebug> 22:] 23 24%parserclass (protected declaration) 25[: 26void expectedSymbol(cc::AstNode::AstNodeKind kind, const QString& name) { qWarning() << "In AstNode " << kind << ": Expected symbol " << name; } 27void expectedToken(int kind, enum TokenType token, const QString& name) { qWarning() << "In AstNode " << kind << ": Expected token " << name << " (" << token << ")";} 28struct ParserState { 29 int ltCounter; 30}; 31ParserState m_state; 32:] 33 34%parser_declaration_header "ccast.h" 35 36------------------------------------------------------------ 37-- T O K E N L I S T 38------------------------------------------------------------ 39 40-- keywords: 41%token CASE ("case"), DEFAULT ("default"), IF ("if"), ELSE ("else"), 42 SWITCH ("switch"), WHILE ("while"), DO ("do"), FOR ("for"), 43 BREAK ("break"), CONTINUE ("continue"), GOTO ("goto"), 44 RETURN ("return"), TYPEDEF ("typedef"), EXTERN ("extern"), 45 STATIC ("static"), AUTO ("auto"), REGISTER ("register"), VOID ("void"), 46 CHAR ("char"), SHORT ("short"), INT ("int"), LONG ("long"), 47 FLOAT ("float"), DOUBLE ("double"), SIGNED ("signed"), 48 UNSIGNED ("unsigned"), TYPEDEF_NAME ("pre-defined type specification"), 49 STRUCT ("struct"), UNION ("union"), ENUM ("enum"), CONST ("const"), 50 VOLATILE ("volatile") ;; 51 52-- seperators: 53%token LPAREN ("("), RPAREN (")"), LBRACE ("{"), RBRACE ("}"), LBRACKET ("["), 54 RBRACKET ("]"), DOT ("."), ARROW ("->"), COLON (":"), COMMA (","), 55 SEMICOLON (";") ;; 56 57-- operators: 58%token PLUS ("+"), MINUS ("-"), STAR ("*"), DIVIDE ("/"), REMAINDER ("%"), 59 TILDE ("~"), AND ("&"), OR ("|"), XOR ("^"), NOT ("!"), 60 SIZEOF ("sizeof"), PLUS_PLUS ("++"), MINUS_MINUS ("--"), LSHIFT ("<<"), 61 RSHIFT (">>"), AND_AND ("&&"), OR_OR ("||"), QUESTION ("?"), 62 EQUAL ("="), PLUS_EQUAL ("+="), MINUS_EQUAL ("-="), STAR_EQUAL ("*="), 63 DIVIDE_EQUAL ("/="), REMAINDER_EQUAL ("%="), AND_EQUAL ("&="), 64 OR_EQUAL ("|="), XOR_EQUAL ("^="), LSHIFT_EQUAL ("<<="), 65 RSHIFT_EQUAL (">>="), EQUAL_EQUAL ("=="), NOT_EQUAL ("!="), 66 LESS ("<"), GREATER (">"), LESS_EQUAL ("<="), GREATER_EQUAL (">="), 67 ELLIPSIS ("...") ;; 68 69-- identifiers and literals: 70%token IDENTIFIER ("identifier"), STRING_LITERAL ("string literal"), 71 X_CONSTANT ;; 72 73-- GCC extensions 74%token INLINE ("inline"), EXTENSION ("__extension__"), ASM ("asm") ;; 75 76 77 78 79------------------------------------------------------------ 80-- E X T E R N A L D E C L A R A T I O N S 81------------------------------------------------------------ 82 (#ddeclaration=ddeclaration)* 83-> document ;; 84 85 enum_specifier=enum_specifier SEMICOLON 86 | struct_or_union_specifier=struct_or_union_specifier SEMICOLON 87 | TYPEDEF typedef_d=typedef_d SEMICOLON 88 | try/rollback(external_block=external_block) catch( 89 value_declaration=value_declaration 90 ) 91-> ddeclaration ;; 92 93 typed_identifier ( SEMICOLON 94 | try/rollback(function_definition=function_definition) catch( 95 function_declaration=function_declaration 96 )) 97-> value_declaration ;; 98 99 EXTERN STRING_LITERAL LBRACE (#ddeclaration=ddeclaration)* RBRACE 100-> external_block ;; 101 102-- VOID 103-- | CHAR 104-- | SHORT 105-- | INT 106-- | LONG 107-- | FLOAT 108-- | DOUBLE 109---> type_name_d ;; 110 111 LPAREN type_attribute_identifier=type_attribute_identifier RPAREN (LPAREN (0 | (#function_pointer_parameter=function_pointer_parameter @ COMMA)) RPAREN | 0) 112 | STAR type_attribute_identifier=type_attribute_identifier 113 | type_attribute_identifier=type_attribute_identifier LBRACKET (0 | X_CONSTANT) RBRACKET 114 | IDENTIFIER 115-> type_attribute_identifier ;; 116 117 type_name=type_name type_attribute_identifier=type_attribute_identifier 118-> typed_identifier ;; 119 120 typed_identifier=typed_identifier SEMICOLON 121-> variable_declaration ;; 122 123 try/rollback(typed_identifier=typed_identifier) catch( 124 type_name=type_name 125 ) 126-> parameter ;; 127 128 struct_or_union_specifier=struct_or_union_specifier 129 | enum_specifier=enum_specifier 130 | typed_identifier=typed_identifier 131-> typedef_d ;; 132 133 (#statement = statement)* 134-> execution_block ;; 135 136 LPAREN (0|#declaration_parameter=declaration_parameter @ COMMA) RPAREN (0|asm_against_mangling=asm_against_mangling) SEMICOLON 137-> function_declaration ;; 138 139 typed_identifier=typed_identifier (0 | EQUAL constant_expression=constant_expression) 140 | ELLIPSIS 141-> named_parameter ;; 142 143 try/rollback(typed_identifier=typed_identifier (0 | EQUAL constant_expression=constant_expression)) catch( 144 type_name=type_name 145 ) 146 | ELLIPSIS 147-> declaration_parameter ;; 148 149 try/rollback(typed_identifier=typed_identifier) catch( 150 type_name=type_name 151 ) 152 | ELLIPSIS 153-> function_pointer_parameter ;; 154 155 LPAREN (#named_parameter=named_parameter @ COMMA) RPAREN LBRACE execution_block=execution_block RBRACE 156-> function_definition ;; 157 158-- (#external_declaration=external_declaration)* 159---> translation_unit ;; 160 161 #declaration_specifier=declaration_specifier (#declaration_specifier=declaration_specifier)* 162-> declaration_header ;; 163 164-- declaration_header=declaration_header variable_or_function=variable_or_function 165---> external_declaration ;; 166 167-- declarator=declarator (COMMA #init_declarator=init_declarator @ COMMA SEMICOLON 168-- | SEMICOLON 169-- | ?[:is_fun_definition:] declaration* compound_statement 170-- | (#declaration=declaration)* compound_statement=compound_statement 171-- | initializer=initializer (COMMA #init_declarator=init_declarator)* SEMICOLON) 172---> variable_or_function ;; 173 174------------------------------------------------------------ 175-- GCC-STUFF 176------------------------------------------------------------ 177 STRING_LITERAL LPAREN IDENTIFIER 178-> asm_specifier ;; 179 180 ASM (0 | VOLATILE) LPAREN (STRING_LITERAL*) (0 | COLON (#output_operands=asm_specifier @ COMMA) (0 | COLON (#input_operands=asm_specifier @ COMMA) (0 | COLON (STRING_LITERAL @ COMMA)))) 181-> inline_asm ;; 182 183 EXTENSION LPAREN LBRACE execution_block RBRACE RPAREN 184-> ext_expression ;; 185 186 ASM LPAREN STRING_LITERAL RPAREN 187-> asm_against_mangling ;; 188 189------------------------------------------------------------ 190-- E X P R E S S I O N S 191------------------------------------------------------------ 192 193 identifier=IDENTIFIER 194 | constant=constant 195 | string_literal=STRING_LITERAL 196 | LPAREN expression=expression RPAREN 197-> primary_expression ;; 198 199 primary_expression=primary_expression (#postfix_expression_rest=postfix_expression_rest)* 200-> postfix_expression ;; 201 202 (DOT | ARROW) IDENTIFIER 203 | PLUS_PLUS 204 | MINUS_MINUS 205 | LPAREN (argument_expression_list=argument_expression_list | 0) RPAREN 206 | LBRACKET expression=expression RBRACKET 207-> postfix_expression_rest ;; 208 209 #assignment_expression=assignment_expression @ COMMA 210-> argument_expression_list ;; 211 212 postfix_expression=postfix_expression 213 | PLUS_PLUS unary_expression=unary_expression 214 | MINUS_MINUS unary_expression=unary_expression 215 | unary_operator cast_expression=cast_expression 216 | SIZEOF LPAREN type_name=type_name RPAREN 217-> unary_expression ;; 218 219 AND 220 | STAR 221 | PLUS 222 | MINUS 223 | TILDE 224 | NOT 225-> unary_operator ;; 226 227 LPAREN type_name=type_name RPAREN cast_expression=cast_expression 228 | unary_expression=unary_expression 229-> cast_expression ;; 230 231 #cast_expression=cast_expression @ (STAR | DIVIDE | REMAINDER) 232-> multiplicative_expression ;; 233 234 #multiplicative_expression=multiplicative_expression @ (PLUS | MINUS) 235-> additive_expression ;; 236 237 #additive_expression=additive_expression @ (LSHIFT | RSHIFT) 238-> shift_expression ;; 239 240 #shift_expression=shift_expression @ (LESS | GREATER | LESS_EQUAL | GREATER_EQUAL) 241-> relational_expression ;; 242 243 #relational_expression=relational_expression @ (EQUAL_EQUAL | NOT_EQUAL) 244-> equality_expression ;; 245 246 #equality_expression=equality_expression @ AND 247-> AND_expression ;; 248 249 #AND_expression=AND_expression @ XOR 250-> exclusive_OR_expression ;; 251 252 #exclusive_OR_expression=exclusive_OR_expression @ OR 253-> inclusive_OR_expression ;; 254 255 #inclusive_OR_expression=inclusive_OR_expression @ AND_AND 256-> logical_AND_expression ;; 257 258 #logical_AND_expression=logical_AND_expression @ OR_OR 259-> logical_OR_expression ;; 260 261 logical_OR_expression=logical_OR_expression (QUESTION expression COLON conditional_expression | 0) 262-> conditional_expression ;; 263 264 #conditional_expression=conditional_expression @ assignment_operator 265-> assignment_expression ;; 266 267 EQUAL 268 | STAR_EQUAL 269 | DIVIDE_EQUAL 270 | REMAINDER_EQUAL 271 | PLUS_EQUAL 272 | MINUS_EQUAL 273 | LSHIFT_EQUAL 274 | RSHIFT_EQUAL 275 | AND_EQUAL 276 | XOR_EQUAL 277 | OR_EQUAL 278-> assignment_operator ;; 279 280 #assignment_expression=assignment_expression @ COMMA 281-> expression ;; 282 283 conditional_expression=conditional_expression 284-> constant_expression ;; 285 286 X_CONSTANT 287-> constant ;; 288 289------------------------------------------------------------ 290-- S T A T E M E N T S 291------------------------------------------------------------ 292 IDENTIFIER COLON 293 | labeled_statement=labeled_statement 294 | compound_statement=compound_statement 295 | expression_statement=expression_statement 296 | selection_statement=selection_statement 297 | iteration_statement=iteration_statement 298 | jump_statement=jump_statement 299 | inline_asm 300 | SEMICOLON 301-> statement ;; 302 303 CASE constant_expression=constant_expression COLON statement=statement 304 | DEFAULT COLON statement=statement 305-> labeled_statement ;; 306 307 LBRACE (#declaration=declaration)* (#statement=statement)* RBRACE 308-> compound_statement ;; 309 310 expression=expression SEMICOLON 311-> expression_statement ;; 312 313 IF LPAREN expression=expression RPAREN statement=statement (ELSE alternative_statement=statement | 0) 314 | SWITCH LPAREN expression=expression RPAREN statement=statement 315-> selection_statement ;; 316 317 WHILE LPAREN (expression=expression | ext_expression=ext_expression) RPAREN statement=statement 318 | DO statement WHILE LPAREN expression=expression RPAREN SEMICOLON 319 | FOR LPAREN (for_1=expression|for1_ext=ext_expression|0) SEMICOLON (for_2=expression|for2_ext=ext_expression|0) SEMICOLON (for_3=expression|for3_ext=ext_expression|0) RPAREN statement=statement 320-> iteration_statement ;; 321 322 GOTO IDENTIFIER SEMICOLON 323 | CONTINUE SEMICOLON 324 | BREAK SEMICOLON 325 | RETURN (expression=expression | 0) SEMICOLON 326-> jump_statement ;; 327 328------------------------------------------------------------ 329-- D E C L A R A T I O N S 330------------------------------------------------------------ 331 332 #declaration_specifier=declaration_specifier (#declaration_specifier=declaration_specifier)* (#init_declarator=init_declarator @ COMMA | 0) SEMICOLON 333-> declaration ;; 334 335 storage_class_specifier=storage_class_specifier 336 | type_specifier=type_specifier 337 | type_qualifier=type_qualifier 338-> declaration_specifier ;; 339 340 declarator=declarator (EQUAL initializer=initializer | 0) 341-> init_declarator ;; 342 343 TYPEDEF 344 | EXTERN 345 | STATIC 346 | AUTO 347 | REGISTER 348-> storage_class_specifier ;; 349 350 VOID 351 | CHAR 352 | SHORT 353 | INT 354 | LONG 355 | FLOAT 356 | DOUBLE 357 | SIGNED 358 | UNSIGNED 359 | struct_or_union_specifier=struct_or_union_specifier 360 | enum_specifier=enum_specifier 361 | TYPEDEF_NAME 362-> type_specifier ;; 363 364-- EXTERN STRING_LITERAL LBRACE (#external_declaration=external_declaration)* RBRACE 365---> external_block ;; 366 367 (STRUCT | UNION) (IDENTIFIER LBRACE (#struct_declaration=struct_declaration)* RBRACE 368 | LBRACE (#struct_declaration=struct_declaration)* RBRACE) 369-> struct_or_union_specifier ;; 370 371 #specifier_qualifier=specifier_qualifier #specifier_qualifier=specifier_qualifier (#specifier_qualifier=specifier_qualifier*) (#struct_declarator=struct_declarator)* SEMICOLON 372-> struct_declaration ;; 373 374 type_specifier=type_specifier 375 | type_qualifier=type_qualifier 376-> specifier_qualifier ;; 377 378 declarator=declarator (constant_expression=constant_expression | 0) 379 | COLON constant_expression=constant_expression 380-> struct_declarator ;; 381 382 ENUM (IDENTIFIER LBRACE #enumerator=enumerator @ COMMA RBRACE | LBRACE #enumerator=enumerator @ COMMA RBRACE) 383-> enum_specifier ;; 384 385 IDENTIFIER (EQUAL constant_expression=constant_expression | 0) 386-> enumerator ;; 387 388 CONST 389 | VOLATILE 390-> type_qualifier ;; 391 392 (pointer=pointer direct_declarator=direct_declarator | direct_declarator=direct_declarator) (#direct_declarator_rest=direct_declarator_rest)* 393 | direct_declarator=direct_declarator 394-> declarator ;; 395 396 IDENTIFIER 397 | LPAREN declarator=declarator RPAREN 398-> direct_declarator ;; 399 400 LBRACKET (constant_expression | 0) RBRACKET 401 | LPAREN (IDENTIFIER @ COMMA | parameter_type_list=parameter_type_list | 0) RPAREN 402-> direct_declarator_rest ;; 403 404 STAR (#type_qualifier=type_qualifier | STAR)* 405-> pointer ;; 406 407 (#parameter_declaration=parameter_declaration | ELLIPSIS) @ COMMA 408 | 0 409-> parameter_type_list ;; 410 411 #declaration_specifier=declaration_specifier (#declaration_specifier=declaration_specifier)* (declarator=declarator | abstract_declarator=abstract_declarator | 0) 412-> parameter_declaration ;; 413 414 #specifier_qualifier=specifier_qualifier (#specifier_qualifier=specifier_qualifier)* 415-> type_name ;; 416 417 (pointer=pointer #direct_abstract_declarator=direct_abstract_declarator | #direct_abstract_declarator=direct_abstract_declarator) (#direct_abstract_declarator=direct_abstract_declarator)* 418-> abstract_declarator ;; 419 420 LPAREN (abstract_declarator=abstract_declarator | parameter_type_list=parameter_type_list) RPAREN 421 | LBRACKET (constant_expression | 0) RBRACKET 422-> direct_abstract_declarator ;; 423 424 assignment_expression=assignment_expression 425 | LBRACE #initializer=initializer (COMMA (#initializer=initializer | 0))* RBRACE 426-> initializer ;; 427 428[: 429namespace cc 430{ 431Parser::ParserState *Parser::copyCurrentState() 432{ 433 ParserState *state = new ParserState(); 434 state->ltCounter = m_state.ltCounter; 435 return state; 436} 437 438void Parser::restoreState( Parser::ParserState *state ) 439{ 440 m_state.ltCounter = state->ltCounter; 441} 442}; 443:] 444