src/main.rs

Thu, 19 Oct 2023 15:36:39 -0500

author
Tuomo Valkonen <tuomov@iki.fi>
date
Thu, 19 Oct 2023 15:36:39 -0500
changeset 4
8082eb5a754b
parent 3
cec573b16b46
child 6
de1cf8032322
permissions
-rw-r--r--

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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
33
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
34 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
35 enum Element {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
36 Added,
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
37 Deleted,
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
38 Replaced,
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
39 Other,
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
40 Comment,
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
41 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
42
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
43 #[derive(Clone, Copy, Debug, PartialEq, Eq)]
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
44 enum Status {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
45 Output(Element),
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
46 Ignore(Element),
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
47 Scan(Element, bool),
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
48 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
142 let mut lineno = 0;
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
143
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
144 for l in input.lines().map(|l| l.unwrap()) {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
145 lineno += 1;
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
146 let mut chars = l.chars();
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
150 'process_line: loop {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
151 let next_char = match maybe_next_char {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
152 None => chars.next(),
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
153 Some(c) => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
154 maybe_next_char = None;
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
155 Some(c)
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
156 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
160 (_, None) => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
161 break 'process_line;
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
162 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
163 (st @ (Output(e) | Ignore(e)), Some('\\')) if e != Comment => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
164 let mut command = String::new();
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
165 let mut first = true;
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
166 maybe_next_char = 'scan_command: loop {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
167 match chars.next() {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
168 Some(c) if first && (c=='{' || c=='}' || c=='\\') => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
169 command.push(c);
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
170 break 'scan_command None;
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
171 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
172 Some(c) if c.is_alphanumeric() => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
173 command.push(c);
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
174 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
175 maybe_c => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
176 break 'scan_command maybe_c;
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
177 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
178 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
179 first = false;
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
180 };
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
181 let output_guard = if let Ignore(_) = st { false } else { true };
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
182 match command.as_str() {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
185 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
188 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
191 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
192 _ => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
196 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
197 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
198 };
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
199 },
2
254e1e4bd795 Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
200 (Scan(next, out), Some(c)) => {
0
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
201 match c {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
205 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
206 ' ' => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
207 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
208 _ => panic!("Non-whitespace character ({c}) separating arguments on\
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
209 line {lineno}"),
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
210 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
211 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
215 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
218 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
221 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
225 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
232 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
245 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
251 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
254 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
255 (Ignore(_), Some(_)) => {
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
256 },
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
257 };
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
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
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
261 }
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
262
2
254e1e4bd795 Add whitespace and comment stripping
Tuomo Valkonen <tuomov@iki.fi>
parents: 0
diff changeset
263 o.flush();
0
548bf3cc032e Initial rough version
Tuomo Valkonen <tuomov@iki.fi>
parents:
diff changeset
264 }

mercurial