mirror of
https://github.com/yhirose/cpp-peglib.git
synced 2024-12-22 11:55:30 +00:00
Cleanup grammar.
This commit is contained in:
parent
8f585f958e
commit
ff6231bcbe
@ -1,59 +1,56 @@
|
|||||||
|
|
||||||
PROGRAM <- _ STATEMENTS
|
PROGRAM <- _ STATEMENTS
|
||||||
|
|
||||||
STATEMENTS <- (EXPRESSION (';' _)?)*
|
STATEMENTS <- (EXPRESSION (';' _)?)*
|
||||||
|
|
||||||
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
|
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
|
||||||
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
||||||
WHILE <- 'while' _ EXPRESSION BLOCK
|
WHILE <- 'while' _ EXPRESSION BLOCK
|
||||||
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
||||||
|
|
||||||
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
||||||
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
||||||
CONDITION <- ADDITIVE (CONDITION_OPERATOR ADDITIVE)*
|
CONDITION <- ADDITIVE (CONDITION_OPERATOR ADDITIVE)*
|
||||||
ADDITIVE <- UNARY_PLUS (ADDITIVE_OPERATOR UNARY_PLUS)*
|
ADDITIVE <- UNARY_PLUS (ADDITIVE_OPERATOR UNARY_PLUS)*
|
||||||
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
||||||
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
||||||
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
||||||
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
|
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
|
||||||
|
|
||||||
CALL <- PRIMARY (ARGUMENTS / INDEX)*
|
CALL <- PRIMARY (ARGUMENTS / INDEX)*
|
||||||
|
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
|
||||||
|
INDEX <- '[' _ EXPRESSION ']' _
|
||||||
|
|
||||||
#FUNCTION_CALL <- IDENTIFIER ARGUMENTS
|
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
||||||
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
|
|
||||||
#ARRAY_REFERENCE <- IDENTIFIER INDEX
|
|
||||||
INDEX <- '[' _ EXPRESSION ']' _
|
|
||||||
|
|
||||||
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
||||||
|
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
||||||
|
PARAMETER <- MUTABLE IDENTIFIER
|
||||||
|
|
||||||
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
BLOCK <- '{' _ STATEMENTS '}' _
|
||||||
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
|
||||||
PARAMETER <- MUTABLE IDENTIFIER
|
|
||||||
|
|
||||||
BLOCK <- '{' _ STATEMENTS '}' _
|
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
||||||
|
ADDITIVE_OPERATOR <- < [-+] > _
|
||||||
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
UNARY_PLUS_OPERATOR <- < '+' > _
|
||||||
ADDITIVE_OPERATOR <- < [-+] > _
|
UNARY_MINUS_OPERATOR <- < '-' > _
|
||||||
UNARY_PLUS_OPERATOR <- < '+' > _
|
UNARY_NOT_OPERATOR <- < '!' > _
|
||||||
UNARY_MINUS_OPERATOR <- < '-' > _
|
|
||||||
UNARY_NOT_OPERATOR <- < '!' > _
|
|
||||||
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
|
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
|
||||||
|
|
||||||
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
||||||
|
|
||||||
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
||||||
NUMBER <- < [0-9]+ > _
|
NUMBER <- < [0-9]+ > _
|
||||||
BOOLEAN <- < ('true' / 'false') > _
|
BOOLEAN <- < ('true' / 'false') > _
|
||||||
STRING <- ['] < (!['] .)* > ['] _
|
STRING <- ['] < (!['] .)* > ['] _
|
||||||
|
|
||||||
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
|
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
|
||||||
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
|
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
|
||||||
|
|
||||||
MUTABLE <- < 'mut'? > _
|
MUTABLE <- < 'mut'? > _
|
||||||
|
|
||||||
~_ <- (Space / EndOfLine / Comment)*
|
~_ <- (Space / EndOfLine / Comment)*
|
||||||
Space <- ' ' / '\t'
|
Space <- ' ' / '\t'
|
||||||
EndOfLine <- '\r\n' / '\n' / '\r'
|
EndOfLine <- '\r\n' / '\n' / '\r'
|
||||||
EndOfFile <- !.
|
EndOfFile <- !.
|
||||||
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
||||||
|
|
||||||
|
@ -5,63 +5,60 @@ using namespace std;
|
|||||||
|
|
||||||
static auto g_grammar = R"(
|
static auto g_grammar = R"(
|
||||||
|
|
||||||
PROGRAM <- _ STATEMENTS
|
PROGRAM <- _ STATEMENTS
|
||||||
|
|
||||||
STATEMENTS <- (EXPRESSION (';' _)?)*
|
STATEMENTS <- (EXPRESSION (';' _)?)*
|
||||||
|
|
||||||
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
|
EXPRESSION <- ASSIGNMENT / LOGICAL_OR
|
||||||
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
ASSIGNMENT <- MUTABLE IDENTIFIER '=' _ EXPRESSION
|
||||||
WHILE <- 'while' _ EXPRESSION BLOCK
|
WHILE <- 'while' _ EXPRESSION BLOCK
|
||||||
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
IF <- 'if' _ EXPRESSION BLOCK ('else' _ 'if' _ EXPRESSION BLOCK)* ('else' _ BLOCK)?
|
||||||
|
|
||||||
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
LOGICAL_OR <- LOGICAL_AND ('||' _ LOGICAL_AND)*
|
||||||
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
LOGICAL_AND <- CONDITION ('&&' _ CONDITION)*
|
||||||
CONDITION <- ADDITIVE (CONDITION_OPERATOR ADDITIVE)*
|
CONDITION <- ADDITIVE (CONDITION_OPERATOR ADDITIVE)*
|
||||||
ADDITIVE <- UNARY_PLUS (ADDITIVE_OPERATOR UNARY_PLUS)*
|
ADDITIVE <- UNARY_PLUS (ADDITIVE_OPERATOR UNARY_PLUS)*
|
||||||
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
UNARY_PLUS <- UNARY_PLUS_OPERATOR? UNARY_MINUS
|
||||||
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
UNARY_MINUS <- UNARY_MINUS_OPERATOR? UNARY_NOT
|
||||||
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
UNARY_NOT <- UNARY_NOT_OPERATOR? MULTIPLICATIVE
|
||||||
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
|
MULTIPLICATIVE <- CALL (MULTIPLICATIVE_OPERATOR CALL)*
|
||||||
|
|
||||||
CALL <- PRIMARY (ARGUMENTS / INDEX)*
|
CALL <- PRIMARY (ARGUMENTS / INDEX)*
|
||||||
|
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
|
||||||
|
INDEX <- '[' _ EXPRESSION ']' _
|
||||||
|
|
||||||
#FUNCTION_CALL <- IDENTIFIER ARGUMENTS
|
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
||||||
ARGUMENTS <- '(' _ (EXPRESSION (',' _ EXPRESSION)*)? ')' _
|
|
||||||
#ARRAY_REFERENCE <- IDENTIFIER INDEX
|
|
||||||
INDEX <- '[' _ EXPRESSION ']' _
|
|
||||||
|
|
||||||
PRIMARY <- WHILE / IF / FUNCTION / IDENTIFIER / ARRAY / NUMBER / BOOLEAN / STRING / INTERPOLATED_STRING / '(' _ EXPRESSION ')' _
|
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
||||||
|
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
||||||
|
PARAMETER <- MUTABLE IDENTIFIER
|
||||||
|
|
||||||
FUNCTION <- 'fn' _ PARAMETERS BLOCK
|
BLOCK <- '{' _ STATEMENTS '}' _
|
||||||
PARAMETERS <- '(' _ (PARAMETER (',' _ PARAMETER)*)? ')' _
|
|
||||||
PARAMETER <- MUTABLE IDENTIFIER
|
|
||||||
|
|
||||||
BLOCK <- '{' _ STATEMENTS '}' _
|
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
||||||
|
ADDITIVE_OPERATOR <- < [-+] > _
|
||||||
CONDITION_OPERATOR <- < ('==' / '!=' / '<=' / '<' / '>=' / '>') > _
|
UNARY_PLUS_OPERATOR <- < '+' > _
|
||||||
ADDITIVE_OPERATOR <- < [-+] > _
|
UNARY_MINUS_OPERATOR <- < '-' > _
|
||||||
UNARY_PLUS_OPERATOR <- < '+' > _
|
UNARY_NOT_OPERATOR <- < '!' > _
|
||||||
UNARY_MINUS_OPERATOR <- < '-' > _
|
|
||||||
UNARY_NOT_OPERATOR <- < '!' > _
|
|
||||||
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
|
MULTIPLICATIVE_OPERATOR <- < [*/%] > _
|
||||||
|
|
||||||
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
IDENTIFIER <- < [a-zA-Z_][a-zA-Z0-9_]* > _
|
||||||
|
|
||||||
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
ARRAY <- '[' _ (EXPRESSION (',' _ EXPRESSION)*) ']' _
|
||||||
NUMBER <- < [0-9]+ > _
|
NUMBER <- < [0-9]+ > _
|
||||||
BOOLEAN <- < ('true' / 'false') > _
|
BOOLEAN <- < ('true' / 'false') > _
|
||||||
STRING <- ['] < (!['] .)* > ['] _
|
STRING <- ['] < (!['] .)* > ['] _
|
||||||
|
|
||||||
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
|
INTERPOLATED_STRING <- '"' ('{' _ EXPRESSION '}' / INTERPOLATED_CONTENT)* '"' _
|
||||||
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
|
INTERPOLATED_CONTENT <- (!["{] .) (!["{] .)*
|
||||||
|
|
||||||
MUTABLE <- < 'mut'? > _
|
MUTABLE <- < 'mut'? > _
|
||||||
|
|
||||||
~_ <- (Space / EndOfLine / Comment)*
|
~_ <- (Space / EndOfLine / Comment)*
|
||||||
Space <- ' ' / '\t'
|
Space <- ' ' / '\t'
|
||||||
EndOfLine <- '\r\n' / '\n' / '\r'
|
EndOfLine <- '\r\n' / '\n' / '\r'
|
||||||
EndOfFile <- !.
|
EndOfFile <- !.
|
||||||
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
Comment <- '/*' (!'*/' .)* '*/' / ('#' / '//') (!(EndOfLine / EndOfFile) .)* (EndOfLine / EndOfFile)
|
||||||
|
|
||||||
)";
|
)";
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user