Skip to content

Make examples and generated code work with rust_2018_idioms lint. #367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- [grmtools parsing idioms](parsing_idioms.md)
- [Error recovery](errorrecovery.md)
- [An AST evaluator](ast_example.md)
- [Rust Editions](editions.md)
- [The individual libraries and tools](libsandtools.md)
- [lrpar](lrpar.md)
- [lrlex](lrlex.md)
Expand Down
21 changes: 21 additions & 0 deletions doc/src/editions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Rust Editions

The [edition](https://doc.rust-lang.org/edition-guide/rust-2021/index.html)
of rust used by `grmtools` updates as the rust language evolves. We try to
keep code generated by `CTParserBuilder` and `CTLexerBuilder` building with
older versions of rust, so that downstream users can use the edition that
suits their requirements.

## Controlling edition used during code generation

`CTLexerBuilder` and `CTParserBuilder` both have functions, `rust_edition()`
that accept a `lrpar::RustEdition` and `lrlex::RustEdition` respectively.

## Known edition incompatibility in the book

While there is a preference for keeping the code in this manual working with all
editions, exceptions may be made when for clarity.

* In [An AST evaluator](ast_example.md), with the rust_2018_idioms lint deprecates
some behavior which was previously accepted by the 2015 edition. The `eval` function has
an elided lifetime that must be given explicitly as `lexer: &dyn NonStreamingLexer<'_, DefaultLexeme, u32>`.
19 changes: 19 additions & 0 deletions lrlex/src/lib/ctbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,16 @@ impl Visibility {
}
}

/// Specifies the [Rust Edition] that will be emitted during code generation.
///
/// [Rust Edition]: https://doc.rust-lang.org/edition-guide/rust-2021/index.html
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum RustEdition {
Rust2015,
Rust2018,
Rust2021,
}

/// A `CTLexerBuilder` allows one to specify the criteria for building a statically generated
/// lexer.
pub struct CTLexerBuilder<'a, LexemeT: Lexeme<StorageT>, StorageT: Debug + Eq + Hash = u32> {
Expand All @@ -79,6 +89,7 @@ pub struct CTLexerBuilder<'a, LexemeT: Lexeme<StorageT>, StorageT: Debug + Eq +
lexerkind: LexerKind,
mod_name: Option<&'a str>,
visibility: Visibility,
rust_edition: RustEdition,
rule_ids_map: Option<HashMap<String, StorageT>>,
allow_missing_terms_in_lexer: bool,
allow_missing_tokens_in_parser: bool,
Expand Down Expand Up @@ -124,6 +135,7 @@ where
lexerkind: LexerKind::LRNonStreamingLexer,
mod_name: None,
visibility: Visibility::Private,
rust_edition: RustEdition::Rust2021,
rule_ids_map: None,
allow_missing_terms_in_lexer: false,
allow_missing_tokens_in_parser: true,
Expand Down Expand Up @@ -249,6 +261,13 @@ where
self
}

/// Sets the rust edition to be used for generated code. Defaults to the latest edition of
/// rust supported by grmtools.
pub fn rust_edition(mut self, edition: RustEdition) -> Self {
self.rust_edition = edition;
self
}

