-
Notifications
You must be signed in to change notification settings - Fork 312
/
gram.y
4543 lines (4077 loc) · 123 KB
/
gram.y
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
%define parse.error verbose
%{
/*
* R : A Computer Language for Statistical Data Analysis
* Copyright (C) 1997--2024 The R Core Team
* Copyright (C) 2009--2011 Romain Francois
* Copyright (C) 1995--1997 Robert Gentleman and Ross Ihaka
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, a copy is available at
* https://www.R-project.org/Licenses/
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define R_USE_SIGNALS 1
#include "IOStuff.h" /*-> Defn.h */
#include "Fileio.h"
#include "Parse.h"
#include <R_ext/Print.h>
#if !defined(__STDC_ISO_10646__) && (defined(__APPLE__) || defined(__FreeBSD__) || defined(__sun))
/* This may not be 100% true (see the comment in rlocale.h),
but it seems true in normal locales.
*/
# define __STDC_ISO_10646__
#endif
/* #define YYDEBUG 1 */
#define YYERROR_VERBOSE 1
#define PARSE_ERROR_SIZE 256 /* Parse error messages saved here */
#define PARSE_CONTEXT_SIZE 256 /* Recent parse context kept in a circular buffer */
static Rboolean busy = FALSE;
static SEXP R_NullSymbol = NULL;
static int identifier ;
static void incrementId(void);
static void initData(void);
static void initId(void);
static void record_( int, int, int, int, int, int, char* ) ;
static void yyerror(const char *);
static int yylex(void);
int yyparse(void);
static FILE *fp_parse;
static int (*ptr_getc)(void);
static int SavedToken;
static SEXP SavedLval;
#define yyconst const
typedef struct yyltype
{
int first_line;
int first_column;
int first_byte;
int last_line;
int last_column;
int last_byte;
int first_parsed;
int last_parsed;
int id;
} yyltype;
#define INIT_DATA_COUNT 16384 /* init parser data to this size */
#define MAX_DATA_COUNT 65536 /* release it at the end if it is this size or larger*/
#define DATA_COUNT (length( PS_DATA ) / DATA_ROWS)
#define ID_COUNT ((length( PS_IDS ) / 2) - 1)
static void finalizeData(void) ;
static void growData(void) ;
static void growID( int ) ;
#define DATA_ROWS 8
#define _FIRST_PARSED( i ) INTEGER( PS_DATA )[ DATA_ROWS*(i) ]
#define _FIRST_COLUMN( i ) INTEGER( PS_DATA )[ DATA_ROWS*(i) + 1 ]
#define _LAST_PARSED( i ) INTEGER( PS_DATA )[ DATA_ROWS*(i) + 2 ]
#define _LAST_COLUMN( i ) INTEGER( PS_DATA )[ DATA_ROWS*(i) + 3 ]
#define _TERMINAL( i ) INTEGER( PS_DATA )[ DATA_ROWS*(i) + 4 ]
#define _TOKEN( i ) INTEGER( PS_DATA )[ DATA_ROWS*(i) + 5 ]
#define _ID( i ) INTEGER( PS_DATA )[ DATA_ROWS*(i) + 6 ]
#define _PARENT(i) INTEGER( PS_DATA )[ DATA_ROWS*(i) + 7 ]
#define ID_ID( i ) INTEGER(PS_IDS)[ 2*(i) ]
#define ID_PARENT( i ) INTEGER(PS_IDS)[ 2*(i) + 1 ]
static void modif_token( yyltype*, int ) ;
static void recordParents( int, yyltype*, int) ;
static int _current_token ;
/**
* Records the current non-terminal token expression and gives it an id
*
* @param loc the location of the expression
*/
static void setId(yyltype loc){
record_(
(loc).first_parsed, (loc).first_column, (loc).last_parsed, (loc).last_column,
_current_token, (loc).id, 0 ) ;
}
# define YYLTYPE yyltype
# define YYLLOC_DEFAULT(Current, Rhs, N) \
do { \
if (N){ \
(Current).first_line = YYRHSLOC (Rhs, 1).first_line; \
(Current).first_column = YYRHSLOC (Rhs, 1).first_column; \
(Current).first_byte = YYRHSLOC (Rhs, 1).first_byte; \
(Current).last_line = YYRHSLOC (Rhs, N).last_line; \
(Current).last_column = YYRHSLOC (Rhs, N).last_column; \
(Current).last_byte = YYRHSLOC (Rhs, N).last_byte; \
(Current).first_parsed = YYRHSLOC (Rhs, 1).first_parsed; \
(Current).last_parsed = YYRHSLOC (Rhs, N).last_parsed; \
incrementId( ) ; \
(Current).id = identifier ; \
_current_token = yyr1[yyn] ; \
if (ParseState.keepSrcRefs && ParseState.keepParseData) { \
yyltype childs[N]; \
int ii = 0; \
for(ii=0; ii<N; ii++){ \
childs[ii] = YYRHSLOC (Rhs, (ii+1) ) ; \
} \
recordParents( identifier, childs, N) ; \
} \
} else { \
(Current).first_line = (Current).last_line = \
YYRHSLOC (Rhs, 0).last_line; \
(Current).first_parsed = (Current).last_parsed = \
YYRHSLOC (Rhs, 0).last_parsed; \
(Current).first_column = YYRHSLOC (Rhs, 0).last_column; \
(Current).last_column = (Current).first_column - 1; \
(Current).first_byte = YYRHSLOC (Rhs, 0).last_byte; \
(Current).last_byte = (Current).first_byte - 1; \
(Current).id = NA_INTEGER; \
} \
} while (0)
# define YY_LOCATION_PRINT(File,Loc) \
fprintf ( File, "%d.%d.%d-%d.%d.%d (%d)", \
(Loc).first_line, (Loc).first_column, (Loc).first_byte, \
(Loc).last_line, (Loc).last_column, (Loc).last_byte, \
(Loc).id )
/* Useful defines so editors don't get confused ... */
#define LBRACE '{'
#define RBRACE '}'
/* Functions used in the parsing process */
static void CheckFormalArgs(SEXP, SEXP, YYLTYPE *);
static SEXP FirstArg(SEXP, SEXP); /* create list with one element */
static void GrowList(SEXP, SEXP); /* add element to list end */
static void SetSingleSrcRef(SEXP);
static void AppendToSrcRefs(SEXP);
static void PrependToSrcRefs(SEXP);
static SEXP SrcRefsToVectorList(void);
static void IfPush(void);
static int KeywordLookup(const char *);
static SEXP NewList(void);
static void NextArg(SEXP, SEXP, SEXP); /* add named element to list end */
static SEXP TagArg(SEXP, SEXP, YYLTYPE *);
static int processLineDirective(int *);
static int checkForPlaceholder(SEXP placeholder, SEXP arg);
static int HavePlaceholder = FALSE;
attribute_hidden SEXP R_PlaceholderToken = NULL;
static int HavePipeBind = FALSE;
static SEXP R_PipeBindSymbol = NULL;
/* These routines allocate constants */
static SEXP mkComplex(const char *);
SEXP mkFalse(void);
static SEXP mkFloat(const char *);
static SEXP mkInt(const char *);
static SEXP mkNA(void);
SEXP mkTrue(void);
/* Internal lexer / parser state variables */
static int EatLines = 0;
static int GenerateCode = 0;
static int EndOfFile = 0;
static int Status = 1;
static int xxgetc(void);
static int xxungetc(int);
static int xxcharcount, xxcharsave;
static int xxlinesave, xxbytesave, xxcolsave, xxparsesave;
static SrcRefState ParseState;
#define PS_SET_SRCREFS(x) SET_VECTOR_ELT(ParseState.sexps, 0, (x))
#define PS_SET_SRCFILE(x) SET_VECTOR_ELT(ParseState.sexps, 1, (x))
#define PS_SET_ORIGINAL(x) SET_VECTOR_ELT(ParseState.sexps, 2, (x))
/* direct pointer to data is kept for performance of finalizeData() */
#define PS_SET_DATA(x) do { \
SEXP __x__ = (x); \
SET_VECTOR_ELT(ParseState.sexps, 3, __x__); \
ParseState.data = __x__; \
} while(0);
#define PS_SET_TEXT(x) SET_VECTOR_ELT(ParseState.sexps, 4, (x))
#define PS_SET_IDS(x) SET_VECTOR_ELT(ParseState.sexps, 5, (x))
#define PS_SET_SVS(x) SET_VECTOR_ELT(ParseState.sexps, 6, (x))
#define PS_SRCREFS VECTOR_ELT(ParseState.sexps, 0)
#define PS_SRCFILE VECTOR_ELT(ParseState.sexps, 1)
#define PS_ORIGINAL VECTOR_ELT(ParseState.sexps, 2)
#define PS_DATA ParseState.data
#define PS_TEXT VECTOR_ELT(ParseState.sexps, 4)
#define PS_IDS VECTOR_ELT(ParseState.sexps, 5)
#define PS_SVS VECTOR_ELT(ParseState.sexps, 6)
/* These definitions are for error conditions */
#define NO_VALUE 0
#define STRING_VALUE 1
#define INT_VALUE 2
#define UINT_VALUE 3
#define CHAR_VALUE 4
#define UCS_VALUE 5
NORET static void raiseParseError(const char *, SEXP, int,
const void *, YYLTYPE *, const char *);
NORET static void raiseLexError(const char *, int,
const void *, const char *);
/* Memory protection in the parser
The generated code of the parser keeps semantic values (SEXPs) on its
semantic values stack. Values are added to the stack during shift and
reduce operations and are removed during reduce operations or error
handling. Values are created by the lexer before they are added to the
stack. Values are also held in a local SEXP variable once removed from
the stack but still needed. The stack is automatically expanded on demand.
For memory protection, it would be natural to have that stack on the R heap
and to use PROTECT/UNPROTECT to protect values in local SEXP variables.
Unfortunately, bison does not seem to be customizable enough to allow this.
Hence, semantic values, when created by the lexer or reduce operations, are
placed on parser state precious multi-set via PRESERVE_SV. They are removed
from the multi-set in reduce operations using RELEASE_SV, because by design
of the bison parsers such values are subsequently removed from the stack.
They are also automatically removed when the parsing finishes, including
parser error (also on R error, via the context on-end action).
Previously semantic values were protected via PROTECT/UNPROTECT_PTR with
similar semantics but using protect stack shared with PROTECT/UNPROTECT.
Using a separate precious multi-set is safe even with interleaving of the
two protection schemes.
*/
#define INIT_SVS() PS_SET_SVS(R_NewPreciousMSet(200))
#define PRESERVE_SV(x) R_PreserveInMSet((x), PS_SVS)
#define RELEASE_SV(x) R_ReleaseFromMSet((x), PS_SVS)
#define CLEAR_SVS() R_ReleaseMSet(PS_SVS, 500)
/* Memory leak
yyparse(), as generated by bison, allocates extra space for the parser
stack using malloc(). Unfortunately this means that there is a memory
leak in case of an R error (long-jump). In principle, we could define
yyoverflow() to relocate the parser stacks for bison and allocate say on
the R heap, but yyoverflow() is undocumented and somewhat complicated
(we would have to replicate some macros from the generated parser here).
The same problem exists at least in the Rd and LaTeX parsers in tools.
*/
#include <rlocale.h>
#ifdef HAVE_LANGINFO_CODESET
# include <langinfo.h>
#endif
// FIXME potentially need R_wchar_t with UTF-8 Windows.
static int mbcs_get_next(int c, wchar_t *wc)
{
int i, res, clen = 1; char s[9];
mbstate_t mb_st;
s[0] = (char) c;
/* This assumes (probably OK) that all MBCS embed ASCII as single-byte
lead bytes, including control chars */
if((unsigned int) c < 0x80) {
*wc = (wchar_t) c;
return 1;
}
if(utf8locale) {
clen = utf8clen((char) c);
for(i = 1; i < clen; i++) {
c = xxgetc();
if(c == R_EOF) { /* EOF whilst reading MBCS char */
for(i--; i > 0; i--) xxungetc(s[i]);
return -1;
}
s[i] = (char) c;
}
s[clen] ='\0'; /* x86 Solaris requires this */
mbs_init(&mb_st);
res = (int) mbrtowc(wc, s, clen, &mb_st);
if(res == -1) raiseLexError("invalidMBCS", NO_VALUE, NULL,
_("invalid multibyte character in parser (%s:%d:%d)"));
} else {
/* This is not necessarily correct for stateful MBCS */
while(clen <= R_MB_CUR_MAX) {
mbs_init(&mb_st);
res = (int) mbrtowc(wc, s, clen, &mb_st);
if(res >= 0) break;
if(res == -1)
raiseLexError("invalidMBCS", NO_VALUE, NULL,
_("invalid multibyte character in parser (%s:%d:%d)"));
/* so res == -2 */
c = xxgetc();
if(c == R_EOF) { /* EOF whilst reading MBCS char */
for(i = clen - 1; i > 0; i--) xxungetc(s[i]);
return -1;
}
s[clen++] = (char) c;
} /* we've tried enough, so must be complete or invalid by now */
}
for(i = clen - 1; i > 0; i--) xxungetc(s[i]);
return clen;
}
/* Soon to be defunct entry points */
void R_SetInput(int);
int R_fgetc(FILE*);
/* Routines used to build the parse tree */
static SEXP xxnullformal(void);
static SEXP xxfirstformal0(SEXP);
static SEXP xxfirstformal1(SEXP, SEXP);
static SEXP xxaddformal0(SEXP, SEXP, YYLTYPE *);
static SEXP xxaddformal1(SEXP, SEXP, SEXP, YYLTYPE *);
static SEXP xxexprlist0(void);
static SEXP xxexprlist1(SEXP, YYLTYPE *);
static SEXP xxexprlist2(SEXP, SEXP, YYLTYPE *);
static SEXP xxsub0(void);
static SEXP xxsub1(SEXP, YYLTYPE *);
static SEXP xxsymsub0(SEXP, YYLTYPE *);
static SEXP xxsymsub1(SEXP, SEXP, YYLTYPE *);
static SEXP xxnullsub0(YYLTYPE *);
static SEXP xxnullsub1(SEXP, YYLTYPE *);
static SEXP xxsublist1(SEXP);
static SEXP xxsublist2(SEXP, SEXP);
static SEXP xxcond(SEXP);
static SEXP xxifcond(SEXP);
static SEXP xxif(SEXP, SEXP, SEXP);
static SEXP xxifelse(SEXP, SEXP, SEXP, SEXP);
static SEXP xxforcond(SEXP, SEXP);
static SEXP xxfor(SEXP, SEXP, SEXP);
static SEXP xxwhile(SEXP, SEXP, SEXP);
static SEXP xxrepeat(SEXP, SEXP);
static SEXP xxnxtbrk(SEXP);
static SEXP xxfuncall(SEXP, SEXP);
static SEXP xxdefun(SEXP, SEXP, SEXP, YYLTYPE *);
static SEXP xxpipe(SEXP, SEXP, YYLTYPE *);
static SEXP xxpipebind(SEXP, SEXP, SEXP, YYLTYPE *);
static SEXP xxunary(SEXP, SEXP);
static SEXP xxbinary(SEXP, SEXP, SEXP);
static SEXP xxparen(SEXP, SEXP);
static SEXP xxsubscript(SEXP, SEXP, SEXP);
static SEXP xxexprlist(SEXP, YYLTYPE *, SEXP);
static int xxvalue(SEXP, int, YYLTYPE *);
#define YYSTYPE SEXP
%}
%token-table
%token END_OF_INPUT ERROR
%token STR_CONST NUM_CONST NULL_CONST SYMBOL FUNCTION
%token INCOMPLETE_STRING
%token LEFT_ASSIGN EQ_ASSIGN RIGHT_ASSIGN LBB
%token FOR IN IF ELSE WHILE NEXT BREAK REPEAT
%token GT GE LT LE EQ NE AND OR AND2 OR2
%token NS_GET NS_GET_INT
%token COMMENT LINE_DIRECTIVE
%token SYMBOL_FORMALS
%token EQ_FORMALS
%token EQ_SUB SYMBOL_SUB
%token SYMBOL_FUNCTION_CALL
%token SYMBOL_PACKAGE
/* no longer used: %token COLON_ASSIGN */
%token SLOT
%token PIPE
%token PLACEHOLDER
%token PIPEBIND
/* This is the precedence table, low to high */
%left '?'
%left LOW WHILE FOR REPEAT
%right IF
%left ELSE
%right LEFT_ASSIGN
%right EQ_ASSIGN
%left RIGHT_ASSIGN
%left '~' TILDE
%left OR OR2
%left AND AND2
%left UNOT NOT
%nonassoc GT GE LT LE EQ NE
%left '+' '-'
%left '*' '/'
%left SPECIAL PIPE
%left PIPEBIND
%left ':'
%left UMINUS UPLUS
%right '^'
%left '$' '@'
%left NS_GET NS_GET_INT
%nonassoc '(' '[' LBB
%%
prog : END_OF_INPUT { Status = 0; YYACCEPT; }
| '\n' { Status = 2; yyresult = xxvalue(NULL,2,NULL); YYACCEPT; }
| expr_or_assign_or_help '\n' { Status = 3; yyresult = xxvalue($1,3,&@1); YYACCEPT; }
| expr_or_assign_or_help ';' { Status = 4; yyresult = xxvalue($1,4,&@1); YYACCEPT; }
| error { Status = 1; YYABORT; }
;
expr_or_assign_or_help : expr { $$ = $1; }
| expr_or_assign_or_help EQ_ASSIGN expr_or_assign_or_help { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr_or_assign_or_help '?' expr_or_assign_or_help { $$ = xxbinary($2,$1,$3); setId(@$); }
;
expr_or_help : expr { $$ = $1; }
| expr_or_help '?' expr_or_help { $$ = xxbinary($2,$1,$3); setId(@$); }
;
expr : NUM_CONST { $$ = $1; setId(@$); }
| STR_CONST { $$ = $1; setId(@$); }
| NULL_CONST { $$ = $1; setId(@$); }
| PLACEHOLDER { $$ = $1; setId(@$); }
| SYMBOL { $$ = $1; setId(@$); }
| '{' exprlist '}' { $$ = xxexprlist($1,&@1,$2); setId(@$); }
| '(' expr_or_assign_or_help ')' { $$ = xxparen($1,$2); setId(@$); }
| '-' expr %prec UMINUS { $$ = xxunary($1,$2); setId(@$); }
| '+' expr %prec UMINUS { $$ = xxunary($1,$2); setId(@$); }
| '!' expr %prec UNOT { $$ = xxunary($1,$2); setId(@$); }
| '~' expr %prec TILDE { $$ = xxunary($1,$2); setId(@$); }
| '?' expr_or_assign_or_help { $$ = xxunary($1,$2); setId(@$); }
| expr ':' expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '+' expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '-' expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '*' expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '/' expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '^' expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr SPECIAL expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '~' expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr LT expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr LE expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr EQ expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr NE expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr GE expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr GT expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr AND expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr OR expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr AND2 expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr OR2 expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr PIPE expr { $$ = xxpipe($1,$3,&@3); setId(@$); }
| expr PIPEBIND expr { $$ = xxpipebind($2,$1,$3,&@2); setId(@$); }
| expr LEFT_ASSIGN expr { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr RIGHT_ASSIGN expr { $$ = xxbinary($2,$3,$1); setId(@$); }
| FUNCTION '(' formlist ')' cr expr_or_assign_or_help %prec LOW
{ $$ = xxdefun($1,$3,$6,&@$); setId(@$); }
| '\\' '(' formlist ')' cr expr_or_assign_or_help %prec LOW { $$ = xxdefun(R_FunctionSymbol,$3,$6,&@$); setId(@$); }
| expr '(' sublist ')' { $$ = xxfuncall($1,$3); setId(@$); modif_token( &@1, SYMBOL_FUNCTION_CALL ) ; }
| IF ifcond expr_or_assign_or_help { $$ = xxif($1,$2,$3); setId(@$); }
| IF ifcond expr_or_assign_or_help ELSE expr_or_assign_or_help { $$ = xxifelse($1,$2,$3,$5); setId(@$); }
| FOR forcond expr_or_assign_or_help %prec FOR { $$ = xxfor($1,$2,$3); setId(@$); }
| WHILE cond expr_or_assign_or_help { $$ = xxwhile($1,$2,$3); setId(@$); }
| REPEAT expr_or_assign_or_help { $$ = xxrepeat($1,$2); setId(@$); }
| expr LBB sublist ']' ']' { $$ = xxsubscript($1,$2,$3); setId(@$); }
| expr '[' sublist ']' { $$ = xxsubscript($1,$2,$3); setId(@$); }
| SYMBOL NS_GET SYMBOL { $$ = xxbinary($2,$1,$3); setId(@$); modif_token( &@1, SYMBOL_PACKAGE ) ; }
| SYMBOL NS_GET STR_CONST { $$ = xxbinary($2,$1,$3); setId(@$); modif_token( &@1, SYMBOL_PACKAGE ) ; }
| STR_CONST NS_GET SYMBOL { $$ = xxbinary($2,$1,$3); setId(@$); }
| STR_CONST NS_GET STR_CONST { $$ = xxbinary($2,$1,$3); setId(@$); }
| SYMBOL NS_GET_INT SYMBOL { $$ = xxbinary($2,$1,$3); setId(@$); modif_token( &@1, SYMBOL_PACKAGE ) ;}
| SYMBOL NS_GET_INT STR_CONST { $$ = xxbinary($2,$1,$3); setId(@$); modif_token( &@1, SYMBOL_PACKAGE ) ;}
| STR_CONST NS_GET_INT SYMBOL { $$ = xxbinary($2,$1,$3); setId(@$); }
| STR_CONST NS_GET_INT STR_CONST { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '$' SYMBOL { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '$' STR_CONST { $$ = xxbinary($2,$1,$3); setId(@$); }
| expr '@' SYMBOL { $$ = xxbinary($2,$1,$3); setId(@$); modif_token( &@3, SLOT ) ; }
| expr '@' STR_CONST { $$ = xxbinary($2,$1,$3); setId(@$); }
| NEXT { $$ = xxnxtbrk($1); setId(@$); }
| BREAK { $$ = xxnxtbrk($1); setId(@$); }
;
cond : '(' expr_or_help ')' { $$ = xxcond($2); }
;
ifcond : '(' expr_or_help ')' { $$ = xxifcond($2); }
;
forcond : '(' SYMBOL IN expr_or_help ')' { $$ = xxforcond($2,$4); setId(@$); }
;
exprlist: { $$ = xxexprlist0(); setId(@$); }
| expr_or_assign_or_help { $$ = xxexprlist1($1, &@1); }
| exprlist ';' expr_or_assign_or_help { $$ = xxexprlist2($1, $3, &@3); }
| exprlist ';' { $$ = $1; setId(@$); }
| exprlist '\n' expr_or_assign_or_help { $$ = xxexprlist2($1, $3, &@3); }
| exprlist '\n' { $$ = $1;}
;
sublist : sub { $$ = xxsublist1($1); }
| sublist cr ',' sub { $$ = xxsublist2($1,$4); }
;
sub : { $$ = xxsub0(); }
| expr_or_help { $$ = xxsub1($1, &@1); }
| SYMBOL EQ_ASSIGN { $$ = xxsymsub0($1, &@1); modif_token( &@2, EQ_SUB ) ; modif_token( &@1, SYMBOL_SUB ) ; }
| SYMBOL EQ_ASSIGN expr_or_help { $$ = xxsymsub1($1,$3, &@1); modif_token( &@2, EQ_SUB ) ; modif_token( &@1, SYMBOL_SUB ) ; }
| STR_CONST EQ_ASSIGN { $$ = xxsymsub0($1, &@1); modif_token( &@2, EQ_SUB ) ; }
| STR_CONST EQ_ASSIGN expr_or_help { $$ = xxsymsub1($1,$3, &@1); modif_token( &@2, EQ_SUB ) ; }
| NULL_CONST EQ_ASSIGN { $$ = xxnullsub0(&@1); modif_token( &@2, EQ_SUB ) ; }
| NULL_CONST EQ_ASSIGN expr_or_help { $$ = xxnullsub1($3, &@1); modif_token( &@2, EQ_SUB ) ; }
;
formlist: { $$ = xxnullformal(); }
| SYMBOL { $$ = xxfirstformal0($1); modif_token( &@1, SYMBOL_FORMALS ) ; }
| SYMBOL EQ_ASSIGN expr_or_help { $$ = xxfirstformal1($1,$3); modif_token( &@1, SYMBOL_FORMALS ) ; modif_token( &@2, EQ_FORMALS ) ; }
| formlist ',' SYMBOL { $$ = xxaddformal0($1,$3, &@3); modif_token( &@3, SYMBOL_FORMALS ) ; }
| formlist ',' SYMBOL EQ_ASSIGN expr_or_help
{ $$ = xxaddformal1($1,$3,$5,&@3); modif_token( &@3, SYMBOL_FORMALS ) ; modif_token( &@4, EQ_FORMALS ) ;}
;
cr : { EatLines = 1; }
;
%%
/*----------------------------------------------------------------------------*/
static int (*ptr_getc)(void);
/* Private pushback, since file ungetc only guarantees one byte.
We need up to one MBCS-worth */
#define DECLARE_YYTEXT_BUFP(bp) char *bp = yytext ;
#define YYTEXT_PUSH(c, bp) do { \
if ((bp) - yytext >= sizeof(yytext) - 1){ \
raiseLexError("bufferOverflow", NO_VALUE, NULL, \
_("input buffer overflow (%s:%d:%d)")); \
} \
*(bp)++ = ((char)c); \
} while(0) ;
#define PUSHBACK_BUFSIZE 16
static int pushback[PUSHBACK_BUFSIZE];
static unsigned int npush = 0;
static int prevpos = 0;
static int prevlines[PUSHBACK_BUFSIZE];
static int prevcols[PUSHBACK_BUFSIZE];
static int prevbytes[PUSHBACK_BUFSIZE];
static int prevparse[PUSHBACK_BUFSIZE];
static int xxgetc(void)
{
int c;
if(npush) c = pushback[--npush]; else c = ptr_getc();
prevpos = (prevpos + 1) % PUSHBACK_BUFSIZE;
prevbytes[prevpos] = ParseState.xxbyteno;
prevlines[prevpos] = ParseState.xxlineno;
prevparse[prevpos] = ParseState.xxparseno;
prevcols[prevpos] = ParseState.xxcolno;
if (c == EOF) {
EndOfFile = 1;
return R_EOF;
}
R_ParseContextLast = (R_ParseContextLast + 1) % PARSE_CONTEXT_SIZE;
R_ParseContext[R_ParseContextLast] = (char) c;
if (c == '\n') {
ParseState.xxlineno += 1;
ParseState.xxcolno = 0;
ParseState.xxbyteno = 0;
ParseState.xxparseno += 1;
} else {
/* We only advance the column for the 1st byte in UTF-8, so handle later bytes specially */
if (!known_to_be_utf8 || (unsigned char)c < 0x80 || 0xC0 <= (unsigned char)c)
ParseState.xxcolno++;
ParseState.xxbyteno++;
}
if (c == '\t') ParseState.xxcolno = ((ParseState.xxcolno + 7) & ~7);
R_ParseContextLine = ParseState.xxlineno;
xxcharcount++;
return c;
}
static int xxungetc(int c)
{
/* this assumes that c was the result of xxgetc; if not, some edits will be needed */
ParseState.xxlineno = prevlines[prevpos];
ParseState.xxbyteno = prevbytes[prevpos];
ParseState.xxcolno = prevcols[prevpos];
ParseState.xxparseno = prevparse[prevpos];
prevpos = (prevpos + PUSHBACK_BUFSIZE - 1) % PUSHBACK_BUFSIZE;
R_ParseContextLine = ParseState.xxlineno;
xxcharcount--;
R_ParseContext[R_ParseContextLast] = '\0';
/* precaution as to how % is implemented for < 0 numbers */
R_ParseContextLast = (R_ParseContextLast + PARSE_CONTEXT_SIZE -1) % PARSE_CONTEXT_SIZE;
if(npush >= PUSHBACK_BUFSIZE) return EOF;
pushback[npush++] = c;
return c;
}
/* Only used from finish_mbcs_in_parse_context. */
static int add_mbcs_byte_to_parse_context(void)
{
int c;
if (EndOfFile)
raiseLexError("invalidMBCS", NO_VALUE, NULL,
_("invalid multibyte character in parser (%s:%d:%d)"));
if(npush)
c = pushback[--npush];
else
c = ptr_getc();
if (c == EOF)
raiseLexError("invalidMBCS", NO_VALUE, NULL,
_("invalid multibyte character in parser (%s:%d:%d)"));
R_ParseContextLast = (R_ParseContextLast + 1) % PARSE_CONTEXT_SIZE;
R_ParseContext[R_ParseContextLast] = (char) c;
return c;
}
/* On error, the parse context may end inside a multi-byte character. Add
the missing bytes to the context to so that it contains full characters. */
static void finish_mbcs_in_parse_context(void)
{
int i, c, nbytes = 0, first;
Rboolean mbcs = FALSE;
/* find the first byte of the context */
for(i = R_ParseContextLast;
R_ParseContext[i];
i = (i + PARSE_CONTEXT_SIZE - 1) % PARSE_CONTEXT_SIZE) {
nbytes++;
if (nbytes == PARSE_CONTEXT_SIZE)
break;
}
if (!nbytes)
return;
if (!R_ParseContext[i])
first = (i + 1) % PARSE_CONTEXT_SIZE;
else
/* The beginning of the context has been overwritten and for a general
encoding there is not way to recover it. It is possible for UTF-8,
though. */
return;
/* decode multi-byte characters */
for(i = 0; i < nbytes; i++) {
c = R_ParseContext[(first + i) % PARSE_CONTEXT_SIZE];
if ((unsigned int)c < 0x80) continue; /* ASCII */
if (utf8locale) {
/* UTF-8 could be handled more efficiently, searching from the end
of the string */
i += utf8clen((char) c) - 1;
if (i >= nbytes) {
while (i >= nbytes) {
add_mbcs_byte_to_parse_context();
nbytes++;
}
return;
}
} else
mbcs = TRUE;
}
if (!mbcs)
return;
/* copy the context to a linear buffer */
char buf[nbytes + R_MB_CUR_MAX];
for(i = 0; i < nbytes; i++)
buf[i] = R_ParseContext[(first + i) % PARSE_CONTEXT_SIZE];
for(i = 0; i < nbytes; i++) {
wchar_t wc;
int res;
mbstate_t mb_st;
mbs_init(&mb_st);
res = (int) mbrtowc(&wc, buf + i, nbytes - i, &mb_st);
while (res == -2 && nbytes < sizeof(buf)) {
/* This is not necessarily correct for stateful MBCS */
buf[nbytes++] = (char) add_mbcs_byte_to_parse_context();
mbs_init(&mb_st);
res = (int) mbrtowc(&wc, buf + i, nbytes - i, &mb_st);
}
if (res == -1)
raiseLexError("invalidMBCS", NO_VALUE, NULL,
_("invalid multibyte character in parser (%s:%d:%d)"));
i += res - 1;
}
}
/*
* Increments/inits the token/grouping counter
*/
static void incrementId(void){
identifier++;
}
static void initId(void){
identifier = 0 ;
}
static SEXP makeSrcref(YYLTYPE *lloc, SEXP srcfile)
{
SEXP val;
PROTECT(val = allocVector(INTSXP, 8));
INTEGER(val)[0] = lloc->first_line;
INTEGER(val)[1] = lloc->first_byte;
INTEGER(val)[2] = lloc->last_line;
INTEGER(val)[3] = lloc->last_byte;
INTEGER(val)[4] = lloc->first_column;
INTEGER(val)[5] = lloc->last_column;
INTEGER(val)[6] = lloc->first_parsed;
INTEGER(val)[7] = lloc->last_parsed;
setAttrib(val, R_SrcfileSymbol, srcfile);
setAttrib(val, R_ClassSymbol, mkString("srcref"));
UNPROTECT(1); /* val */
return val;
}
static void attachSrcrefs(SEXP val)
{
SEXP srval;
PROTECT(srval = SrcRefsToVectorList());
setAttrib(val, R_SrcrefSymbol, srval);
setAttrib(val, R_SrcfileSymbol, PS_SRCFILE);
{
YYLTYPE wholeFile;
wholeFile.first_line = 1;
wholeFile.first_byte = 0;
wholeFile.first_column = 0;
wholeFile.last_line = ParseState.xxlineno;
wholeFile.last_byte = ParseState.xxbyteno;
wholeFile.last_column = ParseState.xxcolno;
wholeFile.first_parsed = 1;
wholeFile.last_parsed = ParseState.xxparseno;
setAttrib(val, R_WholeSrcrefSymbol, makeSrcref(&wholeFile, PS_SRCFILE));
}
PS_SET_SRCREFS(R_NilValue);
ParseState.didAttach = TRUE;
UNPROTECT(1); /* srval */
}
static int xxvalue(SEXP v, int k, YYLTYPE *lloc)
{
if (k > 2) {
if (ParseState.keepSrcRefs) {
SEXP s = PROTECT(makeSrcref(lloc, PS_SRCFILE));
AppendToSrcRefs(s);
UNPROTECT(1); /* s */
}
RELEASE_SV(v);
}
R_CurrentExpr = v;
return k;
}
static SEXP xxnullformal(void)
{
SEXP ans;
PRESERVE_SV(ans = R_NilValue);
return ans;
}
static SEXP xxfirstformal0(SEXP sym)
{
SEXP ans;
if (GenerateCode)
PRESERVE_SV(ans = FirstArg(R_MissingArg, sym));
else
PRESERVE_SV(ans = R_NilValue);
RELEASE_SV(sym);
return ans;
}
static SEXP xxfirstformal1(SEXP sym, SEXP expr)
{
SEXP ans;
if (GenerateCode)
PRESERVE_SV(ans = FirstArg(expr, sym));
else
PRESERVE_SV(ans = R_NilValue);
RELEASE_SV(expr);
RELEASE_SV(sym);
return ans;
}
static SEXP xxaddformal0(SEXP formlist, SEXP sym, YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode) {
CheckFormalArgs(formlist, sym, lloc);
NextArg(formlist, R_MissingArg, sym);
ans = formlist;
} else {
RELEASE_SV(formlist);
PRESERVE_SV(ans = R_NilValue);
}
RELEASE_SV(sym);
return ans;
}
static SEXP xxaddformal1(SEXP formlist, SEXP sym, SEXP expr, YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode) {
CheckFormalArgs(formlist, sym, lloc);
NextArg(formlist, expr, sym);
ans = formlist;
} else {
RELEASE_SV(formlist);
PRESERVE_SV(ans = R_NilValue);
}
RELEASE_SV(expr);
RELEASE_SV(sym);
return ans;
}
static SEXP xxexprlist0(void)
{
SEXP ans;
if (GenerateCode) {
PRESERVE_SV(ans = NewList());
if (ParseState.keepSrcRefs) {
setAttrib(ans, R_SrcrefSymbol, PS_SRCREFS);
PS_SET_SRCREFS(R_NilValue);
}
}
else
PRESERVE_SV(ans = R_NilValue);
return ans;
}
static SEXP xxexprlist1(SEXP expr, YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode) {
PRESERVE_SV(ans = NewList());
if (ParseState.keepSrcRefs) {
setAttrib(ans, R_SrcrefSymbol, PS_SRCREFS);
SEXP s = PROTECT(makeSrcref(lloc, PS_SRCFILE));
SetSingleSrcRef(s);
UNPROTECT(1); /* s */
}
GrowList(ans, expr);
}
else
PRESERVE_SV(ans = R_NilValue);
RELEASE_SV(expr);
return ans;
}
static SEXP xxexprlist2(SEXP exprlist, SEXP expr, YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode) {
if (ParseState.keepSrcRefs) {
SEXP s = PROTECT(makeSrcref(lloc, PS_SRCFILE));
AppendToSrcRefs(s);
UNPROTECT(1); /* s */
}
GrowList(exprlist, expr);
ans = exprlist;
} else {
RELEASE_SV(exprlist);
PRESERVE_SV(ans = R_NilValue);
}
RELEASE_SV(expr);
return ans;
}
static SEXP xxsub0(void)
{
SEXP ans;
if (GenerateCode)
PRESERVE_SV(ans = lang2(R_MissingArg,R_NilValue));
else
PRESERVE_SV(ans = R_NilValue);
return ans;
}
static SEXP xxsub1(SEXP expr, YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode)
PRESERVE_SV(ans = TagArg(expr, R_NilValue, lloc));
else
PRESERVE_SV(ans = R_NilValue);
RELEASE_SV(expr);
return ans;
}
static SEXP xxsymsub0(SEXP sym, YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode)
PRESERVE_SV(ans = TagArg(R_MissingArg, sym, lloc));
else
PRESERVE_SV(ans = R_NilValue);
RELEASE_SV(sym);
return ans;
}
static SEXP xxsymsub1(SEXP sym, SEXP expr, YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode)
PRESERVE_SV(ans = TagArg(expr, sym, lloc));
else
PRESERVE_SV(ans = R_NilValue);
RELEASE_SV(expr);
RELEASE_SV(sym);
return ans;
}
static SEXP xxnullsub0(YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode)
PRESERVE_SV(ans = TagArg(R_MissingArg, R_NullSymbol, lloc));
else
PRESERVE_SV(ans = R_NilValue);
RELEASE_SV(R_NilValue);
return ans;
}
static SEXP xxnullsub1(SEXP expr, YYLTYPE *lloc)
{
SEXP ans;
if (GenerateCode)
PRESERVE_SV(ans = TagArg(expr, R_NullSymbol, lloc));
else
PRESERVE_SV(ans = R_NilValue);
RELEASE_SV(R_NilValue);
RELEASE_SV(expr);
return ans;
}