@@ -2,6 +2,7 @@ use std::error::Error;
2
2
use std:: fs:: File ;
3
3
use std:: io:: prelude:: * ;
4
4
use std:: path:: Path ;
5
+ use std:: env;
5
6
6
7
extern crate pest;
7
8
#[ macro_use]
@@ -13,30 +14,7 @@ use pest::Parser;
13
14
#[ grammar = "parser/rosmsg.pest" ]
14
15
struct IdentParser ;
15
16
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 > ) {
40
18
for file in files {
41
19
42
20
// Because ident_list is silent, the iterator will contain idents
@@ -73,28 +51,60 @@ fn main() {
73
51
}
74
52
}
75
53
}
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
+ }
76
83
77
84
#[ test]
78
85
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 ( _) => ( ) ,
99
105
}
106
+
107
+ let files = IdentParser :: parse ( Rule :: file, & s) . unwrap_or_else ( |e| panic ! ( "{}" , e) ) ;
108
+
109
+ dump_ast ( files) ;
100
110
}
0 commit comments