mirror of
https://github.com/fluencelabs/lalrpop
synced 2025-04-24 18:52:16 +00:00
feat: Add a setting to strip indentation from the generated grammar
This commit is contained in:
parent
2d50a5d703
commit
9f3a978fde
@ -927,6 +927,7 @@ fn verify_lalrpop_generates_itself() {
|
||||
Command::new("../target/debug/lalrpop")
|
||||
.args(&[
|
||||
"--force",
|
||||
"--no-whitespace",
|
||||
"--out-dir",
|
||||
out_dir,
|
||||
copied_grammar_file
|
||||
|
@ -103,6 +103,13 @@ impl Configuration {
|
||||
self
|
||||
}
|
||||
|
||||
/// If false, shrinks the generated code by removing redundant white space.
|
||||
/// Default is true.
|
||||
pub fn emit_whitespace(&mut self, val: bool) -> &mut Configuration {
|
||||
self.session.emit_whitespace = val;
|
||||
self
|
||||
}
|
||||
|
||||
/// If true, emit report file about generated code.
|
||||
pub fn emit_report(&mut self, val: bool) -> &mut Configuration {
|
||||
self.session.emit_report = val;
|
||||
|
@ -51,6 +51,10 @@ fn main1() -> io::Result<()> {
|
||||
config.emit_comments(true);
|
||||
}
|
||||
|
||||
if args.flag_no_whitespace {
|
||||
config.emit_whitespace(false);
|
||||
}
|
||||
|
||||
if args.flag_report {
|
||||
config.emit_report(true);
|
||||
}
|
||||
@ -101,6 +105,7 @@ Options:
|
||||
--features FEATURES Comma separated list of features for conditional compilation.
|
||||
-f, --force Force execution, even if the .lalrpop file is older than the .rs file.
|
||||
-c, --color Force colorful output, even if this is not a TTY.
|
||||
--no-whitespace Removes redundant whitespace from the generated file. (Default: false)
|
||||
--comments Enable comments in the generated code.
|
||||
--report Generate report files.
|
||||
";
|
||||
@ -114,6 +119,7 @@ struct Args {
|
||||
flag_force: bool,
|
||||
flag_color: bool,
|
||||
flag_comments: bool,
|
||||
flag_no_whitespace: bool,
|
||||
flag_report: bool,
|
||||
flag_version: bool,
|
||||
}
|
||||
@ -181,4 +187,13 @@ mod test {
|
||||
.unwrap();
|
||||
assert_eq!(args.flag_features, Some("test,abc".to_string()));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn emit_whitespace() {
|
||||
let argv = || vec!["lalrpop", "--no-whitespace", "file.lalrpop"];
|
||||
let args: Args = Docopt::new(USAGE)
|
||||
.and_then(|d| d.argv(argv().into_iter()).deserialize())
|
||||
.unwrap();
|
||||
assert!(args.flag_no_whitespace, true);
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -53,11 +53,15 @@ impl<W: Write> RustWrite<W> {
|
||||
}
|
||||
|
||||
fn write_indentation(&mut self) -> io::Result<()> {
|
||||
write!(self.write, "{0:1$}", "", self.indent)
|
||||
if Tls::session().emit_whitespace {
|
||||
write!(self.write, "{0:1$}", "", self.indent)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn write_indented(&mut self, out: &str) -> io::Result<()> {
|
||||
writeln!(self.write, "{0:1$}{2}", "", self.indent, out)
|
||||
self.write_indentation()?;
|
||||
writeln!(self.write, "{}", out)
|
||||
}
|
||||
|
||||
pub fn write_table_row<I, C>(&mut self, iterable: I) -> io::Result<()>
|
||||
@ -65,7 +69,8 @@ impl<W: Write> RustWrite<W> {
|
||||
I: IntoIterator<Item = (i32, C)>,
|
||||
C: fmt::Display,
|
||||
{
|
||||
if Tls::session().emit_comments {
|
||||
let session = Tls::session();
|
||||
if session.emit_comments {
|
||||
for (i, comment) in iterable {
|
||||
try!(self.write_indentation());
|
||||
try!(writeln!(self.write, "{}, {}", i, comment));
|
||||
@ -74,7 +79,7 @@ impl<W: Write> RustWrite<W> {
|
||||
try!(self.write_indentation());
|
||||
let mut first = true;
|
||||
for (i, _comment) in iterable {
|
||||
if !first {
|
||||
if !first && session.emit_whitespace {
|
||||
try!(write!(self.write, " "));
|
||||
}
|
||||
try!(write!(self.write, "{},", i));
|
||||
|
@ -41,6 +41,9 @@ pub struct Session {
|
||||
/// forth.
|
||||
pub emit_comments: bool,
|
||||
|
||||
/// Emit whitespace in the generated code to improve readability.
|
||||
pub emit_whitespace: bool,
|
||||
|
||||
/// Emit report file about generated code
|
||||
pub emit_report: bool,
|
||||
|
||||
@ -92,6 +95,7 @@ impl Session {
|
||||
out_dir: None,
|
||||
force_build: false,
|
||||
emit_comments: false,
|
||||
emit_whitespace: true,
|
||||
emit_report: false,
|
||||
color_config: ColorConfig::default(),
|
||||
max_errors: 1,
|
||||
@ -117,6 +121,7 @@ impl Session {
|
||||
out_dir: None,
|
||||
force_build: false,
|
||||
emit_comments: false,
|
||||
emit_whitespace: true,
|
||||
emit_report: false,
|
||||
color_config: ColorConfig::IfTty,
|
||||
max_errors: 1,
|
||||
|
Loading…
x
Reference in New Issue
Block a user