Changed the way to handle default tag in AST.

pull/3/head
yhirose 9 years ago
parent e0c657e52c
commit b91efa1489
  1. 51
      language/parser.cc
  2. 2
      language/parser.hpp
  3. 16
      peglib.h

@ -4,6 +4,7 @@ using namespace peglib;
using namespace std; using namespace std;
static auto g_grammar = R"( static auto g_grammar = R"(
PROGRAM <- _ STATEMENTS PROGRAM <- _ STATEMENTS
STATEMENTS <- (EXPRESSION (';' _)?)* STATEMENTS <- (EXPRESSION (';' _)?)*
@ -53,12 +54,12 @@ static auto g_grammar = R"(
EndOfLine <- '\r\n' / '\n' / '\r' EndOfLine <- '\r\n' / '\n' / '\r'
EndOfFile <- !. EndOfFile <- !.
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile) Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
)"; )";
peg& get_parser() peg& get_parser()
{ {
static peg parser; static peg parser;
static bool initialized = false; static bool initialized = false;
if (!initialized) { if (!initialized) {
@ -73,28 +74,30 @@ peg& get_parser()
} }
parser.enable_ast({ parser.enable_ast({
{ "STATEMENTS", Statements }, /*
{ "WHILE", While }, Definition, Tag Optimize
{ "ASSIGNMENT", Assignment }, ---------------------- ------------------ ---------- */
{ "IF", If }, { "STATEMENTS", Statements },
{ "FUNCTION", Function }, { "WHILE", While },
{ "PARAMETERS", Undefined }, { "ASSIGNMENT", Assignment },
{ "FUNCTION_CALL", FunctionCall }, { "IF", If },
{ "ARGUMENTS", Undefined }, { "FUNCTION", Function },
{ "PRIMARY", LogicalOr, true }, { "PARAMETERS", Default },
{ "LOGICAL_OR", LogicalAnd, true }, { "FUNCTION_CALL", FunctionCall },
{ "LOGICAL_AND", Condition, true }, { "ARGUMENTS", Default },
{ "CONDITION", BinExpresion, true }, { "PRIMARY", LogicalOr, true },
{ "TERM", UnaryPlus, true }, { "LOGICAL_OR", LogicalAnd, true },
{ "UNARY_PLUS", UnaryMinus, true }, { "LOGICAL_AND", Condition, true },
{ "UNARY_MINUS", UnaryNot, true }, { "CONDITION", BinExpresion, true },
{ "UNARY_NOT", BinExpresion, true }, { "TERM", UnaryPlus, true },
{ "NUMBER", Number }, { "UNARY_PLUS", UnaryMinus, true },
{ "BOOLEAN", Boolean }, { "UNARY_MINUS", UnaryNot, true },
{ "IDENTIFIER", Identifier }, { "UNARY_NOT", BinExpresion, true },
{ "INTERPOLATED_STRING", InterpolatedString }, { "NUMBER", Number },
}, { "BOOLEAN", Boolean },
Undefined); { "IDENTIFIER", Identifier },
{ "INTERPOLATED_STRING", InterpolatedString },
});
} }
return parser; return parser;

@ -2,7 +2,7 @@
enum AstTag enum AstTag
{ {
Undefined, Default = peglib::Ast::DefaultTag,
Statements, While, If, FunctionCall, Assignment, Statements, While, If, FunctionCall, Assignment,
LogicalOr, LogicalAnd, Condition, UnaryPlus, UnaryMinus, UnaryNot, BinExpresion, LogicalOr, LogicalAnd, Condition, UnaryPlus, UnaryMinus, UnaryNot, BinExpresion,
Identifier, InterpolatedString, Identifier, InterpolatedString,

@ -1834,6 +1834,8 @@ private:
struct Ast struct Ast
{ {
static const int DefaultTag = -1;
Ast(const char* _name, int _tag, const std::vector<std::shared_ptr<Ast>>& _nodes) Ast(const char* _name, int _tag, const std::vector<std::shared_ptr<Ast>>& _nodes)
: name(_name), tag(_tag), is_token(false), nodes(_nodes) {} : name(_name), tag(_tag), is_token(false), nodes(_nodes) {}
@ -2031,16 +2033,16 @@ public:
bool optimize; bool optimize;
}; };
peg& enable_ast(std::initializer_list<AstNodeInfo> list, int tag) { peg& enable_ast(std::initializer_list<AstNodeInfo> list) {
for (const auto& info: list) { for (const auto& info: list) {
ast_node(info); ast_node(info);
} }
ast_end(tag); ast_end();
return *this; return *this;
} }
peg& enable_ast() { peg& enable_ast() {
ast_end(-1); ast_end();
return *this; return *this;
} }
@ -2073,21 +2075,21 @@ private:
}; };
} }
void ast_end(int tag) { void ast_end() {
for (auto& x: *grammar_) { for (auto& x: *grammar_) {
const auto& name = x.first; const auto& name = x.first;
auto& def = x.second; auto& def = x.second;
auto& action = def.actions.front(); auto& action = def.actions.front();
if (!action) { if (!action) {
action = [tag, name](const SemanticValues& sv) { action = [name](const SemanticValues& sv) {
if (sv.is_token()) { if (sv.is_token()) {
return std::make_shared<Ast>(name.c_str(), tag, std::string(sv.s, sv.n)); return std::make_shared<Ast>(name.c_str(), Ast::DefaultTag, std::string(sv.s, sv.n));
} }
if (sv.size() == 1) { if (sv.size() == 1) {
std::shared_ptr<Ast> ast = sv[0].get<std::shared_ptr<Ast>>(); std::shared_ptr<Ast> ast = sv[0].get<std::shared_ptr<Ast>>();
return ast; return ast;
} }
return std::make_shared<Ast>(name.c_str(), tag, sv.map<std::shared_ptr<Ast>>()); return std::make_shared<Ast>(name.c_str(), Ast::DefaultTag, sv.map<std::shared_ptr<Ast>>());
}; };
} }
} }

Loading…
Cancel
Save