Skip to content

Commit

Permalink
Add entire PEG grammar
Browse files Browse the repository at this point in the history
  • Loading branch information
evansmal committed Sep 6, 2023
1 parent 71f1167 commit c3b556d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 5 deletions.
62 changes: 59 additions & 3 deletions src/grammar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,33 @@ const auto GRAMMAR = ast::Grammar(
ast::Sequence(ast::Identifier("Spacing"),
ast::OneOrMore(ast::Identifier("Definition")),
ast::Identifier("EndOfFile"))),

ast::Definition(ast::Identifier("Definition"),
ast::Sequence(ast::Identifier("Identifier"),
ast::Identifier("LEFT_ARROW"),
ast::Identifier("Expression"))),

ast::Definition(
ast::Identifier("Expression"),
ast::Sequence(ast::Identifier("Sequence"),
ast::ZeroOrMore(ast::Sequence(ast::Identifier("SLASH"),
ast::Identifier("Sequence"))))),

ast::Definition(ast::Identifier("Sequence"),
ast::ZeroOrMore(ast::Identifier("Prefix"))),

ast::Definition(ast::Identifier("Prefix"),
ast::Sequence(ast::Optional(ast::Alternative(ast::Identifier("AND"),
ast::Identifier("NOT"))),
ast::Identifier("Suffix"))),

ast::Definition(
ast::Identifier("Suffix"),
ast::Sequence(ast::Identifier("Primary"),
ast::Optional(ast::Alternative(ast::Identifier("QUESTION"),
ast::Identifier("STAR"),
ast::Identifier("PLUS"))))),

ast::Definition(
ast::Identifier("Primary"),
ast::Alternative(ast::Sequence(ast::Identifier("Identifier"),
Expand All @@ -38,16 +44,20 @@ const auto GRAMMAR = ast::Grammar(
ast::Identifier("Literal"),
ast::Identifier("Class"),
ast::Identifier("DOT"))),

ast::Definition(ast::Identifier("Identifier"),
ast::Sequence(ast::Identifier("IdentStart"),
ast::ZeroOrMore(ast::Identifier("IdentCont")),
ast::Identifier("Spacing"))),

ast::Definition(
ast::Identifier("IdentStart"),
ast::Class({ast::Range("a", "z"), ast::Range("A", "Z")}, {ast::Literal("_")})),

ast::Definition(ast::Identifier("IdentCont"),
ast::Alternative(ast::Identifier("IdentStart"),
ast::Class({ast::Range("0", "9")}, {}))),

ast::Definition(
ast::Identifier("Literal"),
ast::Alternative(ast::Sequence(ast::Class({}, {ast::Literal("'")}),
Expand All @@ -62,16 +72,15 @@ const auto GRAMMAR = ast::Grammar(
ast::Identifier("Char"))),
ast::Class({}, {ast::Literal("\"")}),
ast::Identifier("Spacing")))),
ast::Definition(ast::Identifier("IdentCont"),
ast::Alternative(ast::Identifier("IdentStart"),
ast::Class({ast::Range("0", "9")}, {}))),

ast::Definition(
ast::Identifier("Class"),
ast::Sequence(ast::Literal("["),
ast::ZeroOrMore(ast::Sequence(ast::Not(ast::Literal("]")),
ast::Identifier("Range"))),
ast::Literal("]"),
ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("Range"),
ast::Alternative(ast::Sequence(ast::Identifier("Char"),
ast::Literal("-"),
Expand Down Expand Up @@ -99,4 +108,51 @@ const auto GRAMMAR = ast::Grammar(
ast::Optional(ast::Class({ast::Range("0", "7")}, {}))),
ast::Sequence(ast::Literal("\\"), ast::Literal("-")),
ast::Sequence(ast::Not(ast::Literal("\\")), ast::Dot()))),

ast::Definition(ast::Identifier("LEFT_ARROW"),
ast::Sequence(ast::Literal("<-"), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("SLASH"),
ast::Sequence(ast::Literal("/"), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("AND"),
ast::Sequence(ast::Literal("&"), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("NOT"),
ast::Sequence(ast::Literal("!"), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("QUESTION"),
ast::Sequence(ast::Literal("?"), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("STAR"),
ast::Sequence(ast::Literal("*"), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("PLUS"),
ast::Sequence(ast::Literal("+"), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("OPEN"),
ast::Sequence(ast::Literal("("), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("CLOSE"),
ast::Sequence(ast::Literal(")"), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("DOT"),
ast::Sequence(ast::Literal("."), ast::Identifier("Spacing"))),

ast::Definition(ast::Identifier("Spacing"),
ast::ZeroOrMore(ast::Alternative(ast::Identifier("Space"),
ast::Identifier("Comment")))),
ast::Definition(ast::Identifier("Comment"),
ast::Sequence(ast::Literal("#"),
ast::ZeroOrMore(ast::Sequence(
ast::Not(ast::Identifier("EndOfLine")),
ast::Dot())))),
ast::Definition(ast::Identifier("Space"),
ast::Alternative(ast::Literal(" "),
ast::Literal("\t"),
ast::Identifier("EndOfLine"))),
ast::Definition(
ast::Identifier("EndOfLine"),
ast::Alternative(ast::Literal("\r\n"), ast::Literal("\n"), ast::Literal("\r"))),

ast::Definition(ast::Identifier("EndOfFile"), ast::Not(ast::Dot())));
25 changes: 23 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#include "ast.hpp"
#include "combinator.hpp"
#include "generator.hpp"
#include "parser.hpp"
#include "grammar.hpp"
#include "parser.hpp"

#include <iostream>
#include <map>
Expand All @@ -23,7 +23,28 @@ auto main() -> int

std::cout << "Created grammar" << std::endl;

std::cout << ast::ToString(GRAMMAR) << std::endl;
const auto peg = Generate(GRAMMAR);

const std::string grammar = "Expression <- Number BinaryOperand Number\n"
"BinaryOperand <- Plus / Minus\n"
"Plus <- '+'\n"
"Minus <- '-'\n"
"Number <- [0-9]\n";

const auto parse = peg.Get("Grammar")(grammar);
if (std::holds_alternative<Success>(parse))
{
std::cout << "Parser success!" << std::endl;
const auto &success = std::get<Success>(parse);
for (const auto &node : success.node)
{
Dump(node);
}
}
else
{
std::cout << "Parser failure!" << std::endl;
}

const auto collection = Generate(g);

Expand Down

0 comments on commit c3b556d

Please sign in to comment.