Skip to content

Commit 14bd4a5

Browse files
committed
a006
1 parent 3552feb commit 14bd4a5

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

runtime/Cpp/tests/ATestTraits.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ namespace Antlr3Test {
2323
class a003Lexer; class a003Parser;
2424
class a004Lexer; class a004Parser;
2525
class a005Lexer; class a005Parser;
26+
class a006Lexer; class a006Parser;
2627

2728
// Instantiate the Traits class(will be used for Lexer/Parser template instantiations)
2829
typedef antlr3::Traits<a001Lexer, a001Parser, UserTraits> a001LexerTraits; typedef a001LexerTraits a001ParserTraits;
2930
typedef antlr3::Traits<a002Lexer, a002Parser, UserTraits> a002LexerTraits; typedef a002LexerTraits a002ParserTraits;
3031
typedef antlr3::Traits<a003Lexer, a003Parser, UserTraits> a003LexerTraits; typedef a003LexerTraits a003ParserTraits;
3132
typedef antlr3::Traits<a004Lexer, a004Parser, UserTraits> a004LexerTraits; typedef a004LexerTraits a004ParserTraits;
3233
typedef antlr3::Traits<a005Lexer, a005Parser, UserTraits> a005LexerTraits; typedef a005LexerTraits a005ParserTraits;
34+
typedef antlr3::Traits<a006Lexer, a006Parser, UserTraits> a006LexerTraits; typedef a006LexerTraits a006ParserTraits;
3335
};
3436

3537
#endif

runtime/Cpp/tests/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ STGS = $(wildcard ../../../tool/src/main/resources/org/antlr/codegen/templates/
1515

1616
INCLUDES= -I. -I../include/
1717

18+
#-I/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/ -I/usr/lib/gcc/x86_64-pc-cygwin/4.8.3/include/c++/x86_64-pc-cygwin
19+
1820
#CXX=/usr/bin/clang++
1921
CFLAGS=-ggdb3 -O0 -fno-inline -Wall -std=c++0x
2022
#CFLAGS=-ggdb3 -O3 -std=c++0x
@@ -47,6 +49,9 @@ a004: a004.cpp a004.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS)
4749
a005: a005.cpp a005.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS)
4850
$(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@
4951

52+
a006: a006.cpp a006.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS)
53+
$(CXX) $(CFLAGS) -DUSESTL $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@
54+
5055
s001: s001.cpp s001.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS)
5156
$(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) utils.cpp -o $@
5257

