4 * Copyright (c) 2003-2004, Ascher Stefan <stievie@utanet.at>
6 * This source code is released for free distribution under the terms of the
7 * GNU General Public License.
9 * This module contains functions for generating tags for R language files.
10 * R is a programming language for statistical computing.
11 * R is GPL Software, get it from http://www.r-project.org/
17 #include "general.h" /* must always come first */
20 #include <ctype.h> /* to define isalpha(), isalnum(), isspace() */
28 #define SKIPSPACE(ch) while (isspace((int)*ch)) \
32 static kindOption RKinds
[] = {
33 { TRUE
, 'f', "function", "functions" },
34 { TRUE
, 's', "other", "libraries" }
39 static void installRRegex (const langType language
)
41 /* This is a function, looks as follows:
42 * itent <- function(arg1, arg2) {
46 addTagRegex (language
,
47 "^[ \t]*([.a-zA-Z0-9_]+)([ \t]*)<-([ \t]*)function", "\\1", "f,function", NULL
);
48 /* This loads someting, e.g. a library, simply: library(libname) */
49 addTagRegex (language
,
50 "^[ \t]*(library|source|load|data)[\\(]([a-zA-Z0-9_]+)[\\)]", "\\2", "s,other", NULL
);
53 static void makeRTag(const vString
* const name
, int kind
)
56 initTagEntry(&e
, vStringValue(name
));
58 e
.kindName
= RKinds
[kind
].name
;
59 e
.kind
= RKinds
[kind
].letter
;
64 extern void createRTags(void)
66 vString
*vLine
= vStringNew();
67 vString
*name
= vStringNew();
69 const unsigned char *line
;
71 while ((line
= fileReadLine()) != NULL
)
73 const unsigned char *cp
= (const unsigned char*)line
;
76 while ((*cp
!= '\0') && (*cp
!= '#')) {
77 /* iterate to the end of line or to a comment */
82 if (strncasecmp((const char*)cp
, "library", (size_t)7) == 0) {
83 /* load a library: library(tools) */
90 } else if (strncasecmp((const char*)cp
, "source", (size_t)6) == 0) {
91 /* load a source file: source("myfile.r") */
103 while ((!isspace((int)*cp
)) && *cp
!= '\0' && *cp
!= ')') {
104 vStringPut(name
, (int)*cp
);
107 vStringTerminate(name
);
109 /* if the string really exists, make a tag of it */
110 if (vStringLength(name
) > 0)
111 makeRTag(name
, ikind
);
113 /* prepare for the next iteration */
116 vStringPut(name
, (int)*cp
);
123 /* assignment: ident <- someval */
128 /* not in this line, read next */
129 /* sometimes functions are declared this way:
135 I don't know if there is a reason to write the function keyword
138 if ((line
= fileReadLine()) != NULL
) {
139 cp
= (const unsigned char*)line
;
144 if (strncasecmp((const char*)cp
, "function", (size_t)8) == 0) {
145 /* it's a function: ident <- function(args) */
147 vStringTerminate(name
);
148 /* if the string really exists, make a tag of it */
149 if (vStringLength(name
) > 0)
152 /* prepare for the next iteration */
159 /* skip whitespace */
163 /* collect all characters that could be a part of an identifier */
164 vStringPut(name
, (int)*cp
);
172 vStringDelete(vLine
);
176 extern parserDefinition
* RParser (void)
181 static const char *const extensions
[] = { "r", "s", "q", NULL
};
182 parserDefinition
* const def
= parserNew ("R");
187 def
->extensions
= extensions
;
189 def
->parser
= createRTags
;
191 def
->initialize
= installRRegex
;
197 /* vi:set tabstop=8 shiftwidth=4: */