Skip to content

Commit

Permalink
Merge pull request #3839 from masatake/rst--code-block
Browse files Browse the repository at this point in the history
ReStructuredText: run guest parsers on code blocks
  • Loading branch information
masatake authored Oct 22, 2023
2 parents a792671 + a024af5 commit 5595e38
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 4 deletions.
3 changes: 3 additions & 0 deletions Units/parser-restructuredtext.r/code-blocks.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
--sort=no
--extras=+g
--fields=+lE
10 changes: 10 additions & 0 deletions Units/parser-restructuredtext.r/code-blocks.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
C Language Example input.rst /^C Language Example$/;" H language:ReStructuredText
Test 1 input.rst /^Test 1$/;" c language:ReStructuredText title:C Language Example
Test 2 input.rst /^Test 2$/;" c language:ReStructuredText title:C Language Example
test1_0 input.rst /^ int test1_0(void)$/;" f language:C typeref:typename:int extras:guest
test1_1 input.rst /^ int test1_1(void)$/;" f language:C typeref:typename:int extras:guest
test1_2 input.rst /^ int test1_2(void)$/;" f language:C typeref:typename:int extras:guest
test1_3 input.rst /^ int test1_3(void)$/;" f language:C typeref:typename:int extras:guest
test2_0 input.rst /^ int test2_0(void)$/;" f language:C typeref:typename:int extras:guest
TITLE input-1.rst /^TITLE$/;" H language:ReStructuredText
DEF input-2.rst /^ #define DEF /;" d language:C file: extras:fileScope,guest
2 changes: 2 additions & 0 deletions Units/parser-restructuredtext.r/code-blocks.d/input-0.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
No thing here.
.. code-block:: c
5 changes: 5 additions & 0 deletions Units/parser-restructuredtext.r/code-blocks.d/input-1.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.. code-block:: c
.. code-block:: c
.. code-block:: c
TITLE
---------------------
9 changes: 9 additions & 0 deletions Units/parser-restructuredtext.r/code-blocks.d/input-2.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
* an item

.. code-block:: C
#define DEF 1

int this_is_not_c_code ();

* another item
44 changes: 44 additions & 0 deletions Units/parser-restructuredtext.r/code-blocks.d/input.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
==========================
C Language Example
==========================

Test 1
---------------------

.. code-block:: c
int test1_0(void)
{
return 0;
}
int test1_1(void)
{
return 0;
}
Some descriptions here.

.. code-block:: c
int test1_2(void)
{
return 0;
}
int test1_3(void)
{
return 0;
}
Test 2
---------------------

Some descriptions here.

.. code-block:: c
int test2_0(void)
{
return 0;
}
109 changes: 105 additions & 4 deletions parsers/rst.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#include "field.h"
#include "htable.h"
#include "debug.h"
#include "promise.h"

/*
* DATA DEFINITIONS
Expand Down Expand Up @@ -94,6 +95,14 @@ struct olineTracker
size_t len;
};

struct codeblockTracker {
size_t blockIndent;
char *language;
unsigned long startLine;
unsigned long endLine;
unsigned long endLineLength;
};

/*
* FUNCTION DEFINITIONS
*/
Expand Down Expand Up @@ -217,14 +226,22 @@ static int get_kind(char c, bool overline, struct sectionTracker tracker[])
return -1;
}

static const unsigned char *is_markup_line (const unsigned char *line, char reftype)
static const unsigned char *is_markup_line_with_char (const unsigned char *line, char reftype)
{
if ((line [0] == '.') && (line [1] == '.') && (line [2] == ' ')
&& (line [3] == reftype))
return line + 4;
return NULL;
}

static const unsigned char *is_markup_line_with_cstr (const unsigned char *line, char *str, size_t len)
{
if ((line [0] == '.') && (line [1] == '.') && (line [2] == ' ')
&& (strncmp ((char *)line + 3, str, len) == 0))
return line + 3 + len;
return NULL;
}

static int capture_markup (const unsigned char *target_line, char defaultTerminator, rstKind kindex)
{
vString *name = vStringNew ();
Expand Down Expand Up @@ -300,6 +317,54 @@ static bool has_overline(struct olineTracker *ol)
return (ol->c != 0);
}

