Skip to content

Commit

Permalink
Test 303 ---- !!! Baseline !!!
Browse files Browse the repository at this point in the history
  • Loading branch information
maxirmx committed May 25, 2022
1 parent ba1a558 commit cbeb7ed
Show file tree
Hide file tree
Showing 11 changed files with 9,087 additions and 112 deletions.
16 changes: 8 additions & 8 deletions .github/workflows/rake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: rake

on:
push:
branches: [ master, main ]
# branches: [ master, main ]
pull_request:

jobs:
Expand Down Expand Up @@ -63,15 +63,15 @@ jobs:
ruby-version: ${{ matrix.ruby }}
bundler-cache: true

- name: Process cache
uses: actions/cache@v2
id: cache
with:
path: lib/expressir/express/express_parser.*
key: v4-${{ runner.os }}-${{ matrix.ruby }}-${{ hashFiles('ext/express-parser/extconf.rb', 'ext/express-parser/antlrgen/**', 'ext/express-parser/express_parser.cpp', '.git/modules/ext/express-parser/antlr4-upstream/HEAD') }}
# - name: Process cache
# uses: actions/cache@v2
# id: cache
# with:
# path: lib/expressir/express/express_parser.*
# key: v4-${{ runner.os }}-${{ matrix.ruby }}-${{ hashFiles('ext/express-parser/extconf.rb', 'ext/express-parser/antlrgen/**', 'ext/express-parser/express_parser.cpp', '.git/modules/ext/express-parser/antlr4-upstream/HEAD') }}

- name: Build native extension
if: steps.cache.outputs.cache-hit != 'true'
# if: steps.cache.outputs.cache-hit != 'true'
run: bundle exec rake compile

- name: Run tests
Expand Down
224 changes: 123 additions & 101 deletions ext/express-parser/express_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
#include "antlrgen/ExpressBaseVisitor.h"
#include "antlrgen/ExpressLexer.h"

#include <rice/rice.hpp>
#include <rice/stl.hpp>
#include "rice-x/rice.hpp"
#include "rice-x/stl.hpp"


#ifdef _WIN32
#undef OPTIONAL
Expand Down Expand Up @@ -223,13 +224,13 @@ Class rb_cSyntaxContext;
Class rb_cUnderlyingTypeContext;
Class rb_cUniqueRuleContext;
Class rb_cWidthContext;
Class rb_cToken;
Class rb_cTokenProxy;
Class rb_cParser;
Class rb_cParseTree;
Class rb_cTerminalNode;
Class rb_cContextProxy;