runtime/Cpp/tests/a006.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#include "utils.hpp"
2+
#include "ATestTraits.hpp"
3+
#include "a006Lexer.hpp"
4+
#include "a006Parser.hpp"
5+
6+
#include <iostream>
7+
#include <sstream>
8+
#include <fstream>
9+
10+
using namespace Antlr3Test;
11+
using namespace std;
12+
13+
void test1(const char* input);
14+
void test2(const char* input);
15+
void test3(const char* input);
16+
17+
int
18+
main (int argc, char *argv[])
19+
{
20+
test1("ABC");
21+
test1("(ABC)");
22+
test1("ABC AS 5");
23+
test1("ABC AS(1,2,3,4,5,6)");
24+
test1("(ABC,ABD,ABE,ABF)AS(1,2,3,4,5,6)");
25+
26+
test2("ABC");
27+
test2("(ABC)");
28+
test2("ABC AS 5");
29+
test2("ABC AS(1,2,3,4,5,6)");
30+
test2("(ABC,ABD,ABE,ABF)AS(1,2,3,4,5,6)");
31+
32+
test3("SAMPLE (4)");
33+
test3("SAMPLE BLOCK(4,5)");
34+
35+
printf("finished parsing OK\n"); // Finnish parking is pretty good - I think it is all the snow
36+
37+
return 0;
38+
}
39+
40+
void test1(const char* input)
41+
{
42+
a006LexerTraits::InputStreamType* istream = new a006LexerTraits::InputStreamType((const ANTLR_UINT8 *)input
43+
, ANTLR_ENC_8BIT
44+
, strlen(input)
45+
, (ANTLR_UINT8*)"test1");
46+
istream->setUcaseLA(true);
47+
48+
a006Lexer* lxr = new a006Lexer(istream);
49+
a006LexerTraits::TokenStreamType* tstream = new a006LexerTraits::TokenStreamType(ANTLR_SIZE_HINT, lxr->get_tokSource());
50+
a006Parser* psr = new a006Parser(tstream);
51+
{
52+
auto r1 = psr->test1();
53+
std::cout << r1.tree->toStringTree() << std::endl;
54+
}
55+
56+
delete psr;
57+
delete tstream;
58+
delete lxr;
59+
delete istream;
60+
}
61+
62+
void test2(const char* input)
63+
{
64+
a006LexerTraits::InputStreamType* istream = new a006LexerTraits::InputStreamType((const ANTLR_UINT8 *)input
65+
, ANTLR_ENC_8BIT
66+
, strlen(input)
67+
, (ANTLR_UINT8*)"test2");
68+
istream->setUcaseLA(true);
69+
70+
a006Lexer* lxr = new a006Lexer(istream);
71+
a006LexerTraits::TokenStreamType* tstream = new a006LexerTraits::TokenStreamType(ANTLR_SIZE_HINT, lxr->get_tokSource());
72+
a006Parser* psr = new a006Parser(tstream);
73+
{
74+
auto r1 = psr->test2();
75+
std::cout << r1.tree->toStringTree() << std::endl;
76+
}
77+
78+
delete psr;
79+
delete tstream;
80+
delete lxr;
81+
delete istream;
82+
}
83+
84+
void test3(const char* input)
85+
{
86+
a006LexerTraits::InputStreamType* istream = new a006LexerTraits::InputStreamType((const ANTLR_UINT8 *)input
87+
, ANTLR_ENC_8BIT
88+
, strlen(input)
89+
, (ANTLR_UINT8*)"test3");
90+
istream->setUcaseLA(true);
91+
92+
a006Lexer* lxr = new a006Lexer(istream);
93+
a006LexerTraits::TokenStreamType* tstream = new a006LexerTraits::TokenStreamType(ANTLR_SIZE_HINT, lxr->get_tokSource());
94+
a006Parser* psr = new a006Parser(tstream);
95+
{
96+
auto r1 = psr->test3();
97+
std::cout << r1.tree->toStringTree() << std::endl;
98+
}
99+
100+
delete psr;
101+
delete tstream;
102+
delete lxr;
103+
delete istream;
104+
}

runtime/Cpp/tests/a006.g

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
grammar a006;
2+
3+
options {
4+
language=Cpp;
5+
output=AST;
6+
}
7+
8+
tokens {
9+
T_A = 'token A';
10+
P_R = 'token pivot root';
11+
S_R = 'token sample root';
12+
E_R = 'token expression root';
13+
}
14+
15+
@lexer::includes
16+
{
17+
#include "ATestTraits.hpp"
18+
}
19+
@lexer::namespace
20+
{ Antlr3Test }
21+
22+
@parser::includes {
23+
#include "ATestTraits.hpp"
24+
#include "a006Lexer.hpp"
25+
}
26+
@parser::namespace
27+
{ Antlr3Test }
28+
29+
test1 // see unpivot_in_elements
30+
: ( column_name
31+
| '(' column_name (',' column_name)* ')'
32+
)
33+
( 'AS'
34+
( constant
35+
| ('(')=> '(' constant (',' constant)* ')'
36+
)
37+
)?
38+
-> column_name+ ^(P_R constant*)
39+
;
40+
41+
test2 // see unpivot_in_elements
42+
: ( column_name
43+
| '(' column_name (',' column_name)* ')'
44+
)
45+
( 'AS'
46+
( constant
47+
| ('(')=> '(' constant (',' constant)* ')'
48+
)
49+
)?
50+
-> column_name+ ^(P_R constant)*
51+
;
52+
53+
54+
test3 //sample_clause
55+
: s='SAMPLE' 'BLOCK'?
56+
'(' c1=constant (',' c2=constant)? ')'
57+
-> ^(S_R[$s] 'BLOCK'? ^(E_R $c1) ^(E_R $c2)?)
58+
;
59+
60+
column_name
61+
: T_COLUMN_NAME;
62+
63+
constant
64+
: T_CONSTANT;
65+
66+
T_COLUMN_NAME
67+
: ('A'..'Z')+;
68+
69+
T_CONSTANT
70+
: ('0'..'9')+;
71+
72+
WS
73+
: ' '+ { $channel = HIDDEN; };

0 commit comments

Comments
 (0)