Skip to content

Commit

Permalink
feat: rename lang_util(as) to lang_util(parser)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
alixinne committed Aug 19, 2024
1 parent 8aa6e1e commit 4ed7678
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 65 deletions.
File renamed without changes.
76 changes: 40 additions & 36 deletions lang-lexer/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))]
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand All @@ -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")]
Expand Down Expand Up @@ -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 = "<whitespace>", as(display), kind = "trivia")]
#[lang_util(display = "<whitespace>", parser(display), kind = "trivia")]
Whitespace,
#[cfg_attr(feature = "v1", regex("//(.|\\\\\r?\n)*", |lex| { parse_cmt(lex, true); logos::Skip }))]
#[lang_util(display = "<single line comment>", as(display), kind = "trivia")]
#[lang_util(display = "<single line comment>", parser(display), kind = "trivia")]
SingleLineComment,
#[cfg_attr(feature = "v1", regex("/\\*([^*]|\\*[^/])+\\*/", |lex| { parse_cmt(lex, false); logos::Skip }))]
#[lang_util(display = "<multi line comment>", as(display), kind = "trivia")]
#[lang_util(display = "<multi line comment>", 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 = "<preprocessor string>",
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 = "<invalid token>", as(display), kind = "error")]
#[lang_util(display = "<invalid token>", parser(display), kind = "error")]
Error,
}

Expand Down
2 changes: 1 addition & 1 deletion lang-pp/src/processor/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
20 changes: 10 additions & 10 deletions lang-pp/src/types/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
/// <<
Expand Down Expand Up @@ -1601,13 +1601,13 @@ pub enum Token {
USING,
// Other
/// Whitespaace
#[lang_util(display = "<whitespace>", as(display), kind = "trivia")]
#[lang_util(display = "<whitespace>", parser(display), kind = "trivia")]
WS,
/// Comment (single-line or multi-line)
#[lang_util(display = "<comment>", as(display), kind = "trivia")]
#[lang_util(display = "<comment>", parser(display), kind = "trivia")]
COMMENT,
/// Marker for invalid tokens
#[lang_util(display = "<invalid token>", as(display), kind = "error")]
#[lang_util(display = "<invalid token>", parser(display), kind = "error")]
ERROR(ErrorKind),
}

Expand Down
2 changes: 1 addition & 1 deletion lang-util-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ proc-macro = true
darling = "0.20"
proc-macro2 = "1.0"
quote = "1.0"
syn = "1.0"
syn = "2.0"
32 changes: 15 additions & 17 deletions lang-util-derive/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl FromMeta for TokenDisplay {
})
}

fn from_list(items: &[syn::NestedMeta]) -> darling::Result<Self> {
fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
Ok(Self {
format: String::from_nested_meta(
items
Expand All @@ -44,7 +44,7 @@ struct TokenAttr {
}

impl FromMeta for TokenAttr {
fn from_list(items: &[syn::NestedMeta]) -> darling::Result<Self> {
fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
Ok(Self {
token: String::from_nested_meta(
items
Expand All @@ -65,14 +65,14 @@ impl FromMeta for AsParser {
Ok(Self::RawString(value.to_owned()))
}

fn from_nested_meta(item: &syn::NestedMeta) -> darling::Result<Self> {
fn from_nested_meta(item: &darling::ast::NestedMeta) -> darling::Result<Self> {
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<Self> {
fn from_list(items: &[darling::ast::NestedMeta]) -> darling::Result<Self> {
if let Some(item) = items.first() {
return Self::from_nested_meta(item);
}
Expand All @@ -89,7 +89,7 @@ struct TokenVariant {
attrs: Vec<syn::Attribute>,
#[darling(default)]
display: Option<TokenDisplay>,
#[darling(default, rename = "as")]
#[darling(default, rename = "parser")]
as_parser: Option<AsParser>,
#[darling(default, rename = "token")]
fallback_token: Option<String>,
Expand Down Expand Up @@ -381,14 +381,9 @@ type TokenAttrTy<'s> = Option<(darling::Result<TokenAttr>, 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(),
Expand Down Expand Up @@ -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"),
}
}
}
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 4ed7678

Please sign in to comment.