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
|
/*
* Copyright (c) 2020, Masatake YAMATO
* 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 S4 Classes and Methods.
* https://www.r-project.org/conferences/useR-2004/Keynotes/Leisch.pdf
*/
/*
* INCLUDE FILES
*/
#include "general.h" /* must always come first */
#include "r.h"
#include "kind.h"
#include "tokeninfo.h"
#include "parse.h"
#include "subparser.h"
#include <string.h>
/*
* DATA DECLARATIONS
*/
struct s4Subparser {
rSubparser r;
};
/*
* FUNCTION PROTOTYPES
*/
static int s4ReadFuncall (rSubparser *s,
tokenInfo *const func, tokenInfo *const token,
int parent);
static int s4ReadRightSideSymbol (rSubparser *s,
tokenInfo *const symbol,
const char *const assignmentOperator,
int parent,
tokenInfo *const token);
static bool s4AskTagAcceptancy (rSubparser *s, tagEntryInfo *pe);
static bool s4HasFunctionAlikeKind (rSubparser *s, tagEntryInfo *e);
/*
* DATA DEFINITIONS
*/
typedef enum {
S4_K_CLASS,
S4_K_REPRESENTATION,
S4_K_GENERIC,
S4_K_METHOD,
} s4Kind;
static kindDefinition S4Kinds[] = {
{ true, 'c', "class", "classes" },
{ true, 'r', "repr", "representations" },
{ true, 'g', "generic", "generics" },
{ true, 'm', "method", "methods" },
};
static struct s4Subparser s4Subparser = {
.r = {
.subparser = {
.direction = SUBPARSER_BI_DIRECTION,
},
.readFuncall = s4ReadFuncall,
.readRightSideSymbol = s4ReadRightSideSymbol,
.askTagAcceptancy = s4AskTagAcceptancy,
.hasFunctionAlikeKind = s4HasFunctionAlikeKind,
},
};
/*
* FUNCTION DEFINITIONS
*/
/*
* V=================V: parse this area
* representation(...)) ...
* representation(...), ...
*/
static void parseRepresentation (rSubparser *s, tokenInfo *const token, int parent)
{
rTokenReadNoNewline (token);
if (tokenIsTypeVal (token, '('))
{
rTokenReadNoNewline (token);
while (!(tokenIsTypeVal (token, ')') || tokenIsTypeVal (token, ',')))
{
if (!rParseStatement (token, parent, false))
break;
else if (tokenIsTypeVal (token, '\n'))
rTokenReadNoNewline (token);
}
}
else
tokenUnread (token);
}
/*
* V==============V: parse this area
* contains = "..."
*/
static void parseContains (rSubparser *s, tokenInfo *const token, int parent)
{
rTokenReadNoNewline (token);
if (tokenIsTypeVal (token, '='))
{
rTokenReadNoNewline (token);
if (tokenIsType (token, R_STRING))
{
tagEntryInfo *e = getEntryInCorkQueue (parent);
if (e)
{
vString *n = rExtractNameFromString (token->string);
e->extensionFields.inheritance = vStringDeleteUnwrap (n);
}
}
else
tokenUnread (token);
}
else
tokenUnread (token);
}
/*
* V====V: parse this area
* setClass ("class", ...)
*/
static bool parseClassArgs (rSubparser *s, tokenInfo *const token, int parent,
tokenInfo *const open_paren CTAGS_ATTR_UNUSED)
{
do
{
rTokenReadNoNewline (token);
if (tokenIsTypeVal (token, ')'))
break;
else if (tokenIsTypeVal (token, '('))
tokenSkipOverPair (token);
else if (tokenIsTypeVal (token, '{'))
tokenSkipOverPair (token);
else if (tokenIsTypeVal (token, '['))
tokenSkipOverPair (token);
else if (tokenIsType (token, R_SYMBOL))
{
const char *str = tokenString (token);
if (strcmp (str, "representation") == 0)
parseRepresentation (s, token, parent);
else if (strcmp (str, "contains") == 0)
parseContains (s, token, parent);
}
}
while (!tokenIsEOF (token));
return false;
}
/*
* V============V: parse this area
* setMethod ("method", c(...), ...)
* V====================V: parse this area
* setMethod ("method", signature(...), ...)
*
* Attaching c(...) or signature(...) to "signature:" field of
* the tag for "method" specified by parent.
*/
static bool parseMethodArgs (rSubparser *s, tokenInfo *const token, int parent,
tokenInfo *const open_paren)
{
rTokenReadNoNewline (token);
if (tokenIsTypeVal (token, ','))
{
rTokenReadNoNewline (token);
if (tokenIsKeyword (token, R_C)
|| (tokenIsType (token, R_SYMBOL)
&& (strcmp (tokenString (token), "signature") == 0)))
{
rTokenReadNoNewline (token);
if (tokenIsTypeVal (token, '('))
{
vString *signature = vStringNewInit("(");
rSetupCollectingSignature (token, signature);
tokenSkipOverPair (token);
rTeardownCollectingSignature (token);
tagEntryInfo *e = getEntryInCorkQueue (parent);
if (e)
{
e->extensionFields.signature = vStringDeleteUnwrap (signature);
signature = NULL;
}
vStringDelete (signature); /* NULL is acceptable */
}
rTokenReadNoNewline (token);
if (tokenIsTypeVal (token, ','))
{
rTokenReadNoNewline (token);
/* anonymous function for implementing this method may be here.*/
while (!tokenIsTypeVal (token, ')'))
{
if (!rParseStatement (token, parent, true))
break;
else if (tokenIsTypeVal (token, '\n'))
rTokenReadNoNewline (token);
}
}
return false;
}
else
tokenUnread (token);
}
else
tokenUnread (token);
return true;
}
/*
* V====V: parse this area
* setGeneric ("generic", ...)
*/
static bool parseGenericArgs (rSubparser *s, tokenInfo *const token, int parent,
tokenInfo *const open_paren)
{
while (!tokenIsTypeVal (token, ')'))
{
if (!rParseStatement (token, parent, true))
break;
else if (tokenIsTypeVal (token, '\n'))
rTokenReadNoNewline (token);
}
return false;
}
/* parse
* this
* area ---\ /--- parseArgs parses this area.
* V====VV----V
* setXXX("...", ...)
*/
static int parseSetCommon (rSubparser *s, tokenInfo *const token, int parent,
int kind,
bool (* parseArgs) (rSubparser *, tokenInfo *const, int, tokenInfo *const))
{
int q = CORK_NIL;
tokenInfo *const open_paren = newTokenByCopying (token);
rTokenReadNoNewline (token);
if (tokenIsType (token, R_STRING))
{
vString *n = rExtractNameFromString (token->string);
if (n)
{
q = makeSimpleTag (n, kind);
vStringDelete (n);
}
}
bool skip = true;
if (q != CORK_NIL && parseArgs)
skip = (* parseArgs) (s, token, q, open_paren);
if (skip)
{
tokenCopy (token, open_paren);
tokenSkipOverPair (token);
}
tokenDelete (open_paren);
return q;
}
static int parseSetClass (rSubparser *s, tokenInfo *const token, int parent)
{
return parseSetCommon (s, token, parent, S4_K_CLASS, parseClassArgs);
}
static int parseSetGeneric (rSubparser *s, tokenInfo *const token, int parent)
{
return parseSetCommon (s, token, parent, S4_K_GENERIC, parseGenericArgs);
}
static int parseSetMethod (rSubparser *s, tokenInfo *const token, int parent)
{
return parseSetCommon (s, token, parent, S4_K_METHOD, parseMethodArgs);
}
static int s4ReadFuncall (rSubparser *s,
tokenInfo *const func, tokenInfo *const token,
int parent)
{
if (strcmp (tokenString (func), "setClass") == 0)
return parseSetClass (s, token, parent);
else if (strcmp (tokenString (func), "setGeneric") == 0)
return parseSetGeneric (s, token, parent);
else if (strcmp (tokenString (func), "setMethod") == 0)
return parseSetMethod (s, token, parent);
else
return CORK_NIL;
}
static bool s4AskTagAcceptancy (rSubparser *s, tagEntryInfo *pe)
{
return (pe->kindIndex == S4_K_CLASS);
}
static bool s4HasFunctionAlikeKind (rSubparser *s, tagEntryInfo *e)
{
return e->kindIndex == S4_K_METHOD ||
e->kindIndex == S4_K_GENERIC;
}
/*
* setClass("class", representation(r0 = "t0", r1 = "t1", ...
*
* Attaching t as "typeref:typename:" field of r.
*
* the tag for "class" -> parent.
* r0, r1, ... -> symbol
*/
static int s4ReadRightSideSymbol (rSubparser *s,
tokenInfo *const symbol,
const char *const assignmentOperator,
int parent,
tokenInfo *const token)
{
tagEntryInfo *pe = getEntryInCorkQueue (parent);
if (! (pe
&& pe->langType == s->subparser.slaveParser->id
&& pe->kindIndex == S4_K_CLASS))
return CORK_NIL;
tagEntryInfo e;
int q;
vString *t = NULL;
if (tokenIsType (token, R_STRING))
t = rExtractNameFromString (token->string);
initTagEntry (&e, tokenString (symbol), S4_K_REPRESENTATION);
e.extensionFields.scopeIndex = parent;
if (t)
{
e.extensionFields.typeRef[0] = "typename";
e.extensionFields.typeRef[1] = vStringValue (t);
}
q = makeTagEntry (&e);
vStringDelete (t); /* NULL is acceptable. */
return q;
}
static void findS4Tags(void)
{
scheduleRunningBaseparser (RUN_DEFAULT_SUBPARSERS);
}
extern parserDefinition* S4ClassParser (void)
{
parserDefinition* const def = parserNew("S4Class");
static parserDependency dependencies [] = {
[0] = { DEPTYPE_SUBPARSER, "R", &s4Subparser },
};
def->dependencies = dependencies;
def->dependencyCount = ARRAY_SIZE (dependencies);
def->kindTable = S4Kinds;
def->kindCount = ARRAY_SIZE(S4Kinds);
def->parser = findS4Tags;
def->useCork = CORK_QUEUE;
return def;
}
|