namespace Rice::detail {
/*namespace Rice::detail {
template <>
class To_Ruby<Token*> {
public:
Expand Down Expand Up @@ -257,6 +258,28 @@ namespace Rice::detail {
}
};
}
*/
class TokenProxy : public Object {
public:
TokenProxy(Token* orig) {
this -> orig = orig;
}

std::string getText() {
return orig->getText();
}

size_t getChannel() {
return orig->getChannel();
}

size_t getTokenIndex() {
return orig->getTokenIndex();
}

private:
Token * orig = nullptr;
};

class ContextProxy {
public:
Expand All @@ -274,14 +297,16 @@ class ContextProxy {

Object getStart() {
auto token = ((ParserRuleContext*) orig) -> getStart();
TokenProxy proxy(token);

return detail::To_Ruby<Token*>().convert(token);
return detail::To_Ruby<TokenProxy>().convert(proxy);
}

Object getStop() {
auto token = ((ParserRuleContext*) orig) -> getStop();
TokenProxy proxy(token);

return detail::To_Ruby<Token*>().convert(token);
return detail::To_Ruby<TokenProxy>().convert(proxy);
}

Array getChildren() {
Expand Down Expand Up @@ -2028,7 +2053,7 @@ class WidthContextProxy : public ContextProxy {

};


/*
namespace Rice::detail {
template <>
class To_Ruby<ExpressParser::AttributeRefContext*> {
Expand Down Expand Up @@ -2309,7 +2334,7 @@ namespace Rice::detail {
};
}

namespace Rice::detail {
/*namespace Rice::detail {
template <>
class To_Ruby<ExpressParser::RuleLabelRefContext*> {
public:
Expand Down Expand Up @@ -6008,7 +6033,7 @@ namespace Rice::detail {
}
};
}

*/

Object AttributeRefContextProxy::attributeId() {
if (orig == nullptr) {
Expand Down Expand Up @@ -17230,91 +17255,6 @@ class VisitorProxy : public ExpressBaseVisitor, public Director {
};


class ParserProxy {
public:
static ParserProxy* parse(string code) {
auto input = new ANTLRInputStream(code);
return parseStream(input);
}

static ParserProxy* parseFile(string file) {
ifstream stream;
stream.open(file);

auto input = new ANTLRInputStream(stream);
auto parser = parseStream(input);

stream.close();

return parser;
}

Object syntax() {
auto ctx = this -> parser -> syntax();

SyntaxContextProxy proxy((ExpressParser::SyntaxContext*) ctx);
return detail::To_Ruby<SyntaxContextProxy>().convert(proxy);
}

Array getTokens() {
Array a;

std::vector<Token*> tokens = this -> tokens -> getTokens();

for (auto &token : tokens) {
a.push(token);
}

return a;
}
Object visit(VisitorProxy* visitor) {
auto result = visitor -> visit(this -> parser -> syntax());

// reset for the next visit call
this -> lexer -> reset();
this -> parser -> reset();

return std::any_cast<Object>(result);
}

~ParserProxy() {
delete this -> parser;
delete this -> tokens;
delete this -> lexer;
delete this -> input;
}

private:
static ParserProxy* parseStream(ANTLRInputStream* input) {
ParserProxy* parser = new ParserProxy();

parser -> input = input;
parser -> lexer = new ExpressLexer(parser -> input);
parser -> tokens = new CommonTokenStream(parser -> lexer);
parser -> parser = new ExpressParser(parser -> tokens);

return parser;
}

ParserProxy() {};

ANTLRInputStream* input;
ExpressLexer* lexer;
CommonTokenStream* tokens;
ExpressParser* parser;
};

namespace Rice::detail {
template <>
class To_Ruby<ParserProxy*> {
public:
VALUE convert(ParserProxy* const &x) {
if (!x) return Nil;
return Data_Object<ParserProxy>(x, false, rb_cParser);
}
};
}


Object ContextProxy::wrapParseTree(tree::ParseTree* node) {
if (antlrcpp::is<ExpressParser::AttributeRefContext*>(node)) {
Expand Down Expand Up @@ -18121,15 +18061,96 @@ Object ContextProxy::wrapParseTree(tree::ParseTree* node) {
}
}

class ParserProxy : public Object {
public:
Object syntax() {
auto ctx = this -> parser -> syntax();

SyntaxContextProxy proxy((ExpressParser::SyntaxContext*) ctx);
return detail::To_Ruby<SyntaxContextProxy>().convert(proxy);
}

Array getTokens() {
Array a;
std::vector<Token*> tokens = this -> tokens -> getTokens();
for (auto token : tokens) {
a.push(new TokenProxy(token));
}
return a;
}

Object visit(VisitorProxy* visitor) {
auto result = visitor -> visit(this -> parser -> syntax());

// reset for the next visit call
lexer -> reset();
parser -> reset();

try {
return std::any_cast<Object>(result);
} catch(std::bad_cast) {
return Qnil;
}
}

~ParserProxy() {
delete this -> parser;
delete this -> tokens;
delete this -> lexer;
delete this -> input;
}

ParserProxy(Object self, string file) {
ifstream stream;
stream.open(file);
input = new ANTLRInputStream(stream);
lexer = new ExpressLexer(input);
tokens = new CommonTokenStream(lexer);
parser = new ExpressParser(tokens);
stream.close();
};


private:
/* static ParserProxy* parseStream(ANTLRInputStream* input) {
ParserProxy* parser = new ParserProxy();

parser -> input = input;
parser -> lexer = new ExpressLexer(parser -> input);
parser -> tokens = new CommonTokenStream(parser -> lexer);
parser -> parser = new ExpressParser(parser -> tokens);

return parser;
}
*/
ANTLRInputStream* input;
ExpressLexer* lexer;
CommonTokenStream* tokens;
ExpressParser* parser;
};

/*namespace Rice::detail {
template <>
class To_Ruby<ParserProxy*> {
public:
VALUE convert(ParserProxy* const &x) {
if (!x) return Nil;
return Data_Object<ParserProxy>(x, false, rb_cParser);
}
};
}
*/

Class rb_cVisitorProxy;

extern "C"
void Init_express_parser() {
Module rb_mExpressParser = define_module("ExpressParser");

rb_cToken = define_class_under<Token>(rb_mExpressParser, "Token")
.define_method("text", &Token::getText)
.define_method("channel", &Token::getChannel)
.define_method("token_index", &Token::getTokenIndex);
rb_cTokenProxy = define_class_under<TokenProxy>(rb_mExpressParser, "Token")
.define_method("text", &TokenProxy::getText)
.define_method("channel", &TokenProxy::getChannel)
.define_method("token_index", &TokenProxy::getTokenIndex);

rb_cParseTree = define_class_under<tree::ParseTree>(rb_mExpressParser, "ParseTree");

Expand All @@ -18144,7 +18165,7 @@ void Init_express_parser() {

rb_cTerminalNode = define_class_under<TerminalNodeProxy, ContextProxy>(rb_mExpressParser, "TerminalNodeImpl");

define_class_under<ExpressBaseVisitor>(rb_mExpressParser, "Visitor")
rb_cVisitorProxy = define_class_under<ExpressBaseVisitor>(rb_mExpressParser, "Visitor")
.define_director<VisitorProxy>()
.define_constructor(Constructor<VisitorProxy, Object>())
.define_method("visit", &VisitorProxy::ruby_visit)
Expand Down Expand Up @@ -18350,8 +18371,9 @@ void Init_express_parser() {
.define_method("visit_width_spec", &VisitorProxy::ruby_visitChildren);

rb_cParser = define_class_under<ParserProxy>(rb_mExpressParser, "Parser")
.define_singleton_function("parse", &ParserProxy::parse)
.define_singleton_function("parse_file", &ParserProxy::parseFile)
// .define_singleton_function("parse", &ParserProxy::parse)
// .define_singleton_function("parse_file", &ParserProxy::parseFile)
.define_constructor(Constructor<ParserProxy, Object, string>())
.define_method("syntax", &ParserProxy::syntax, Return().keepAlive())
.define_method("tokens", &ParserProxy::getTokens)
.define_method("visit", &ParserProxy::visit, Return().keepAlive());
Expand Down
1 change: 1 addition & 0 deletions ext/express-parser/extconf.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
end

$CPPFLAGS << " -std=c++17 -DANTLR4CPP_STATIC -DHAVE_CXX11 -fno-omit-frame-pointer"

$INCFLAGS << " -I#{__dir__}/#{antlr4_src}"
$srcs = []

Expand Down
Loading

0 comments on commit cbeb7ed

Please sign in to comment.