Fri, 03 May 2024 14:53:53 -0500
Properly handle \%
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
1 | // The main documentation is in the README. |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
2 | #![doc = include_str!("../README.md")] |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
3 | |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
4 | #![feature(trait_upcasting)] |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
5 | |
0 | 6 | use std::io; |
3
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
7 | use std::fs::File; |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
8 | use std::io::{BufWriter, BufRead, BufReader, Write, Read}; |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
9 | use std::fmt::Debug; |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
10 | use clap::Parser; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
11 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
12 | /// Command line parameters |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
13 | #[derive(Parser, Debug, Clone)] |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
14 | #[clap( |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
15 | about = env!("CARGO_PKG_DESCRIPTION"), |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
16 | author = env!("CARGO_PKG_AUTHORS"), |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
17 | version = env!("CARGO_PKG_VERSION"), |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
18 | )] |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
19 | struct CommandLineArgs { |
3
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
20 | /// Input file (default is stdin) |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
21 | input : Option<String>, |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
22 | |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
23 | /// Output file (defalt is stdout) |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
24 | #[arg(long, short = 'o')] |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
25 | output : Option<String>, |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
26 | |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
27 | #[clap(flatten)] |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
28 | config : Config |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
29 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
30 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
31 | #[derive(Parser, Debug, Clone)] |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
32 | struct Config { |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
33 | #[arg(long, short = 'c')] |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
34 | /// Strip comments |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
35 | strip_comments : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
36 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
37 | #[arg(long, short = 'w')] |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
38 | /// Strip unnecessary whitespace |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
39 | strip_whitespace : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
40 | } |
0 | 41 | |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
42 | struct Context { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
43 | lineno : usize, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
44 | input_only_ws : bool, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
45 | cli : Config |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
46 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
47 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
48 | type AnyChainRule<W> = Box<dyn ChainRule<W>>; |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
49 | type AnyNestedRule<W> = Box<dyn NestedRule<W>>; |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
50 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
51 | trait ChainRule<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
52 | fn consume(self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W>; |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
53 | fn flush(self : Box<Self>, ctx : &Context) -> W; |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
54 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
55 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
56 | trait NestedRule<W : Write> : ChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
57 | fn produce(&mut self, c : char, ctx : &Context); |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
58 | fn next(self : Box<Self>) -> AnyChainRule<W>; |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
59 | fn produce_string(&mut self, s : String, ctx : &Context) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
60 | s.chars().for_each(|c| self.produce(c, ctx)); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
61 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
62 | fn start_ignored_comment(&mut self, c : char); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
63 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
64 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
65 | impl<W : Write + 'static> ChainRule<W> for Out<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
66 | fn consume(self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
67 | basic_consume(self, c, ctx, true) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
68 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
69 | fn flush(mut self : Box<Self>, _ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
70 | self.output.flush().unwrap(); |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
71 | self.output |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
72 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
73 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
74 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
75 | impl<W : Write + 'static> NestedRule<W> for Out<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
76 | fn produce(&mut self, c : char, ctx : &Context) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
77 | if c == '\n' { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
78 | self.line_end(ctx.cli.strip_whitespace, ctx.input_only_ws) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
79 | } else if c.is_whitespace() { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
80 | self.stored_whitespace.push(c); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
81 | } else { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
82 | write!(self.output, "{}{}", self.stored_whitespace, c).unwrap(); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
83 | self.stored_whitespace.clear(); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
84 | self.only_whitespace = false; |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
85 | self.whitespace_satisfied = false; |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
86 | self.par_satisfied = false; |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
87 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
88 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
89 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
90 | fn next(self : Box<Self>) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
91 | self |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
92 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
93 | |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
94 | fn start_ignored_comment(&mut self, c : char) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
95 | if self.stored_whitespace.is_empty() && !self.only_whitespace { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
96 | // The marker needs to be inserted if there is to be no whitespace inserted |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
97 | write!(self.output, "{c}").unwrap(); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
98 | self.whitespace_satisfied = false; |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
99 | self.par_satisfied = false; |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
100 | self.only_whitespace = false; |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
101 | } else if self.only_whitespace { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
102 | self.ignored_comment_only_line = true |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
103 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
104 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
105 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
106 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
107 | fn basic_consume<W : Write + 'static>( |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
108 | mut s : AnyNestedRule<W>, |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
109 | c : char, |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
110 | ctx : &Context, |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
111 | print_end : bool |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
112 | ) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
113 | match c { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
114 | '{' => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
115 | s.produce(c, ctx); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
116 | Box::new(Group(s)) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
117 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
118 | '}' => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
119 | if print_end { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
120 | s.produce(c, ctx); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
121 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
122 | s.next() |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
123 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
124 | '\\' => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
125 | Box::new(CommandName{parent : s, command : "\\".to_string()}) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
126 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
127 | '%' => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
128 | if !ctx.cli.strip_comments { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
129 | s.produce(c, ctx); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
130 | Box::new(Comment(s)) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
131 | } else { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
132 | s.start_ignored_comment(c); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
133 | Box::new(IgnoreComment(s)) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
134 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
135 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
136 | _ => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
137 | s.produce(c, ctx); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
138 | s |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
139 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
140 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
141 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
142 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
143 | struct CommandName<W : Write> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
144 | parent : AnyNestedRule<W>, |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
145 | command : String |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
146 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
147 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
148 | impl<W : Write + 'static> ChainRule<W> for CommandName<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
149 | fn consume(mut self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
150 | match c { |
9 | 151 | '}' | '{' | '\\' | '%' if self.command.len() <= 1 => { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
152 | self.command.push(c); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
153 | self.handle(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
154 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
155 | c if c.is_alphanumeric() => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
156 | self.command.push(c); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
157 | self |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
158 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
159 | c => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
160 | self.handle(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
161 | .consume(c, ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
162 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
163 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
164 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
165 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
166 | fn flush(self : Box<Self>, ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
167 | self.handle(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
168 | .flush(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
169 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
170 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
171 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
172 | impl<W : Write + 'static> CommandName<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
173 | fn handle(mut self, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
174 | match self.command.as_str() { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
175 | "\\added" => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
176 | Scan::new(Added(self.parent)) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
177 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
178 | "\\replaced" => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
179 | Scan::new(Replaced(self.parent)) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
180 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
181 | "\\deleted" => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
182 | Scan::new(Deleted(self.parent)) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
183 | }, |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
184 | _ => { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
185 | self.parent.produce_string(self.command, ctx); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
186 | self.parent |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
187 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
188 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
189 | } |
0 | 190 | } |
191 | ||
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
192 | struct Comment<W : Write>(AnyNestedRule<W>); |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
193 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
194 | impl<W : Write + 'static> ChainRule<W> for Comment<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
195 | fn consume(mut self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
196 | if c == '\n' { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
197 | self.0.consume(c, ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
198 | } else { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
199 | self.0.produce(c, ctx); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
200 | self |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
201 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
202 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
203 | fn flush(self : Box<Self>, ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
204 | self.0.flush(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
205 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
206 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
207 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
208 | struct IgnoreComment<W : Write>(AnyChainRule<W>); |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
209 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
210 | impl<W : Write +'static> ChainRule<W> for IgnoreComment<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
211 | fn consume(self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
212 | if c == '\n' { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
213 | self.0.consume(c, ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
214 | } else { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
215 | self |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
216 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
217 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
218 | fn flush(self : Box<Self>, ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
219 | self.0.flush(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
220 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
221 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
222 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
223 | struct Group<W : Write>(AnyNestedRule<W>); |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
224 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
225 | impl<W : Write + 'static> ChainRule<W> for Group<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
226 | fn consume(self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
227 | basic_consume(self, c, ctx, true) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
228 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
229 | fn flush(self : Box<Self>, ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
230 | self.0.flush(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
231 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
232 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
233 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
234 | impl<W : Write + 'static> NestedRule<W> for Group<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
235 | fn produce(&mut self, c : char, ctx : &Context) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
236 | self.0.produce(c, ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
237 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
238 | fn next(self : Box<Self>) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
239 | self.0 |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
240 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
241 | fn start_ignored_comment(&mut self, c : char) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
242 | self.0.start_ignored_comment(c) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
243 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
244 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
245 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
246 | struct Added<W : Write>(AnyNestedRule<W>); |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
247 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
248 | impl<W : Write + 'static> ChainRule<W> for Added<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
249 | fn consume(self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
250 | basic_consume(self, c, ctx, false) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
251 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
252 | fn flush(self : Box<Self>, ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
253 | self.0.flush(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
254 | } |
0 | 255 | } |
256 | ||
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
257 | impl<W : Write + 'static> NestedRule<W> for Added<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
258 | fn produce(&mut self, c : char, ctx : &Context) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
259 | self.0.produce(c, ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
260 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
261 | fn next(self : Box<Self>) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
262 | self.0 |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
263 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
264 | fn start_ignored_comment(&mut self, c : char) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
265 | self.0.start_ignored_comment(c) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
266 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
267 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
268 | struct Deleted<W : Write>(AnyNestedRule<W>); |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
269 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
270 | impl<W : Write + 'static> ChainRule<W> for Deleted<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
271 | fn consume(self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
272 | basic_consume(self, c, ctx, false) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
273 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
274 | fn flush(self : Box<Self>, ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
275 | self.0.flush(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
276 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
277 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
278 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
279 | impl<W : Write + 'static> NestedRule<W> for Deleted<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
280 | fn produce(&mut self, _c : char, _ctx : &Context) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
281 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
282 | fn next(self : Box<Self>) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
283 | self.0 |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
284 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
285 | fn start_ignored_comment(&mut self, c : char) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
286 | self.0.start_ignored_comment(c) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
287 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
288 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
289 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
290 | struct Replaced<W : Write>(AnyNestedRule<W>); |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
291 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
292 | impl<W : Write + 'static> ChainRule<W> for Replaced<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
293 | fn consume(self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
294 | basic_consume(self, c, ctx, false) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
295 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
296 | fn flush(self : Box<Self>, ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
297 | self.0.flush(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
298 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
299 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
300 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
301 | impl<W : Write + 'static> NestedRule<W> for Replaced<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
302 | fn produce(&mut self, c : char, ctx : &Context) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
303 | self.0.produce(c, ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
304 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
305 | fn next(self : Box<Self>) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
306 | Scan::new(Deleted(self.0)) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
307 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
308 | fn start_ignored_comment(&mut self, c : char) { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
309 | self.0.start_ignored_comment(c) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
310 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
311 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
312 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
313 | struct Scan<W : Write>(AnyNestedRule<W>); |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
314 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
315 | impl<W : Write + 'static> ChainRule<W> for Scan<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
316 | fn consume(self : Box<Self>, c : char, ctx : &Context) -> AnyChainRule<W> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
317 | if c.is_whitespace() || c == '\n' { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
318 | self |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
319 | } else if c == '{' { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
320 | self.0 |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
321 | } else if c == '%' { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
322 | Box::new(IgnoreComment(self)) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
323 | } else { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
324 | panic!("Non-whitespace character ({c}) separating arguments on \ |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
325 | line {lineno}", lineno = ctx.lineno) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
326 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
327 | } |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
328 | fn flush(self : Box<Self>, ctx : &Context) -> W { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
329 | self.0.flush(ctx) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
330 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
331 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
332 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
333 | impl<W : Write + 'static> Scan<W> { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
334 | fn new<R : NestedRule<W> + 'static>(r : R) -> Box<dyn ChainRule<W>> { |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
335 | Box::new(Scan(Box::new(r))) |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
336 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
337 | } |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
338 | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
339 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
340 | struct Out<W : Write> { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
341 | only_whitespace : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
342 | stored_whitespace : String, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
343 | output : W, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
344 | whitespace_satisfied : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
345 | par_satisfied : bool, |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
346 | ignored_comment_only_line : bool |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
347 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
348 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
349 | impl<W : Write> Out<W> { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
350 | pub fn line_end(&mut self, strip_ws : bool, input_only_ws : bool) { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
351 | let skip_linefeed = if input_only_ws { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
352 | // Need a paragraph break |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
353 | strip_ws && self.par_satisfied |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
354 | } else if strip_ws { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
355 | self.only_whitespace && self.whitespace_satisfied |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
356 | } else { |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
357 | // Skip comment-only lines if the comment is ignored |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
358 | self.ignored_comment_only_line |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
359 | }; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
360 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
361 | if !skip_linefeed { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
362 | if !strip_ws { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
363 | write!(self.output, "{}", self.stored_whitespace).unwrap(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
364 | } |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
365 | write!(self.output, "\n").unwrap(); |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
366 | self.whitespace_satisfied = true; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
367 | self.par_satisfied = self.only_whitespace; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
368 | } |
0 | 369 | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
370 | self.stored_whitespace.clear(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
371 | self.only_whitespace = true; |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
372 | self.ignored_comment_only_line = false; |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
373 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
374 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
375 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
376 | fn main() { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
377 | let cli = CommandLineArgs::parse(); |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
378 | |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
379 | match (cli.input, cli.output) { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
380 | (None, None) => { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
381 | process_buffered(cli.config, io::stdin(), io::stdout()); |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
382 | }, |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
383 | (None, Some(o)) => { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
384 | process_buffered(cli.config, io::stdin(), File::create(o).unwrap()); |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
385 | } |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
386 | (Some(i), None) => { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
387 | process_buffered(cli.config, File::open(i).unwrap(), io::stdout()); |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
388 | } |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
389 | (Some(i), Some(o)) => { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
390 | process_buffered(cli.config, File::open(i).unwrap(), File::create(o).unwrap()); |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
391 | } |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
392 | } |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
393 | } |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
394 | |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
395 | fn process_buffered<I : Read, O : Debug + Write + 'static>( |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
396 | config : Config, |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
397 | input : I, |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
398 | output : O |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
399 | ) -> O { |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
400 | process(config, BufReader::new(input), BufWriter::new(output)).into_inner().unwrap() |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
401 | } |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
402 | |
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
403 | fn process<I : BufRead, O : Write + 'static>(config : Config, input : I, output : O) -> O { |
3
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
404 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
405 | let mut rule : Box<dyn ChainRule<O>> = Box::new(Out { |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
406 | only_whitespace : true, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
407 | stored_whitespace : String::new(), |
3
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
408 | output, |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
409 | whitespace_satisfied : true, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
410 | par_satisfied : true, |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
411 | ignored_comment_only_line : false |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
412 | }); |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
413 | |
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
414 | let mut ctx = Context{ lineno : 0, cli : config, input_only_ws : true}; |
0 | 415 | |
416 | for l in input.lines().map(|l| l.unwrap()) { | |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
417 | ctx.lineno += 1; |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
418 | ctx.input_only_ws = true; |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
419 | for c in l.chars() { |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
420 | ctx.input_only_ws = ctx.input_only_ws && c.is_whitespace(); |
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
421 | rule = rule.consume(c, &ctx); |
0 | 422 | } |
6
de1cf8032322
Implement a more expansible parser
Tuomo Valkonen <tuomov@iki.fi>
parents:
3
diff
changeset
|
423 | rule = rule.consume('\n', &ctx); |
0 | 424 | } |
425 | ||
7
68538da191c7
Make ChainRules dependent on the Write
Tuomo Valkonen <tuomov@iki.fi>
parents:
6
diff
changeset
|
426 | rule.flush(&ctx) |
0 | 427 | } |
8 | 428 | |
429 | ||
430 | #[cfg(test)] | |
431 | mod tests { | |
432 | use super::*; | |
433 | use std::io::Cursor; | |
434 | ||
435 | fn process_str(config : Config, input : &str) -> String { | |
436 | let b = process(config, Cursor::new(input.as_bytes()), Vec::new()); | |
437 | String::from_utf8(b).unwrap() | |
438 | } | |
439 | ||
440 | #[test] | |
441 | fn test1_changes() { | |
442 | let p = |s| process_str(Config { strip_comments : false, strip_whitespace : false }, s); | |
443 | ||
444 | assert_eq!(p("x\\added{y}z\n"), "xyz\n"); | |
445 | assert_eq!(p("x\\deleted{y}z\n"), "xz\n"); | |
446 | assert_eq!(p("x\\replaced{y}{q}z\n"), "xyz\n"); | |
447 | ||
448 | // TODO: working as intended, but not how would be good for LaTeX consistency | |
449 | assert_eq!(p("x\\added{y{q}\\bar}z\n"), "xy{q}\\barz\n"); | |
450 | assert_eq!(p("x\\deleted{y{q}\\bar}z\n"), "xz\n"); | |
451 | ||
452 | assert_eq!(p("x\\added{y\na}z\n"), "xy\naz\n"); | |
453 | assert_eq!(p("x\\deleted{y\na}z\n"), "xz\n"); | |
454 | assert_eq!(p("x\\replaced{y\na}{q}z\n"), "xy\naz\n"); | |
455 | ||
456 | assert_eq!(p("x\\added{y\\test{\na}}z\n"), "xy\\test{\na}z\n"); | |
457 | assert_eq!(p("x\\deleted{y\\test{\na}}z\n"), "xz\n"); | |
458 | ||
459 | assert_eq!(p("x\\deleted{y\\added{a}}z\n"), "xz\n"); | |
460 | ||
461 | assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), | |
462 | "%\n\\begin{a}\n x\n y\n z\\end{a}\n"); | |
463 | ||
464 | assert_eq!(p("\\added\n\n {q}\n"), "q\n"); | |
465 | assert_eq!(p("\\replaced{\\{q}\n \n{z}\n"), "\\{q\n"); | |
466 | assert_eq!(p("\\replaced{q\\}}\n \n{z}\n"), "q\\}\n"); | |
467 | } | |
468 | ||
469 | #[test] | |
470 | fn test2_comments() { | |
471 | let p = |s| process_str(Config { strip_comments : true, strip_whitespace : false }, s); | |
472 | ||
473 | assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), | |
474 | "\\begin{a}\n x\n y\n z\\end{a}\n"); | |
475 | ||
476 | assert_eq!(p(" test % comments"), " test \n"); | |
477 | ||
478 | assert_eq!(p(" test% comments"), " test%\n"); | |
479 | ||
480 | assert_eq!(p(" % comments\nline"), "line\n"); | |
481 | } | |
482 | ||
483 | #[test] | |
484 | fn test3_whitespace() { | |
485 | let p = |s| process_str(Config { strip_comments : false, strip_whitespace : true }, s); | |
486 | ||
487 | assert_eq!(p("a\n\n\n\nb\n"), "a\n\nb\n"); | |
488 | assert_eq!(p("a\n\n \n\nb\n"), "a\n\nb\n"); | |
489 | assert_eq!(p("a \n\n\n\nb\n"), "a\n\nb\n"); | |
490 | ||
491 | assert_eq!(p("a\n\n% comment\n\nb\n"), "a\n\n% comment\n\nb\n"); | |
492 | assert_eq!(p("a\n\n % comment \n\nb\n"), "a\n\n % comment\n\nb\n"); | |
493 | assert_eq!(p("a % comment \n\n\n\nb\n"), "a % comment\n\nb\n"); | |
494 | } | |
495 | ||
496 | #[test] | |
497 | fn test4_comments_whitespace() { | |
498 | let p = |s| process_str(Config { strip_comments : true, strip_whitespace : true }, s); | |
499 | ||
500 | assert_eq!(p("\\added{%\n\\begin{a}\n x\n y\n z\\end{a}}\n"), | |
501 | "\\begin{a}\n x\n y\n z\\end{a}\n"); | |
502 | ||
503 | assert_eq!(p(" test % comments"), " test\n"); | |
504 | ||
505 | assert_eq!(p(" % comments\nline"), "line\n"); | |
506 | ||
507 | assert_eq!(p("a\n\n% comment\n\nb\n"), "a\n\nb\n"); | |
508 | assert_eq!(p("a\n\n % comment \n\nb\n"), "a\n\nb\n"); | |
509 | assert_eq!(p("a % comment \n\n\n\nb\n"), "a\n\nb\n"); | |
510 | ||
511 | assert_eq!(p("\\added{a % comment \n\n\n\nb}\n"), "a\n\nb\n"); | |
512 | assert_eq!(p("c\\added{a % comment \n\n\n\nb}\n"), "ca\n\nb\n"); | |
513 | } | |
514 | } |