Thu, 19 Oct 2023 15:36:39 -0500
doc typofix
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 | |
0 | 4 | use std::io; |
3
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
5 | use std::fs::File; |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
6 | use std::io::{BufWriter, BufRead, BufReader}; |
0 | 7 | use std::io::Write; |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
8 | use clap::Parser; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
9 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
10 | /// Command line parameters |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
11 | #[derive(Parser, Debug)] |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
12 | #[clap( |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
13 | about = env!("CARGO_PKG_DESCRIPTION"), |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
14 | author = env!("CARGO_PKG_AUTHORS"), |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
15 | version = env!("CARGO_PKG_VERSION"), |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
16 | )] |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
17 | struct CommandLineArgs { |
3
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
18 | /// Input file (default is stdin) |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
19 | input : Option<String>, |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
20 | |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
21 | /// Output file (defalt is stdout) |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
22 | #[arg(long, short = 'o')] |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
23 | output : Option<String>, |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
24 | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
25 | #[arg(long, short = 'c')] |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
26 | /// Strip comments |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
27 | strip_comments : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
28 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
29 | #[arg(long, short = 'w')] |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
30 | /// Strip unnecessary whitespace |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
31 | strip_whitespace : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
32 | } |
0 | 33 | |
34 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
35 | enum Element { | |
36 | Added, | |
37 | Deleted, | |
38 | Replaced, | |
39 | Other, | |
40 | Comment, | |
41 | } | |
42 | ||
43 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] | |
44 | enum Status { | |
45 | Output(Element), | |
46 | Ignore(Element), | |
47 | Scan(Element, bool), | |
48 | } | |
49 | ||
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
50 | use Status::*; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
51 | use Element::*; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
52 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
53 | struct Out<W : Write> { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
54 | only_whitespace : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
55 | stored_whitespace : String, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
56 | output : W, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
57 | stack : Vec<Status>, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
58 | whitespace_satisfied : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
59 | par_satisfied : bool, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
60 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
61 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
62 | impl<W : Write> Out<W> { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
63 | fn current(&self) -> Status { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
64 | self.stack.last().map_or(Output(Other), |s| *s) |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
65 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
66 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
67 | fn raw_out(&mut self, c : char) { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
68 | write!(self.output, "{}", c).unwrap(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
69 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
70 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
71 | pub fn out(&mut self, c : char) { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
72 | self.only_whitespace = false; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
73 | write!(self.output, "{}{}", self.stored_whitespace, c).unwrap(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
74 | self.stored_whitespace.clear(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
75 | self.whitespace_satisfied = false; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
76 | self.par_satisfied = false; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
77 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
78 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
79 | pub fn whitespace(&mut self, c : char) { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
80 | self.stored_whitespace.push(c); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
81 | } |
0 | 82 | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
83 | 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
|
84 | let cur = self.current(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
85 | let skip_linefeed = if input_only_ws { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
86 | // Need a paragraph break |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
87 | strip_ws && self.par_satisfied |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
88 | } else if strip_ws { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
89 | self.only_whitespace && self.whitespace_satisfied |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
90 | } else if let Ignore(Comment) = cur { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
91 | // Skip comment-only lines if the comment is ignored |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
92 | self.only_whitespace |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
93 | } else if let Ignore(_) = cur { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
94 | // Skip line feeds in ignored bits |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
95 | true |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
96 | } else { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
97 | false |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
98 | }; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
99 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
100 | if !skip_linefeed { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
101 | if !strip_ws { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
102 | write!(self.output, "{}", self.stored_whitespace).unwrap(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
103 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
104 | self.raw_out('\n'); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
105 | self.whitespace_satisfied = true; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
106 | self.par_satisfied = self.only_whitespace; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
107 | } |
0 | 108 | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
109 | if let Ignore(Comment) | Output(Comment) = cur { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
110 | self.stack.pop(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
111 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
112 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
113 | self.stored_whitespace.clear(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
114 | self.only_whitespace = true; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
115 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
116 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
117 | pub fn flush(&mut self) { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
118 | self.output.flush().unwrap(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
119 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
120 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
121 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
122 | fn main() { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
123 | let cli = CommandLineArgs::parse(); |
3
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
124 | let input = cli.input.map_or_else( |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
125 | || Box::new(BufReader::new(io::stdin())) as Box<dyn BufRead>, |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
126 | |f| Box::new(BufReader::new(File::open(f).unwrap())) as Box<dyn BufRead> |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
127 | ); |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
128 | let output = cli.output.map_or_else( |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
129 | || Box::new(BufWriter::new(io::stdout())) as Box<dyn Write>, |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
130 | |f| Box::new(BufWriter::new(File::create(f).unwrap())) as Box<dyn Write> |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
131 | ); |
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
132 | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
133 | let mut o = Out { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
134 | only_whitespace : true, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
135 | stored_whitespace : String::new(), |
3
cec573b16b46
Add command line options for input and output files
Tuomo Valkonen <tuomov@iki.fi>
parents:
2
diff
changeset
|
136 | output, |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
137 | stack : Vec::new(), |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
138 | whitespace_satisfied : true, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
139 | par_satisfied : true, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
140 | }; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
141 | |
0 | 142 | let mut lineno = 0; |
143 | ||
144 | for l in input.lines().map(|l| l.unwrap()) { | |
145 | lineno += 1; | |
146 | let mut chars = l.chars(); | |
147 | let mut maybe_next_char = None; | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
148 | let mut input_only_ws = true; |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
149 | |
0 | 150 | 'process_line: loop { |
151 | let next_char = match maybe_next_char { | |
152 | None => chars.next(), | |
153 | Some(c) => { | |
154 | maybe_next_char = None; | |
155 | Some(c) | |
156 | } | |
157 | }; | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
158 | input_only_ws = input_only_ws && next_char.map_or(true, |c| c.is_whitespace()); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
159 | match(o.current(), next_char) { |
0 | 160 | (_, None) => { |
161 | break 'process_line; | |
162 | }, | |
163 | (st @ (Output(e) | Ignore(e)), Some('\\')) if e != Comment => { | |
164 | let mut command = String::new(); | |
165 | let mut first = true; | |
166 | maybe_next_char = 'scan_command: loop { | |
167 | match chars.next() { | |
168 | Some(c) if first && (c=='{' || c=='}' || c=='\\') => { | |
169 | command.push(c); | |
170 | break 'scan_command None; | |
171 | }, | |
172 | Some(c) if c.is_alphanumeric() => { | |
173 | command.push(c); | |
174 | }, | |
175 | maybe_c => { | |
176 | break 'scan_command maybe_c; | |
177 | } | |
178 | } | |
179 | first = false; | |
180 | }; | |
181 | let output_guard = if let Ignore(_) = st { false } else { true }; | |
182 | match command.as_str() { | |
183 | "added" => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
184 | o.stack.push(Scan(Added, true && output_guard)); |
0 | 185 | }, |
186 | "replaced" => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
187 | o.stack.push(Scan(Replaced, true && output_guard)); |
0 | 188 | }, |
189 | "deleted" => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
190 | o.stack.push(Scan(Deleted, false)); |
0 | 191 | }, |
192 | _ => { | |
193 | if output_guard { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
194 | o.out('\\'); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
195 | command.chars().for_each(|c| o.out(c.clone())); |
0 | 196 | } |
197 | } | |
198 | }; | |
199 | }, | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
200 | (Scan(next, out), Some(c)) => { |
0 | 201 | match c { |
202 | '{' => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
203 | o.stack.pop(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
204 | o.stack.push(if out { Output(next) } else { Ignore(next) }); |
0 | 205 | }, |
206 | ' ' => { | |
207 | }, | |
208 | _ => panic!("Non-whitespace character ({c}) separating arguments on\ | |
209 | line {lineno}"), | |
210 | } | |
211 | }, | |
212 | (Output(e), Some('{')) if e != Comment => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
213 | o.out('{'); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
214 | o.stack.push(Output(Other)); |
0 | 215 | }, |
216 | (Ignore(e), Some('{')) if e != Comment => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
217 | o.stack.push(Ignore(Other)); |
0 | 218 | }, |
219 | (Output(Added) | Ignore(Added) | Output(Deleted) | Ignore(Deleted), Some('}')) => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
220 | o.stack.pop(); |
0 | 221 | }, |
222 | (Output(Replaced) | Ignore(Replaced), Some('}')) => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
223 | o.stack.pop(); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
224 | o.stack.push(Scan(Deleted, false)); |
0 | 225 | }, |
226 | (Output(Other), Some('}')) => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
227 | o.out('}'); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
228 | o.stack.pop(); |
0 | 229 | }, |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
230 | (Ignore(e), Some('}')) if e != Comment => { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
231 | o.stack.pop(); |
0 | 232 | }, |
233 | (Output(e), Some('%')) if e != Comment=> { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
234 | if cli.strip_comments { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
235 | if o.stored_whitespace.is_empty() && !o.only_whitespace { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
236 | // Output comment marker if it is required to maintain |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
237 | // lack of whitespace. |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
238 | o.out('%'); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
239 | } |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
240 | o.stack.push(Ignore(Comment)); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
241 | } else { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
242 | o.out('%'); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
243 | o.stack.push(Output(Comment)); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
244 | } |
0 | 245 | }, |
246 | (Ignore(e), Some('%')) if e != Comment => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
247 | o.stack.push(Ignore(Comment)); |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
248 | }, |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
249 | (Output(_), Some(c)) if c.is_whitespace() => { |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
250 | o.whitespace(c); |
0 | 251 | }, |
252 | (Output(_), Some(c)) => { | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
253 | o.out(c); |
0 | 254 | }, |
255 | (Ignore(_), Some(_)) => { | |
256 | }, | |
257 | }; | |
258 | } | |
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
259 | |
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
260 | o.line_end(cli.strip_whitespace, input_only_ws); |
0 | 261 | } |
262 | ||
2
254e1e4bd795
Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents:
0
diff
changeset
|
263 | o.flush(); |
0 | 264 | } |