Skip to content

Commit 228df52

Browse files
committed
Minor refactoring on debug output
1 parent 432efb4 commit 228df52

File tree

1 file changed

+54
-44
lines changed

1 file changed

+54
-44
lines changed

src/main.rs

Lines changed: 54 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use std::error::Error;
22
use std::fs::File;
33
use std::io::prelude::*;
44
use std::path::Path;
5+
use std::env;
56

67
extern crate pest;
78
#[macro_use]
@@ -13,30 +14,7 @@ use pest::Parser;
1314
#[grammar = "parser/rosmsg.pest"]
1415
struct IdentParser;
1516

16-
fn main() {
17-
let path = Path::new("hello.txt");
18-
let display = path.display();
19-
20-
println!("Processing: {}", display);
21-
22-
let mut file = match File::open(&path) {
23-
// The `description` method of `io::Error` returns a string that
24-
// describes the error
25-
Err(why) => panic!("couldn't open {}: {}", display,
26-
why.description()),
27-
Ok(file) => file,
28-
};
29-
30-
// Read the file contents into a string, returns `io::Result<usize>`
31-
let mut s = String::new();
32-
match file.read_to_string(&mut s) {
33-
Err(why) => panic!("couldn't read {}: {}", display,
34-
why.description()),
35-
Ok(_) => (),
36-
}
37-
38-
let files = IdentParser::parse(Rule::file, &s).unwrap_or_else(|e| panic!("{}", e));
39-
17+
fn dump_ast(files : pest::iterators::Pairs<Rule>){
4018
for file in files {
4119

4220
// Because ident_list is silent, the iterator will contain idents
@@ -73,28 +51,60 @@ fn main() {
7351
}
7452
}
7553
}
54+
fn main() {
55+
56+
let args: Vec<String> = env::args().collect();
57+
58+
let path = Path::new(&args[1]);
59+
let display = path.display();
60+
61+
println!("Processing: {}", display);
62+
63+
let mut file = match File::open(&path) {
64+
// The `description` method of `io::Error` returns a string that
65+
// describes the error
66+
Err(why) => panic!("couldn't open {}: {}", display,
67+
why.description()),
68+
Ok(file) => file,
69+
};
70+
71+
// Read the file contents into a string, returns `io::Result<usize>`
72+
let mut s = String::new();
73+
match file.read_to_string(&mut s) {
74+
Err(why) => panic!("couldn't read {}: {}", display,
75+
why.description()),
76+
Ok(_) => (),
77+
}
78+
79+
let files = IdentParser::parse(Rule::file, &s).unwrap_or_else(|e| panic!("{}", e));
80+
81+
dump_ast(files);
82+
}
7683

7784
#[test]
7885
fn it_works() {
79-
let pairs = IdentParser::parse(Rule::file, "a1 b2").unwrap_or_else(|e| panic!("{}", e));
80-
81-
// Because ident_list is silent, the iterator will contain idents
82-
for pair in pairs {
83-
84-
let span = pair.clone().into_span();
85-
// A pair is a combination of the rule which matched and a span of input
86-
println!("Rule: {:?}", pair.as_rule());
87-
println!("Span: {:?}", span);
88-
println!("Text: {}", span.as_str());
89-
90-
// A pair can be converted to an iterator of the tokens which make it up:
91-
for inner_pair in pair.into_inner() {
92-
let inner_span = inner_pair.clone().into_span();
93-
match inner_pair.as_rule() {
94-
Rule::itype => println!("Letter: {}", inner_span.as_str()),
95-
Rule::identifier => println!("Digit: {}", inner_span.as_str()),
96-
_ => println!("UNK: {}", inner_span.as_str())
97-
};
98-
}
86+
let path = Path::new("hello.txt");
87+
let display = path.display();
88+
89+
println!("Processing: {}", display);
90+
91+
let mut file = match File::open(&path) {
92+
// The `description` method of `io::Error` returns a string that
93+
// describes the error
94+
Err(why) => panic!("couldn't open {}: {}", display,
95+
why.description()),
96+
Ok(file) => file,
97+
};
98+
99+
// Read the file contents into a string, returns `io::Result<usize>`
100+
let mut s = String::new();
101+
match file.read_to_string(&mut s) {
102+
Err(why) => panic!("couldn't read {}: {}", display,
103+
why.description()),
104+
Ok(_) => (),
99105
}
106+
107+
let files = IdentParser::parse(Rule::file, &s).unwrap_or_else(|e| panic!("{}", e));
108+
109+
dump_ast(files);
100110
}

0 commit comments

Comments
 (0)