Skip to content

Commit

Permalink
Improve error for named rets
Browse files Browse the repository at this point in the history
  • Loading branch information
nezuo committed Dec 1, 2024
1 parent 7f258d0 commit ad76696
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
21 changes: 17 additions & 4 deletions zap/src/parser/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,16 @@ impl<'src> Converter<'src> {
self.check_duplicate_parameters(syntax_parameters);
}

if let Some(syntax_parameters) = &fndecl.rets {
for parameter in &syntax_parameters.parameters {
if let Some(identifier) = parameter.0 {
self.report(Report::AnalyzeNamedReturn {
name_span: identifier.span(),
});
}
}
}

let name = fndecl.name.name;
let call = fndecl.call;
let args = fndecl.args.as_ref().map(|parameters| {
Expand All @@ -415,10 +425,13 @@ impl<'src> Converter<'src> {
.collect::<Vec<_>>()
});

let rets = fndecl
.rets
.as_ref()
.map(|types| types.iter().map(|ty| self.ty(ty)).collect::<Vec<_>>());
let rets = fndecl.rets.as_ref().map(|parameters| {
parameters
.parameters
.iter()
.map(|(_, ty)| self.ty(ty))
.collect::<Vec<_>>()
});

FnDecl {
name,
Expand Down
7 changes: 1 addition & 6 deletions zap/src/parser/grammar.lalrpop
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ FnDecl: SyntaxFnDecl<'input> = {
<start:@L> "funct" <name:Identifier> "=" "{"
"call" ":" <call:FnCall>
<args:("," "args" ":" <Parameters>)?>
<rets:("," "rets" ":" <TyList>)?>
<rets:("," "rets" ":" <Parameters>)?>
","?
"}" ";"? <end:@R> => SyntaxFnDecl { start, name, call, args, rets, end },
}
Expand Down Expand Up @@ -116,11 +116,6 @@ TyKind: SyntaxTyKind<'input> = {
"Instance" <c:("(" <Identifier> ")")?> => SyntaxTyKind::Instance(c),
}

TyList: Vec<SyntaxTy<'input>> = {
<ty:Ty> => vec![ty],
"(" <tys:Comma<Ty>> ")" => tys,
}

Parameters: SyntaxParameters<'input> = {
<start:@L>
<parameters:ParameterList>
Expand Down
12 changes: 12 additions & 0 deletions zap/src/parser/reports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ pub enum Report<'src> {
dup_span: Span,
name: &'src str,
},

AnalyzeNamedReturn {
name_span: Span,
},
}

impl<'src> Report<'src> {
Expand Down Expand Up @@ -129,6 +133,7 @@ impl<'src> Report<'src> {
Self::AnalyzeMissingOptValue { .. } => Severity::Error,
Self::AnalyzeDuplicateDecl { .. } => Severity::Error,
Self::AnalyzeDuplicateParameter { .. } => Severity::Error,
Self::AnalyzeNamedReturn { .. } => Severity::Error,
}
}

Expand Down Expand Up @@ -159,6 +164,7 @@ impl<'src> Report<'src> {
Self::AnalyzeMissingOptValue { .. } => "missing option expected".to_string(),
Self::AnalyzeDuplicateDecl { name, .. } => format!("duplicate declaration '{}'", name),
Self::AnalyzeDuplicateParameter { name, .. } => format!("duplicate parameter '{}'", name),
Self::AnalyzeNamedReturn { .. } => "rets cannot be named".to_string(),
}
}

Expand Down Expand Up @@ -186,6 +192,7 @@ impl<'src> Report<'src> {
Self::AnalyzeMissingOptValue { .. } => "3013",
Self::AnalyzeDuplicateDecl { .. } => "3014",
Self::AnalyzeDuplicateParameter { .. } => "3015",
Self::AnalyzeNamedReturn { .. } => "3016",
}
}

Expand Down Expand Up @@ -290,6 +297,10 @@ impl<'src> Report<'src> {
Label::primary((), dup_span.clone()).with_message("duplicate parameter"),
]
}

Self::AnalyzeNamedReturn { name_span } => {
vec![Label::primary((), name_span.clone()).with_message("must be removed")]
}
}
}

Expand Down Expand Up @@ -354,6 +365,7 @@ impl<'src> Report<'src> {
)]),
Self::AnalyzeDuplicateDecl { .. } => None,
Self::AnalyzeDuplicateParameter { .. } => None,
Self::AnalyzeNamedReturn { .. } => None,
}
}

Expand Down
2 changes: 1 addition & 1 deletion zap/src/parser/syntax_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub struct SyntaxFnDecl<'src> {
pub name: SyntaxIdentifier<'src>,
pub call: FnCall,
pub args: Option<SyntaxParameters<'src>>,
pub rets: Option<Vec<SyntaxTy<'src>>>,
pub rets: Option<SyntaxParameters<'src>>,
pub end: usize,
}

Expand Down

0 comments on commit ad76696

Please sign in to comment.