Skip to content

Commit c2cc60d

Browse files
committed
Prolog: new parser
Close #2628. Signed-off-by: Masatake YAMATO <[email protected]>
1 parent 46ea1f1 commit c2cc60d

File tree

16 files changed

+511
-2
lines changed

16 files changed

+511
-2
lines changed

Tmain/extras-long.d/stdout-expected.txt

+5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ z nulltag no NONE no Include tags with empty st
1515
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
1616
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
1717
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
18+
- arityAppended yes Prolog no Include predicates with their arises
1819
- funcmap yes QemuHX no Include mapping SQMP to C function name
1920
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
2021
# enabling 1
@@ -34,6 +35,7 @@ z nulltag no NONE no Include tags with empty st
3435
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
3536
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
3637
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
38+
- arityAppended yes Prolog no Include predicates with their arises
3739
- funcmap yes QemuHX no Include mapping SQMP to C function name
3840
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
3941
# disabling 1
@@ -53,6 +55,7 @@ z nulltag no NONE no Include tags with empty st
5355
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
5456
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
5557
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
58+
- arityAppended yes Prolog no Include predicates with their arises
5659
- funcmap yes QemuHX no Include mapping SQMP to C function name
5760
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
5861
# combination
@@ -72,6 +75,7 @@ z nulltag no NONE no Include tags with empty st
7275
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
7376
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
7477
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
78+
- arityAppended yes Prolog no Include predicates with their arises
7579
- funcmap yes QemuHX no Include mapping SQMP to C function name
7680
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
7781
# combination with letters
@@ -91,5 +95,6 @@ z nulltag no NONE no Include tags with empty st
9195
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
9296
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
9397
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
98+
- arityAppended yes Prolog no Include predicates with their arises
9499
- funcmap yes QemuHX no Include mapping SQMP to C function name
95100
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars

Tmain/list-extras.d/stdout-expected.txt

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ z nulltag yes NONE no Include tags with empty st
1515
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
1616
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
1717
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
18+
- arityAppended yes Prolog no Include predicates with their arises
1819
- funcmap yes QemuHX no Include mapping SQMP to C function name
1920
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
2021
#LETTER NAME ENABLED LANGUAGE FIXED DESCRIPTION
@@ -34,6 +35,7 @@ z nulltag yes NONE no Include tags with empty strings as their names
3435
- configPrefixed yes Kconfig no prepend CONFIG_ to config names
3536
- CppDef no Make no Include FOO in -DFOO as as a name of CPreProcessor macro
3637
- guessedFromFileName yes PkgConfig no the guessed package name of the .pc file
38+
- arityAppended yes Prolog no Include predicates with their arises
3739
- funcmap yes QemuHX no Include mapping SQMP to C function name
3840
- whitespaceSwapped yes Robot no Include tags swapping whitespace and underscore chars
3941
#LETTER NAME ENABLED LANGUAGE FIXED DESCRIPTION

Tmain/list-fields-with-prefix.d/stdout-expected.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ x UCTAGSxpath no NONE s-- no -- xpath for
4141
- UCTAGSprotocols yes ObjectiveC s-- no -- protocols that the class (or category) confirms to
4242
- UCTAGShome yes Passwd s-- no rw home directory
4343
- UCTAGSshell yes Passwd s-- no rw login shell
44+
- UCTAGSarity yes Prolog -i- no rw arity of predicate
4445
- UCTAGSdecorators no Python s-- no -- decorators on functions and classes
4546
- UCTAGSnameref yes Python s-- no -- the original name for the tag
4647
- UCTAGSassignmentop no R s-- no -- operator for assignment

Tmain/list-fields.d/stdout-expected.txt

