From 4ed76783a12153c6a7d3857769c52b325032a298 Mon Sep 17 00:00:00 2001 From: Alixinne Date: Mon, 19 Aug 2024 20:18:27 +0200 Subject: [PATCH] feat: rename lang_util(as) to lang_util(parser) This is due to upgrading to syn v2, where as is parsed as a keyword by parse_args(). To avoid re-implementing a parser for parse_nested_meta, we just rename this argument to parser which isn't a reserved keyword. --- .cargo/{config => config.toml} | 0 lang-lexer/src/token.rs | 76 +++++++++++++++-------------- lang-pp/src/processor/definition.rs | 2 +- lang-pp/src/types/token.rs | 20 ++++---- lang-util-derive/Cargo.toml | 2 +- lang-util-derive/src/token.rs | 32 ++++++------ 6 files changed, 67 insertions(+), 65 deletions(-) rename .cargo/{config => config.toml} (100%) diff --git a/.cargo/config b/.cargo/config.toml similarity index 100% rename from .cargo/config rename to .cargo/config.toml diff --git a/lang-lexer/src/token.rs b/lang-lexer/src/token.rs index 2d62c176e..217645845 100644 --- a/lang-lexer/src/token.rs +++ b/lang-lexer/src/token.rs @@ -10,7 +10,7 @@ use logos::Logos; #[derive(Debug, Clone, PartialEq, lang_util::Token)] #[cfg_attr(feature = "v1", derive(Logos))] -#[cfg_attr(feature = "v1", logos(extras = (crate::ParseContext, crate::ParseOptions)))] +#[cfg_attr(feature = "v1", logos(extrparser = (crate::ParseContext, crate::ParseOptions)))] #[allow(missing_docs)] pub enum Token { #[cfg_attr(feature = "v1", token("const"))] @@ -766,9 +766,9 @@ pub enum Token { feature = "v1", regex("#\\s*\\(\\s*[a-zA-Z_][a-zA-Z_0-9]*\\s*\\)", parse_rs_ident) )] - #[lang_util(as = "ident", kind = "identifier")] + #[lang_util(parser = "ident", kind = "identifier")] Identifier(SmolStr), - #[lang_util(as = "ty_name", kind = "type name")] + #[lang_util(parser = "ty_name", kind = "type name")] TypeName(SmolStr), // Cast from Identifier depending on known type names #[cfg_attr( feature = "v1", @@ -778,21 +778,21 @@ pub enum Token { ) )] #[cfg_attr(feature = "v1", regex(r"[0-9]+[eE][+-]?[0-9]+(f|F)?", parse_f32))] - #[lang_util(as = "float_constant", kind = "literal")] + #[lang_util(parser = "float_constant", kind = "literal")] FloatConstant(f32), #[cfg_attr(feature = "v1", regex(r"0[0-7]*", |lex| parse_int(lex, 8)))] #[cfg_attr(feature = "v1", regex(r"[1-9][0-9]*", |lex| parse_int(lex, 10)))] #[cfg_attr(feature = "v1", regex(r"0[xX][0-9A-Fa-f]+", |lex| parse_int(lex, 16)))] - #[lang_util(as = "int_constant", kind = "literal")] + #[lang_util(parser = "int_constant", kind = "literal")] IntConstant(i32), #[cfg_attr(feature = "v1", regex(r"0[0-7]*[uU]", |lex| parse_uint(lex, 8)))] #[cfg_attr(feature = "v1", regex(r"[1-9][0-9]*[uU]", |lex| parse_uint(lex, 10)))] #[cfg_attr(feature = "v1", regex(r"0[xX][0-9A-Fa-f]+[uU]", |lex| parse_uint(lex, 16)))] - #[lang_util(as = "uint_constant", kind = "literal")] + #[lang_util(parser = "uint_constant", kind = "literal")] UIntConstant(u32), #[cfg_attr(feature = "v1", token("true", |_| true))] #[cfg_attr(feature = "v1", token("false", |_| false))] - #[lang_util(as = "bool_constant", kind = "literal")] + #[lang_util(parser = "bool_constant", kind = "literal")] BoolConstant(bool), #[cfg_attr( feature = "v1", @@ -802,7 +802,7 @@ pub enum Token { ) )] #[cfg_attr(feature = "v1", regex(r"[0-9]+[eE][+-]?[0-9]+(lf|LF)", parse_f64))] - #[lang_util(as = "double_constant", kind = "literal")] + #[lang_util(parser = "double_constant", kind = "literal")] DoubleConstant(f64), #[cfg_attr(feature = "v1", token("<<"))] #[lang_util(token = "<<", kind = "binary operator", kind = "operator")] @@ -974,89 +974,93 @@ pub enum Token { // TODO: Line continuation can happen inside tokens #[cfg_attr(feature = "v1", regex("([ \t\r\n]|\\\\\r?\n)+", logos::skip))] - #[lang_util(display = "", as(display), kind = "trivia")] + #[lang_util(display = "", parser(display), kind = "trivia")] Whitespace, #[cfg_attr(feature = "v1", regex("//(.|\\\\\r?\n)*", |lex| { parse_cmt(lex, true); logos::Skip }))] - #[lang_util(display = "", as(display), kind = "trivia")] + #[lang_util(display = "", parser(display), kind = "trivia")] SingleLineComment, #[cfg_attr(feature = "v1", regex("/\\*([^*]|\\*[^/])+\\*/", |lex| { parse_cmt(lex, false); logos::Skip }))] - #[lang_util(display = "", as(display), kind = "trivia")] + #[lang_util(display = "", parser(display), kind = "trivia")] MultiLineComment, // TODO: Line continuations in preprocessor pragmas? #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*define"))] - #[lang_util(display = "#define", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#define", parser(display), kind = "preprocessor directive")] PpDefine, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*else"))] - #[lang_util(display = "#else", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#else", parser(display), kind = "preprocessor directive")] PpElse, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*elif"))] - #[lang_util(display = "#elif", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#elif", parser(display), kind = "preprocessor directive")] PpElif, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*endif"))] - #[lang_util(display = "#endif", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#endif", parser(display), kind = "preprocessor directive")] PpEndIf, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*error"))] - #[lang_util(display = "#error", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#error", parser(display), kind = "preprocessor directive")] PpError, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*if"))] - #[lang_util(display = "#if", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#if", parser(display), kind = "preprocessor directive")] PpIf, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*ifdef"))] - #[lang_util(display = "#ifdef", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#ifdef", parser(display), kind = "preprocessor directive")] PpIfDef, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*ifndef"))] - #[lang_util(display = "#ifndef", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#ifndef", parser(display), kind = "preprocessor directive")] PpIfNDef, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*include"))] - #[lang_util(display = "#include", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#include", parser(display), kind = "preprocessor directive")] PpInclude, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*line"))] - #[lang_util(display = "#line", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#line", parser(display), kind = "preprocessor directive")] PpLine, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*pragma"))] - #[lang_util(display = "#pragma", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#pragma", parser(display), kind = "preprocessor directive")] PpPragma, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*undef"))] - #[lang_util(display = "#undef", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#undef", parser(display), kind = "preprocessor directive")] PpUndef, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*version"))] - #[lang_util(display = "#version", as(display), kind = "preprocessor directive")] + #[lang_util(display = "#version", parser(display), kind = "preprocessor directive")] PpVersion, #[cfg_attr(feature = "v1", regex("#([ \t]|\\\\\r?\n)*extension"))] - #[lang_util(display = "#extension", as(display), kind = "preprocessor directive")] + #[lang_util( + display = "#extension", + parser(display), + kind = "preprocessor directive" + )] PpExtension, #[lang_util( display = "", - as(display), + parser(display), kind = "preprocessor string" )] PpRest(String), - #[lang_util(display = "core", as(display), kind = "version profile")] + #[lang_util(display = "core", parser(display), kind = "version profile")] PpCore, - #[lang_util(display = "compatibility", as(display), kind = "version profile")] + #[lang_util(display = "compatibility", parser(display), kind = "version profile")] PpCompatibility, - #[lang_util(display = "es", as(display), kind = "version profile")] + #[lang_util(display = "es", parser(display), kind = "version profile")] PpEs, - #[lang_util(display = "require", as(display), kind = "extension behavior")] + #[lang_util(display = "require", parser(display), kind = "extension behavior")] PpExtRequire, - #[lang_util(display = "enable", as(display), kind = "extension behavior")] + #[lang_util(display = "enable", parser(display), kind = "extension behavior")] PpExtEnable, - #[lang_util(display = "warn", as(display), kind = "extension behavior")] + #[lang_util(display = "warn", parser(display), kind = "extension behavior")] PpExtWarn, - #[lang_util(display = "disable", as(display), kind = "extension behavior")] + #[lang_util(display = "disable", parser(display), kind = "extension behavior")] PpExtDisable, - #[lang_util(display = "<{}>", as(display), kind = "include path")] + #[lang_util(display = "<{}>", parser(display), kind = "include path")] PpPathAbsolute(String), - #[lang_util(display = "\"{}\"", as(display), kind = "include path")] + #[lang_util(display = "\"{}\"", parser(display), kind = "include path")] PpPathRelative(String), #[cfg_attr(feature = "v1", error)] - #[lang_util(display = "", as(display), kind = "error")] + #[lang_util(display = "", parser(display), kind = "error")] Error, } diff --git a/lang-pp/src/processor/definition.rs b/lang-pp/src/processor/definition.rs index 2dc54fc3c..2eac13dd6 100644 --- a/lang-pp/src/processor/definition.rs +++ b/lang-pp/src/processor/definition.rs @@ -838,7 +838,7 @@ impl<'d> MacroInvocation<'d> { // spec-defined expected result. let result: Vec<_> = events .into_iter() - .group_by(Event::is_token) + .chunk_by(Event::is_token) .into_iter() .flat_map(|(is_token, events)| { if is_token { diff --git a/lang-pp/src/types/token.rs b/lang-pp/src/types/token.rs index 840b1cd8c..7041d7476 100644 --- a/lang-pp/src/types/token.rs +++ b/lang-pp/src/types/token.rs @@ -1160,25 +1160,25 @@ impl TypeName { #[allow(non_camel_case_types)] pub enum Token { /// Identifier - #[lang_util(as = "IDENT", kind = "identifier")] + #[lang_util(parser = "IDENT", kind = "identifier")] IDENT(SmolStr), /// Type name - #[lang_util(as = "TYPE_NAME", kind = "type name")] + #[lang_util(parser = "TYPE_NAME", kind = "type name")] TYPE_NAME(TypeName), /// Float constant - #[lang_util(as = "FLOAT_CONST", kind = "float constant", kind = "literal")] + #[lang_util(parser = "FLOAT_CONST", kind = "float constant", kind = "literal")] FLOAT_CONST(f32), /// Int constant - #[lang_util(as = "INT_CONST", kind = "int constant", kind = "literal")] + #[lang_util(parser = "INT_CONST", kind = "int constant", kind = "literal")] INT_CONST(i32), /// Unsigned int constant - #[lang_util(as = "UINT_CONST", kind = "uint constant", kind = "literal")] + #[lang_util(parser = "UINT_CONST", kind = "uint constant", kind = "literal")] UINT_CONST(u32), /// Bool constant - #[lang_util(as = "BOOL_CONST", kind = "bool constant", kind = "literal")] + #[lang_util(parser = "BOOL_CONST", kind = "bool constant", kind = "literal")] BOOL_CONST(bool), /// Double constant - #[lang_util(as = "DOUBLE_CONST", kind = "double constant", kind = "literal")] + #[lang_util(parser = "DOUBLE_CONST", kind = "double constant", kind = "literal")] DOUBLE_CONST(f64), // Multi-char tokens /// << @@ -1601,13 +1601,13 @@ pub enum Token { USING, // Other /// Whitespaace - #[lang_util(display = "", as(display), kind = "trivia")] + #[lang_util(display = "", parser(display), kind = "trivia")] WS, /// Comment (single-line or multi-line) - #[lang_util(display = "", as(display), kind = "trivia")] + #[lang_util(display = "", parser(display), kind = "trivia")] COMMENT, /// Marker for invalid tokens - #[lang_util(display = "", as(display), kind = "error")] + #[lang_util(display = "", parser(display), kind = "error")] ERROR(ErrorKind), } diff --git a/lang-util-derive/Cargo.toml b/lang-util-derive/Cargo.toml index 293794df0..cd1c645c1 100644 --- a/lang-util-derive/Cargo.toml +++ b/lang-util-derive/Cargo.toml @@ -20,4 +20,4 @@ proc-macro = true darling = "0.20" proc-macro2 = "1.0" quote = "1.0" -syn = "1.0" +syn = "2.0" diff --git a/lang-util-derive/src/token.rs b/lang-util-derive/src/token.rs index 79e256b8a..3c13b790a 100644 --- a/lang-util-derive/src/token.rs +++ b/lang-util-derive/src/token.rs @@ -23,7 +23,7 @@ impl FromMeta for TokenDisplay { }) } - fn from_list(items: &[syn::NestedMeta]) -> darling::Result { + fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result { Ok(Self { format: String::from_nested_meta( items @@ -44,7 +44,7 @@ struct TokenAttr { } impl FromMeta for TokenAttr { - fn from_list(items: &[syn::NestedMeta]) -> darling::Result { + fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result { Ok(Self { token: String::from_nested_meta( items @@ -65,14 +65,14 @@ impl FromMeta for AsParser { Ok(Self::RawString(value.to_owned())) } - fn from_nested_meta(item: &syn::NestedMeta) -> darling::Result { + fn from_nested_meta(item: &darling::ast::NestedMeta) -> darling::Result { match item { - syn::NestedMeta::Meta(syn::Meta::Path(p)) => Ok(Self::Path(p.to_owned())), + darling::ast::NestedMeta::Meta(syn::Meta::Path(p)) => Ok(Self::Path(p.to_owned())), _ => Err(darling::Error::unsupported_format("meta")), } } - fn from_list(items: &[syn::NestedMeta]) -> darling::Result { + fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result { if let Some(item) = items.first() { return Self::from_nested_meta(item); } @@ -89,7 +89,7 @@ struct TokenVariant { attrs: Vec, #[darling(default)] display: Option, - #[darling(default, rename = "as")] + #[darling(default, rename = "parser")] as_parser: Option, #[darling(default, rename = "token")] fallback_token: Option, @@ -381,14 +381,9 @@ type TokenAttrTy<'s> = Option<(darling::Result, proc_macro2::Span)>; fn parse_token_attr(variant: &TokenVariant) -> TokenAttrTy { // Unit struct, is there a token impl? for attr in variant.attrs.iter() { - if attr - .path - .get_ident() - .map(|ident| ident == "token") - .unwrap_or(false) - { + if attr.path().is_ident("token") { return Some(( - attr.parse_meta() + attr.parse_args() .map_err(darling::Error::custom) .and_then(|meta| TokenAttr::from_meta(&meta)), attr.span(), @@ -420,10 +415,10 @@ enum AsParserError { impl std::fmt::Display for AsParserError { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Self::MissingDisplayImpl => write!(f, "in lang_util attribute, `as = display` specified but not display implementation provided"), - Self::InvalidAs => write!(f, "invalid `as` value, expected display or a literal string"), + Self::MissingDisplayImpl => write!(f, "in lang_util attribute, `parser = display` specified but not display implementation provided"), + Self::InvalidAs => write!(f, "invalid `parser` value, expected display or a literal string"), Self::InvalidTokenAttribute(error) => write!(f, "invalid token attribute: {}", error), - Self::NoTokenOrAs => write!(f, "missing token or lang_util(as = \"...\") attributes"), + Self::NoTokenOrAs => write!(f, "missing token or lang_util(parser = \"...\") attributes"), } } } @@ -544,7 +539,10 @@ pub(crate) fn token(input: proc_macro::TokenStream) -> proc_macro::TokenStream { // Parse the input tokens into a syntax tree let input = parse_macro_input!(input as DeriveInput); // Find out enum-level options - TokenOpts::from_derive_input(&input).expect("failed to parse options") + match TokenOpts::from_derive_input(&input) { + Ok(res) => res, + Err(err) => return err.write_errors().into(), + } }; let base_ident = &opts.ident;