Skip to content

Commit

Permalink
a006
Browse files Browse the repository at this point in the history
  • Loading branch information
ibre5041 committed Sep 5, 2015
1 parent 3552feb commit 14bd4a5
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 0 deletions.
2 changes: 2 additions & 0 deletions runtime/Cpp/tests/ATestTraits.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ namespace Antlr3Test {
class a003Lexer; class a003Parser;
class a004Lexer; class a004Parser;
class a005Lexer; class a005Parser;
class a006Lexer; class a006Parser;

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

#endif
5 changes: 5 additions & 0 deletions runtime/Cpp/tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ STGS = $(wildcard ../../../tool/src/main/resources/org/antlr/codegen/templates/

INCLUDES= -I. -I../include/

#-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

#CXX=/usr/bin/clang++
CFLAGS=-ggdb3 -O0 -fno-inline -Wall -std=c++0x
#CFLAGS=-ggdb3 -O3 -std=c++0x
Expand Down Expand Up @@ -47,6 +49,9 @@ a004: a004.cpp a004.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS)
a005: a005.cpp a005.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS)
$(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@

a006: a006.cpp a006.tokens ATestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS)
$(CXX) $(CFLAGS) -DUSESTL $(INCLUDES) $< $(wildcard $@?*.cpp) -o $@

s001: s001.cpp s001.tokens UserTestTraits.hpp Makefile $(ANTLR) $(RUNTIME_HEADERS)
$(CXX) $(CFLAGS) $(INCLUDES) $< $(wildcard $@?*.cpp) utils.cpp -o $@

Expand Down
104 changes: 104 additions & 0 deletions runtime/Cpp/tests/a006.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#include "utils.hpp"
#include "ATestTraits.hpp"
#include "a006Lexer.hpp"
#include "a006Parser.hpp"

#include <iostream>
#include <sstream>
#include <fstream>

using namespace Antlr3Test;
using namespace std;

void test1(const char* input);
void test2(const char* input);
void test3(const char* input);

int
main (int argc, char *argv[])
{
test1("ABC");
test1("(ABC)");
test1("ABC AS 5");
test1("ABC AS(1,2,3,4,5,6)");
test1("(ABC,ABD,ABE,ABF)AS(1,2,3,4,5,6)");

test2("ABC");
test2("(ABC)");
test2("ABC AS 5");
test2("ABC AS(1,2,3,4,5,6)");
test2("(ABC,ABD,ABE,ABF)AS(1,2,3,4,5,6)");

test3("SAMPLE (4)");
test3("SAMPLE BLOCK(4,5)");

printf("finished parsing OK\n"); // Finnish parking is pretty good - I think it is all the snow

return 0;
}

void test1(const char* input)
{
a006LexerTraits::InputStreamType* istream = new a006LexerTraits::InputStreamType((const ANTLR_UINT8 *)input
, ANTLR_ENC_8BIT
, strlen(input)
, (ANTLR_UINT8*)"test1");
istream->setUcaseLA(true);

a006Lexer* lxr = new a006Lexer(istream);
a006LexerTraits::TokenStreamType* tstream = new a006LexerTraits::TokenStreamType(ANTLR_SIZE_HINT, lxr->get_tokSource());
a006Parser* psr = new a006Parser(tstream);
{
auto r1 = psr->test1();
std::cout << r1.tree->toStringTree() << std::endl;
}

delete psr;
delete tstream;
delete lxr;
delete istream;
}

void test2(const char* input)
{
a006LexerTraits::InputStreamType* istream = new a006LexerTraits::InputStreamType((const ANTLR_UINT8 *)input
, ANTLR_ENC_8BIT
, strlen(input)
, (ANTLR_UINT8*)"test2");
istream->setUcaseLA(true);

a006Lexer* lxr = new a006Lexer(istream);
a006LexerTraits::TokenStreamType* tstream = new a006LexerTraits::TokenStreamType(ANTLR_SIZE_HINT, lxr->get_tokSource());
a006Parser* psr = new a006Parser(tstream);
{
auto r1 = psr->test2();
std::cout << r1.tree->toStringTree() << std::endl;
}

delete psr;
delete tstream;
delete lxr;
delete istream;
}

void test3(const char* input)
{
a006LexerTraits::InputStreamType* istream = new a006LexerTraits::InputStreamType((const ANTLR_UINT8 *)input
, ANTLR_ENC_8BIT
, strlen(input)
, (ANTLR_UINT8*)"test3");
istream->setUcaseLA(true);

a006Lexer* lxr = new a006Lexer(istream);
a006LexerTraits::TokenStreamType* tstream = new a006LexerTraits::TokenStreamType(ANTLR_SIZE_HINT, lxr->get_tokSource());
a006Parser* psr = new a006Parser(tstream);
{
auto r1 = psr->test3();
std::cout << r1.tree->toStringTree() << std::endl;
}

delete psr;
delete tstream;
delete lxr;
delete istream;
}
73 changes: 73 additions & 0 deletions runtime/Cpp/tests/a006.g
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
grammar a006;

options {
language=Cpp;
output=AST;
}

tokens {
T_A = 'token A';
P_R = 'token pivot root';
S_R = 'token sample root';
E_R = 'token expression root';
}

@lexer::includes
{
#include "ATestTraits.hpp"
}
@lexer::namespace
{ Antlr3Test }

@parser::includes {
#include "ATestTraits.hpp"
#include "a006Lexer.hpp"
}
@parser::namespace
{ Antlr3Test }

test1 // see unpivot_in_elements
: ( column_name
| '(' column_name (',' column_name)* ')'
)
( 'AS'
( constant
| ('(')=> '(' constant (',' constant)* ')'
)
)?
-> column_name+ ^(P_R constant*)
;

test2 // see unpivot_in_elements
: ( column_name
| '(' column_name (',' column_name)* ')'
)
( 'AS'
( constant
| ('(')=> '(' constant (',' constant)* ')'
)
)?
-> column_name+ ^(P_R constant)*
;


test3 //sample_clause
: s='SAMPLE' 'BLOCK'?
'(' c1=constant (',' c2=constant)? ')'
-> ^(S_R[$s] 'BLOCK'? ^(E_R $c1) ^(E_R $c2)?)
;

column_name
: T_COLUMN_NAME;

constant
: T_CONSTANT;

T_COLUMN_NAME
: ('A'..'Z')+;

T_CONSTANT
: ('0'..'9')+;

WS
: ' '+ { $channel = HIDDEN; };

0 comments on commit 14bd4a5

Please sign in to comment.