Use PascalCase for exposed pest grammar

main
Elnu 1 year ago
parent f178baf1e6
commit 6707b97ef5

@ -178,15 +178,15 @@ fn parse_token(pair: Pair) -> Token {
}; };
} }
match token { match token {
Rule::string => { Rule::String => {
let contents = contents!(); let contents = contents!();
Token::Str(match contents.as_rule() { Token::Str(match contents.as_rule() {
Rule::single_quote_string_data => contents.as_str().replace("\\'", "'"), Rule::SingleQuoteStringData => contents.as_str().replace("\\'", "'"),
Rule::double_quote_string_data => contents.as_str().replace("\\\"", "\""), Rule::DoubleQuoteStringData => contents.as_str().replace("\\\"", "\""),
_ => unreachable!(), _ => unreachable!(),
}) })
} }
Rule::array => { Rule::Array => {
let contents = contents!(); let contents = contents!();
let mut array = Vec::new(); let mut array = Vec::new();
for token in contents.into_inner() { for token in contents.into_inner() {
@ -194,13 +194,13 @@ fn parse_token(pair: Pair) -> Token {
} }
Token::Array(array) Token::Array(array)
} }
Rule::boolean => Token::Boolean(match pair.as_str() { Rule::Boolean => Token::Boolean(match pair.as_str() {
"True" => true, "True" => true,
"False" => false, "False" => false,
_ => unreachable!(), _ => unreachable!(),
}), }),
Rule::number => Token::Number(pair.as_str().parse().unwrap()), Rule::Number => Token::Number(pair.as_str().parse().unwrap()),
Rule::keyword => Token::Keyword(pair.as_str().to_owned()), Rule::Keyword => Token::Keyword(pair.as_str().to_owned()),
__ => unreachable!(), __ => unreachable!(),
} }
} }

@ -1,52 +1,54 @@
// characters are anything but newlines // characters are anything but newlines
char = { !NEWLINE ~ ANY } char = { !NEWLINE ~ ANY }
// token definition // Token definition
// http://pest.rs/book/grammars/syntax.html#atomic // http://pest.rs/book/grammars/syntax.html#atomic
inner = @{ char* } inner = @{ char* }
token = { string | array | boolean | number | keyword } Token = { String | Array | Boolean | Number | Keyword }
// KEYWORDS // KEYWORDS
// has to be atomic for no implicit separate (spaces) // has to be atomic for no implicit separate (spaces)
keyword = ${ (!(whitespace | NEWLINE) ~ ANY)+ } Keyword = ${ (!(whitespace | NEWLINE) ~ ANY)+ }
// STRING // STRING
single_quote_string_data = @{ ( SingleQuoteStringData = @{ (
"\\'" // Escaped single quotes "\\'" // Escaped single quotes
| (!"'" ~ ANY) | (!"'" ~ ANY)
)* } )* }
double_quote_string_data = @{ ( DoubleQuoteStringData = @{ (
"\\\"" // Escaped double quotes "\\\"" // Escaped double quotes
| (!"\"" ~ ANY) | (!"\"" ~ ANY)
)* } )* }
string = ${ String = ${
("'" ~ single_quote_string_data ~ "'") ("'" ~ SingleQuoteStringData ~ "'")
| ("\"" ~ double_quote_string_data ~ "\"") | ("\"" ~ DoubleQuoteStringData ~ "\"")
} }
// ARRAY // Array
array = ${ Array = ${
"[" ~ "]" "[" ~ "]"
| "[" ~ whitespace* ~ NEWLINE* ~ whitespace* ~ token ~ ("," ~ whitespace* ~ NEWLINE* ~ whitespace* ~ token)* ~ NEWLINE* ~ "]" | "[" ~ whitespace* ~ NEWLINE* ~ whitespace* ~ Token ~ ("," ~ whitespace* ~ NEWLINE* ~ whitespace* ~ Token)* ~ NEWLINE* ~ "]"
} }
// BOOLEAN // BOOLEAN
boolean = ${ "True" | "False" } Boolean = ${ "True" | "False" }
// NUMBER // NUMBER
number = @{ Number = @{
"-"? "-"?
~ ("0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*) ~ ("0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*)
~ ("." ~ ASCII_DIGIT*)? ~ ("." ~ ASCII_DIGIT*)?
} }
// comments are a # followed by // comments are a # followed by
// any number of non-newline characters // any Number of non-newline characters
COMMENT = _{ "#" ~ char* } COMMENT = _{ "#" ~ char* }
Colon = { ":" }
// lines are comprised of a statement // lines are comprised of a statement
line = @{ (token ~ whitespace+)* ~ token } line = @{ (Token ~ whitespace+)* ~ Token ~ Colon? }
file = { SOI ~ NEWLINE* ~ block_content* ~ NEWLINE* ~ EOI } file = { SOI ~ NEWLINE* ~ block_content* ~ NEWLINE* ~ EOI }

Loading…
Cancel
Save