chore: Clean up parse logic for Value

This commit is contained in:
2025-01-11 11:35:45 -05:00
parent ebc4cc0bd4
commit 0858e04c41
2 changed files with 23 additions and 9 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
/target /target
/expand.rs

View File

@@ -1,5 +1,10 @@
use quote::{quote, ToTokens}; use quote::{quote, ToTokens};
use syn::{braced, bracketed, parse::Parse, punctuated::Punctuated, token, Token}; use syn::{
braced, bracketed,
parse::{discouraged::Speculative, Parse},
punctuated::Punctuated,
token, Token,
};
pub struct Command { pub struct Command {
program: Value, program: Value,
@@ -376,15 +381,23 @@ impl Parse for Value {
fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> { fn parse(input: syn::parse::ParseStream) -> syn::Result<Self> {
let expr_fork = input.fork(); let expr_fork = input.fork();
expr_fork expr_fork
.parse::<syn::Expr>() .parse()
.and_then(|_| input.parse().map(Self::Expr)) .map(|expr| {
.or_else(|_| { input.advance_to(&expr_fork);
let ident_fork = input.fork(); Self::Expr(expr)
ident_fork })
.parse::<syn::Ident>() .or_else(|_| {
.and_then(|_| input.parse().map(Self::Ident)) if input.peek(syn::Ident) {
input.parse().map(Self::Ident)
} else if input.peek(syn::Lit) {
input.parse().map(Self::Lit)
} else {
Err(syn::Error::new(
input.span(),
"Expected an expression, ident, or literal",
))
}
}) })
.or_else(|_| input.parse().map(Self::Lit))
} }
} }