Skip to content

Commit

Permalink
Consistently prefer unwrap() in build.rs.
Browse files Browse the repository at this point in the history
Previously we had a mix of `?` (requiring a complex `main` return type)
and `unwrap`. This extent plumps for the latter, partly because if
something does go wrong, `unwrap` gives an error message which
pinpoints which particular line caused the problem.
  • Loading branch information
ltratt committed Feb 9, 2023
1 parent 3da61f8 commit 58b28e9
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 40 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ the root of our project with the following content:
use cfgrammar::yacc::YaccKind;
use lrlex::CTLexerBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
CTLexerBuilder::new()
.lrpar_config(|ctp| {
ctp.yacckind(YaccKind::Grmtools)
.grammar_in_src_dir("calc.y")
.unwrap()
})
.lexer_in_src_dir("calc.l")?
.build()?;
.lexer_in_src_dir("calc.l")
.unwrap()
.build()
.unwrap();
Ok(())
}
```
Expand Down
10 changes: 6 additions & 4 deletions doc/src/manuallexer.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,14 @@ use cfgrammar::yacc::YaccKind;
use lrlex::{ct_token_map, DefaultLexerTypes};
use lrpar::CTParserBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
let ctp = CTParserBuilder::<DefaultLexerTypes<u8>>::new()
.yacckind(YaccKind::Grmtools)
.grammar_in_src_dir("grammar.y")?
.build()?;
ct_token_map::<u8>("token_map", ctp.token_map(), None)
.grammar_in_src_dir("grammar.y")
.unwrap()
.build()
.unwrap();
ct_token_map::<u8>("token_map", ctp.token_map(), None).unwrap()
}
```

Expand Down
6 changes: 3 additions & 3 deletions doc/src/quickstart.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ Our `build.rs` file thus looks as follows:
use cfgrammar::yacc::YaccKind;
use lrlex::CTLexerBuilder;
fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
CTLexerBuilder::new()
.lrpar_config(|ctp| {
ctp.yacckind(YaccKind::Grmtools)
.grammar_in_src_dir("calc.y")
.unwrap()
})
.lexer_in_src_dir("calc.l")?
.build()?;
Ok(())
.build()
.unwrap();
}
```

Expand Down
9 changes: 6 additions & 3 deletions lrlex/examples/calc_manual_lex/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@ const TOKENS_MAP: &[(&str, &str)] = &[
(")", "RBRACK"),
];

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
let ctp = CTParserBuilder::<DefaultLexerTypes<u8>>::new()
.yacckind(YaccKind::Grmtools)
.grammar_in_src_dir("calc.y")?
.build()?;
.grammar_in_src_dir("calc.y")
.unwrap()
.build()
.unwrap();
ct_token_map::<u8>(
"token_map",
ctp.token_map(),
Some(&TOKENS_MAP.iter().cloned().collect()),
)
.unwrap();
}
9 changes: 6 additions & 3 deletions lrlex/examples/calclex/build.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use lrlex::CTLexerBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
CTLexerBuilder::new().lexer_in_src_dir("calc.l")?.build()?;
Ok(())
fn main() {
CTLexerBuilder::new()
.lexer_in_src_dir("calc.l")
.unwrap()
.build()
.unwrap();
}
9 changes: 5 additions & 4 deletions lrpar/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,17 @@ as follows:
use cfgrammar::yacc::YaccKind;
use lrlex::CTLexerBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
CTLexerBuilder::new()
.lrpar_config(|ctp| {
ctp.yacckind(YaccKind::Grmtools)
.grammar_in_src_dir("calc.y")
.unwrap()
})
.lexer_in_src_dir("calc.l")?
.build()?;
Ok(())
.lexer_in_src_dir("calc.l")
.unwrap()
.build()
.unwrap();
}
```

Expand Down
9 changes: 5 additions & 4 deletions lrpar/examples/calc_actions/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use cfgrammar::yacc::YaccKind;
use lrlex::{self, CTLexerBuilder};

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
// Since we're using both lrlex and lrpar, we use lrlex's `lrpar_config` convenience function
// that makes it easy to a) create a lexer and parser and b) link them together.
CTLexerBuilder::new()
Expand All @@ -13,7 +13,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.grammar_in_src_dir("calc.y")
.unwrap()
})
.lexer_in_src_dir("calc.l")?
.build()?;
Ok(())
.lexer_in_src_dir("calc.l")
.unwrap()
.build()
.unwrap();
}
9 changes: 5 additions & 4 deletions lrpar/examples/calc_ast/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use cfgrammar::yacc::YaccKind;
use lrlex::CTLexerBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
// Since we're using both lrlex and lrpar, we use lrlex's `lrpar_config` convenience function
// that makes it easy to a) create a lexer and parser and b) link them together.
CTLexerBuilder::new()
Expand All @@ -13,7 +13,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.grammar_in_src_dir("calc.y")
.unwrap()
})
.lexer_in_src_dir("calc.l")?
.build()?;
Ok(())
.lexer_in_src_dir("calc.l")
.unwrap()
.build()
.unwrap();
}
9 changes: 5 additions & 4 deletions lrpar/examples/calc_parsetree/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cfgrammar::yacc::{YaccKind, YaccOriginalActionKind};
use lrlex::CTLexerBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
// Since we're using both lrlex and lrpar, we use lrlex's `lrpar_config` convenience function
// that makes it easy to a) create a lexer and parser and b) link them together.
CTLexerBuilder::new()
Expand All @@ -12,7 +12,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.grammar_in_src_dir("calc.y")
.unwrap()
})
.lexer_in_src_dir("calc.l")?
.build()?;
Ok(())
.lexer_in_src_dir("calc.l")
.unwrap()
.build()
.unwrap();
}
9 changes: 5 additions & 4 deletions lrpar/examples/start_states/build.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use cfgrammar::yacc::{YaccKind, YaccOriginalActionKind};
use lrlex::CTLexerBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
fn main() {
// Since we're using both lrlex and lrpar, we use lrlex's `lrpar_config` convenience function
// that makes it easy to a) create a lexer and parser and b) link them together.
CTLexerBuilder::new()
Expand All @@ -12,7 +12,8 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.grammar_in_src_dir("comment.y")
.unwrap()
})
.lexer_in_src_dir("comment.l")?
.build()?;
Ok(())
.lexer_in_src_dir("comment.l")
.unwrap()
.build()
.unwrap();
}
9 changes: 5 additions & 4 deletions lrpar/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,17 @@
//! use cfgrammar::yacc::YaccKind;
//! use lrlex::CTLexerBuilder;
//!
//! fn main() -> Result<(), Box<dyn std::error::Error>> {
//! fn main() {
//! CTLexerBuilder::new()
//! .lrpar_config(|ctp| {
//! ctp.yacckind(YaccKind::Grmtools)
//! .grammar_in_src_dir("calc.y")
//! .unwrap()
//! })
//! .lexer_in_src_dir("calc.l")?
//! .build()?;
//! Ok(())
//! .lexer_in_src_dir("calc.l")
//! .unwrap()
//! .build()
//! .unwrap();
//! }
//! ```
//!
Expand Down

0 comments on commit 58b28e9

Please sign in to comment.