Warning, /kdevelop/kdevelop-pg-qt/examples/fact/fact.g is written in an unsupported language. File is not indexed.
0001 ----------------------------------------------------------- 0002 -- Global declarations 0003 ----------------------------------------------------------- 0004 0005 0006 [: 0007 0008 #include <QString> 0009 0010 namespace fact 0011 { 0012 class Lexer; 0013 } 0014 0015 :] 0016 0017 ------------------------------------------------------------ 0018 -- Parser class members 0019 ------------------------------------------------------------ 0020 0021 %parserclass (public declaration) 0022 [: 0023 /** 0024 * Transform the raw input into tokens. 0025 * When this method returns, the parser's token stream has been filled 0026 * and any parse_*() method can be called. 0027 */ 0028 void tokenize( const QString& contents ); 0029 0030 enum ProblemType { 0031 Error, 0032 Warning, 0033 Info 0034 }; 0035 void reportProblem( Parser::ProblemType type, const QString& message ); 0036 0037 QString tokenText(qint64 begin, qint64 end) const; 0038 0039 void setDebug( bool debug ); 0040 0041 :] 0042 0043 %parserclass (private declaration) 0044 [: 0045 QString m_contents; 0046 bool m_debug; 0047 :] 0048 0049 ------------------------------------------------------------ 0050 -- List of defined tokens 0051 ------------------------------------------------------------ 0052 0053 %token FUNCTION ("function"), VAR ("var"), IF ("if"), ELSE ("else"), 0054 RETURN ("return") ;; 0055 0056 %token LPAREN ("("), RPAREN (")"), LBRACE ("{"), RBRACE ("}"), 0057 COMMA (","), SEMICOLON (";") ;; 0058 0059 %token ASSIGN ("="), EQUAL ("=="), STAR ("*"), MINUS ("-") ;; 0060 0061 %token IDENTIFIER ("identifier"), NUMBER ("integer literal") ;; 0062 0063 %token INVALID ("invalid token") ;; 0064 0065 0066 0067 ------------------------------------------------------------ 0068 -- Start of the actual grammar 0069 ------------------------------------------------------------ 0070 0071 -- Declaration rules 0072 ---------------------- 0073 0074 (#fun=functionDefinition)* 0075 -> program ;; 0076 0077 FUNCTION id=IDENTIFIER 0078 LPAREN (#param=IDENTIFIER @ COMMA | 0) RPAREN body=body 0079 -> functionDefinition ;; 0080 0081 LBRACE (#decl=declaration)* (#stmt=statement)* RBRACE 0082 -> body ;; 0083 0084 VAR (var=variable @ COMMA) SEMICOLON 0085 -> declaration ;; 0086 0087 id=IDENTIFIER 0088 -> variable ;; 0089 0090 0091 -- Statement rules 0092 -------------------- 0093 0094 id=IDENTIFIER ASSIGN expr=expression SEMICOLON 0095 -> assignmentStatement ;; 0096 0097 IF LPAREN cond=condition RPAREN ifStmt=statement 0098 (ELSE elseStmt=statement | 0) 0099 -> ifStatement ;; 0100 0101 LBRACE (#stmt=statement)* RBRACE 0102 -> blockStatement ;; 0103 0104 RETURN expr=expression SEMICOLON 0105 -> returnStatement ;; 0106 0107 assignStmt=assignmentStatement 0108 | ifStmt=ifStatement 0109 | blockStmt=blockStatement 0110 | returnStmt=returnStatement 0111 -> statement ;; 0112 0113 0114 -- Expression rules 0115 -------------------- 0116 0117 num=NUMBER 0118 | id=IDENTIFIER (LPAREN (#argument=expression @ COMMA) RPAREN | 0) 0119 -> primary ;; 0120 0121 leftExpr=primary (STAR rightExpr=primary)* 0122 -> multExpression ;; 0123 0124 leftExpr=multExpression (MINUS rightExpr=multExpression)* 0125 -> expression ;; 0126 0127 leftExpr=expression EQUAL rightExpr=expression 0128 -> condition ;; 0129 0130 0131 0132 0133 ----------------------------------------------------------------- 0134 -- Code segments copied to the implementation (.cpp) file. 0135 -- If existent, kdevelop-pg's current syntax requires this block 0136 -- to occur at the end of the file. 0137 ----------------------------------------------------------------- 0138 0139 [: 0140 #include "factlexer.h" 0141 #include <QString> 0142 #include <QDebug> 0143 0144 namespace fact 0145 { 0146 0147 void Parser::tokenize( const QString& contents ) 0148 { 0149 m_contents = contents; 0150 Lexer lexer( this, contents ); 0151 0152 int kind = Parser::Token_EOF; 0153 do 0154 { 0155 kind = lexer.nextTokenKind(); 0156 0157 if ( !kind ) // when the lexer returns 0, the end of file is reached 0158 kind = Parser::Token_EOF; 0159 0160 Parser::Token &t = tokenStream->next(); 0161 t.kind = kind; 0162 if( t.kind == Parser::Token_EOF ) 0163 { 0164 t.begin = -1; 0165 t.end = -1; 0166 } 0167 else 0168 { 0169 t.begin = lexer.tokenBegin(); 0170 t.end = lexer.tokenEnd(); 0171 } 0172 0173 if( m_debug ) 0174 { 0175 qDebug() << kind << "(" << t.begin << "," << t.end << ")::" << tokenText(t.begin, t.end); 0176 } 0177 0178 } 0179 while ( kind != Parser::Token_EOF ); 0180 0181 this->yylex(); // produce the look ahead token 0182 } 0183 0184 QString Parser::tokenText( qint64 begin, qint64 end ) const 0185 { 0186 return m_contents.mid((int)begin, (int)end-begin+1); 0187 } 0188 0189 void Parser::reportProblem( Parser::ProblemType type, const QString& message ) 0190 { 0191 if (type == Error) 0192 qDebug() << "** ERROR:" << message; 0193 else if (type == Warning) 0194 qDebug() << "** WARNING:" << message; 0195 else if (type == Info) 0196 qDebug() << "** Info:" << message; 0197 } 0198 0199 // custom error recovery 0200 void Parser::expectedToken(int /*expected*/, qint64 /*where*/, const QString& name) 0201 { 0202 reportProblem( 0203 Parser::Error, 0204 QString("Expected token \"%1\"").arg(name)); 0205 } 0206 0207 void Parser::expectedSymbol(int /*expected_symbol*/, const QString& name) 0208 { 0209 qint64 line; 0210 qint64 col; 0211 size_t index = tokenStream->index()-1; 0212 Token &token = tokenStream->token(index); 0213 qDebug() << "token starts at:" << token.begin; 0214 qDebug() << "index is:" << index; 0215 tokenStream->startPosition(index, &line, &col); 0216 QString tokenValue = tokenText(token.begin, token.end); 0217 reportProblem( 0218 Parser::Error, 0219 QString("Expected symbol \"%1\" (current token: \"%2\" [%3] at line: %4 col: %5)") 0220 .arg(name) 0221 .arg(token.kind != 0 ? tokenValue : "EOF") 0222 .arg(token.kind) 0223 .arg(line) 0224 .arg(col)); 0225 } 0226 0227 void Parser::setDebug( bool debug ) 0228 { 0229 m_debug = debug; 0230 } 0231 0232 } // end of namespace fact 0233 0234 :] 0235 0236 -- kate: space-indent on; indent-width 4; tab-width 4; replace-tabs on