mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-11-17 15:55:31 +00:00
Updated grammar files.
This commit is contained in:
parent
9e69ee9272
commit
1a897db7b4
@ -1,6 +1,11 @@
|
||||
|
||||
PROGRAM <- _ STATEMENTS
|
||||
STATEMENTS <- (EXPRESSION (';' _)?)*
|
||||
STATEMENTS <- (STATEMENT (';' _)?)*
|
||||
STATEMENT <- DEBUGGER / RETURN / EXPRESSION
|
||||
|
||||
DEBUGGER <- debugger
|
||||
RETURN <- return End _ / return EXPRESSION
|
||||
|
||||
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
|
||||
|
||||
ASSIGNMENT <- MUTABLE PRIMARY (ARGUMENTS / INDEX / DOT)* '=' _ EXPRESSION
|
||||
@ -19,12 +24,12 @@
|
||||
INDEX <- '[' _ EXPRESSION ']' _
|
||||
DOT <- '.' _ IDENTIFIER
|
||||
|
||||
WHILE <- 'while' _ EXPRESSION BLOCK
|
||||
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
||||
WHILE <- while EXPRESSION BLOCK
|
||||
IF <- if EXPRESSION BLOCK (else if EXPRESSION BLOCK)* (else BLOCK)?
|
||||
|
||||
PRIMARY <- WHILE / IF / FUNCTION / OBJECT / ARRAY / UNDEFINED / BOOLEAN / NUMBER / IDENTIFIER / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
||||
|
||||
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
||||
FUNCTION <- fn PARAMETERS BLOCK
|
||||
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
||||
PARAMETER <- MUTABLE IDENTIFIER
|
||||
|
||||
@ -37,26 +42,37 @@
|
||||
UNARY_NOT_OPERATOR <- < '!' > _
|
||||
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
|
||||
|
||||
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
||||
IDENTIFIER <- < IdentInitChar IdentChar* > _
|
||||
|
||||
OBJECT <- '{' _ (OBJECT_PROPERTY (',' _ OBJECT_PROPERTY)*)? '}' _
|
||||
OBJECT_PROPERTY <- MUTABLE IDENTIFIER ':' _ EXPRESSION
|
||||
|
||||
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*)? ']' _
|
||||
|
||||
UNDEFINED <- < 'undefined' > _
|
||||
BOOLEAN <- < ('true' / 'false') > _
|
||||
UNDEFINED <- < 'undefined' > __
|
||||
BOOLEAN <- < ('true' / 'false') > __
|
||||
MUTABLE <- (< 'mut' > __)?
|
||||
|
||||
~debugger <- 'debugger' __
|
||||
~return <- 'return' !IdentInitChar Space*
|
||||
~while <- 'while' __
|
||||
~if <- 'if' __
|
||||
~else <- 'else' __
|
||||
~fn <- 'fn' __
|
||||
|
||||
NUMBER <- < [0-9]+ > _
|
||||
STRING <- ['] < (!['] .)* > ['] _
|
||||
|
||||
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
|
||||
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
|
||||
|
||||
MUTABLE <- < 'mut'? > _
|
||||
|
||||
~_ <- (Space / EndOfLine / Comment)*
|
||||
Space <- ' ' / '\t'
|
||||
~_ <- (Space / End)*
|
||||
__ <- !IdentInitChar (Space / End)*
|
||||
~Space <- ' ' / '\t' / Comment
|
||||
~End <- EndOfLine / EndOfFile
|
||||
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!End .)* &End
|
||||
EndOfLine <- '\r\n' / '\n' / '\r'
|
||||
EndOfFile <- !.
|
||||
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
||||
IdentInitChar <- [a-zA-Z_]
|
||||
IdentChar <- [a-zA-Z0-9_]
|
||||
|
||||
|
@ -2,21 +2,21 @@
|
||||
program <- _ block '.' _
|
||||
|
||||
block <- const var procedure statement
|
||||
const <- ('CONST' _ ident '=' _ number (',' _ ident '=' _ number)* ';' _)?
|
||||
var <- ('VAR' _ ident (',' _ ident)* ';' _)?
|
||||
procedure <- ('PROCEDURE' _ ident ';' _ block ';' _)*
|
||||
const <- ('CONST' __ ident '=' _ number (',' _ ident '=' _ number)* ';' _)?
|
||||
var <- ('VAR' __ ident (',' _ ident)* ';' _)?
|
||||
procedure <- ('PROCEDURE' __ ident ';' _ block ';' _)*
|
||||
|
||||
statement <- (assignment / call / statements / if / while / out / in)?
|
||||
assignment <- ident ':=' _ expression
|
||||
call <- 'CALL' _ ident
|
||||
statements <- 'BEGIN' _ statement (';' _ statement )* 'END' _
|
||||
if <- 'IF' _ condition 'THEN' _ statement
|
||||
while <- 'WHILE' _ condition 'DO' _ statement
|
||||
out <- ('out' / 'write' / '!') _ expression
|
||||
in <- ('in' / 'read' / '?') _ ident
|
||||
call <- 'CALL' __ ident
|
||||
statements <- 'BEGIN' __ statement (';' _ statement )* 'END' __
|
||||
if <- 'IF' __ condition 'THEN' __ statement
|
||||
while <- 'WHILE' __ condition 'DO' __ statement
|
||||
out <- ('out' __ / 'write' __ / '!' _) expression
|
||||
in <- ('in' __ / 'read' __ / '?' _) ident
|
||||
|
||||
condition <- odd / compare
|
||||
odd <- 'ODD' _ expression
|
||||
odd <- 'ODD' __ expression
|
||||
compare <- expression compare_op expression
|
||||
compare_op <- < '=' / '#' / '<=' / '<' / '>=' / '>' > _
|
||||
|
||||
@ -29,8 +29,9 @@ factor_op <- < [*/] > _
|
||||
|
||||
factor <- ident / number / '(' _ expression ')' _
|
||||
|
||||
ident <- < [a-z] ([a-z] / [0-9])* > _
|
||||
ident <- < [a-z] [a-z0-9]* > _
|
||||
number <- < [0-9]+ > _
|
||||
|
||||
~_ <- [ \t\r\n]*
|
||||
~__ <- ![a-z0-9_] _
|
||||
|
||||
|
@ -136,7 +136,7 @@ private:
|
||||
}
|
||||
|
||||
static void constants(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) {
|
||||
// const <- ('CONST' _ ident '=' _ number(',' _ ident '=' _ number)* ';' _) ?
|
||||
// const <- ('CONST' __ ident '=' _ number(',' _ ident '=' _ number)* ';' _) ?
|
||||
const auto& nodes = ast->nodes;
|
||||
for (auto i = 0u; i < nodes.size(); i += 2) {
|
||||
const auto& ident = nodes[i + 0]->token;
|
||||
@ -149,7 +149,7 @@ private:
|
||||
}
|
||||
|
||||
static void variables(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) {
|
||||
// var <- ('VAR' _ ident(',' _ ident)* ';' _) ?
|
||||
// var <- ('VAR' __ ident(',' _ ident)* ';' _) ?
|
||||
const auto& nodes = ast->nodes;
|
||||
for (auto i = 0u; i < nodes.size(); i += 1) {
|
||||
const auto& ident = nodes[i]->token;
|
||||
@ -161,7 +161,7 @@ private:
|
||||
}
|
||||
|
||||
static void procedures(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) {
|
||||
// procedure <- ('PROCEDURE' _ ident ';' _ block ';' _)*
|
||||
// procedure <- ('PROCEDURE' __ ident ';' _ block ';' _)*
|
||||
const auto& nodes = ast->nodes;
|
||||
for (auto i = 0u; i < nodes.size(); i += 2) {
|
||||
const auto& ident = nodes[i + 0]->token;
|
||||
@ -182,7 +182,7 @@ private:
|
||||
}
|
||||
|
||||
static void call(const shared_ptr<AstPL0> ast, shared_ptr<SymbolScope> scope) {
|
||||
// call <- 'CALL' _ ident
|
||||
// call <- 'CALL' __ ident
|
||||
const auto& ident = ast->nodes[0]->token;
|
||||
if (!scope->has_procedure(ident)) {
|
||||
throw_runtime_error(ast->nodes[0], "undefined procedure '" + ident + "'...");
|
||||
@ -273,26 +273,26 @@ private:
|
||||
}
|
||||
|
||||
static void exec_call(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
|
||||
// call <- 'CALL' _ ident
|
||||
// call <- 'CALL' __ ident
|
||||
exec_block(env->get_procedure(ast->nodes[0]->token), env);
|
||||
}
|
||||
|
||||
static void exec_statements(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
|
||||
// statements <- 'BEGIN' _ statement (';' _ statement )* 'END' _
|
||||
// statements <- 'BEGIN' __ statement (';' _ statement )* 'END' __
|
||||
for (auto stmt: ast->nodes) {
|
||||
exec(stmt, env);
|
||||
}
|
||||
}
|
||||
|
||||
static void exec_if(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
|
||||
// if <- 'IF' _ condition 'THEN' _ statement
|
||||
// if <- 'IF' __ condition 'THEN' __ statement
|
||||
if (eval_condition(ast->nodes[0], env)) {
|
||||
exec(ast->nodes[1], env);
|
||||
}
|
||||
}
|
||||
|
||||
static void exec_while(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
|
||||
// while <- 'WHILE' _ condition 'DO' _ statement
|
||||
// while <- 'WHILE' __ condition 'DO' __ statement
|
||||
auto cond = ast->nodes[0];
|
||||
auto stmt = ast->nodes[1];
|
||||
while (eval_condition(cond, env)) {
|
||||
@ -301,12 +301,12 @@ private:
|
||||
}
|
||||
|
||||
static void exec_out(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
|
||||
// out <- '!' _ expression
|
||||
// out <- ('out' __ / 'write' __ / '!' _) expression
|
||||
cout << eval(ast->nodes[0], env) << endl;
|
||||
}
|
||||
|
||||
static void exec_in(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
|
||||
// in <- '?' _ ident
|
||||
// in <- ('in' __ / 'read' __ / '?' _) ident
|
||||
int val;
|
||||
cin >> val;
|
||||
env->set_variable(ast->nodes[0]->token, val);
|
||||
@ -323,7 +323,7 @@ private:
|
||||
}
|
||||
|
||||
static bool eval_odd(const shared_ptr<AstPL0> ast, shared_ptr<Environment> env) {
|
||||
// odd <- 'ODD' _ expression
|
||||
// odd <- 'ODD' __ expression
|
||||
return eval_expression(ast->nodes[0], env) != 0;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user