static void init_codeblock (struct codeblockTracker *codeblock, const char *language,
size_t blockIndent, unsigned long startLine)
{
codeblock->language = eStrdup (language);
codeblock->blockIndent = blockIndent;
codeblock->startLine = startLine;
codeblock->endLine = 0;
codeblock->endLineLength = 0;
}

static bool is_in_codeblock (struct codeblockTracker *codeblock)
{
return (codeblock->language != NULL);
}

static void reset_codeblock (struct codeblockTracker *codeblock)
{
eFree (codeblock->language);
codeblock->language = NULL;
}

static bool does_codeblock_continue (struct codeblockTracker *codeblock, size_t blockOffset,
unsigned char initChar)
{
if (blockOffset > codeblock->blockIndent)
return true;

if (blockOffset <= codeblock->blockIndent && initChar == '\0')
return true;

return false;
}

static void update_codeblock (struct codeblockTracker *codeblock, unsigned int endLine, unsigned long endLineLength)
{
codeblock->endLine = endLine;
codeblock->endLineLength = endLineLength;
}

static void submit_codeblock (struct codeblockTracker *codeblock)
{
if (codeblock->endLine)
makePromise (codeblock->language,
codeblock->startLine, 0,
codeblock->endLine, codeblock->endLineLength, codeblock->startLine);
reset_codeblock (codeblock);
}

static int getFosterEntry(tagEntryInfo *e, int shift)
{
int r = CORK_NIL;
Expand Down Expand Up @@ -412,6 +477,8 @@ static void findRstTags (void)
const unsigned char *markup_line;
struct sectionTracker section_tracker[SECTION_COUNT];
struct olineTracker overline;
struct codeblockTracker codeblock = { .language = NULL };
const bool run_guest = isXtagEnabled (XTAG_GUEST);

memset(&filepos, 0, sizeof(filepos));
memset(section_tracker, 0, sizeof section_tracker);
Expand All @@ -424,7 +491,18 @@ static void findRstTags (void)
while (isspace(*line_trimmed))
line_trimmed++;

if ((markup_line = is_markup_line (line_trimmed, '_')) != NULL)
if (run_guest && is_in_codeblock (&codeblock))
{
if (does_codeblock_continue (&codeblock, (line_trimmed - line), *line_trimmed))
{
update_codeblock(&codeblock, getInputLineNumber(), strlen((const char *)line));
continue;
}
else
submit_codeblock(&codeblock);
}

if ((markup_line = is_markup_line_with_char (line_trimmed, '_')) != NULL)
{
overline_clear(&overline);
/* Handle .. _target:
Expand All @@ -436,7 +514,7 @@ static void findRstTags (void)
continue;
}
}
else if ((markup_line = is_markup_line (line_trimmed, '[')) != NULL)
else if ((markup_line = is_markup_line_with_char (line_trimmed, '[')) != NULL)
{
overline_clear(&overline);
/* Handle .. [citation]
Expand All @@ -448,7 +526,7 @@ static void findRstTags (void)
continue;
}
}
else if ((markup_line = is_markup_line (line_trimmed, '|')) != NULL)
else if ((markup_line = is_markup_line_with_char (line_trimmed, '|')) != NULL)
{
overline_clear(&overline);
/* Hanle .. |substitute definition|
Expand All @@ -460,6 +538,22 @@ static void findRstTags (void)
continue;
}
}
else if (run_guest
&& (markup_line = is_markup_line_with_cstr (line_trimmed, "code-block::", 12)) != NULL)
{
if (is_in_codeblock (&codeblock))
reset_codeblock (&codeblock);

while (isspace(*markup_line))
markup_line++;

if (*markup_line)
{
init_codeblock (&codeblock, (const char *)markup_line, line_trimmed - line,
getInputLineNumber() + 1);
continue;
}
}

int line_len = strlen((const char*) line);
int name_len_bytes = vStringLength(name);
Expand Down Expand Up @@ -527,13 +621,20 @@ static void findRstTags (void)
filepos = getInputFilePosition();
}
}

if (run_guest && is_in_codeblock (&codeblock))
submit_codeblock(&codeblock);

/* Force popping all nesting levels */
getNestingLevel (K_EOF);
vStringDelete (name);
nestingLevelsFree(nestingLevels);

adjustSectionKinds(section_tracker);
inlineScopes();

if (run_guest && codeblock.language)
eFree (codeblock.language);
}

extern parserDefinition* RstParser (void)
Expand Down

0 comments on commit 5595e38

Please sign in to comment.