Skip to content

Commit

Permalink
r: Update and enable regex parser
Browse files Browse the repository at this point in the history
Changes are based on this StackOverflow question:
http://stackoverflow.com/questions/32206608/ctags-and-r-regex

* Add two new types: global variables and function variables.
* Require a '(' after the function in order to guarantee distinction
  between variable and function declarations.
* Add description to all tag types.
* Add test cases.
  • Loading branch information
vhda committed Sep 3, 2015
1 parent e8b3358 commit 3733de2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Units/parser-r.r/r-extended.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.First input.r /^.First <- function() {$/;" f
MASS input.r /^ library(MASS) # attach a package$/;" s
11 changes: 11 additions & 0 deletions Units/parser-r.r/r-extended.d/input.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# From:
# https://cran.r-project.org/doc/manuals/r-release/R-intro.html#Customizing-the-environment
.First <- function() {
options(prompt="$ ", continue="+\t") # $ is the prompt
options(digits=5, length=999) # custom numbers and printout
x11() # for graphics
par(pch = "+") # plotting character
source(file.path(Sys.getenv("HOME"), "R", "mystuff.R"))
# my personal functions
library(MASS) # attach a package
}
3 changes: 3 additions & 0 deletions Units/parser-r.r/r-simple.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
foo input.r /^foo <- function () {$/;" f
x input.r /^x <- 1$/;" g
y input.r /^ y <- 2$/;" v
7 changes: 7 additions & 0 deletions Units/parser-r.r/r-simple.d/input.r
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Copuied from:
# http://stackoverflow.com/questions/32206608/ctags-and-r-regex
x <- 1
foo <- function () {
y <- 2
return(y)
}
20 changes: 15 additions & 5 deletions parsers/r.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "read.h"
#include "vstring.h"

/*#define R_REGEX*/
#define R_REGEX

#define SKIPSPACE(ch) while (isspace((int)*ch)) \
ch++
Expand Down Expand Up @@ -51,12 +51,22 @@ static void installRRegex (const langType language)
* }
*/
addTagRegex (language,
"^[ \t]*([.a-zA-Z0-9_]+)([ \t]*)<-([ \t]*)function", "\\1",
"f,function", NULL);
"^[ \t]*\"?([.a-zA-Z][.a-zA-Z0-9_]+)\"?[ \t]*<-[ \t]*function[ \t]*\\(", "\\1",
"f,function,functions", NULL);
/* Global variables */
addTagRegex (language,
"^\"?([.a-zA-Z][.a-zA-Z0-9_]*)\"?[ \t]*<-[ \t][^\(]+$", "\\1",
"g,globalVar,global variables", NULL);
/* Function local variable
* Assumes that code in functions is indented
*/
addTagRegex (language,
"[ \t]\"?([.A-Za-z][.A-Za-z0-9_]*)\"?[ \t]*<-[ \t][^\(]+$", "\\1",
"v,functionVar,function variables", NULL);
/* This loads someting, e.g. a library, simply: library(libname) */
addTagRegex (language,
"^[ \t]*(library|source|load|data)[\\(]([a-zA-Z0-9_]+)[\\)]", "\\2",
"s,other", NULL);
"s,other,library/source/load/data", NULL);
}
#else
static void makeRTag (const vString * const name, rKind kind)
Expand Down Expand Up @@ -215,7 +225,7 @@ extern parserDefinition *RParser (void)
def->parser = createRTags;
#else
def->initialize = installRRegex;
def->regex = TRUE;
def->method = METHOD_NOT_CRAFTED | METHOD_REGEX;
#endif
return def;
}
Expand Down

0 comments on commit 3733de2

Please sign in to comment.