generated from ElnuDev/rust-project
Add basic keyword, string system
This commit is contained in:
parent
46acbce60e
commit
24e33aa550
2 changed files with 29 additions and 10 deletions
18
src/lib.rs
18
src/lib.rs
|
@ -12,13 +12,21 @@ pub fn parse(file_path: &str) {
|
|||
let file = RpyParser::parse(Rule::file, &unparsed_file)
|
||||
.expect("unsuccessful parse") // unwrap the parse result
|
||||
.next().unwrap(); // get and unwrap the `file` rule; never fails
|
||||
|
||||
for record in file.into_inner() {
|
||||
match record.as_rule() {
|
||||
for line in file.into_inner() {
|
||||
match line.as_rule() {
|
||||
Rule::line => {
|
||||
for field in record.into_inner().map(|pairs| pairs.as_str()) {
|
||||
println!("'{field}'");
|
||||
println!("Line:");
|
||||
for token in line.into_inner() {
|
||||
match token.as_rule() {
|
||||
Rule::token => {
|
||||
println!("{}", token.as_str());
|
||||
},
|
||||
_ => {
|
||||
println!("{}", token.as_str());
|
||||
}
|
||||
}
|
||||
}
|
||||
println!()
|
||||
},
|
||||
Rule::EOI => (),
|
||||
_ => unreachable!(),
|
||||
|
|
21
src/rpy.pest
21
src/rpy.pest
|
@ -4,14 +4,25 @@ WHITESPACE = _{ " " }
|
|||
// characters are anything but newlines
|
||||
char = { !NEWLINE ~ ANY }
|
||||
|
||||
// token definition
|
||||
// http://pest.rs/book/grammars/syntax.html#atomic
|
||||
inner = @{ char* }
|
||||
|
||||
token = { string | keyword }
|
||||
|
||||
// has to be atomic for no implicit separate (spaces)
|
||||
keyword = @{ (!(WHITESPACE | NEWLINE) ~ ANY)+ }
|
||||
|
||||
// strings cannot contain quotes
|
||||
// TODO: escaped quotes
|
||||
string_data = @{ (!"\"" ~ ANY)* }
|
||||
string = ${ "\"" ~ string_data ~ "\"" }
|
||||
|
||||
// comments are a # followed by
|
||||
// any number of non-newline characters
|
||||
COMMENT = _{ "#" ~ char* }
|
||||
|
||||
// statements are comprised of at least one character
|
||||
statement = { char+ }
|
||||
|
||||
// lines are comprised of a statement
|
||||
line = { statement }
|
||||
line = { token+ }
|
||||
|
||||
file = { SOI ~ (line ~ ("\r\n" | "\n")*)* ~ "\n"* ~ EOI }
|
||||
file = { SOI ~ line ~ (NEWLINE+ ~ line)* ~ NEWLINE* ~ EOI }
|
Loading…
Add table
Reference in a new issue