2 * Copyright (c) 2003-2004, Ascher Stefan <stievie@utanet.at>
4 * This source code is released for free distribution under the terms of the
5 * GNU General Public License.
7 * This module contains functions for generating tags for R language files.
8 * R is a programming language for statistical computing.
9 * R is GPL Software, get it from http://www.r-project.org/
15 #include "general.h" /* must always come first */
18 #include <ctype.h> /* to define isalpha(), isalnum(), isspace() */
26 #define SKIPSPACE(ch) while (isspace((int)*ch)) \
30 static kindOption RKinds
[] = {
31 { TRUE
, 'f', "function", "functions" },
32 { TRUE
, 's', "other", "libraries" }
37 static void installRRegex (const langType language
)
39 /* This is a function, looks as follows:
40 * itent <- function(arg1, arg2) {
44 addTagRegex (language
,
45 "^[ \t]*([.a-zA-Z0-9_]+)([ \t]*)<-([ \t]*)function", "\\1", "f,function", NULL
);
46 /* This loads someting, e.g. a library, simply: library(libname) */
47 addTagRegex (language
,
48 "^[ \t]*(library|source|load|data)[\\(]([a-zA-Z0-9_]+)[\\)]", "\\2", "s,other", NULL
);
51 static void makeRTag(const vString
* const name
, int kind
)
54 initTagEntry(&e
, vStringValue(name
));
56 e
.kindName
= RKinds
[kind
].name
;
57 e
.kind
= RKinds
[kind
].letter
;
62 extern void createRTags(void)
64 vString
*vLine
= vStringNew();
65 vString
*name
= vStringNew();
67 const unsigned char *line
;
69 while ((line
= fileReadLine()) != NULL
)
71 const unsigned char *cp
= (const unsigned char*)line
;
74 while ((*cp
!= '\0') && (*cp
!= '#')) {
75 /* iterate to the end of line or to a comment */
80 if (strncasecmp((const char*)cp
, "library", (size_t)7) == 0) {
81 /* load a library: library(tools) */
88 } else if (strncasecmp((const char*)cp
, "source", (size_t)6) == 0) {
89 /* load a source file: source("myfile.r") */
101 while ((!isspace((int)*cp
)) && *cp
!= '\0' && *cp
!= ')') {
102 vStringPut(name
, (int)*cp
);
105 vStringTerminate(name
);
107 /* if the string really exists, make a tag of it */
108 if (vStringLength(name
) > 0)
109 makeRTag(name
, ikind
);
111 /* prepare for the next iteration */
114 vStringPut(name
, (int)*cp
);
121 /* assignment: ident <- someval */
126 /* not in this line, read next */
127 /* sometimes functions are declared this way:
133 I don't know if there is a reason to write the function keyword
136 if ((line
= fileReadLine()) != NULL
) {
137 cp
= (const unsigned char*)line
;
142 if (strncasecmp((const char*)cp
, "function", (size_t)8) == 0) {
143 /* it's a function: ident <- function(args) */
145 vStringTerminate(name
);
146 /* if the string really exists, make a tag of it */
147 if (vStringLength(name
) > 0)
150 /* prepare for the next iteration */
157 /* skip whitespace */
161 /* collect all characters that could be a part of an identifier */
162 vStringPut(name
, (int)*cp
);
170 vStringDelete(vLine
);
174 extern parserDefinition
* RParser (void)
179 static const char *const extensions
[] = { "r", "s", "q", NULL
};
180 parserDefinition
* const def
= parserNew ("R");
185 def
->extensions
= extensions
;
187 def
->parser
= createRTags
;
189 def
->initialize
= installRRegex
;
195 /* vi:set tabstop=8 shiftwidth=4: */