+1
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ z kind no NONE s-- no r- [tags output] prepend "kind:" to k/ (or K/) field outpu
5959
- protocols yes ObjectiveC s-- no -- protocols that the class (or category) confirms to
6060
- home yes Passwd s-- no rw home directory
6161
- shell yes Passwd s-- no rw login shell
62+
- arity yes Prolog -i- no rw arity of predicate
6263
- decorators no Python s-- no -- decorators on functions and classes
6364
- nameref yes Python s-- no -- the original name for the tag
6465
- assignmentop no R s-- no -- operator for assignment
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-G
2+
--sort=no
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
mother_child input.pl /^mother_child(trude, sally).$/;" p arity:0
2+
mother_child/0 input.pl /^mother_child(trude, sally).$/;" p
3+
father_child input.pl /^father_child(tom, sally).$/;" p arity:0
4+
father_child/0 input.pl /^father_child(tom, sally).$/;" p
5+
father_child input.pl /^father_child(tom, erica).$/;" p arity:0
6+
father_child/0 input.pl /^father_child(tom, erica).$/;" p
7+
father_child input.pl /^father_child(mike, tom).$/;" p arity:0
8+
father_child/0 input.pl /^father_child(mike, tom).$/;" p
9+
sibling input.pl /^sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).$/;" p arity:2
10+
X input.pl /^sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).$/;" v predicate:sibling
11+
Y input.pl /^sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).$/;" v predicate:sibling
12+
sibling/2 input.pl /^sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).$/;" p
13+
parent_child input.pl /^parent_child(X, Y) :- father_child(X, Y).$/;" p arity:2
14+
X input.pl /^parent_child(X, Y) :- father_child(X, Y).$/;" v predicate:parent_child
15+
Y input.pl /^parent_child(X, Y) :- father_child(X, Y).$/;" v predicate:parent_child
16+
parent_child/2 input.pl /^parent_child(X, Y) :- father_child(X, Y).$/;" p
17+
parent_child input.pl /^parent_child(X, Y) :- mother_child(X, Y).$/;" p arity:2
18+
X input.pl /^parent_child(X, Y) :- mother_child(X, Y).$/;" v predicate:parent_child
19+
Y input.pl /^parent_child(X, Y) :- mother_child(X, Y).$/;" v predicate:parent_child
20+
parent_child/2 input.pl /^parent_child(X, Y) :- mother_child(X, Y).$/;" p
21+
not_dummy input.pl /^not_dummy().$/;" p arity:0
22+
not_dummy/0 input.pl /^not_dummy().$/;" p
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
%% Taken from https://en.wikipedia.org/wiki/Prolog
2+
mother_child(trude, sally).
3+
4+
father_child(tom, sally).
5+
father_child(tom, erica).
6+
father_child(mike, tom).
7+
8+
sibling(X, Y) :- parent_child(Z, X), parent_child(Z, Y).
9+
10+
parent_child(X, Y) :- father_child(X, Y).
11+
parent_child(X, Y) :- mother_child(X, Y).
12+
13+
% dummy0()
14+
/* dummy1() */
15+
not_dummy().

main/parsers_p.h

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
PkgConfigParser, \
148148
PodParser, \
149149
PowerShellParser, \
150+
PrologParser, \
150151
ProtobufParser, \
151152
PuppetManifestParser, \
152153
PythonParser, \

main/selectors.c

+60
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ static const char *TR_FORTRAN = "Fortran";
4343
static const char *TR_V = "V";
4444
static const char *TR_VERILOG = "Verilog";
4545

46+
static const char *TR_PROLOG = "Prolog";
47+
4648
#define startsWith(line,prefix) \
4749
(strncmp(line, prefix, strlen(prefix)) == 0? true: false)
4850

@@ -422,6 +424,64 @@ selectVOrVerilogByKeywords (MIO *input,
422424
return TR_UNKNOWN;
423425
}
424426

427+
struct PerlOrPrologScore {
428+
int perl;
429+
int prolog;
430+
unsigned int linum;
431+
};
432+
433+
static const char *
434+
tastePerlOrPrologLines (const char *line, void *data)
435+
{
436+
struct PerlOrPrologScore *score = (struct PerlOrPrologScore *)data;
437+
438+
score->linum++;
439+
440+
if (score->linum > 16)
441+
return TR_BREAK;
442+
443+
if (line[0] == '#' && line[1] != '!')
444+
{
445+
/* This is not a shebang: the comment line of perl */
446+
score->perl++;
447+
}
448+
else if (line[0] == '%')
449+
{
450+
score->prolog++;
451+
}
452+
else if (startsWith (line, "use "))
453+
{
454+
score->perl++;
455+
}
456+
else if (startsWith (line, ":-"))
457+
score->prolog+=2;
458+
else if (strstr (line, ":-"))
459+
score->prolog++;
460+
else if (strstr (line, "/*"))
461+
score->prolog++;
462+
463+
return TR_UNKNOWN;
464+
}
465+
466+
const char *
467+
selectPerlOrPrologByDistinctiveToken (struct _MIO *input, langType *candidates, unsigned int nCandidates)
468+
{
469+
struct PerlOrPrologScore score = {
470+
.perl = 0,
471+
.prolog = 0,
472+
.linum = 0,
473+
};
474+
selectByLines (input, tastePerlOrPrologLines, TR_UNKNOWN, &score);
475+
476+
int d = score.perl - score.prolog;
477+
if (d > 0)
478+
return TR_PERL5;
479+
else if (d < 0)
480+
return TR_PROLOG;
481+
else
482+
return TR_UNKNOWN;
483+
}
484+
425485
#ifdef HAVE_LIBXML
426486
#include <libxml/parser.h>
427487
#include <libxml/xpath.h>

main/selectors.h

+3
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,7 @@ selectByXpathFileSpec (struct _MIO *input, langType *candidates, unsigned int nC
3939
const char *
4040
selectFortranOrForthByForthMarker (struct _MIO *input, langType *candidates, unsigned int nCandidates);
4141

42+
const char *
43+
selectPerlOrPrologByDistinctiveToken (struct _MIO *input, langType *candidates, unsigned int nCandidates);
44+
4245
#endif

0 commit comments

Comments
 (0)