File: STGrammar.y

package info (click to toggle)
steptalk 0.10.0%2Bgit20200629-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,732 kB
  • sloc: objc: 12,182; yacc: 400; makefile: 40; sh: 34; csh: 4; awk: 3; lisp: 3
file content (477 lines) | stat: -rw-r--r-- 16,328 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/**
    STGrammar.y
    StepTalk grammar
   
    Copyright (c) 2000 Stefan Urbanek
   
    This file is part of the StepTalk project.
  
    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Lesser General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    This library 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
    Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public
    License along with this library; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  
 */

%{

    #define YYSTYPE id
    #define YYLTYPE int
    #undef YYDEBUG
    
    #import <Foundation/NSException.h>
    #import "STCompiler.h"
    #import "STCompilerUtils.h"
    #import "STSourceReader.h"
    #import "Externs.h"
    
    #import <StepTalk/STExterns.h>


/* extern int STCerror(const char *str);
   extern int STClex (YYSTYPE *lvalp, void *context);
*/
    #define YYPARSE_PARAM    context
    #define YYLEX_PARAM      context
    #define YYERROR_VERBOSE

    #define CONTEXT ((STParserContext *)context)
    #define COMPILER (CONTEXT->compiler)
    #define READER   (CONTEXT->reader)
    #define RESULT   (CONTEXT->result)

    int STClex (YYSTYPE *lvalp, void *context);
    int STCerror(const char *str);
%}

%pure_parser

/* BISON declarations */

%token TK_SEPARATOR TK_BAR TK_ASSIGNMENT
%token TK_LPAREN TK_RPAREN TK_BLOCK_OPEN TK_BLOCK_CLOSE TK_ARRAY_OPEN
%token TK_DOT TK_COLON TK_SEMICOLON TK_RETURN
%token TK_IDENTIFIER TK_BINARY_SELECTOR TK_KEYWORD
%token TK_INTNUMBER TK_REALNUMBER TK_SYMBOL TK_STRING TK_CHARACTER

/* Grammar */

%%
source: /* empty string */  { 
                                [COMPILER compileMethod:nil]; 
                            }


    |   plain_code       {
                                [COMPILER compileMethod:$1];
                            }
        
    /* FIXME: this is a hack */
    |   TK_SEPARATOR TK_SEPARATOR method              
                            {
                                [COMPILER compileMethod:$3];
                            }
                            
    | 
        TK_BLOCK_OPEN TK_BAR
                            {
                                [COMPILER beginScript];
                            }
            methods 
        TK_BLOCK_CLOSE      
;

plain_code: statements
                            {
                                $$ =  [STCMethod methodWithPattern:nil
                                /**/                    statements:$1];
                            }
    | temporaries statements
                            {
                                [$2 setTemporaries:$1];
                                $$ =  [STCMethod methodWithPattern:nil
                                /**/                    statements:$2];
                            }
;

methods:
      block_var_list
                            {
                                [COMPILER setReceiverVariables:$1];
                            }
      method_list
                            
    | method_list
;

method_list: method
                            {
                                [COMPILER compileMethod:$1];
                            }
    | method_list TK_SEPARATOR method
                            {
                                [COMPILER compileMethod:$3];
                            }
;

method: message_pattern statements
                            {
                                $$ =  [STCMethod methodWithPattern:$1
                                /**/                    statements:$2];
                            }
    | message_pattern temporaries statements
                            {
                                [$3 setTemporaries:$2];
                                $$ =  [STCMethod methodWithPattern:$1
                                /**/                    statements:$3];
                            }
;

message_pattern:
    unary_selector
                            {
                                $$ = [STCMessage message];
                                [$$ addKeyword:$1 object:nil];
                            }
    | binary_selector variable_name
                            {
                                $$ = [STCMessage message];
                                [$$ addKeyword:$1 object:$2];
                            }
    | keyword_list
;

keyword_list: keyword variable_name
                            {
                                $$ = [STCMessage message];
                                [$$ addKeyword:$1 object:$2];
                            }
    | keyword_list keyword variable_name
                            {
                                [$1 addKeyword:$2 object:$3];
                                $$ = $1;
                            }
;

temporaries: TK_BAR TK_BAR
                            {
                                $$ = [NSMutableArray array];
                            }
    | TK_BAR variable_list TK_BAR
                            {
                                $$ = $2;
                            }
;