/// Set this lexer builder's map of rule IDs to `rule_ids_map`. By default, lexing rules have
/// arbitrary, but distinct, IDs. Setting the map of rule IDs (from rule names to `StorageT`)
/// allows users to synchronise a lexer and parser and to check that all rules are used by both
Expand Down
2 changes: 1 addition & 1 deletion lrlex/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ mod parser;
#[allow(deprecated)]
pub use crate::ctbuilder::LexerBuilder;
pub use crate::{
ctbuilder::{ct_token_map, CTLexer, CTLexerBuilder, LexerKind, Visibility},
ctbuilder::{ct_token_map, CTLexer, CTLexerBuilder, LexerKind, RustEdition, Visibility},
lexemes::DefaultLexeme,
lexer::{LRNonStreamingLexer, LRNonStreamingLexerDef, LexerDef, Rule},
parser::StartState,
Expand Down
5 changes: 4 additions & 1 deletion lrpar/examples/calc_actions/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#![deny(rust_2018_idioms)]
use cfgrammar::yacc::YaccKind;
use lrlex::CTLexerBuilder;
use lrlex::{self, CTLexerBuilder};

fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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()
.rust_edition(lrlex::RustEdition::Rust2021)
.lrpar_config(|ctp| {
ctp.yacckind(YaccKind::Grmtools)
.rust_edition(lrpar::RustEdition::Rust2021)
.grammar_in_src_dir("calc.y")
.unwrap()
})
Expand Down
3 changes: 3 additions & 0 deletions lrpar/examples/calc_ast/build.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
#![deny(rust_2018_idioms)]
use cfgrammar::yacc::YaccKind;
use lrlex::CTLexerBuilder;

fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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()
.rust_edition(lrlex::RustEdition::Rust2021)
.lrpar_config(|ctp| {
ctp.yacckind(YaccKind::Grmtools)
.rust_edition(lrpar::RustEdition::Rust2021)
.grammar_in_src_dir("calc.y")
.unwrap()
})
Expand Down
2 changes: 2 additions & 0 deletions lrpar/examples/calc_parsetree/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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()
.rust_edition(lrlex::RustEdition::Rust2021)
.lrpar_config(|ctp| {
ctp.yacckind(YaccKind::Original(YaccOriginalActionKind::GenericParseTree))
.rust_edition(lrpar::RustEdition::Rust2021)
.grammar_in_src_dir("calc.y")
.unwrap()
})
Expand Down
2 changes: 2 additions & 0 deletions lrpar/examples/start_states/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
// 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()
.rust_edition(lrlex::RustEdition::Rust2021)
.lrpar_config(|ctp| {
ctp.yacckind(YaccKind::Original(YaccOriginalActionKind::GenericParseTree))
.rust_edition(lrpar::RustEdition::Rust2021)
.grammar_in_src_dir("comment.y")
.unwrap()
})
Expand Down
48 changes: 40 additions & 8 deletions lrpar/src/lib/ctbuilder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,16 @@ pub enum Visibility {
PublicIn(String),
}

/// Specifies the [Rust Edition] that will be emitted during code generation.
///
/// [Rust Edition]: https://doc.rust-lang.org/edition-guide/rust-2021/index.html
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum RustEdition {
Rust2015,
Rust2018,
Rust2021,
}

impl Visibility {
fn cow_str(&self) -> Cow<'static, str> {
match self {
Expand Down Expand Up @@ -152,6 +162,7 @@ where
warnings_are_errors: bool,
show_warnings: bool,
visibility: Visibility,
rust_edition: RustEdition,
phantom: PhantomData<(LexemeT, StorageT)>,
}

Expand Down Expand Up @@ -192,6 +203,7 @@ where
warnings_are_errors: true,
show_warnings: true,
visibility: Visibility::Private,
rust_edition: RustEdition::Rust2021,
phantom: PhantomData,
}
}
Expand Down Expand Up @@ -309,12 +321,19 @@ where
}

/// If set to true, [CTParserBuilder::build] will print warnings to stderr, or via cargo when
/// running under cargo. Defaults to `true`.
/// running under cargo. Defaults to `true`.
pub fn show_warnings(mut self, b: bool) -> Self {
self.show_warnings = b;
self
}

/// Sets the rust edition to be used for generated code. Defaults to the latest edition of
/// rust supported by grmtools.
pub fn rust_edition(mut self, edition: RustEdition) -> Self {
self.rust_edition = edition;
self
}

/// Statically compile the Yacc file specified by [CTParserBuilder::grammar_path()] into Rust,
/// placing the output into the file spec [CTParserBuilder::output_path()]. Note that three
/// additional files will be created with the same name as specified in [self.output_path] but
Expand Down Expand Up @@ -636,6 +655,7 @@ where
warnings_are_errors: self.warnings_are_errors,
show_warnings: self.show_warnings,
visibility: self.visibility.clone(),
rust_edition: self.rust_edition,
phantom: PhantomData,
};
Ok(cl.build()?.rule_ids)
Expand Down Expand Up @@ -758,12 +778,17 @@ where
outs,
"
#[allow(dead_code)]
pub fn parse(lexer: &dyn ::lrpar::NonStreamingLexer<{lexemet}, {storaget}>)
pub fn parse(lexer: &dyn ::lrpar::NonStreamingLexer<{edition_lifetime} {lexemet}, {storaget}>)
-> (::std::option::Option<::lrpar::Node<{lexemet}, {storaget}>>,
::std::vec::Vec<::lrpar::LexParseError<{lexemet}, {storaget}>>)
{{",
lexemet = type_name::<LexemeT>(),
storaget = type_name::<StorageT>()
storaget = type_name::<StorageT>(),
edition_lifetime = if self.rust_edition != RustEdition::Rust2015 {
"'_, "
} else {
""
},
)
.ok();
}
Expand All @@ -772,11 +797,16 @@ where
outs,
"
#[allow(dead_code)]
pub fn parse(lexer: &dyn ::lrpar::NonStreamingLexer<{lexemet}, {storaget}>)
pub fn parse(lexer: &dyn ::lrpar::NonStreamingLexer<{edition_lifetime} {lexemet}, {storaget}>)
-> ::std::vec::Vec<::lrpar::LexParseError<{lexemet}, {storaget}>>
{{",
lexemet = type_name::<LexemeT>(),
storaget = type_name::<StorageT>()
storaget = type_name::<StorageT>(),
edition_lifetime = if self.rust_edition != RustEdition::Rust2015 {
"'_, "
} else {
""
},
)
.ok();
}
Expand Down Expand Up @@ -818,14 +848,15 @@ where
let actions: ::std::vec::Vec<&dyn Fn(::cfgrammar::RIdx<{storaget}>,
&'lexer dyn ::lrpar::NonStreamingLexer<'input, {lexemet}, {storaget}>,
::cfgrammar::Span,
::std::vec::Drain<::lrpar::parser::AStackType<{lexemet}, {actionskind}<'input>>>,
::std::vec::Drain<{edition_lifetime} ::lrpar::parser::AStackType<{lexemet}, {actionskind}<'input>>>,
{parse_paramty})
-> {actionskind}<'input>> = ::std::vec![{wrappers}];\n",
actionskind = ACTIONS_KIND,
lexemet = type_name::<LexemeT>(),
storaget = type_name::<StorageT>(),
parse_paramty = parse_paramty,
wrappers = wrappers
wrappers = wrappers,
edition_lifetime = if self.rust_edition != RustEdition::Rust2015 { "'_, " } else { "" },
).ok();
write!(
outs,
Expand Down Expand Up @@ -934,7 +965,7 @@ where
" fn {prefix}wrapper_{}<'lexer, 'input: 'lexer>({prefix}ridx: ::cfgrammar::RIdx<{storaget}>,
{prefix}lexer: &'lexer dyn ::lrpar::NonStreamingLexer<'input, {lexemet}, {storaget}>,
{prefix}span: ::cfgrammar::Span,
mut {prefix}args: ::std::vec::Drain<::lrpar::parser::AStackType<{lexemet}, {actionskind}<'input>>>,
mut {prefix}args: ::std::vec::Drain<{edition_lifetime} ::lrpar::parser::AStackType<{lexemet}, {actionskind}<'input>>>,
{parse_paramdef})
-> {actionskind}<'input> {{",
usize::from(pidx),
Expand All @@ -943,6 +974,7 @@ where
prefix = ACTION_PREFIX,
parse_paramdef = parse_paramdef,
actionskind = ACTIONS_KIND,
edition_lifetime = if self.rust_edition != RustEdition::Rust2015 { "'_, " } else { "" },
).ok();

if grm.action(pidx).is_some() {
Expand Down
2 changes: 1 addition & 1 deletion lrpar/src/lib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub mod parser;
mod test_utils;

pub use crate::{
ctbuilder::{CTParser, CTParserBuilder, Visibility},
ctbuilder::{CTParser, CTParserBuilder, RustEdition, Visibility},
lex_api::{LexError, Lexeme, Lexer, NonStreamingLexer},
parser::{LexParseError, Node, ParseError, ParseRepair, RTParserBuilder, RecoveryKind},
};
Expand Down