Warning, /kdevelop/kdevelop-pg-qt/examples/cool/cool.g is written in an unsupported language. File is not indexed.

0001 [:
0002 #include <QString>
0003 
0004 :]
0005 ------------------------------------------------------------
0006 -- Parser class members
0007 ------------------------------------------------------------
0008 
0009 %parserclass (private declaration)
0010 [:
0011     QString m_contents;
0012 :]
0013 
0014 %parserclass (public declaration)
0015 [:
0016   /**
0017    * Transform the raw input into tokens.
0018    * When this method returns, the parser's token stream has been filled
0019    * and any parse_*() method can be called.
0020    */
0021   void tokenize( char *contents );
0022 
0023   enum problem_type {
0024       error,
0025       warning,
0026       info
0027   };
0028 //  void report_problem( Parser::problem_type type, const char* message );
0029   void report_problem( Parser::problem_type type, const QString & message );
0030   
0031   QString tokenText(qint64 begin, qint64 end);
0032 :]
0033 
0034 %parser_declaration_header "coolast.h"
0035 
0036 
0037 ------------------------------------------------------------
0038 -- List of defined tokens
0039 ------------------------------------------------------------
0040 
0041 -- keywords:
0042 %token CLASS ("class"), INHERITS ("inherits"), NEW ("new"),
0043        IF ("if"), THEN ("then"), ELSE ("else"), FI ("fi"), WHILE ("while"),
0044        LOOP ("loop"), POOL ("pool"), LET ("let"), IN ("in"),
0045        CASE ("case"), OF ("of"), ESAC ("esac") ;;
0046 
0047 -- seperators:
0048 %token LPAREN ("("), RPAREN (")"), LBRACE ("{"), RBRACE ("}"), SEMICOLON (";"),
0049        COMMA (","), DOT ("."), AT ("@") ;;
0050 
0051 -- operators:
0052 %token PLUS ("+"), MINUS ("-"), STAR ("*"), SLASH ("/"), EQUAL ("="),
0053        LESS_EQUAL ("<="), LESS ("<"), COLON (":"), ARROW_LEFT ("<-"),
0054        ARROW_RIGHT ("=>"), TILDE ("~"), NOT ("not"), ISVOID ("isvoid") ;;
0055 
0056 -- literals and identifiers:
0057 %token IDENTIFIER ("identifier"), TYPE ("type specification"),
0058        INTEGER ("integer literal"), STRING ("string literal"),
0059        TRUE ("true"), FALSE ("false") ;;
0060 
0061 -- token that makes the parser fail in any case:
0062 %token INVALID ("invalid token") ;;
0063 
0064 
0065 
0066 ------------------------------------------------------------
0067 -- Start of the actual grammar
0068 ------------------------------------------------------------
0069 
0070    (#klass=class SEMICOLON)*
0071 -> program ;;
0072 
0073    CLASS type=TYPE (INHERITS base_type=TYPE | 0) LBRACE (#feature=feature SEMICOLON)* RBRACE
0074 -> class ;;
0075 
0076    name=IDENTIFIER COLON type=TYPE
0077 -> formal ;;
0078 
0079  ( ?[: LA(2).kind == Token_LPAREN :]
0080    name=IDENTIFIER LPAREN (#formal=formal @ COMMA | 0) RPAREN
0081    COLON type=TYPE LBRACE expression=expression RBRACE
0082  |
0083    name=IDENTIFIER COLON type=TYPE (ARROW_LEFT expression=expression | 0)
0084  )
0085 -> feature ;;
0086 
0087  (
0088    ?[: LA(2).kind == Token_ARROW_LEFT :]
0089    name=IDENTIFIER ARROW_LEFT expression=expression                  -- assignment
0090  |
0091    ?[: LA(2).kind == Token_LPAREN :]
0092    name=IDENTIFIER LPAREN (#argument=expression @ COMMA | 0) RPAREN  -- dispatch
0093  |
0094    variable=IDENTIFIER
0095  | integer_literal=INTEGER
0096  | string_literal=STRING
0097  | true_literal=TRUE
0098  | false_literal=FALSE
0099  | NEW new_type=TYPE
0100  | LPAREN expression=expression RPAREN
0101  | if_expression=if_expression
0102  | while_expression=while_expression
0103  | block_expression=block_expression
0104  | let_expression=let_expression
0105  | case_expression=case_expression
0106  )
0107 -> primary_expression ;;
0108 
0109    op=TILDE  expression=primary_expression -- ^tilde_expression
0110  | op=NOT    expression=primary_expression -- ^not_expression
0111  | op=ISVOID expression=primary_expression -- ^isvoid_expression
0112  |           expression=primary_expression
0113 -> unary_expression ;;
0114 
0115    base_expression=unary_expression
0116    (AT at_type=TYPE DOT name=IDENTIFIER LPAREN (#arguments=expression @ COMMA | 0) RPAREN
0117    |                DOT name=IDENTIFIER LPAREN (#arguments=expression @ COMMA | 0) RPAREN
0118    )*
0119 -> postfix_expression ;;
0120 
0121    #expression=postfix_expression @ (op=STAR | op=SLASH)
0122 -> multiplicative_expression ;;
0123 
0124    #expression=multiplicative_expression @ (op=PLUS | op=MINUS)
0125 -> additive_expression ;;
0126 
0127    #expression=additive_expression @ (op=EQUAL | op=LESS_EQUAL | op=LESS)
0128 -> relational_expression ;;
0129 
0130    IF condition=expression THEN true_expression=expression ELSE false_expression=expression FI
0131 -> if_expression ;;
0132 
0133    WHILE condition=expression LOOP loop_expression=expression POOL
0134 -> while_expression ;;
0135 
0136    LBRACE (#expression=expression SEMICOLON)* RBRACE
0137 -> block_expression ;;
0138 
0139    LET #declaration=let_declaration @ COMMA IN body_expression=expression
0140 -> let_expression ;;
0141 
0142    CASE expression=expression OF (#condition=case_condition SEMICOLON)* ESAC
0143 -> case_expression ;;
0144 
0145    .=relational_expression
0146 -> expression ;;
0147 
0148    name=IDENTIFIER COLON type=TYPE (ARROW_LEFT expression=expression | 0)
0149 -> let_declaration ;;
0150 
0151    name=IDENTIFIER COLON type=TYPE ARROW_RIGHT expression=expression
0152 -> case_condition ;;
0153 
0154 
0155 
0156 
0157 -----------------------------------------------------------------
0158 -- Code segments copied to the implementation (.cpp) file.
0159 -- If existent, kdevelop-pg's current syntax requires this block
0160 -- to occur at the end of the file.
0161 -----------------------------------------------------------------
0162 
0163 [:
0164 #include "cool_lexer.h"
0165 
0166 
0167 namespace cool
0168 {
0169 
0170 void Parser::tokenize( char *contents )
0171 {
0172     m_contents = contents;
0173     Lexer lexer( this, contents );
0174 
0175     int kind = Parser::Token_EOF;
0176     do
0177     {
0178         kind = lexer.yylex();
0179         //std::cerr << lexer.YYText() << std::endl; //" "; // debug output
0180 
0181         if ( !kind ) // when the lexer returns 0, the end of file is reached
0182             kind = Parser::Token_EOF;
0183 
0184         Parser::Token &t = this->tokenStream->push();
0185         t.kind = kind;
0186         t.begin = lexer.tokenBegin();
0187         t.end = lexer.tokenEnd();
0188 //        t.text = contents;
0189     }
0190     while ( kind != Parser::Token_EOF );
0191 
0192     this->yylex(); // produce the look ahead token
0193 }
0194 
0195 QString Parser::tokenText(qint64 begin, qint64 end)
0196 {
0197     return m_contents.mid(begin,end-begin+1);
0198 }
0199 
0200 
0201 } // end of namespace cool
0202 
0203 :]