Code cleanup and fixed build error.

pull/3/head
yhirose 9 years ago
parent b91efa1489
commit ab3d8b0d0e
  1. 24
      language/parser.cc
  2. 2
      language/parser.hpp
  3. 12
      peglib.h
  4. 2
      test/test.cc

@ -77,14 +77,14 @@ peg& get_parser()
/* /*
Definition, Tag Optimize Definition, Tag Optimize
---------------------- ------------------ ---------- */ ---------------------- ------------------ ---------- */
{ "STATEMENTS", Statements }, { "STATEMENTS", Statements, false },
{ "WHILE", While }, { "WHILE", While, false },
{ "ASSIGNMENT", Assignment }, { "ASSIGNMENT", Assignment, false },
{ "IF", If }, { "IF", If, false },
{ "FUNCTION", Function }, { "FUNCTION", Function, false },
{ "PARAMETERS", Default }, { "PARAMETERS", Default, false },
{ "FUNCTION_CALL", FunctionCall }, { "FUNCTION_CALL", FunctionCall, false },
{ "ARGUMENTS", Default }, { "ARGUMENTS", Default, false },
{ "PRIMARY", LogicalOr, true }, { "PRIMARY", LogicalOr, true },
{ "LOGICAL_OR", LogicalAnd, true }, { "LOGICAL_OR", LogicalAnd, true },
{ "LOGICAL_AND", Condition, true }, { "LOGICAL_AND", Condition, true },
@ -93,10 +93,10 @@ peg& get_parser()
{ "UNARY_PLUS", UnaryMinus, true }, { "UNARY_PLUS", UnaryMinus, true },
{ "UNARY_MINUS", UnaryNot, true }, { "UNARY_MINUS", UnaryNot, true },
{ "UNARY_NOT", BinExpresion, true }, { "UNARY_NOT", BinExpresion, true },
{ "NUMBER", Number }, { "NUMBER", Number, false },
{ "BOOLEAN", Boolean }, { "BOOLEAN", Boolean, false },
{ "IDENTIFIER", Identifier }, { "IDENTIFIER", Identifier, false },
{ "INTERPOLATED_STRING", InterpolatedString }, { "INTERPOLATED_STRING", InterpolatedString, false },
}); });
} }

@ -2,7 +2,7 @@
enum AstTag enum AstTag
{ {
Default = peglib::Ast::DefaultTag, Default = peglib::AstDefaultTag,
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,

@ -1832,10 +1832,10 @@ private:
* AST * AST
*---------------------------------------------------------------------------*/ *---------------------------------------------------------------------------*/
const int AstDefaultTag = -1;
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) {}
@ -2020,7 +2020,7 @@ public:
return (*grammar_)[s]; return (*grammar_)[s];
} }
void packrat_parsing(bool sw) { void enable_packrat_parsing(bool sw) {
if (grammar_ != nullptr) { if (grammar_ != nullptr) {
auto& rule = (*grammar_)[start_]; auto& rule = (*grammar_)[start_];
rule.enablePackratParsing = sw; rule.enablePackratParsing = sw;
@ -2029,7 +2029,7 @@ public:
struct AstNodeInfo { struct AstNodeInfo {
const char* name; const char* name;
int tag; int tag; // TODO: It should be calculated at compile-time from 'name' with constexpr hash function.
bool optimize; bool optimize;
}; };
@ -2083,13 +2083,13 @@ private:
if (!action) { if (!action) {
action = [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(), Ast::DefaultTag, std::string(sv.s, sv.n)); return std::make_shared<Ast>(name.c_str(), AstDefaultTag, 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(), Ast::DefaultTag, sv.map<std::shared_ptr<Ast>>()); return std::make_shared<Ast>(name.c_str(), AstDefaultTag, sv.map<std::shared_ptr<Ast>>());
}; };
} }
} }

@ -225,7 +225,7 @@ TEST_CASE("Backtracking test", "[general]")
count++; count++;
}; };
parser.packrat_parsing(true); parser.enable_packrat_parsing(true);
bool ret = parser.parse("Hello Two"); bool ret = parser.parse("Hello Two");
REQUIRE(ret == true); REQUIRE(ret == true);

Loading…
Cancel
Save