%{ /*************************************************************************** * Copyright (C) 2005 by Alexander Dymo * * adymo@kdevelop.org * * Copyright (C) 2006 by Andreas Pakulat * * apaku@gmx.de * * * * This program is free software; you can redistribute it and/or modify * * it under the terms of the GNU Library General Public License as * * published by the Free Software Foundation; either version 2 of the * * License, or (at your option) any later version. * * * * This program is distributed in the hope that it will be useful, * * but WITHOUT ANY WARRANTY; without even the implied warranty of * * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * * GNU General Public License for more details. * * * * You should have received a copy of the GNU Library General Public * * License along with this program; if not, write to the * * Free Software Foundation, Inc., * * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * ***************************************************************************/ /** @file qmake.yy QMake Parser Simple LALR parser which builds the syntax tree (see @ref QMake::AST). @todo Recognize comments after statements like: SOURCES = foo #regognize me @fixme Parser fails on files that do not end with a newline @fixme 1 shift/reduce conflict in "line_body" rule */ #include #include "qmakeast.h" #include #define YYSTYPE_IS_DECLARED namespace QMake { class Lexer; /** The yylval type. */ struct Result { Result(): node(0) {} /**Type of semantic value for simple grammar rules.*/ QString value; /**Type of semantic value for grammar rules which are parts of AST.*/ AST *node; /**Type of semantic value for "multiline_values" grammar rule. Each line of multiline value is stored as a string in the list. For example we have in .pro file: @code SOURCE = foo1.cpp \ foo2.cpp \ foo3.cpp foo4.cpp @endcode The string list will be populated with three strings:
    foo1.cpp
    foo2.cpp
    foo3.cpp foo4.cpp
    
*/ QStringList values; QString indent; }; #define YYSTYPE Result typedef Result YYSTYPE; } extern int QMakelex( QMake::Result* yylval, QMake::Lexer* lexer ); /** The stack to store ProjectAST pointers when a new child ProjectAST is created and filled with statements. Parser creates root ProjectAST for a .pro file, pushes it onto the stack and starts adding statements. Each statement is added as a child StatementAST to the ProjectAST currently on the top in the stack. When a scope or function scope statement is parsed, the child ProjectAST is created and pushed onto the stack. Therefore all statements which belong to the scope or function scope are added as childs to their direct parent (scope or function scope). */ //QValueStack projects; /** The current depth of AST node is stored here. AST depth is important to know because automatic indentation can be easily implemented (the parser itself looses all information about indentation). */ // int depth = 0; /* To debug this parser, put the line below into the next bison file section. Don't forget to uncomment "yydebug = 1" line in qmakedriver.cpp. %debug */ %} %skeleton "lalr1.cc" %define "parser_class_name" "Parser" %name-prefix="QMake" %parse-param { QMake::Lexer* lexer } %parse-param { QValueStack& projects } %parse-param { int depth } %lex-param { QMake::Lexer* lexer } %start project %token ID_SIMPLE %token EQ %token PLUSEQ %token MINUSEQ %token STAREQ %token TILDEEQ %token LBRACE %token RBRACE %token COLON %token NEWLINE %token COMMENT %token CONT %token COMMENT_CONT %token RCURLY %token LCURLY %token ID_ARGS %token QUOTED_VARIABLE_VALUE %token VARIABLE_VALUE %token LIST_WS %token ENDOFFILE %% project : { ProjectAST *projectAST = new ProjectAST(); projects.push(projectAST); } statements ; statements : statements statement { projects.top()->addChildAST($2); $2->setDepth(depth); } | ; statement : variable_assignment { $$ = $1; } | scope { $$ = $1; } | function_call { $$ = $1; } | comment { $$ = $1; } | emptyline { $$ = new NewLineAST(); } ; variable_assignment : ID_SIMPLE operator multiline_values listws NEWLINE { AssignmentAST *node = new AssignmentAST(); node->scopedID = $1; node->op = $2; node->values = $3 ; node->values.append( $4 ); node->values.append( $5 ); node->indent = $3; $$ = node; } | ID_SIMPLE operator multiline_values listws ENDOFFILE { AssignmentAST *node = new AssignmentAST(); node->scopedID = $1; node->op = $2; node->values = $3 ; node->values.append( $4 ); node->indent = $3; $$ = node; } | ID_SIMPLE operator multiline_values listws CONT { AssignmentAST *node = new AssignmentAST(); node->scopedID = $1; node->op = $2; node->values = $3 ; node->values.append( $4 ); node->values.append( $5 ); node->indent = $3; $$ = node; } | ID_SIMPLE operator multiline_values listws COMMENT_CONT { AssignmentAST *node = new AssignmentAST(); node->scopedID = $1; node->op = $2; node->values = $3 ; node->values.append( $4 ); node->values.append( $5 ); node->indent = $3; $$ = node; } | ID_SIMPLE operator listws NEWLINE { AssignmentAST *node = new AssignmentAST(); node->scopedID = $1; node->op = $2; node->values.append( $3 ); node->values.append( $4 ); $$ = node; } | ID_SIMPLE operator listws ENDOFFILE { AssignmentAST *node = new AssignmentAST(); node->scopedID = $1; node->op = $2; node->values.append( $3 ); $$ = node; } | ID_SIMPLE operator listws COMMENT { AssignmentAST *node = new AssignmentAST(); node->scopedID = $1; node->op = $2; node->values.append( $3 ); node->values.append( $4 ); $$ = node; } | ID_SIMPLE operator multiline_values listws COMMENT { AssignmentAST *node = new AssignmentAST(); node->scopedID = $1; node->op = $2; node->values = $3; node->values.append( $4 ); node->values.append( $5 ); node->indent = $3; $$ = node; } ; possible_value : variable_value | COMMENT_CONT | CONT multiline_values : multiline_values LIST_WS possible_value { $$.append( $2 ); $$.append( $3 ); } | multiline_values variable_value { $$.append( $2 ); } | multiline_values listws CONT listws possible_value { $$.append( $2 ); $$.append( $3 ); $$.append( $4 ); $$.append( $5 ); if( $4 != "" && $$ == "" ) $$ = $4; } | multiline_values listws COMMENT_CONT listws possible_value { $$.append( $2 ); $$.append( $3 ); $$.append( $4 ); $$.append( $5 ); if( $4 != "" && $$ == "" ) $$ = $4; } | listws possible_value { $$ = QStringList(); $$.append( $1 ); $$.append( $2 ); } ; variable_value : VARIABLE_VALUE { $$ = $1; } | QUOTED_VARIABLE_VALUE { $$ = $1; } ; listws: LIST_WS { $$ = $1; $$ = $1; } | { $$ = QString(); $$ = QString(); } ; operator : EQ { $$ = $1; } | PLUSEQ { $$ = $1; } | MINUSEQ { $$ = $1; } | STAREQ { $$ = $1; } | TILDEEQ { $$ = $1; } ; scope : ID_SIMPLE { ProjectAST *projectAST = new ProjectAST(ProjectAST::Scope); projects.push(projectAST); projects.top()->scopedID = $1; depth++; } scope_body { $$ = projects.pop(); depth--; } ; function_call : ID_SIMPLE LBRACE function_args RBRACE { ProjectAST *projectAST = new ProjectAST(ProjectAST::FunctionScope); projects.push(projectAST); projects.top()->scopedID = $1; projects.top()->args = $3; depth++; //qWarning("%s", $1.ascii()); if ($1.contains("include")) { IncludeAST *includeAST = new IncludeAST(); includeAST->projectName = $3; projects.top()->addChildAST(includeAST); includeAST->setDepth(depth); } } scope_body else_statement { $$ = projects.pop(); depth--; } ; function_args : ID_ARGS { $$ = $1; } | { $$ = ""; } ; scope_body : LCURLY statements RCURLY | COLON statement { projects.top()->addChildAST($2); $2->setDepth(depth); } | ; else_statement : "else" LCURLY { ProjectAST *projectAST = new ProjectAST(ProjectAST::FunctionScope); projects.push(projectAST); projects.top()->scopedID = "else"; projects.top()->args = ""; depth++; } scope_body RCURLY { $$ = projects.pop(); depth--; } | { $$ = new ProjectAST(); } ; comment : COMMENT { CommentAST *node = new CommentAST(); node->comment = $1; $$ = node; } ; emptyline : NEWLINE ; %% namespace QMake { void Parser::error(const location_type& /*l*/, const std::string& m) { std::cerr << m << std::endl; } }