mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 20:05:31 +00:00
Refactoring.
This commit is contained in:
parent
620c775eee
commit
49665ce113
@ -3,6 +3,94 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
#include <peglib.h>
|
#include <peglib.h>
|
||||||
|
|
||||||
|
namespace culebra {
|
||||||
|
|
||||||
|
const auto grammar_ = R"(
|
||||||
|
|
||||||
|
PROGRAM <- _ STATEMENTS
|
||||||
|
|
||||||
|
STATEMENTS <- (EXPRESSION (';' _)?)*
|
||||||
|
|
||||||
|
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
|
||||||
|
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
||||||
|
WHILE <- 'while' _ EXPRESSION BLOCK
|
||||||
|
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
||||||
|
|
||||||
|
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
||||||
|
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
||||||
|
CONDITION <- ADDITIVE (CONDITION_OPERATOR ADDITIVE)*
|
||||||
|
ADDITIVE <- UNARY_PLUS (ADDITIVE_OPERATOR UNARY_PLUS)*
|
||||||
|
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
||||||
|
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
||||||
|
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
||||||
|
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
|
||||||
|
|
||||||
|
CALL <- PRIMARY (ARGUMENTS / INDEX / DOT)*
|
||||||
|
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
|
||||||
|
INDEX <- '[' _ EXPRESSION ']' _
|
||||||
|
DOT <- '.' _ IDENTIFIER
|
||||||
|
|
||||||
|
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / OBJECT / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
||||||
|
|
||||||
|
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
||||||
|
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
||||||
|
PARAMETER <- MUTABLE IDENTIFIER
|
||||||
|
|
||||||
|
BLOCK <- '{' _ STATEMENTS '}' _
|
||||||
|
|
||||||
|
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
||||||
|
ADDITIVE_OPERATOR <- < [-+] > _
|
||||||
|
UNARY_PLUS_OPERATOR <- < '+' > _
|
||||||
|
UNARY_MINUS_OPERATOR <- < '-' > _
|
||||||
|
UNARY_NOT_OPERATOR <- < '!' > _
|
||||||
|
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
|
||||||
|
|
||||||
|
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
||||||
|
|
||||||
|
OBJECT <- '{' _ (OBJECT_PROPERTY (',' _ OBJECT_PROPERTY)*)? '}' _
|
||||||
|
OBJECT_PROPERTY <- IDENTIFIER ':' _ EXPRESSION
|
||||||
|
|
||||||
|
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*)? ']' _
|
||||||
|
|
||||||
|
NUMBER <- < [0-9]+ > _
|
||||||
|
BOOLEAN <- < ('true' / 'false') > _
|
||||||
|
STRING <- ['] < (!['] .)* > ['] _
|
||||||
|
|
||||||
|
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
|
||||||
|
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
|
||||||
|
|
||||||
|
MUTABLE <- < 'mut'? > _
|
||||||
|
|
||||||
|
~_ <- (Space / EndOfLine / Comment)*
|
||||||
|
Space <- ' ' / '\t'
|
||||||
|
EndOfLine <- '\r\n' / '\n' / '\r'
|
||||||
|
EndOfFile <- !.
|
||||||
|
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
||||||
|
|
||||||
|
)";
|
||||||
|
|
||||||
|
inline peglib::peg& get_parser()
|
||||||
|
{
|
||||||
|
static peglib::peg parser;
|
||||||
|
static bool initialized = false;
|
||||||
|
|
||||||
|
if (!initialized) {
|
||||||
|
initialized = true;
|
||||||
|
|
||||||
|
parser.log = [&](size_t ln, size_t col, const std::string& msg) {
|
||||||
|
std::cerr << ln << ":" << col << ": " << msg << std::endl;
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!parser.load_grammar(grammar_)) {
|
||||||
|
throw std::logic_error("invalid peg grammar");
|
||||||
|
}
|
||||||
|
|
||||||
|
parser.enable_ast(true, { "PARAMETERS", "ARGUMENTS" });
|
||||||
|
}
|
||||||
|
|
||||||
|
return parser;
|
||||||
|
}
|
||||||
|
|
||||||
struct Value;
|
struct Value;
|
||||||
struct Environment;
|
struct Environment;
|
||||||
|
|
||||||
@ -766,95 +854,6 @@ private:
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
inline peglib::peg& get_parser()
|
|
||||||
{
|
|
||||||
using namespace peglib;
|
|
||||||
using namespace std;
|
|
||||||
|
|
||||||
static auto grammar_ = R"(
|
|
||||||
|
|
||||||
PROGRAM <- _ STATEMENTS
|
|
||||||
|
|
||||||
STATEMENTS <- (EXPRESSION (';' _)?)*
|
|
||||||
|
|
||||||
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
|
|
||||||
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
|
||||||
WHILE <- 'while' _ EXPRESSION BLOCK
|
|
||||||
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
|
||||||
|
|
||||||
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
|
||||||
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
|
||||||
CONDITION <- ADDITIVE (CONDITION_OPERATOR ADDITIVE)*
|
|
||||||
ADDITIVE <- UNARY_PLUS (ADDITIVE_OPERATOR UNARY_PLUS)*
|
|
||||||
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
|
||||||
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
|
||||||
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
|
||||||
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
|
|
||||||
|
|
||||||
CALL <- PRIMARY (ARGUMENTS / INDEX / DOT)*
|
|
||||||
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
|
|
||||||
INDEX <- '[' _ EXPRESSION ']' _
|
|
||||||
DOT <- '.' _ IDENTIFIER
|
|
||||||
|
|
||||||
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / OBJECT / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
|
||||||
|
|
||||||
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
|
||||||
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
|
||||||
PARAMETER <- MUTABLE IDENTIFIER
|
|
||||||
|
|
||||||
BLOCK <- '{' _ STATEMENTS '}' _
|
|
||||||
|
|
||||||
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
|
||||||
ADDITIVE_OPERATOR <- < [-+] > _
|
|
||||||
UNARY_PLUS_OPERATOR <- < '+' > _
|
|
||||||
UNARY_MINUS_OPERATOR <- < '-' > _
|
|
||||||
UNARY_NOT_OPERATOR <- < '!' > _
|
|
||||||
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
|
|
||||||
|
|
||||||
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
|
||||||
|
|
||||||
OBJECT <- '{' _ (OBJECT_PROPERTY (',' _ OBJECT_PROPERTY)*)? '}' _
|
|
||||||
OBJECT_PROPERTY <- IDENTIFIER ':' _ EXPRESSION
|
|
||||||
|
|
||||||
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*)? ']' _
|
|
||||||
|
|
||||||
NUMBER <- < [0-9]+ > _
|
|
||||||
BOOLEAN <- < ('true' / 'false') > _
|
|
||||||
STRING <- ['] < (!['] .)* > ['] _
|
|
||||||
|
|
||||||
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
|
|
||||||
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
|
|
||||||
|
|
||||||
MUTABLE <- < 'mut'? > _
|
|
||||||
|
|
||||||
~_ <- (Space / EndOfLine / Comment)*
|
|
||||||
Space <- ' ' / '\t'
|
|
||||||
EndOfLine <- '\r\n' / '\n' / '\r'
|
|
||||||
EndOfFile <- !.
|
|
||||||
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
|
||||||
|
|
||||||
)";
|
|
||||||
|
|
||||||
static peg parser;
|
|
||||||
static bool initialized = false;
|
|
||||||
|
|
||||||
if (!initialized) {
|
|
||||||
initialized = true;
|
|
||||||
|
|
||||||
parser.log = [&](size_t ln, size_t col, const string& msg) {
|
|
||||||
cerr << ln << ":" << col << ": " << msg << endl;
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!parser.load_grammar(grammar_)) {
|
|
||||||
throw logic_error("invalid peg grammar");
|
|
||||||
}
|
|
||||||
|
|
||||||
parser.enable_ast(true, { "PARAMETERS", "ARGUMENTS" });
|
|
||||||
}
|
|
||||||
|
|
||||||
return parser;
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool run(
|
inline bool run(
|
||||||
const std::string& path,
|
const std::string& path,
|
||||||
std::shared_ptr<Environment> env,
|
std::shared_ptr<Environment> env,
|
||||||
@ -890,3 +889,4 @@ inline bool run(
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
} // namespace culebra
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
|
using namespace culebra;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
bool read_file(const char* path, vector<char>& buff)
|
bool read_file(const char* path, vector<char>& buff)
|
||||||
|
Loading…
Reference in New Issue
Block a user