Skip to content

Commit a1032d2

Browse files
authored
Merge pull request #3182 from masatake/thrift-parser
Thrift: extract C++ headers specified with cpp_include
2 parents 4dfe928 + 243bfd5 commit a1032d2

File tree

11 files changed

+106
-14
lines changed

11 files changed

+106
-14
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--sort=no
2+
--extras=+r
3+
--fields=+rln
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
iostream input.thrift /^cpp_include "iostream"$/;" h line:1 language:C++ roles:local
2+
vector input.thrift /^cpp_include 'vector'$/;" h line:2 language:C++ roles:local
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
cpp_include "iostream"
2+
cpp_include 'vector'

main/dependency.c

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ extern void linkDependencyAtInitializeParsing (depType dtype,
4343
{
4444
if (dtype == DEPTYPE_KIND_OWNER)
4545
linkKindDependency (masterKCB, slaveKCB);
46-
else if (dtype == DEPTYPE_SUBPARSER)
46+
else if (dtype == DEPTYPE_SUBPARSER || dtype == DEPTYPE_FOREIGNER)
4747
{
4848
slaveParser *s = xMalloc (1, slaveParser);
4949

@@ -118,8 +118,9 @@ extern void initializeDependencies (parserDefinition *parser,
118118
for (i = 0; i < parser->dependencyCount; i++)
119119
{
120120
parserDependency *d = parser->dependencies + i;
121-
if (d->type == DEPTYPE_SUBPARSER &&
122-
((subparser *)(d->data))->direction & SUBPARSER_SUB_RUNS_BASE)
121+
if ((d->type == DEPTYPE_SUBPARSER &&
122+
((subparser *)(d->data))->direction & SUBPARSER_SUB_RUNS_BASE)
123+
|| (d->type == DEPTYPE_FOREIGNER))
123124
{
124125
langType baseParser;
125126
baseParser = getNamedLanguage (d->upperParser, 0);
@@ -320,6 +321,9 @@ extern void subparserColprintAddSubparsers (struct colprintTable *table,
320321
pushLanguage (scb->owner);
321322
foreachSlaveParser(tmp)
322323
{
324+
if (tmp->type != DEPTYPE_SUBPARSER)
325+
continue;
326+
323327
struct colprintLine *line = colprintTableGetNewLine(table);
324328

325329
colprintLineAppendColumnCString (line, getLanguageName (tmp->id));

main/dependency.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
typedef enum eDepType {
2727
DEPTYPE_KIND_OWNER,
2828
DEPTYPE_SUBPARSER,
29+
DEPTYPE_FOREIGNER,
2930
COUNT_DEPTYPES,
3031
} depType;
3132

main/entry.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@ extern void initTagEntry (tagEntryInfo *const e, const char *const name,
139139
int kindIndex);
140140
extern void initRefTagEntry (tagEntryInfo *const e, const char *const name,
141141
int kindIndex, int roleIndex);
142+
143+
/* initForeignRefTagEntry() is for making a tag for the language X when parsing
144+
* source code of Y language.
145+
* From the view point of the language Y, we call the language X a foreign
146+
* language.
147+
*
148+
* When making a tag for a foreign with this function, you must declare the
149+
* language X in the parser of Y with DEPTYPE_FOREIGNER dependency.
150+
*/
142151
extern void initForeignRefTagEntry (tagEntryInfo *const e, const char *const name,
143152
langType type,
144153
int kindIndex, int roleIndex);

main/parse.c

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1865,19 +1865,37 @@ static void linkDependenciesAtInitializeParsing (parserDefinition *const parser)
18651865
unsigned int i;
18661866
parserDependency *d;
18671867
langType upper;
1868+
parserDefinition *lowerParser;
18681869
parserObject *upperParser;
18691870

18701871
for (i = 0; i < parser->dependencyCount; i++)
18711872
{
18721873
d = parser->dependencies + i;
1873-
upper = getNamedLanguage (d->upperParser, 0);
1874+
1875+
if (d->type == DEPTYPE_FOREIGNER)
1876+
{
1877+
upper = parser->id;
1878+
langType lower = getNamedLanguage (d->upperParser, 0);
1879+
if (lower == LANG_IGNORE)
1880+
error (FATAL,
1881+
"Unknown language: \"%s\" as a foreigner for %s",
1882+
d->upperParser, parser->name);
1883+
1884+
lowerParser = LanguageTable [lower].def;
1885+
}
1886+
else
1887+
{
1888+
upper = getNamedLanguage (d->upperParser, 0);
1889+
lowerParser = parser;
1890+
}
1891+
18741892
upperParser = LanguageTable + upper;
18751893

18761894
linkDependencyAtInitializeParsing (d->type, upperParser->def,
18771895
upperParser->slaveControlBlock,
18781896
upperParser->kindControlBlock,
1879-
parser,
1880-
(LanguageTable + parser->id)->kindControlBlock,
1897+
lowerParser,
1898+
(LanguageTable + lowerParser->id)->kindControlBlock,
18811899
d->data);
18821900
}
18831901
}
@@ -3743,11 +3761,39 @@ static rescanReason createTagsForFile (const langType language,
37433761

37443762
extern void notifyLanguageRegexInputStart (langType language)
37453763
{
3746-
notifyRegexInputStart((LanguageTable + language)->lregexControlBlock);
3764+
parserObject *pobj = LanguageTable + language;
3765+
parserDefinition *pdef = pobj->def;
3766+
3767+
notifyRegexInputStart(pobj->lregexControlBlock);
3768+
for (unsigned int i = 0; i < pdef->dependencyCount; i++)
3769+
{
3770+
parserDependency *d = pdef->dependencies + i;
3771+
if (d->type != DEPTYPE_FOREIGNER)
3772+
continue;
3773+
langType foreigner = getNamedLanguage (d->upperParser, 0);
3774+
if (foreigner == LANG_IGNORE)
3775+
continue;
3776+
3777+
notifyLanguageRegexInputStart (foreigner);
3778+
}
37473779
}
37483780

37493781
extern void notifyLanguageRegexInputEnd (langType language)
37503782
{
3783+
parserObject *pobj = LanguageTable + language;
3784+
parserDefinition *pdef = pobj->def;
3785+
3786+
for (unsigned int i = 0; i < pdef->dependencyCount; i++)
3787+
{
3788+
parserDependency *d = pdef->dependencies + i;
3789+
if (d->type != DEPTYPE_FOREIGNER)
3790+
continue;
3791+
langType foreigner = getNamedLanguage (d->upperParser, 0);
3792+
if (foreigner == LANG_IGNORE)
3793+
continue;
3794+
3795+
notifyLanguageRegexInputEnd (foreigner);
3796+
}
37513797
notifyRegexInputEnd((LanguageTable + language)->lregexControlBlock);
37523798
}
37533799

parsers/rpmspec.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "routines.h"
3232
#include "trace.h"
3333

34+
#include "dependency.h"
3435
#include "autoconf.h"
3536

3637
typedef enum {
@@ -385,10 +386,17 @@ extern parserDefinition* RpmSpecParser (void)
385386
"rpm-spec", /* the mode name in Emacs */
386387
NULL };
387388
parserDefinition* const def = parserNew ("RpmSpec");
389+
390+
static parserDependency dependencies [] = {
391+
[0] = { DEPTYPE_FOREIGNER, "Autoconf", NULL },
392+
};
393+
388394
def->kindTable = RpmSpecKinds;
389395
def->kindCount = ARRAY_SIZE (RpmSpecKinds);
390396
def->extensions = extensions;
391397
def->aliases = aliases;
398+
def->dependencies = dependencies;
399+
def->dependencyCount = ARRAY_SIZE (dependencies);
392400
def->initialize = initializeRpmSpecParser;
393401
def->parser = findRpmSpecTags;
394402
def->method = METHOD_REGEX;

peg/thrift.peg

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,23 @@ Grammar <- __ ( Statement __ )* (EOF / SyntaxError)
5555
SyntaxError <- .
5656

5757
# MODIFIED
58-
Include <- <( "include" / "cpp_include" )> _ <Literal> EOS {{
59-
if ($1[0] == 'i')
58+
Include <- <( "include" / "cpp_include" )> _ <Literal> EOS {
59+
vString *f = unliteral ($2);
60+
if (vStringLength(f) > 0)
6061
{
61-
vString *f = unliteral ($2);
62-
if (vStringLength(f) > 0)
62+
if ($1[0] == 'i')
6363
makeThriftTagFull (auxil, vStringValue (f), $2s, K_THRIFTFILE, THRIFT_THRIFT_FILE_INCLUDED, false);
64-
vStringDelete (f);
64+
else
65+
{
66+
tagEntryInfo e;
67+
initForeignRefTagEntry (&e, vStringValue(f), getNamedLanguage ("C++", 0), CXXTagKindINCLUDE, CR_HEADER_LOCAL);
68+
e.lineNumber = getInputLineNumberForFileOffset ($2s);
69+
e.filePosition = getInputFilePositionForLine (e.lineNumber);
70+
makeTagEntry (&e);
71+
}
6572
}
66-
/* TODO: cpp_include */
67-
}}
73+
vStringDelete (f);
74+
}
6875

6976
Statement <- Include / Namespace / Const / Enum / TypeDef / Struct / Exception / Union / Service
7077

peg/thrift_post.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "options.h"
1515
#include "parse.h"
1616
#include "routines.h"
17+
#include "dependency.h"
1718

1819
static int makeThriftTagFull (struct parserCtx *auxil, const char *name, long offset, int kind, int role,
1920
bool pushScope)
@@ -99,11 +100,18 @@ extern parserDefinition* ThriftParser (void)
99100
{
100101
static const char *const extensions [] = { "thrift", NULL };
101102
parserDefinition* def = parserNew ("Thrift");
103+
104+
static parserDependency dependencies [] = {
105+
[0] = { DEPTYPE_FOREIGNER, "C++", NULL },
106+
};
107+
102108
def->kindTable = ThriftKinds;
103109
def->kindCount = ARRAY_SIZE (ThriftKinds);
104110
def->fieldTable = ThriftFields;
105111
def->fieldCount = ARRAY_SIZE (ThriftFields);
106112
def->extensions = extensions;
113+
def->dependencies = dependencies;
114+
def->dependencyCount = ARRAY_SIZE (dependencies);
107115
def->parser = findThriftTags;
108116
def->useCork = true;
109117
def->enabled = true;

0 commit comments

Comments
 (0)