-
Notifications
You must be signed in to change notification settings - Fork 9
/
nagaqueen.leg
1788 lines (1476 loc) · 68.6 KB
/
nagaqueen.leg
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
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%{
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
// workaround for peg/leg/greg's shady parsing of "{}" even in
// character class literals
#define _OBRACK '{'
#define _CBRACK '}'
#define _OSBRACK "{"
#define _CSBRACK "}"
#ifdef __OOC_USE_GC__
void *GC_malloc(size_t);
void *GC_calloc(size_t, size_t);
void *GC_realloc(void *, size_t);
void GC_free(void *);
#define YY_ALLOC(N, D) GC_malloc(N)
#define YY_CALLOC(N, S, D) GC_malloc((N) * (S))
#define YY_REALLOC(B, N, D) GC_realloc(B, N)
#define YY_FREE GC_free
#else
#define YY_ALLOC(N, D) malloc(N)
#define YY_CALLOC(N, S, D) calloc(N, S)
#define YY_REALLOC(B, N, D) realloc(B, N)
#define YY_FREE free
#endif
#define tokenPos { core->token[0] = thunk->begin + G->offset; core->token[1] = (thunk->end - thunk->begin); }
#define tokenPosPlusOne { core->token[0] = thunk->begin + G->offset + 1; core->token[1] = (thunk->end - thunk->begin); }
#define rewindWhiteSpace { \
/* only rewind if at end of file */ \
if (core->eof == 1) { \
int originalPos = G->pos; \
char *c = G->buf + G->pos; \
/* rewind until we reach something non-whitespace */ \
while (G->pos > 0) { \
c--; G->pos--; \
char cc = *c; \
if (!((cc) == ' ' || (cc) == '\t' || (cc) == '\n' || (cc) == '\r')) break; \
} \
} \
}
// Throw error at current parsing pos. Used when nothing valid matches.
#define throwError(val, message) \
nq_error(core->this, (val), (message), G->pos + G->offset)
// Throw error at last matched token pos. Used with invalid tokens being
// parsed for more helpful messages (e.g. misplaced suffixes).
#define throwTokenError(val, message) \
nq_error(core->this, (val), (message), core->token[0])
#define missingOp(c) { \
rewindWhiteSpace; \
char message[2048]; \
snprintf(message, 2048, "Missing right operand for '%s' operator!\n", (c)); \
throwError(NQE_MISSING_OPERAND, message); \
}
#define YYSTYPE void*
// the default is 1024, but it causes buffers to be reallocated 4 or 5
// times during the parsing. This is a better default for us, only a few
// modules need to reallocate with that setting
#define YY_BUFFER_START_SIZE 16384
// in old peg/leg versions, this was set to 32, but it's wayyy too small
// for a non-trivial grammar like ooc's
#define YY_STACK_SIZE YY_BUFFER_START_SIZE
//#define YY_DEBUG
///////////////////// re-entrant data structures ////////////////////////
typedef int (*_NagaQueenIoInterface_read)(void *, size_t, void *);
struct _NagaQueenIoInterface {
_NagaQueenIoInterface_read read;
};
struct _NagaQueenCore {
/* The user's data */
void *this;
/* Current line number */
int yylineno;
/* Path of the file we're parsing. */
char* path;
/* The stream we're reading from. */
void *stream;
/* Length of the stream (only used for memory streams) */
size_t streamlen;
/* Offset in the stream (only used for memory streams) */
size_t streamoffset;
/* our IO interface */
struct _NagaQueenIoInterface io;
/* The begin position and length of the current token in the text */
int token[2];
/* type parsing buffer */
char typeBuffer[4096];
/* import quantity */
int importQuantity;
/* 0 if still reading file, 1 if eof */
int eof;
};
typedef struct _NagaQueenCore NagaQueenCore;
///////////////////// IO interface: supports memory + files ////////////////////////
static int _nq_memread(void *ptr, size_t size, NagaQueenCore *core) {
char *source = (char *) core->stream;
size_t tocopy = size;
size_t remaining = core->streamlen - core->streamoffset;
if (tocopy > remaining) {
tocopy = remaining;
}
memcpy(ptr, source + core->streamoffset, tocopy);
core->streamoffset += tocopy;
return (int) tocopy;
}
static int _nq_fread(void *ptr, size_t size, NagaQueenCore *core) {
FILE *stream = (FILE *) core->stream;
return fread(ptr, 1, size, (FILE*) stream);
}
#define YY_XTYPE NagaQueenCore *
#define YY_XVAR core
#define YY_INPUT(buf, result, max_size, core) yyInput(buf, &result, max_size, core)
void yyInput(char *buf, int *result, int max_size, NagaQueenCore *core) {
(*result) = core->io.read(buf, max_size, core);
if((*result) == 0 && core->eof == 0) {
core->eof = 1;
(*buf) = '\n';
(*result) = 1;
return;
}
for(int i = 0; i < (*result) - 1; i++) {
// if we encounter a line separation ('\' followed by EOL),
// replace both the '\' and the EOL (CRLF or LF) with spaces -
// it won't hurt the parsing and is faster than memmov-ing the
// rest of the buffer in place.
if(buf[i] == '\\') {
if(buf[i+1] == '\r') {
buf[i] = ' ';
buf[i+1] = ' ';
if(buf[i+2] == '\n') {
// CRLF (Win32)
buf[i+2] = ' ';
i += 2; continue;
}
// CR (AIX?)
i += 1; continue;
} else if(buf[i+1] == '\n') {
// LF (Linux, Mac)
buf[i] = ' ';
buf[i+1] = ' ';
i += 1; continue;
}
}
}
}
///////////////////// callbacks def start, you may want to skip this ////////////////////////
void nq_setTokenPositionPointer(void *this, int *tokenPosPointer, int *lineNoPointer);
char *nq_StringClone(char *string);
char *nq_trailingQuest(char *string);
char *nq_trailingBang (char *string);
void nq_onUse(void *this, char *name);
void nq_onInclude(void *this, char *path);
void nq_onIncludeDefine(void *this, char *name, char *value);
void nq_onImport (void *this, char *path, char *name);
void nq_onImportNamespace(void *this, char *namespace, int quantity);
void *nq_onVersionName(void *this, char *name);
void *nq_onVersionNegation(void *this, void *spec);
void *nq_onVersionAnd(void *this, void *specLeft, void *specRight);
void *nq_onVersionOr(void *this, void *specLeft, void *specRight);
void nq_onVersionStart(void *this, void *spec);
void *nq_onVersionElseIfStart(void *this, void *notSpec, void *elseSpec);
void nq_onVersionElseStart(void *this, void *notSpec);
void *nq_onVersionEnd(void *this);
void nq_onExtendStart(void *this, void *type, char *doc);
void nq_onExtendEnd(void *this);
void nq_onCoverStart(void *this, char *name, char *doc);
void nq_onCoverExtern(void *this, char *externName);
void nq_onCoverProto(void *this);
void nq_onCoverFromType(void *this, void *type);
void nq_onCoverExtends(void *this, void *type);
void nq_onCoverImplements(void *this, void *type);
void nq_onCoverEnd(void *this);
void nq_onEnumStart(void *this, char *name, char *doc);
void nq_onEnumFromType(void *this, void *fromType);
void nq_onEnumIncrementExpr(void *this, char oper, void *step);
void nq_onEnumElementStart(void *this, char *name, char *doc);
void nq_onEnumElementValue(void *this, void *value);
void nq_onEnumElementExtern(void *this, char *externName);
void nq_onEnumElementEnd(void *this);
void nq_onEnumEnd(void *this);
void nq_onClassStart(void *this, char *name, char *doc);
void nq_onClassAbstract(void *this);
void nq_onClassFinal(void *this);
void nq_onClassExtends(void *this, void *type);
void nq_onClassImplements(void *this, void *type);
void nq_onClassBody(void *this);
void nq_onClassEnd(void *this);
void nq_onInterfaceStart(void *this, char *name, char *doc);
void nq_onInterfaceExtends(void *this, void *type);
void nq_onInterfaceEnd(void *this);
void nq_onPropertyDeclStart(void *this, char *name, char *doc);
void nq_onPropertyDeclStatic(void *this);
void nq_onPropertyDeclType(void *this, void *type);
void *nq_onPropertyDeclEnd(void *this);
void nq_onPropertyDeclGetterStart(void *this, char *doc);
void *nq_onPropertyDeclGetterEnd(void *this);
void nq_onPropertyDeclSetterStart(void *this, char *doc);
void nq_onPropertyDeclSetterArgument(void *this, char *name, _Bool conventional);
void *nq_onPropertyDeclSetterEnd(void *this);
void nq_onVarDeclStart(void *this);
void nq_onVarDeclName(void *this, char *name, char *doc);
void nq_onVarDeclTuple(void *this, void *tuple);
void nq_onVarDeclExtern(void *this, char *externName);
void nq_onVarDeclUnmangled(void *this, char *unmangledName);
void nq_onVarDeclExpr(void *this, void *expr);
void nq_onVarDeclType(void *this, void *type);
void nq_onVarDeclStatic(void *this);
void nq_onVarDeclConst(void *this);
void nq_onVarDeclProto(void *this);
void *nq_onVarDeclEnd(void *this);
void *nq_onTypeAccess(void *this, void *type);
void *nq_onTypeNew(void *this, char *name); // $$=nq_onTypeNew(yytext)
void *nq_onTypePointer(void *this, void *type); // $$=nq_onTypePointer($$)
void *nq_onTypeReference(void *this, void *type); // $$=nq_onTypeReference($$)
void *nq_onTypeBrackets(void *this, void *type, void *inner); // $$=nq_onTypeBrackets($$, inner)
void nq_onTypeGenericArgument(void *this, void *type, void *genType);
void nq_onFuncTypeGenericArgument(void *this, void *type, char *name);
void *nq_onTypeList(void *this);
void *nq_onTypeListElement(void *this, void *list, void *elem);
void nq_onTypeNamespace(void *this, void *type, void *ident);
void *nq_onFuncTypeNew(void *this);
void nq_onFuncTypeArgument(void *this, void *funcType, void *argType);
void nq_onFuncTypeVarArg(void *this, void *funcType);
void nq_onFuncTypeReturnType(void *this, void *funcType, void *returnType);
void nq_onOperatorStart(void *this);
void nq_onOperatorAbstract(void *this);
void nq_onOperatorByref(void *this);
void nq_onOperatorSymbol(void *this, char *symbol);
void nq_onOperatorBodyStart(void *this);
void nq_onOperatorEnd(void *this);
void nq_onFunctionStart(void *this, char *name, char *doc);
void nq_onFunctionACS(void *this);
void nq_onFunctionExtern(void *this, char *externName);
void nq_onFunctionUnmangled(void *this, char *unmangledName);
void nq_onFunctionAbstract(void *this);
void nq_onFunctionThisRef(void *this);
void nq_onFunctionArgsStart(void *this);
void nq_onFunctionArgsEnd(void *this);
void nq_onFunctionReturnType(void *this, void *type);
void nq_onFunctionConst(void *this);
void nq_onFunctionStatic(void *this);
void nq_onFunctionInline(void *this);
void nq_onFunctionFinal(void *this);
void nq_onFunctionProto(void *this);
void nq_onFunctionSuper(void *this);
void nq_onFunctionSuffix(void *this, char *name);
void nq_onFunctionBody(void *this);
void *nq_onFunctionEnd(void *this);
void nq_onTypeArg(void *this, void *type);
void nq_onVarArg(void *this, char *name);
void nq_onDotArg(void *this, char *name);
void nq_onAssArg(void *this, char *name);
void nq_onFunctionCallStart(void *this, char *yytext);
void nq_onFunctionCallSuffix(void *this, char *yytext);
void nq_onFunctionCallArg(void *this, void *expr);
void *nq_onFunctionCallEnd(void *this);
void nq_onFunctionCallExpr(void *this, void *call, void *expr);
void *nq_onFunctionCallChain(void *this, void *expr, void *call);
void *nq_onFunctionCallCombo(void *this, void *call, void *expr);
void nq_onArrayLiteralStart(void *this);
void *nq_onArrayLiteralEnd(void *this);
void nq_onTupleStart(void *this);
void *nq_onTupleEnd(void *this);
void nq_onRawStringLiteral(void *this, void *object);
void nq_onStringLiteralStart(void *this);
void nq_onStringInterpolation(void *this, void *expression);
void nq_onStringTextChunk(void *this, char *chunck);
void *nq_onStringLiteralEnd(void *this);
void *nq_onCharLiteral(void *this, char *value);
void nq_onStatement(void *this, void *statement);
void *nq_onReturn(void *this, void *expr);
void *nq_onVarAccess(void *this, void *expr, char *name);
void nq_onArrayAccessStart(void *this, void *array);
void *nq_onArrayAccessEnd(void *this);
void *nq_onCast(void *this, void *expr, void *type);
void *nq_onBreak(void *this);
void *nq_onContinue(void *this);
void nq_onBlockStart(void *this);
void *nq_onBlockEnd(void *this);
void nq_onIfStart(void *this, void *condition);
void *nq_onIfEnd(void *this);
void nq_onElseMatched(void *this, void *_if, void *_else);
void nq_onElseStart(void *this);
void *nq_onElseEnd(void *this);
void nq_onForeachStart(void *this, void *decl, void *collec);
void *nq_onForeachEnd(void *this);
void nq_onWhileStart(void *this, void *condition);
void *nq_onWhileEnd(void *this);
void *nq_onEquals(void *this, void *left, void *right);
void *nq_onNotEquals(void *this, void *left, void *right);
void *nq_onLessThan(void *this, void *left, void *right);
void *nq_onMoreThan(void *this, void *left, void *right);
void *nq_onCmp(void *this, void *left, void *right);
void *nq_onLessThanOrEqual(void *this, void *left, void *right);
void *nq_onMoreThanOrEqual(void *this, void *left, void *right);
void *nq_onDecLiteral(void *this, char *value);
void *nq_onBinLiteral(void *this, char *value);
void *nq_onOctLiteral(void *this, char *value);
void *nq_onHexLiteral(void *this, char *value);
void *nq_onFloatLiteral(void *this, char *value);
void *nq_onBoolLiteral(void *this, bool value);
void *nq_onNull(void *this);
void *nq_onDoubleArrow(void *this, void *left, void *right);
void *nq_onTernary(void *this, void *condition, void *ifTrue, void *ifFalse);
void *nq_onAssignAnd(void *this, void *left, void *right);
void *nq_onAssignOr(void *this, void *left, void *right);
void *nq_onAssignXor(void *this, void *left, void *right);
void *nq_onAssignRightShift(void *this, void *left, void *right);
void *nq_onAssignLeftShift(void *this, void *left, void *right);
void *nq_onAssignDiv(void *this, void *left, void *right);
void *nq_onAssignMul(void *this, void *left, void *right);
void *nq_onAssignExp(void *this, void *left, void *right);
void *nq_onAssignSub(void *this, void *left, void *right);
void *nq_onAssignAdd(void *this, void *left, void *right);
void *nq_onAssignMod(void *this, void *left, void *right);
void *nq_onAssign(void *this, void *left, void *right);
void *nq_onAdd(void *this, void *left, void *right);
void *nq_onSub(void *this, void *left, void *right);
void *nq_onMod(void *this, void *left, void *right);
void *nq_onMul(void *this, void *left, void *right);
void *nq_onExp(void *this, void *left, void *right);
void *nq_onDiv(void *this, void *left, void *right);
void *nq_onRangeLiteral(void *this, void *left, void *right);
void *nq_onBinaryLeftShift(void *this, void *left, void *right);
void *nq_onBinaryRightShift(void *this, void *left, void *right);
void *nq_onNullCoalescing(void *this, void *left, void *right);
void *nq_onLogicalOr(void *this, void *left, void *right);
void *nq_onLogicalAnd(void *this, void *left, void *right);
void *nq_onBinaryOr(void *this, void *left, void *right);
void *nq_onBinaryXor(void *this, void *left, void *right);
void *nq_onBinaryAnd(void *this, void *left, void *right);
void nq_onSafeNavigationStart(void *this, void *expr);
void nq_onSafeNavigationSection(void *this);
void nq_onSafeNavigationIdent(void *this, char *ident);
void *nq_onSafeNavigationEnd(void *this);
void *nq_onLogicalNot(void *this, void *inner);
void *nq_onBinaryNot(void *this, void *inner);
void *nq_onUnaryMinus(void *this, void *inner);
void *nq_onUnaryPlus(void *this, void *inner);
void *nq_onParenthesis(void *this, void *inner);
void nq_onGenericArgument(void *this, char *name);
void *nq_onAddressOf (void *this, void *inner);
void *nq_onDereference(void *this, void *inner);
void nq_onMatchStart(void *this);
void nq_onMatchExpr(void *this, void *value);
void *nq_onMatchEnd(void *this);
void nq_onCaseStart(void *this);
void nq_onCaseExpr(void *this, void *value);
void nq_onCaseEnd(void *this);
void nq_onTryStart(void *this);
void *nq_onTryEnd(void *this);
void nq_onCatchStart(void *this);
void nq_onCatchExpr(void *this, void *value);
void nq_onCatchEnd(void *this);
void nq_error(void *this, int errorID, char *defaultMessage, int index);
// Templates
void nq_onTemplateStart(void *this);
void nq_onTemplateEnd(void *this);
///////////////////// callbacks def end ////////////////////////
///////////////////// error IDs start ////////////////////////
// NQE stands for 'NagaQueen Error"
enum NagaQueenError {
NQE_EXP_STATEMENT_OR_CLOSING_BRACKET = 1,
NQE_EXP_INC_IMP_STMT_OR_DECL,
NQE_EXP_CASE_IN_MATCH,
NQE_EXP_VAR_OR_FUNC_DECL,
NQE_EXP_ENUM_ELEMENT,
NQE_EXP_RET_TYPE,
NQE_EXP_CLOSING_PAREN,
NQE_EXP_CLOSING_SQUAR,
NQE_EXP_CLOSING_BRACK,
NQE_EXP_DOUBLE_ARROW,
NQE_EXP_ARG,
NQE_UNCLOSED_COMMENT,
NQE_MISSING_OPERAND,
NQE_MISPLACED_SUFFIX,
NQE_MALFORMED_STRINGLIT,
NQE_MALFORMED_CHARLIT,
NQE_MALFORMED_TERNARY,
};
///////////////////// error IDs end ////////////////////////
%}
Module = ModuleCore
| WS ( !EOL . )*
EOL { tokenPos; char *message = "Expected include, import, statement or declaration\n";
if(G->buf[core->token[0]] == _CBRACK) { message = "Unmatched closing bracket\n"; }
throwTokenError(NQE_EXP_INC_IMP_STMT_OR_DECL, message);
}
ModuleCore = (WS 'version' { tokenPos }
WS '('
- spec: VersionSpec
WS CLOS_PAREN ~{ rewindWhiteSpace; throwError(NQE_EXP_CLOSING_PAREN, "Malformed version spec!\n") }
((
- '{' { nq_onVersionStart(core->this, spec) }
WS ModuleCore* WS
- CLOS_BRACK ~{ rewindWhiteSpace; throwError(NQE_EXP_INC_IMP_STMT_OR_DECL, "Expected include, import, statement or declaration\n") } { nq_onVersionEnd(core->this) }
)
|
(
- s:Stmt { nq_onVersionStart(core->this, spec); nq_onStatement(core->this, s); nq_onVersionEnd(core->this) }
))
(
WS 'else' - 'version' { tokenPos }
WS '('
- elseSpec: VersionSpec
WS ')'
((
- '{' { spec = nq_onVersionElseIfStart(core->this, spec, elseSpec) }
WS ModuleCore* WS
- CLOS_BRACK ~{ rewindWhiteSpace; throwError(NQE_EXP_INC_IMP_STMT_OR_DECL, "Expected include, import, statement or declaration\n") } { nq_onVersionEnd(core->this) }
)
|
(
- s:Stmt { spec = nq_onVersionElseIfStart(core->this, spec, elseSpec); nq_onStatement(core->this, s); nq_onVersionEnd(core->this) }
))
)*
(
WS 'else' { tokenPos }
((
- '{' { nq_onVersionElseStart(core->this, spec) }
WS ModuleCore* WS
- CLOS_BRACK ~{ rewindWhiteSpace; throwError(NQE_EXP_INC_IMP_STMT_OR_DECL, "Expected include, import, statement or declaration\n") } { nq_onVersionEnd(core->this) }
)
|
(
- s:Stmt { nq_onVersionElseStart(core->this, spec); nq_onStatement(core->this, s); nq_onVersionEnd(core->this) }
))
)?
)
|(WS Include WS
| WS Import WS
| WS Use WS
| WS Decl WS
| WS stmt:Stmt WS { nq_onStatement(core->this, stmt) }
| WS { tokenPos } OocDocCore { char *message = "Unexpected oocdoc comment"; throwTokenError(NQE_EXP_INC_IMP_STMT_OR_DECL, message) }
)
VersionSpec = ((- '(' - l:VersionSpec - ')') | (l:VersionCore))
((- '&&' { tokenPos; } - r:VersionSpec { l=$$=nq_onVersionAnd(core->this, l, r); }) |
(- '||' { tokenPos; } - r:VersionSpec { l=$$=nq_onVersionOr (core->this, l, r); }))*
VersionCore = (VersionNegation | VersionName)
VersionName = - < [a-zA-Z0-9_]+ > { tokenPos; $$=nq_onVersionName(core->this, yytext) }
VersionNegation = - '!' { tokenPos; } - spec:VersionSpec { $$=nq_onVersionNegation(core->this, spec) }
Use = USE_KW [ \t]
- UseCore
(
- ','
- UseCore
)*
UseCore = < ([A-Za-z0-9/._] | '-')+ > { tokenPos; nq_onUse(core->this, nq_StringClone(yytext)) }
Include = INCLUDE_KW [ \t]
- IncludeCore
(
- ','
- IncludeCore
)*
DefineName = < ([A-Za-z0-9_/._] | '-')+ > { $$=nq_StringClone(yytext) }
DefineValue = ('=' < ([A-Za-z0-9_/._] | '-')+ >)? { $$=nq_StringClone(yytext) }
| { $$=""; }
IncludeCore = < ([A-Za-z0-9/._] | '-')+ > { tokenPos; nq_onInclude(core->this, nq_StringClone(yytext)) }
(- '|'
- '('
- defineName:DefineName defineVal:DefineValue { nq_onIncludeDefine(core->this, defineName, defineVal) }
(- ','
- defineName:DefineName defineVal:DefineValue { nq_onIncludeDefine(core->this, defineName, defineVal) }
)*
- ')'
)?
Import = IMPORT_KW [ \t]
- ImportAtom
(
',' WS
- ImportAtom
)*
ImportAtom = path:ImportPath
((name:ImportName { tokenPosPlusOne; } { nq_onImport(core->this, (char*) path, (char*) name) }
(- INTO_KW - namespace:IDENT { nq_onImportNamespace(core->this, (char*) namespace, 1) })?
) | (
'[' { core->importQuantity = 0; }
(name:ImportName { tokenPosPlusOne; } - ',' WS { core->importQuantity++; nq_onImport(core->this, (char*) path, (char*) name) })*
(name:ImportName { tokenPosPlusOne; } { core->importQuantity++; nq_onImport(core->this, (char*) path, (char*) name) })
']'
(- INTO_KW - namespace:IDENT { nq_onImportNamespace(core->this, (char*) namespace, core->importQuantity) })?
))
ImportPath = < (([A-Za-z_0-9] | "." | "-")+ "/")* > { $$=nq_StringClone(yytext) }
ImportName = < ([A-Za-z_0-9] | "-")+ > { $$=nq_StringClone(yytext) }
Decl = ( ClassDecl
| CoverDecl
| ExtendDecl
| EnumDecl
| InterfaceDecl
| OperatorDecl
| FunctionDecl
| PropertyDecl
| vd:VariableDecl Terminator+ { nq_onStatement(core->this, vd) }
)
GenericArguments =
(
- LESSTHAN
- name:IDENT { nq_onGenericArgument(core->this, name) }
(
- ','
- name:IDENT { nq_onGenericArgument(core->this, name) }
)*
MORETHAN -
)
TemplateDef = (
WS "template" WS
{ nq_onTemplateStart(core->this) }
GenericArguments
{ nq_onTemplateEnd(core->this) }
)
OperatorDecl =
(
(OPERATOR_KW { tokenPos; nq_onOperatorStart(core->this) })
| (ABSTRACT_KW - OPERATOR_KW { tokenPos; nq_onOperatorStart(core->this); nq_onOperatorAbstract(core->this) })
) -
("@" { nq_onOperatorByref(core->this) })?
- < ( "=>" | "<=>"| ">>="| "<<="| ">>" | "<<"
| ">=" | "<=" | "!=" | "==" | ">" | "<" | "!" | "??"
| "+=" | "-=" | "*=" | "**="| "/=" | "%=" | "+" | "-" | "**" | "/" | "*" | "="
| "[]="| "[]" | "&&" | "||" | "%" | "as" | "implicit as"
| "&=" | "|=" | "^=" | "&" | "|" | "^" | "~"
) >
{ nq_onOperatorSymbol(core->this, yytext); nq_onOperatorBodyStart(core->this) } -
FunctionDeclBody
{ nq_onOperatorEnd(core->this) }
FunctionDecl = SuperFunctionDecl | RegularFunctionDecl
SuperFunctionDecl = name:IDENT - COLON - 'super' - FUNC_KW -
{ tokenPos; nq_onFunctionStart(core->this, name, ""); nq_onFunctionSuper(core->this) }
# optional suffix
(
- '~' suffix:IDENT { nq_onFunctionSuffix(core->this, suffix) }
)?
{ $$=nq_onFunctionEnd(core->this) }
RegularFunctionDecl =
doc: OocDoc
name:IDENT { tokenPos; nq_onFunctionStart(core->this, name, doc) }
- COLON
# modifiers
(- ( externName:ExternName { nq_onFunctionExtern(core->this, externName) }
| unmangledName:UnmangledName { nq_onFunctionUnmangled(core->this, unmangledName) }
| ABSTRACT_KW { nq_onFunctionAbstract(core->this) }
| STATIC_KW { nq_onFunctionStatic(core->this) }
| INLINE_KW { nq_onFunctionInline(core->this) }
| FINAL_KW { nq_onFunctionFinal(core->this) }
| PROTO_KW { nq_onFunctionProto(core->this) }
)
)*
FunctionDeclCore
AnonymousFunctionDecl = { nq_onFunctionStart(core->this, "", "") } FunctionDeclCore
FunctionDeclCore =
- FUNC_KW
('@' { nq_onFunctionThisRef(core->this) })?
# optional suffix
(
- '~' suffix:IDENT { nq_onFunctionSuffix(core->this, suffix) }
)?
FunctionDeclBody
FunctionDeclBody = (
GenericArguments?
# arguments are optional
(
WS '(' { nq_onFunctionArgsStart(core->this) }
( WS Argument WS
(',' WS Argument WS)*
)?
WS CLOS_PAREN ~{ throwError(NQE_EXP_ARG, "Malformed function argument (remember, it's `name: Type` in ooc, not `Type name`)\n") }
{ nq_onFunctionArgsEnd(core->this) }
)?
# optional suffix
(
- '~' suffix:IDENT { throwTokenError(NQE_MISPLACED_SUFFIX, "Misplaced function suffix (remember, it's `name: func ~suffix (args)`, not `name: func (args) ~suffix`") }
)?
# return type is optional
(
- R_ARROW
- t:Type ~{ throwError(NQE_EXP_RET_TYPE, "Missing return type.\n") }
{ nq_onFunctionReturnType(core->this, t) }
)?
# body is optional (for abstract/extern-named functions)
(
{ nq_onFunctionBody(core->this); }
WS '{' WS
(WS
( s:Stmt { nq_onStatement(core->this, s) }
| { tokenPos } OocDocCore { char *message = "Unexpected oocdoc comment"; throwTokenError(NQE_EXP_STATEMENT_OR_CLOSING_BRACKET, message) })
WS)*
WS CLOS_BRACK ~{ rewindWhiteSpace; throwError(NQE_EXP_STATEMENT_OR_CLOSING_BRACKET, "Malformed statement or closing bracket missing\n") }
)?
) { $$=nq_onFunctionEnd(core->this); }
Argument = ( (DOT dotName:IDENT -) { tokenPos; nq_onDotArg(core->this, dotName) }
| (ASS assName:IDENT -) { tokenPos; nq_onAssArg(core->this, assName) }
| vd:VariableDecl { tokenPos; nq_onStatement(core->this, vd) }
| (vargName:IDENT - ":" - "..." -) { tokenPos; nq_onVarArg(core->this, vargName) } # ooc varargs
| type:Type { tokenPos; nq_onTypeArg(core->this, type) }
| "..." { tokenPos; nq_onVarArg(core->this, NULL) } # C varargs
)
ClassDecl = (
doc: OocDoc
className:IDENT { tokenPos; nq_onClassStart(core->this, className, doc) }
- COLON
# modifiers
(- ( ExternName
| ABSTRACT_KW { nq_onClassAbstract(core->this) }
| FINAL_KW { nq_onClassFinal(core->this) }
)
)*
- CLASS_KW
GenericArguments?
TemplateDef?
# subclassing
(
- EXTENDS_KW - t:Type { nq_onClassExtends(core->this, t) }
)?
# interface subclassing
(
- IMPLEMENTS_KW
- t:Type { nq_onClassImplements(core->this, t) }
(- ',' - t:Type { nq_onClassImplements(core->this, t) })*
)?
{ nq_onClassBody(core->this); }
(
WS '{' WS
# classdecl contents
(WS
( vd:VariableDecl { tokenPos; nq_onStatement(core->this, vd); } Terminator+
| pd:PropertyDecl { tokenPos; nq_onStatement(core->this, pd); }
| fd:FunctionDecl
| od:OperatorDecl
| stmt:Stmt { tokenPos; nq_onStatement(core->this, stmt); }
)
WS)*
WS
CLOS_BRACK ~{ throwError(NQE_EXP_VAR_OR_FUNC_DECL, "Expected member, method, or '"_CSBRACK"' to close class.\n"); }
)
) { nq_onClassEnd(core->this) }
EnumDecl = (
doc: OocDoc
enumName:IDENT { tokenPos; nq_onEnumStart(core->this, enumName, doc); }
- COLON
- ENUM_KW
# optional from-type
(- FROM_KW - fromType:Type { tokenPos; nq_onEnumFromType(core->this, fromType); })?
# increment expression (ex: *2 or +1)
(
- '('
- op:EnumIncrementOper WS step:IntLiteral { nq_onEnumIncrementExpr(core->this, ((char*)op)[0], step); }
- ')'
)?
(
WS '{' WS
# enumeration elements
(EnumElement
(
(Terminator+ WS FunctionDecl) |
((',' | Terminator+) WS EnumElement)
)*
)?
WS
CLOS_BRACK ~{ throwError(NQE_EXP_ENUM_ELEMENT, "Expected enum element!\n"); }
)
) { tokenPos; nq_onEnumEnd(core->this) }
EnumElement = (
doc: OocDoc
i:IDENT { tokenPos; nq_onEnumElementStart(core->this, i, doc); }
- ( ASS value:Expr { nq_onEnumElementValue(core->this, value); }
| COLON
- externName:ExternName { nq_onEnumElementExtern(core->this, externName); }
)?
) { nq_onEnumElementEnd(core->this) }
EnumIncrementOper = ( STAR { $$="*"; }
| PLUS { $$="+"; }
)
ExtendDecl = (
doc: OocDoc
"extend" WS
extendedType: Type { tokenPos; nq_onExtendStart(core->this, extendedType, doc); }
# body
(
WS '{' WS
# coverdecl content
(WS
( fd:FunctionDecl
| pd:PropertyDecl { tokenPos; nq_onStatement(core->this, pd); }
)
WS)*
WS
CLOS_BRACK ~{ throwError(NQE_EXP_VAR_OR_FUNC_DECL, "Expected or function declaration\n"); }
)
{ nq_onExtendEnd(core->this); }
)
CoverDecl = (
doc: OocDoc
coverName:IDENT { tokenPos; nq_onCoverStart(core->this, coverName, doc); }
- COLON
# modifiers
(- externName:ExternName { nq_onCoverExtern(core->this, externName); })?
(- PROTO_KW { nq_onCoverProto(core->this); })?
- COVER_KW
GenericArguments?
TemplateDef?
# covering another type
(
- FROM_KW - t:Type { nq_onCoverFromType(core->this, t) }
)?
# extending another type
(
- EXTENDS_KW - t:Type { nq_onCoverExtends(core->this, t) }
)?
# interface subclassing
(
- IMPLEMENTS_KW
- t:Type { nq_onCoverImplements(core->this, t) }
(- ',' - t:Type { nq_onCoverImplements(core->this, t) })*
)?
# body is optional for covers
(
WS '{' WS
# coverdecl content
(WS
( vd:VariableDecl { tokenPos; nq_onStatement(core->this, vd); } Terminator+
| pd:PropertyDecl { tokenPos; nq_onStatement(core->this, pd); }
| od:OperatorDecl
| fd:FunctionDecl
)
WS)*
WS
CLOS_BRACK ~{ throwError(NQE_EXP_VAR_OR_FUNC_DECL, "Expected variable declaration or function declaration\n"); }
)?
)
{ nq_onCoverEnd(core->this); }
InterfaceDecl = (
doc: OocDoc
interfaceName:IDENT { tokenPos; nq_onInterfaceStart(core->this, interfaceName, doc); }
- COLON
- INTERFACE_KW
GenericArguments?
# extending another interface
(
- EXTENDS_KW - t:Type { nq_onInterfaceExtends(core->this, t) }
)?
# interface subclassing
(
- IMPLEMENTS_KW
- t:Type { nq_onClassImplements(core->this, t) }
(- ',' - t:Type { nq_onClassImplements(core->this, t) })*
)?
# body is required for interfaces
(
WS '{' WS
# interface content
(WS
fd:FunctionDecl
WS)*
WS
CLOS_BRACK ~{ throwError(NQE_EXP_VAR_OR_FUNC_DECL, "Expected method or '"_CSBRACK"' to close interface.\n"); }
)
)
{ nq_onInterfaceEnd(core->this); }
ExternName = EXTERN_KW { $$="" }
(
- '('
- < [A-Za-z_] [A-Za-z0-9_]* > { $$=yytext } # is that correct? should it be $$=$$? what's happening?
- ')'
)?
UnmangledName = UNMANGLED_KW { $$="" }
(
- '('
- unmangledName:IDENT { $$=unmangledName }
- ')'
)?
VarDeclFromExpr = ( doc: OocDoc
( i:IDENT { tokenPos; nq_onVarDeclStart(core->this); nq_onVarDeclName (core->this, i, doc); }
| tup:Tuple { nq_onVarDeclStart(core->this); nq_onVarDeclTuple(core->this, tup); })
- ASS_DECL
(-
(
STATIC_KW { nq_onVarDeclStatic(core->this); }
| CONST_KW { nq_onVarDeclConst (core->this); }
| PROTO_KW { nq_onVarDeclProto (core->this); }
) &[^A-Za-z_0-9]
)*
- r:Expr { nq_onVarDeclExpr(core->this, r); }
- { $$=nq_onVarDeclEnd(core->this); }
)
ConventionalVarDecl =
{ nq_onVarDeclStart(core->this); }
(
doc: OocDoc
varDeclName:IDENT { tokenPos; nq_onVarDeclName(core->this, varDeclName, doc); }
(- ASS - Expr { nq_onVarDeclExpr(core->this, $$); })?
# multi-decls
(
- ','
doc: OocDoc
WS nextDeclName:IDENT { nq_onVarDeclName(core->this, nextDeclName, doc); }
(- ASS - Expr { nq_onVarDeclExpr(core->this, $$); })?
-
)*
WS COLON WS
(-
(
STATIC_KW { nq_onVarDeclStatic(core->this); }
| CONST_KW { nq_onVarDeclConst (core->this); }
| PROTO_KW { nq_onVarDeclProto (core->this); }
| externName:ExternName { nq_onVarDeclExtern(core->this, externName); }
| unmangledName:UnmangledName { nq_onVarDeclUnmangled(core->this, unmangledName); }
)
)*
WS Type { nq_onVarDeclType(core->this, $$); }
(- ASS - Expr { nq_onVarDeclExpr(core->this, $$); })?
) { $$=nq_onVarDeclEnd(core->this); }
VariableDecl = (v:VarDeclFromExpr { $$=v })
| (v:ConventionalVarDecl { $$=v })
PropertyDecl = (p:PropertyDeclFromExpr { $$=p })
| (p:ConventionalPropertyDecl { $$=p })
ConventionalPropertyDecl =
(