variable_list: variable_name
                            { 
                                $$ = [NSMutableArray array];
                                [$$ addObject:$1]; 
                            }
    | variable_list variable_name
                            { 
                                $$ = $1; 
                                [$$ addObject:$2]; 
                            }
;

block: TK_BLOCK_OPEN TK_BLOCK_CLOSE
                            {
                                $$ = [STCStatements statements];
                            }
    | TK_BLOCK_OPEN statements TK_BLOCK_CLOSE
                            {
                                $$ = $2;
                            }
    | TK_BLOCK_OPEN block_var_list TK_BAR statements TK_BLOCK_CLOSE
                            {
                                $$ = $4;
                                [$$ setTemporaries:$2];
                            }
;
block_var_list: TK_COLON variable_name
                            { 
                                $$ = [NSMutableArray array];
                                [$$ addObject:$2]; 
                            }
    | block_var_list TK_COLON variable_name
                            { 
                                $$ = $1; 
                                [$$ addObject:$3]; 
                            }
;
statements: 
    TK_RETURN expression
                            { 
                                $$ = [STCStatements statements];
                                [$$ setReturnExpression:$2];
                            }
    | expressions
                            { 
                                $$ = [STCStatements statements];
                                [$$ setExpressions:$1];
                            }

    | expressions TK_DOT
                            { 
                                $$ = [STCStatements statements];
                                [$$ setExpressions:$1];
                            }
    | expressions TK_DOT TK_RETURN expression
                            { 
                                $$ = [STCStatements statements];
                                [$$ setReturnExpression:$4];
                                [$$ setExpressions:$1];
                            }
;    
    
expressions:
    expression              { 
                                $$ = [NSMutableArray array];
                                [$$ addObject:$1];
                            }
                            
    | expressions TK_DOT expression
                            {   
                                $$ = $1; 
                                [$$ addObject:$3]; 
                            }
;
expression: primary         
                            { 
                                $$ = [STCExpression 
                                /**/          primaryExpressionWithObject:$1];
                            }
    | assignments primary
                            { 
                                $$ = [STCExpression 
                                /**/          primaryExpressionWithObject:$2];
                                [$$ setAssignments:$1];
                            }
    | message_expression
    | assignments message_expression
                            { 
                                $$ = $2;
                                [$$ setAssignments:$1];
                            }
    | cascade
    | assignments cascade
                            { 
                                $$ = $2;
                                [$$ setAssignments:$1];
                            }
;
assignments: assignment     
                            { 
                                $$ = [NSMutableArray array];
                                [$$ addObject:$1];
                            }
    | assignments assignment
                            { 
                                $$ = $1; 
                                [$$ addObject:$2]; 
                            } 
;

assignment: variable_name TK_ASSIGNMENT
                            { $$ = $1;}
;
cascade: message_expression cascade_list
                            { 
                                /* FIXME: check if this is this OK */
                                [$$ setCascade:$2]; 
                            }
;
cascade_list: TK_SEMICOLON cascade_item
                            {
                                $$ = [NSMutableArray array];
                                [$$ addObject:$2]; 
                            }
    | cascade_list TK_SEMICOLON cascade_item
                            { 
                                $$ = $1; 
                                [$$ addObject:$3];
                            }
;
cascade_item: unary_selector
                            { 
                                $$ = [STCMessage message];
                                [$$ addKeyword:$1 object:nil];
                            }
    | binary_selector unary_object
                            { 
                                $$ = [STCMessage message];
                                [$$ addKeyword:$1 object:$2];
                            }
    | keyword_expr_list
;

message_expression: unary_expression
    | binary_expression
    | keyword_expression
;
unary_expression: unary_object unary_selector
                            { 
                                STCMessage *message = [STCMessage message];
                                [message addKeyword:$2 object:nil];
                                $$ = [STCExpression 
                                /**/        messageExpressionWithTarget:$1
                                /**/        message:message];
                            }
;
binary_expression: binary_object binary_selector unary_object
                            { 
                                STCMessage *message = [STCMessage message];
                                [message addKeyword:$2 object:$3];
                                $$ = [STCExpression 
                                /**/        messageExpressionWithTarget:$1
                                /**/        message:message];
                            }
;
keyword_expression: binary_object keyword_expr_list
                            {
                                $$ = [STCExpression 
                                /**/        messageExpressionWithTarget:$1
                                /**/        message:$2];
                            }
