aboutsummaryrefslogtreecommitdiffstats
path: root/src/bin
diff options
context:
space:
mode:
authorbnewbold <bnewbold@robocracy.org>2016-09-16 21:40:50 -0700
committerbnewbold <bnewbold@robocracy.org>2016-09-16 21:40:54 -0700
commit852b2399e7b53e1d349aed5ec9fb6550b117825c (patch)
tree647d5264e50fb8412ec0300ccab05e49351c17f9 /src/bin
parentfdebceb92fe819cdcfeb9c1288102f3b97ab3ff6 (diff)
downloadmodelthing-852b2399e7b53e1d349aed5ec9fb6550b117825c.tar.gz
modelthing-852b2399e7b53e1d349aed5ec9fb6550b117825c.zip
rust parser with LALRPOP: pascal example
This is all just code from the LALRPOP library docs.
Diffstat (limited to 'src/bin')
-rw-r--r--src/bin/parse_pascal.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/src/bin/parse_pascal.rs b/src/bin/parse_pascal.rs
new file mode 100644
index 0000000..7b08f36
--- /dev/null
+++ b/src/bin/parse_pascal.rs
@@ -0,0 +1,47 @@
+
+extern crate modelthing;
+extern crate rustc_serialize;
+extern crate docopt;
+
+use modelthing::pascal;
+use docopt::Docopt;
+use std::env;
+use std::io::Read;
+use std::fs::File;
+use std::time::Instant;
+
+
+fn main() {
+ let args: Args = Docopt::new(USAGE)
+ .and_then(|d| d.argv(env::args()).decode())
+ .unwrap_or_else(|e| e.exit());
+
+ for input in &args.arg_inputs {
+ let mut s = String::new();
+ if let Err(err) = File::open(input).and_then(|mut f| f.read_to_string(&mut s)) {
+ println!("Input `{}`: I/O Error {}",
+ input, err);
+ continue;
+ }
+
+ let time_stamp = Instant::now();
+ let result = pascal::parse_file(&s);
+ let elapsed = time_stamp.elapsed();
+ let elapsed = elapsed.as_secs() as f64 + elapsed.subsec_nanos() as f64 / 1000_000_000.0;
+
+ match result {
+ Ok(()) => println!("Input `{}` ({}s): OK", input, elapsed),
+ Err(err) => println!("Input `{}` ({}s): parse error {:?}", input, elapsed, err),
+ }
+ }
+}
+
+const USAGE: &'static str = "
+Usage: modelthing-pascal <inputs>...
+Parses each input file.
+";
+
+#[derive(Debug, RustcDecodable)]
+struct Args {
+ arg_inputs: Vec<String>,
+}