-
Notifications
You must be signed in to change notification settings - Fork 630
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The parser supports "{r label}" syntax as described in https://bookdown.org/yihui/rmarkdown/. "{r" part is parsed to choose a guest. "label" in "label}" part is tagged as a chunkLabel kind object. Signed-off-by: Masatake YAMATO <[email protected]>
- Loading branch information
Showing
12 changed files
with
248 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
--sort=no | ||
--extras=+g | ||
--fields=+KenlE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
S1 input.rmd /^# S1$/;" chapter line:1 language:Markdown end:14 | ||
xyX input.rmd /^```{r xyX}$/;" chunklabel line:3 language:RMarkdown extras:subparser | ||
S2 input.rmd /^# S2$/;" chapter line:15 language:Markdown end:25 | ||
__anon4a45a9700100 input.rmd /^```{r, cache = TRUE, dependson = "xyX"}$/;" chunklabel line:17 language:RMarkdown extras:subparser,anonymous | ||
__anon4a45a9700200 input.rmd /^```{python}$/;" chunklabel line:21 language:RMarkdown extras:subparser,anonymous | ||
S3 input.rmd /^# S3$/;" chapter line:26 language:Markdown end:27 | ||
x input.rmd /^x <- 1$/;" globalVar line:5 language:R extras:guest end:5 | ||
foo input.rmd /^foo <- function () {$/;" function line:6 language:R extras:guest end:9 | ||
y input.rmd /^ y <- 2$/;" functionVar line:7 language:R function:foo extras:guest end:7 | ||
X input.rmd /^X <- func()$/;" globalVar line:11 language:R extras:guest end:11 | ||
f input.rmd /^def f():$/;" function line:22 language:Python extras:guest end:24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# S1 | ||
|
||
```{r xyX} | ||
x <- 1 | ||
foo <- function () { | ||
y <- 2 | ||
return(y) | ||
} | ||
X <- func() | ||
``` | ||
|
||
# S2 | ||
|
||
```{r, cache = TRUE, dependson = "xyX"} | ||
mean(X) | ||
``` | ||
|
||
```{python} | ||
def f(): | ||
g() | ||
return 3 | ||
``` | ||
# S3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
* Copyright (c) 2022, Masatake YAMATO | ||
* | ||
* 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. | ||
* | ||
* The interface for subparsers of Markdown | ||
*/ | ||
#ifndef CTAGS_PARSER_MARKDOWN_H | ||
#define CTAGS_PARSER_MARKDOWN_H | ||
|
||
/* | ||
* INCLUDE FILES | ||
*/ | ||
#include "general.h" /* must always come first */ | ||
|
||
#include "subparser.h" | ||
#include "vstring.h" | ||
|
||
typedef struct sMarkdownSubparser markdownSubparser; | ||
|
||
struct sMarkdownSubparser { | ||
subparser subparser; | ||
bool (* extractLanguageForCodeBlock) (markdownSubparser *s, | ||
const char *langMarker, | ||
vString *langName); | ||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022, Masatake YAMATO | ||
* | ||
* 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 Markdown files. | ||
* https://bookdown.org/yihui/rmarkdown/ | ||
* | ||
*/ | ||
|
||
/* | ||
* INCLUDE FILES | ||
*/ | ||
#include "general.h" /* must always come first */ | ||
#include "markdown.h" | ||
|
||
#include "entry.h" | ||
#include "parse.h" | ||
|
||
#include <ctype.h> | ||
#include <string.h> | ||
|
||
/* | ||
* DATA DEFINITIONS | ||
*/ | ||
typedef enum { | ||
K_CHUNK_LABEL = 0, | ||
} rmarkdownKind; | ||
|
||
static kindDefinition RMarkdownKinds[] = { | ||
{ true, 'l', "chunklabel", "chunk labels"}, | ||
}; | ||
|
||
struct sRMarkdownSubparser { | ||
markdownSubparser markdown; | ||
}; | ||
|
||
/* | ||
* FUNCTION DEFINITIONS | ||
*/ | ||
|
||
static void findRMarkdownTags (void) | ||
{ | ||
scheduleRunningBaseparser (0); | ||
} | ||
|
||
#define skip_space(CP) while (*CP == ' ' || *CP == '\t') CP++; | ||
|
||
static void makeRMarkdownTag (vString *name, int kindIndex, bool anonymous) | ||
{ | ||
tagEntryInfo e; | ||
initTagEntry (&e, vStringValue (name), kindIndex); | ||
if (anonymous) | ||
markTagExtraBit (&e, XTAG_ANONYMOUS); | ||
makeTagEntry (&e); | ||
} | ||
|
||
static bool extractLanguageForCodeBlock (markdownSubparser *s, | ||
const char *langMarker, | ||
vString *langName) | ||
{ | ||
const char *cp = langMarker; | ||
|
||
if (*cp != '{') | ||
return false; | ||
cp++; | ||
|
||
const char *end = strpbrk(cp, " \t,}"); | ||
if (!end) | ||
return false; | ||
|
||
if (end - cp == 0) | ||
return false; | ||
|
||
vStringNCatS (langName, cp, end - cp); | ||
|
||
cp = end; | ||
if (*cp == ',' || *cp == '}') | ||
{ | ||
vString *name = anonGenerateNew("__anon", K_CHUNK_LABEL); | ||
makeRMarkdownTag (name, K_CHUNK_LABEL, true); | ||
vStringDelete (name); | ||
return true; | ||
} | ||
|
||
skip_space(cp); | ||
|
||
vString *chunk_label = vStringNew (); | ||
bool anonymous = false; | ||
while (isalnum((unsigned char)*cp) || *cp == '-') | ||
vStringPut (chunk_label, *cp++); | ||
|
||
if (vStringLength (chunk_label) == 0) | ||
{ | ||
anonGenerate (chunk_label, "__anon", K_CHUNK_LABEL); | ||
anonymous = true; | ||
} | ||
|
||
skip_space(cp); | ||
if (*cp == ',' || *cp == '}') | ||
makeRMarkdownTag (chunk_label, K_CHUNK_LABEL, anonymous); | ||
|
||
vStringDelete (chunk_label); | ||
return true; | ||
} | ||
|
||
extern parserDefinition* RMarkdownParser (void) | ||
{ | ||
static const char *const extensions [] = { "rmd", NULL }; | ||
static struct sRMarkdownSubparser rmarkdownSubparser = { | ||
.markdown = { | ||
.subparser = { | ||
.direction = SUBPARSER_SUB_RUNS_BASE, | ||
}, | ||
.extractLanguageForCodeBlock = extractLanguageForCodeBlock, | ||
}, | ||
}; | ||
static parserDependency dependencies [] = { | ||
[0] = { DEPTYPE_SUBPARSER, "Markdown", &rmarkdownSubparser }, | ||
}; | ||
|
||
parserDefinition* const def = parserNew ("RMarkdown"); | ||
|
||
|
||
def->dependencies = dependencies; | ||
def->dependencyCount = ARRAY_SIZE(dependencies); | ||
def->kindTable = RMarkdownKinds; | ||
def->kindCount = ARRAY_SIZE (RMarkdownKinds); | ||
def->extensions = extensions; | ||
def->parser = findRMarkdownTags; | ||
return def; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters