forked from universal-ctags/ctags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr.c
1470 lines (1330 loc) · 34.3 KB
/
r.c
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
/*
* Copyright (c) 2003-2004, Ascher Stefan <stievie@utanet.at>
* Copyright (c) 2020, Masatake YAMATO <yamato@redhat.com>
* Copyright (c) 2020, Red Hat, Inc.
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for generating tags for R language files.
* R is a programming language for statistical computing.
* R is GPL Software, get it from http://www.r-project.org/
*
* The language references are available at
* https://cran.r-project.org/manuals.html, and
* https://cran.r-project.org/doc/manuals/r-release/R-lang.html
*
* The base library (including library and source functions) release is at
* https://stat.ethz.ch/R-manual/R-devel/library/base/html/00Index.html
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include "debug.h"
#include "entry.h"
#include "keyword.h"
#include "parse.h"
#include "read.h"
#include "selectors.h"
#include "tokeninfo.h"
#include "trace.h"
#include "vstring.h"
#include "subparser.h"
#include "r.h"
#include <string.h>
#include <ctype.h> /* to define isalpha(), isalnum(), isspace() */
/*
* MACROS
*/
#ifdef DEBUG
#define R_TRACE_TOKEN_TEXT(TXT,T,Q) TRACE_PRINT("<%s> token: %s (%s), parent: %s", \
(TXT), \
tokenIsTypeVal(T, '\n')? "\\n": tokenString(T), \
tokenTypeStr(T->type), \
(Q) == CORK_NIL? "": getEntryInCorkQueue(Q)->name)
#define R_TRACE_TOKEN(T,Q) TRACE_PRINT("token: %s (%s), parent: %s", \
tokenIsTypeVal((T), '\n')? "\\n": tokenString(T), \
tokenTypeStr((T)->type), \
(Q) == CORK_NIL? "": getEntryInCorkQueue(Q)->name)
#define R_TRACE_ENTER() TRACE_ENTER_TEXT("token: %s (%s), parent: %s", \
tokenIsTypeVal(token, '\n')? "\\n": tokenString(token), \
tokenTypeStr(token->type), \
parent == CORK_NIL? "": getEntryInCorkQueue(parent)->name)
#define R_TRACE_LEAVE() TRACE_LEAVE()
#else
#define R_TRACE_TOKEN_TEXT(TXT,T,Q) do {} while (0);
#define R_TRACE_TOKEN(T,Q) do {} while (0);
#define R_TRACE_ENTER() do {} while (0);
#define R_TRACE_LEAVE() do {} while (0);
#endif
/*
* DATA DEFINITIONS
*/
typedef enum {
K_UNDEFINED = -1,
K_FUNCTION,
K_LIBRARY,
K_SOURCE,
K_GLOBALVAR,
K_FUNCVAR,
K_PARAM,
K_VECTOR,
K_LIST,
K_DATAFRAME,
K_NAMEATTR,
KIND_COUNT
} rKind;
typedef enum {
R_LIBRARY_ATTACHED_BY_LIBRARY,
R_LIBRARY_ATTACHED_BY_REQUIRE,
} rLibraryRole;
typedef enum {
R_SOURCE_LOADED_BY_SOURCE,
} rSourceRole;
static roleDefinition RLibraryRoles [] = {
{ true, "library", "library attached by library function" },
{ true, "require", "library attached by require function" },
};
static roleDefinition RSourceRoles [] = {
{ true, "source", "source loaded by source fucntion" },
};
static kindDefinition RKinds[KIND_COUNT] = {
{true, 'f', "function", "functions"},
{true, 'l', "library", "libraries",
.referenceOnly = true, ATTACH_ROLES (RLibraryRoles) },
{true, 's', "source", "sources",
.referenceOnly = true, ATTACH_ROLES (RSourceRoles) },
{true, 'g', "globalVar", "global variables having values other than function()"},
{true, 'v', "functionVar", "function variables having values other than function()"},
{false,'z', "parameter", "function parameters inside function definitions" },
{true, 'c', "vector", "vectors explicitly created with `c()'" },
{true, 'L', "list", "lists explicitly created with `list()'" },
{true, 'd', "dataframe", "data frame explicitly created with `data.frame()'" },
{true, 'n', "nameattr", "names attribtes in vectors, lists, or dataframes" },
};
struct sKindExtraInfo {
const char *anon_prefix;
const char *ctor;
};
static struct sKindExtraInfo kindExtraInfo[KIND_COUNT] = {
[K_FUNCTION] = {
"anonFunc",
"function",
},
[K_VECTOR] = {
"anonVec",
"c",
},
[K_LIST] = {
"anonList",
"list",
},
[K_DATAFRAME] = {
"anonDataFrame",
"data.frame",
},
};
typedef enum {
F_ASSIGNMENT_OPERATOR,
F_CONSTRUCTOR,
} rField;
static fieldDefinition RFields [] = {
{
.name = "assignmentop",
.description = "operator for assignment",
.enabled = false,
},
{
.name = "constructor",
.description = "function used for making value assigned to the nameattr tag",
.enabled = true,
}
};
typedef int keywordId; /* to allow KEYWORD_NONE */
static const keywordTable RKeywordTable [] = {
{ "c", KEYWORD_R_C },
{ "list", KEYWORD_R_LIST },
{ "data.frame",KEYWORD_R_DATAFRAME },
{ "function", KEYWORD_R_FUNCTION },
{ "if", KEYWORD_R_IF },
{ "else", KEYWORD_R_ELSE },
{ "for", KEYWORD_R_FOR },
{ "while", KEYWORD_R_WHILE },
{ "repeat", KEYWORD_R_REPEAT },
{ "in", KEYWORD_R_IN },
{ "next", KEYWORD_R_NEXT },
{ "break", KEYWORD_R_BREAK },
{ "TRUE", KEYWORD_R_TRUE, },
{ "FALSE", KEYWORD_R_FALSE, },
{ "NULL", KEYWORD_R_NULL, },
{ "Inf", KEYWORD_R_INF, },
{ "NaN", KEYWORD_R_NAN, },
{ "NA", KEYWORD_R_NA, },
{ "NA_integer_", KEYWORD_R_NA, },
{ "NA_real_", KEYWORD_R_NA, },
{ "NA_complex_", KEYWORD_R_NA, },
{ "NA_character_", KEYWORD_R_NA, },
{ "source", KEYWORD_R_SOURCE },
{ "library", KEYWORD_R_LIBRARY },
{ "require", KEYWORD_R_LIBRARY },
};
#ifdef DEBUG
static const char *tokenTypeStr(enum RTokenType e);
#endif
static struct tokenTypePair typePairs [] = {
{ '{', '}' },
{ '[', ']' },
{ '(', ')' },
};
typedef struct sRToken {
tokenInfo base;
int scopeIndex;
int parenDepth;
vString *signature;
int kindIndexForParams; /* Used only when gathering parameters */
} rToken;
#define R(TOKEN) ((rToken *)TOKEN)
static int blackHoleIndex;
static langType Lang_R;
static void readToken (tokenInfo *const token, void *data);
static void clearToken (tokenInfo *token);
static struct tokenInfoClass rTokenInfoClass = {
.nPreAlloc = 4,
.typeForUndefined = TOKEN_R_UNDEFINED,
.keywordNone = KEYWORD_NONE,
.typeForKeyword = TOKEN_R_KEYWORD,
.typeForEOF = TOKEN_R_EOF,
.extraSpace = sizeof (rToken) - sizeof (tokenInfo),
.pairs = typePairs,
.pairCount = ARRAY_SIZE (typePairs),
.init = NULL,
.read = readToken,
.clear = clearToken,
.copy = NULL,
};
/*
* FUNCTION PROTOTYPES
*/
static bool parseStatement (tokenInfo *const token, int parent, bool in_arglist, bool in_continuous_pair);
static void parsePair (tokenInfo *const token, int parent, tokenInfo *const funcall);
static int notifyReadRightSideSymbol (tokenInfo *const symbol,
const char *const assignmentOperator,
int parent,
tokenInfo *const token);
static int makeSimpleSubparserTag (int langType, tokenInfo *const token, int parent,
bool in_func, int kindInR, const char *assignmentOperator);
static bool askSubparserTagAcceptancy (tagEntryInfo *pe);
static bool askSubparserTagHasFunctionAlikeKind (tagEntryInfo *e);
static int notifyReadFuncall (tokenInfo *const func, tokenInfo *const token, int parent);
/*
* FUNCTION DEFINITIONS
*/
static bool hasKindsOrCtors (tagEntryInfo * e, int kinds[], size_t count)
{
if (e->langType == Lang_R)
{
for (size_t i = 0; i < count; i++)
{
if (e->kindIndex == kinds[i])
return true;
}
}
else
{
bool function = false;
for (size_t i = 0; i < count; i++)
{
if (K_FUNCTION == kinds[i])
{
function = true;
break;
}
}
if (function && askSubparserTagHasFunctionAlikeKind (e))
return true;
}
const char *tmp = getParserFieldValueForType (e,
RFields [F_CONSTRUCTOR].ftype);
if (tmp == NULL)
return false;
for (size_t i = 0; i < count; i++)
{
const char * ctor = kindExtraInfo [kinds[i]].ctor;
if (ctor && strcmp (tmp, ctor) == 0)
return true;
}
return false;
}
static int searchScopeOtherThan (int scope, int kinds[], size_t count)
{
do
{
tagEntryInfo * e = getEntryInCorkQueue (scope);
if (!e)
return CORK_NIL;
if (!hasKindsOrCtors (e, kinds, count))
return scope;
scope = e->extensionFields.scopeIndex;
}
while (1);
}
static int makeSimpleRTagR (tokenInfo *const token, int parent, int kind,
const char * assignmentOp)
{
if (assignmentOp && (strlen (assignmentOp) == 3))
{
/* <<- or ->> is used here. */
if (anyKindsEntryInScopeRecursive (parent, tokenString (token),
(int[]){K_FUNCTION,
K_GLOBALVAR,
K_FUNCVAR,
K_PARAM}, 4) != CORK_NIL)
return CORK_NIL;
parent = CORK_NIL;
}
/* If the tag (T) to be created is defined in a scope and
the scope already has another tag having the same name
as T, T should not be created. */
tagEntryInfo *pe = getEntryInCorkQueue (parent);
int cousin = CORK_NIL;
if (pe && ((pe->langType == Lang_R && pe->kindIndex == K_FUNCTION)
|| (pe->langType != Lang_R && askSubparserTagHasFunctionAlikeKind (pe))))
{
cousin = anyEntryInScope (parent, tokenString (token));
if (kind == K_GLOBALVAR)
kind = K_FUNCVAR;
}
else if (pe && (kind == K_GLOBALVAR)
&& hasKindsOrCtors (pe, (int[]){K_VECTOR, K_LIST, K_DATAFRAME}, 3))
{
parent = searchScopeOtherThan (pe->extensionFields.scopeIndex,
(int[]){K_VECTOR, K_LIST, K_DATAFRAME}, 3);
if (parent == CORK_NIL)
cousin = anyKindEntryInScope (parent, tokenString (token), K_GLOBALVAR);
else
{
cousin = anyKindEntryInScope (parent, tokenString (token), K_FUNCVAR);
kind = K_FUNCVAR;
}
}
else if (pe)
{
/* The condition for tagging is a bit relaxed here.
Even if the same name tag is created in the scope, a name
is tagged if kinds are different. */
cousin = anyKindEntryInScope (parent, tokenString (token), kind);
}
if (cousin != CORK_NIL)
return CORK_NIL;
int corkIndex = makeSimpleTag (token->string, kind);
tagEntryInfo *tag = getEntryInCorkQueue (corkIndex);
if (tag)
{
tag->extensionFields.scopeIndex = parent;
if (assignmentOp)
{
if (strlen (assignmentOp) > 0)
attachParserField (tag, true,
RFields [F_ASSIGNMENT_OPERATOR].ftype,
assignmentOp);
else
markTagExtraBit (tag, XTAG_ANONYMOUS);
}
registerEntry (corkIndex);
}
return corkIndex;
}
static int makeSimpleRTag (tokenInfo *const token, int parent, bool in_func, int kind,
const char * assignmentOp)
{
int r;
const char *ctor = kindExtraInfo [kind].ctor;
tagEntryInfo *pe = (parent == CORK_NIL)? NULL: getEntryInCorkQueue (parent);
/* makeTagWithTranslation method for subparsers
called from makeSimpleSubparserTag expects
kind should be resolved. */
if (pe && hasKindsOrCtors (pe, (int[]){K_VECTOR, K_LIST, K_DATAFRAME}, 3))
{
if (assignmentOp
&& strcmp (assignmentOp, "=") == 0)
kind = K_NAMEATTR;
}
bool foreign_tag = false;
if (pe == NULL || pe->langType == Lang_R ||
!askSubparserTagAcceptancy (pe))
r = makeSimpleRTagR (token, parent, kind, assignmentOp);
else
{
foreign_tag = true;
r = makeSimpleSubparserTag (pe->langType, token, parent, in_func,
kind, assignmentOp);
}
if ((kind == K_NAMEATTR || foreign_tag) && ctor)
{
tagEntryInfo *e = getEntryInCorkQueue (r);
if (e)
attachParserField (e, true,
RFields [F_CONSTRUCTOR].ftype,
ctor);
}
return r;
}
static void clearToken (tokenInfo *token)
{
R (token)->parenDepth = 0;
R (token)->scopeIndex = CORK_NIL;
R (token)->kindIndexForParams = KIND_GHOST_INDEX;
if (R (token)->signature)
{
vStringDelete (R (token)->signature);
R (token)->signature = NULL;
}
}
static void readString (tokenInfo *const token, void *data)
{
int c;
bool escaped = false;
int c0 = tokenString(token)[0];
while (1)
{
c = getcFromInputFile ();
switch (c)
{
case EOF:
return;
case '\'':
case '"':
case '`':
tokenPutc (token, c);
if (!escaped && c == c0)
return;
escaped = false;
break;
case '\\':
tokenPutc (token, c);
escaped = !escaped;
break;
default:
tokenPutc (token, c);
escaped = false;
break;
}
}
}
static void readNumber (tokenInfo *const token, void *data)
{
int c;
/* 10.3.1 Constants
*
* Valid numeric constants: 1 10 0.1 .2 1e-7 1.2e+7
* Valid integer constants: 1L, 0x10L, 1000000L, 1e6L
* Valid numeric constants: 1.1L, 1e-3L, 0x1.1p-2
* Valid complex constants: 2i 4.1i 1e-2i
*/
while ((c = getcFromInputFile ()))
{
if (isxdigit (c) || c == '.' || c == 'E'
|| c == '+' || c == '-'
|| c == 'L' || c == 'x' || c == 'p'
|| c == 'i')
tokenPutc (token, c);
else
{
ungetcToInputFile (c);
break;
}
}
}
static void readSymbol (tokenInfo *const token, void *data)
{
int c;
while ((c = getcFromInputFile ()))
{
if (isalnum (c) || c == '.' || c == '_')
tokenPutc (token, c);
else
{
ungetcToInputFile (c);
break;
}
}
}
static keywordId resolveKeyword (vString *string)
{
char *s = vStringValue (string);
static langType lang = LANG_AUTO;
if (lang == LANG_AUTO)
lang = getInputLanguage ();
return lookupCaseKeyword (s, lang);
}
static bool signatureExpectingParameter (vString *signature)
{
if (vStringLast (signature) == '(')
return true;
for (size_t i = vStringLength (signature); i > 0; i--)
{
char c = vStringChar (signature, i - 1);
if (c == ' ')
continue;
else if (c == ',')
return true;
break;
}
return false;
}
static void readToken (tokenInfo *const token, void *data)
{
int c, c0;
token->type = TOKEN_R_UNDEFINED;
token->keyword = KEYWORD_NONE;
vStringClear (token->string);
do
c = getcFromInputFile ();
while (c == ' ' || c== '\t' || c == '\f');
token->lineNumber = getInputLineNumber ();
token->filePosition = getInputFilePosition ();
switch (c)
{
case EOF:
token->type = TOKEN_R_EOF;
break;
case '#':
while (1)
{
c = getcFromInputFile ();
if (c == EOF)
{
token->type = TOKEN_R_EOF;
break;
}
else if (c == '\n')
{
token->type = c;
tokenPutc (token, c);
break;
}
}
break;
case '\n':
case ';':
token->type = c;
tokenPutc (token, c);
break;
case '\'':
case '"':
case '`':
token->type = TOKEN_R_STRING;
tokenPutc (token, c);
readString (token, data);
break;
case '+':
case '/':
case '^':
case '~':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
break;
case ':':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
c = getcFromInputFile ();
if (c == ':')
{
tokenPutc (token, c);
token->type = TOKEN_R_SCOPE;
c = getcFromInputFile ();
if (c == ':')
tokenPutc (token, c);
else
ungetcToInputFile (c);
}
else
ungetcToInputFile (c);
break;
case '&':
case '|':
case '*':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
c0 = getcFromInputFile ();
if (c == c0)
tokenPutc (token, c0);
else
ungetcToInputFile (c0);
break;
case '=':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
c = getcFromInputFile ();
if (c == '=')
tokenPutc (token, c);
else
{
token->type = '=';
ungetcToInputFile (c);
}
break;
case '-':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
c = getcFromInputFile ();
if (c == '>')
{
token->type = TOKEN_R_RASSIGN;
tokenPutc (token, c);
c = getcFromInputFile ();
if (c == '>')
tokenPutc (token, c);
else
ungetcToInputFile (c);
}
else
ungetcToInputFile (c);
break;
case '>':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
c = getcFromInputFile ();
if (c == '=')
tokenPutc (token, c);
else
ungetcToInputFile (c);
break;
case '<':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
c = getcFromInputFile ();
/* <<- */
if (c == '<')
{
tokenPutc (token, c);
c = getcFromInputFile ();
}
if (c == '-')
{
token->type = TOKEN_R_LASSIGN;
tokenPutc (token, c);
}
else if (c == '=')
tokenPutc (token, c);
else
ungetcToInputFile (c);
break;
case '%':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
do
{
c = getcFromInputFile ();
if (c == EOF)
break;
tokenPutc (token, c);
if (c == '%')
break;
}
while (1);
break;
case '!':
token->type = TOKEN_R_OPERATOR;
tokenPutc (token, c);
c = getcFromInputFile ();
if (c == '=')
tokenPutc (token, c);
else
ungetcToInputFile (c);
break;
case '{':
case '}':
case '(':
case ')':
case '[':
case ']':
case ',':
case '$':
case '@':
token->type = c;
tokenPutc (token, c);
break;
case '.':
tokenPutc (token, c);
c = getcFromInputFile ();
if (isdigit(c))
{
token->type = TOKEN_R_NUMBER;
tokenPutc (token, c);
readNumber(token, data);
}
else if (isalpha (c) || c == '_')
{
token->type = TOKEN_R_SYMBOL;
tokenPutc (token, c);
readSymbol (token, data);
token->keyword = resolveKeyword (token->string);
if (token->keyword != KEYWORD_NONE)
token->type = TOKEN_R_KEYWORD;
}
else if (c == '.')
{
token->type = TOKEN_R_DOTS;
tokenPutc (token, c);
c = getcFromInputFile ();
if (c == '.')
tokenPutc (token, c);
else if (isdigit(c))
{
token->type = TOKEN_R_DOTS_N;
do
{
tokenPutc (token, c);
c = getcFromInputFile ();
}
while (isdigit(c));
ungetcToInputFile (c);
}
else if (isalpha (c) || c == '_')
{
token->type = TOKEN_R_SYMBOL;
tokenPutc (token, c);
readSymbol (token, data);
token->keyword = resolveKeyword (token->string);
if (token->keyword != KEYWORD_NONE)
token->type = TOKEN_R_KEYWORD;
}
else
{
token->type = TOKEN_R_UNDEFINED;
ungetcToInputFile (c);
}
}
break;
default:
tokenPutc (token, c);
if (isdigit (c))
{
token->type = TOKEN_R_NUMBER;
readNumber(token, data);
}
else if (isalpha (c))
{
token->type = TOKEN_R_SYMBOL;
readSymbol (token, data);
token->keyword = resolveKeyword (token->string);
if (token->keyword != KEYWORD_NONE)
token->type = TOKEN_R_KEYWORD;
}
else
token->type = TOKEN_R_UNDEFINED;
break;
}
/* Handle parameters in a signature */
if (R(token)->signature && !tokenIsType(token, R_EOF) && !tokenIsTypeVal(token, '\n'))
{
vString *signature = R (token)->signature;
if (tokenIsTypeVal (token, '('))
R (token)->parenDepth++;
else if (tokenIsTypeVal (token, ')'))
R (token)->parenDepth--;
if (R (token)->kindIndexForParams != KIND_GHOST_INDEX
&& R (token)->parenDepth == 1 && tokenIsType (token, R_SYMBOL)
&& signatureExpectingParameter (signature))
makeSimpleRTag (token, R (token)->scopeIndex, false,
R (token)->kindIndexForParams, NULL);
if (vStringLast (signature) != '(' &&
!tokenIsTypeVal (token, ',') &&
!tokenIsTypeVal (token, ')'))
vStringPut (signature, ' ');
vStringCat (signature, token->string);
}
}
#define newRToken rNewToken
extern tokenInfo *rNewToken (void)
{
return newToken (&rTokenInfoClass);
}
#define tokenReadNoNewline rTokenReadNoNewline
extern void rTokenReadNoNewline (tokenInfo *const token)
{
while (1)
{
tokenRead(token);
if (!tokenIsTypeVal (token, '\n'))
break;
}
}
static void setupCollectingSignature (tokenInfo *const token,
vString *signature,
int kindIndexForParams,
int corkIndex)
{
R (token)->signature = signature;
R (token)->kindIndexForParams = kindIndexForParams;
R (token)->scopeIndex = corkIndex;
R (token)->parenDepth = 1;
}
extern void rSetupCollectingSignature (tokenInfo *const token,
vString *signature)
{
setupCollectingSignature (token, signature,
KIND_GHOST_INDEX, CORK_NIL);
}
static void teardownCollectingSignature (tokenInfo *const token)
{
R (token)->parenDepth = 0;
R (token)->scopeIndex = CORK_NIL;
R (token)->kindIndexForParams = KIND_GHOST_INDEX;
R (token)->signature = NULL;
}
extern void rTeardownCollectingSignature (tokenInfo *const token)
{
teardownCollectingSignature (token);
}
static int getKindForToken (tokenInfo *const token)
{
if (tokenIsKeyword (token, R_FUNCTION))
return K_FUNCTION;
else if (tokenIsKeyword (token, R_C))
return K_VECTOR;
else if (tokenIsKeyword (token, R_LIST))
return K_LIST;
else if (tokenIsKeyword (token, R_DATAFRAME))
return K_DATAFRAME;
return K_GLOBALVAR;
}
static bool findNonPlaceholder (int corkIndex, tagEntryInfo *entry, void *data)
{
bool *any_non_placehoders = data;
if (!entry->placeholder)
{
*any_non_placehoders = true;
return false;
}
return true;
}
static void parseRightSide (tokenInfo *const token, tokenInfo *const symbol, int parent)
{
R_TRACE_ENTER();
char *const assignment_operator = eStrdup (tokenString (token));
vString *signature = NULL;
tokenReadNoNewline (token);
int kind = getKindForToken (token);
/* Call sub parsers */
int corkIndex = notifyReadRightSideSymbol (symbol,
assignment_operator,
parent,
token);
if (corkIndex == CORK_NIL)
{
/* No subparser handle the symbol */
corkIndex = makeSimpleRTag (symbol, parent, kind == K_FUNCTION,
kind,
assignment_operator);
}
if (kind == K_FUNCTION)
{
/* parse signature */
tokenReadNoNewline (token);
if (tokenIsTypeVal (token, '('))
{
if (corkIndex == CORK_NIL)
tokenSkipOverPair (token);
else
{
signature = vStringNewInit("(");
setupCollectingSignature (token, signature, K_PARAM, corkIndex);
tokenSkipOverPair (token);
teardownCollectingSignature (token);
}
tokenReadNoNewline (token);
}
parent = (corkIndex == CORK_NIL
? blackHoleIndex
: corkIndex);
}
else if (kind == K_VECTOR || kind == K_LIST || kind == K_DATAFRAME)
{
tokenRead (token);
parsePair (token, corkIndex, NULL);
tokenRead (token);
parent = corkIndex;
}
R_TRACE_TOKEN_TEXT("body", token, parent);
parseStatement (token, parent, false, false);
tagEntryInfo *tag = getEntryInCorkQueue (corkIndex);
if (tag)
{
tag->extensionFields.endLine = token->lineNumber;
if (signature)
{
tag->extensionFields.signature = vStringDeleteUnwrap(signature);
signature = NULL;
}
/* If a vector has no named attribte and it has no lval,
* we don't make a tag for the vector. */
if ((kind == K_VECTOR || kind == K_LIST || kind == K_DATAFRAME)
&& *assignment_operator == '\0')
{
bool any_non_placehoders = false;
foreachEntriesInScope (corkIndex, NULL,
findNonPlaceholder, &any_non_placehoders);
if (!any_non_placehoders)
tag->placeholder = 1;
}
}
vStringDelete (signature); /* NULL is acceptable. */
eFree (assignment_operator);
R_TRACE_LEAVE();
}
/* Parse arguments for library and source. */
static bool preParseExternalEntitiy (tokenInfo *const token, tokenInfo *const funcall)
{
TRACE_ENTER();
bool r = true;
tokenInfo *prefetch_token = newRToken ();
tokenReadNoNewline (prefetch_token);
if (tokenIsType (prefetch_token, R_SYMBOL)
|| tokenIsType (prefetch_token, R_STRING))
{
tokenInfo *const loaded_obj_token = newTokenByCopying (prefetch_token);
tokenReadNoNewline (prefetch_token);
if (tokenIsTypeVal (prefetch_token, ')')
|| tokenIsTypeVal (prefetch_token, ','))
{
if (tokenIsTypeVal (prefetch_token, ')'))
r = false;
makeSimpleRefTag (loaded_obj_token->string,
(tokenIsKeyword (funcall, R_LIBRARY)
? K_LIBRARY
: K_SOURCE),
(tokenIsKeyword (funcall, R_LIBRARY)
? (strcmp (tokenString(funcall), "library") == 0
? R_LIBRARY_ATTACHED_BY_LIBRARY
: R_LIBRARY_ATTACHED_BY_REQUIRE)
: R_SOURCE_LOADED_BY_SOURCE));
tokenDelete (loaded_obj_token);
}