-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
Add example json parser for lalrpop
Showing
5 changed files
with
581 additions
and
3 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
[package] | ||
name = "lalrpop-app" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[[bin]] | ||
name = "lalrpop-app" | ||
path = "app.rs" | ||
|
||
[build-dependencies] | ||
lalrpop = { version = "0.20", features = ["lexer", "unicode"] } | ||
|
||
[dependencies] | ||
lalrpop-util = { version = "0.20", features = ["lexer", "unicode"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
extern crate lalrpop_util; | ||
|
||
use std::env; | ||
use std::fs; | ||
|
||
#[rustfmt::skip] | ||
#[allow(clippy::all)] | ||
mod json; | ||
|
||
fn main() { | ||
let src = fs::read_to_string(env::args().nth(1).expect("Expected file argument")) | ||
.expect("Failed to read file"); | ||
|
||
match json::ValueParser::new().parse(&src) { | ||
Ok(json) => { | ||
println!("{:#?}", json); | ||
} | ||
Err(err) => { | ||
eprintln!("{}", err); | ||
std::process::exit(1); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
extern crate lalrpop; | ||
|
||
fn main() { | ||
lalrpop::process_root().unwrap() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
grammar; | ||
|
||
// https://datatracker.ietf.org/doc/html/rfc7159 | ||
|
||
Object: () = { | ||
"{" Comma<Member> "}" | ||
}; | ||
|
||
Member: () = { | ||
String ":" Value, | ||
}; | ||
|
||
pub Value: () = { | ||
Object, | ||
Array, | ||
Number, | ||
String, | ||
"false", | ||
"null", | ||
"true" | ||
}; | ||
|
||
Array: () = { | ||
"[" Comma<Value> "]" | ||
}; | ||
|
||
Number: () = { | ||
"-"? Int Frac? Exp? | ||
}; | ||
|
||
Int: () = { | ||
"0", | ||
r"[1-9][0-9]*" | ||
}; | ||
|
||
Frac: () = { | ||
r"\.[0-9]+" | ||
}; | ||
|
||
Exp: () = { | ||
r"[eE][-+]?[0-9]+" | ||
}; | ||
|
||
String: () = { | ||
r#""[^"]*""# | ||
}; | ||
|
||
Comma<V>: Vec<V> = { | ||
<v: (<V> ",")*> <e: V?> => { | ||
v.into_iter().chain(e).collect() | ||
} | ||
}; |