;
keyword_expr_list: keyword binary_object 
                            { 
                                $$ = [STCMessage message];
                                [$$ addKeyword:$1 object:$2];
                            }
    | keyword_expr_list keyword binary_object 
                            { 
                                $$ = $1;
                                [$$ addKeyword:$2 object:$3];
                            }
;
unary_object: primary
    | unary_expression
;
binary_object: unary_object
    | binary_expression
;
primary: variable_name      
                            {
                                $$ = [STCPrimary primaryWithVariable:$1];
                            }
    | literal
                            {
                                $$ = [STCPrimary primaryWithLiteral:$1];
                            }
    | block
                            {
                                $$ = [STCPrimary primaryWithBlock:$1];
                            }
    | TK_LPAREN expression TK_RPAREN 
                            {
                                $$ = [STCPrimary primaryWithExpression:$2];
                            }
;
variable_name: TK_IDENTIFIER /* STCheckVariable ... */
;
unary_selector: TK_IDENTIFIER
;
binary_selector: TK_BINARY_SELECTOR
;
keyword: TK_KEYWORD
;
literal: 
    TK_INTNUMBER               
                        { $$ = [COMPILER createIntNumberLiteralFrom:$1]; }
    | TK_REALNUMBER               
                        { $$ = [COMPILER createRealNumberLiteralFrom:$1]; }
    | TK_SYMBOL
                        { $$ = [COMPILER createSymbolLiteralFrom:$1]; }
    | TK_STRING
                        { $$ = [COMPILER createStringLiteralFrom:$1]; }
    | TK_CHARACTER
                        { $$ = [COMPILER createCharacterLiteralFrom:$1]; }
    | TK_ARRAY_OPEN array TK_RPAREN
                        { $$ = [COMPILER createArrayLiteralFrom:$2]; }
;
array: /* nothing */    { $$ = [NSMutableArray array]; }
    | literal                   { $$ = [NSMutableArray array]; 
                                   [$$ addObject:$1]; }
    | symbol                     { $$ = [NSMutableArray array];
                                   [$$ addObject:$1]; }
    | array literal              { $$ = $1; [$$ addObject:$2]; }
    | array symbol               { $$ = $1; [$$ addObject:$2]; }
;
symbol: TK_IDENTIFIER                        
                        { $$ = [COMPILER createSymbolLiteralFrom:$1]; }
    | binary_selector
                        { $$ = [COMPILER createSymbolLiteralFrom:$1]; }
    | TK_KEYWORD
                        { $$ = [COMPILER createSymbolLiteralFrom:$1]; }
;
%%

int STCerror(const char *str)
{
    [NSException raise:STCompilerSyntaxException
                 format:@"Unknown parse error (%s)", str];
    return 0;
}



/* 
 * Lexer
 * --------------------------------------------------------------------------
 */

int STClex (YYSTYPE *lvalp, void *context)
{
    STTokenType tokenType = [READER nextToken];

    if(tokenType == STEndTokenType)
    {
        return 0;
    }

    *lvalp = [READER tokenString];

    switch(tokenType)
    {
    case STBarTokenType:            return TK_BAR;
    case STReturnTokenType:         return TK_RETURN;
    case STColonTokenType:          return TK_COLON;
    case STSemicolonTokenType:      return TK_SEMICOLON;
    case STDotTokenType:            return TK_DOT;
    case STLParenTokenType:         return TK_LPAREN;
    case STRParenTokenType:         return TK_RPAREN;
    case STBlockOpenTokenType:      return TK_BLOCK_OPEN;
    case STBlockCloseTokenType:     return TK_BLOCK_CLOSE;
    case STArrayOpenTokenType:      return TK_ARRAY_OPEN;
    case STAssignTokenType:         return TK_ASSIGNMENT;
    case STIdentifierTokenType:     return TK_IDENTIFIER;
    case STKeywordTokenType:        return TK_KEYWORD;
    case STBinarySelectorTokenType: return TK_BINARY_SELECTOR;
    case STSymbolTokenType:         return TK_SYMBOL;
    case STStringTokenType:         return TK_STRING;
    case STCharacterTokenType:      return TK_CHARACTER;
    case STIntNumberTokenType:         return TK_INTNUMBER;
    case STRealNumberTokenType:         return TK_REALNUMBER;
    case STSeparatorTokenType:      return TK_SEPARATOR;

    case STEndTokenType:            return 0;

    case STSharpTokenType:
    case STInvalidTokenType:
    case STErrorTokenType:
                                    return 1;
    }

    return 1;   
}