File: cool.g

package info (click to toggle)
kdevelop-pg-qt 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,284 kB
  • sloc: cpp: 22,743; lex: 928; ansic: 716; yacc: 615; ruby: 68; sh: 14; lisp: 10; fortran: 6; makefile: 2
file content (203 lines) | stat: -rw-r--r-- 5,748 bytes parent folder | download
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
[:
#include <QString>

:]
------------------------------------------------------------
-- Parser class members
------------------------------------------------------------

%parserclass (private declaration)
[:
    QString m_contents;
:]

%parserclass (public declaration)
[:
  /**
   * Transform the raw input into tokens.
   * When this method returns, the parser's token stream has been filled
   * and any parse_*() method can be called.
   */
  void tokenize( char *contents );

  enum problem_type {
      error,
      warning,
      info
  };
//  void report_problem( Parser::problem_type type, const char* message );
  void report_problem( Parser::problem_type type, const QString & message );
  
  QString tokenText(qint64 begin, qint64 end);
:]

%parser_declaration_header "coolast.h"


------------------------------------------------------------
-- List of defined tokens
------------------------------------------------------------

-- keywords:
%token CLASS ("class"), INHERITS ("inherits"), NEW ("new"),
       IF ("if"), THEN ("then"), ELSE ("else"), FI ("fi"), WHILE ("while"),
       LOOP ("loop"), POOL ("pool"), LET ("let"), IN ("in"),
       CASE ("case"), OF ("of"), ESAC ("esac") ;;

-- seperators:
%token LPAREN ("("), RPAREN (")"), LBRACE ("{"), RBRACE ("}"), SEMICOLON (";"),
       COMMA (","), DOT ("."), AT ("@") ;;

-- operators:
%token PLUS ("+"), MINUS ("-"), STAR ("*"), SLASH ("/"), EQUAL ("="),
       LESS_EQUAL ("<="), LESS ("<"), COLON (":"), ARROW_LEFT ("<-"),
       ARROW_RIGHT ("=>"), TILDE ("~"), NOT ("not"), ISVOID ("isvoid") ;;

-- literals and identifiers:
%token IDENTIFIER ("identifier"), TYPE ("type specification"),
       INTEGER ("integer literal"), STRING ("string literal"),
       TRUE ("true"), FALSE ("false") ;;

-- token that makes the parser fail in any case:
%token INVALID ("invalid token") ;;



------------------------------------------------------------
-- Start of the actual grammar
------------------------------------------------------------

   (#klass=class SEMICOLON)*
-> program ;;

   CLASS type=TYPE (INHERITS base_type=TYPE | 0) LBRACE (#feature=feature SEMICOLON)* RBRACE
-> class ;;

   name=IDENTIFIER COLON type=TYPE
-> formal ;;

 ( ?[: LA(2).kind == Token_LPAREN :]
   name=IDENTIFIER LPAREN (#formal=formal @ COMMA | 0) RPAREN
   COLON type=TYPE LBRACE expression=expression RBRACE
 |
   name=IDENTIFIER COLON type=TYPE (ARROW_LEFT expression=expression | 0)
 )
-> feature ;;

 (
   ?[: LA(2).kind == Token_ARROW_LEFT :]
   name=IDENTIFIER ARROW_LEFT expression=expression                  -- assignment
 |
   ?[: LA(2).kind == Token_LPAREN :]
   name=IDENTIFIER LPAREN (#argument=expression @ COMMA | 0) RPAREN  -- dispatch
 |
   variable=IDENTIFIER
 | integer_literal=INTEGER
 | string_literal=STRING
 | true_literal=TRUE
 | false_literal=FALSE
 | NEW new_type=TYPE
 | LPAREN expression=expression RPAREN
 | if_expression=if_expression
 | while_expression=while_expression
 | block_expression=block_expression
 | let_expression=let_expression
 | case_expression=case_expression
 )
-> primary_expression ;;

   op=TILDE  expression=primary_expression -- ^tilde_expression
 | op=NOT    expression=primary_expression -- ^not_expression
 | op=ISVOID expression=primary_expression -- ^isvoid_expression
 |           expression=primary_expression
-> unary_expression ;;

   base_expression=unary_expression
   (AT at_type=TYPE DOT name=IDENTIFIER LPAREN (#arguments=expression @ COMMA | 0) RPAREN
   |                DOT name=IDENTIFIER LPAREN (#arguments=expression @ COMMA | 0) RPAREN
   )*
-> postfix_expression ;;

   #expression=postfix_expression @ (op=STAR | op=SLASH)
-> multiplicative_expression ;;

   #expression=multiplicative_expression @ (op=PLUS | op=MINUS)
-> additive_expression ;;

   #expression=additive_expression @ (op=EQUAL | op=LESS_EQUAL | op=LESS)
-> relational_expression ;;

   IF condition=expression THEN true_expression=expression ELSE false_expression=expression FI
-> if_expression ;;

   WHILE condition=expression LOOP loop_expression=expression POOL
-> while_expression ;;

   LBRACE (#expression=expression SEMICOLON)* RBRACE
-> block_expression ;;

   LET #declaration=let_declaration @ COMMA IN body_expression=expression
-> let_expression ;;

   CASE expression=expression OF (#condition=case_condition SEMICOLON)* ESAC
-> case_expression ;;

   .=relational_expression
-> expression ;;

   name=IDENTIFIER COLON type=TYPE (ARROW_LEFT expression=expression | 0)
-> let_declaration ;;

   name=IDENTIFIER COLON type=TYPE ARROW_RIGHT expression=expression
-> case_condition ;;




-----------------------------------------------------------------
-- Code segments copied to the implementation (.cpp) file.
-- If existent, kdevelop-pg's current syntax requires this block
-- to occur at the end of the file.
-----------------------------------------------------------------

[:
#include "cool_lexer.h"


namespace cool
{

void Parser::tokenize( char *contents )
{
    m_contents = QString::fromUtf8(contents);
    Lexer lexer( this, contents );

    int kind = Parser::Token_EOF;
    do
    {
        kind = lexer.yylex();
        //std::cerr << lexer.YYText() << std::endl; //" "; // debug output

        if ( !kind ) // when the lexer returns 0, the end of file is reached
            kind = Parser::Token_EOF;

        Parser::Token &t = this->tokenStream->push();
        t.kind = kind;
        t.begin = lexer.tokenBegin();
        t.end = lexer.tokenEnd();
//        t.text = contents;
    }
    while ( kind != Parser::Token_EOF );

    this->yylex(); // produce the look ahead token
}

QString Parser::tokenText(qint64 begin, qint64 end)
{
    return m_contents.mid(begin,end-begin+1);
}


} // end of namespace cool

:]