mirror of
https://github.com/ProvableHQ/leo.git
synced 2024-12-24 10:41:57 +03:00
fix parser to match abnf conditional
This commit is contained in:
parent
d4aaac6577
commit
eee9cfc1be
@ -453,7 +453,7 @@ identifier = letter *( letter / digit / "_" ) ; but not a keyword or aleo1...
|
|||||||
; A package name consists of one or more segments separated by single dashes,
|
; A package name consists of one or more segments separated by single dashes,
|
||||||
; where each segment is a non-empty sequence of lowercase letters and digits.
|
; where each segment is a non-empty sequence of lowercase letters and digits.
|
||||||
|
|
||||||
package-name = 1*( lowercase-letter / digit )
|
package-name = 1*lowercase-letter
|
||||||
*( "-" 1*( lowercase-letter / digit ) )
|
*( "-" 1*( lowercase-letter / digit ) )
|
||||||
|
|
||||||
; Annotations have names, which are identifiers immediately preceded by `@`.
|
; Annotations have names, which are identifiers immediately preceded by `@`.
|
||||||
@ -962,7 +962,7 @@ assert-call = %s"assert" "(" expression ")"
|
|||||||
|
|
||||||
print-function = %s"debug" / %s"error" / %s"log"
|
print-function = %s"debug" / %s"error" / %s"log"
|
||||||
|
|
||||||
print-arguments = "(" [ string-literal *( "," expression ) ] ")"
|
print-arguments = "(" string-literal *( "," expression ) ")"
|
||||||
|
|
||||||
print-call = print-function print-arguments
|
print-call = print-function print-arguments
|
||||||
|
|
||||||
@ -1020,7 +1020,7 @@ member-function-declaration = function-declaration
|
|||||||
; this may be relaxed after the backward compatibility is removed,
|
; this may be relaxed after the backward compatibility is removed,
|
||||||
; allowing member variables and member functions to be intermixed.
|
; allowing member variables and member functions to be intermixed.
|
||||||
|
|
||||||
circuit-declaration = *annotation %s"circuit" identifier
|
circuit-declaration = %s"circuit" identifier
|
||||||
"{" [ member-variable-declarations ]
|
"{" [ member-variable-declarations ]
|
||||||
*member-function-declaration "}"
|
*member-function-declaration "}"
|
||||||
|
|
||||||
|
@ -100,6 +100,10 @@ impl SyntaxError {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn unexpected_statement(got: String, expected: &str, span: &Span) -> Self {
|
||||||
|
Self::new_from_span(format!("expected '{}', got '{}'", expected, got.to_string()), span)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn unexpected_str(got: &Token, expected: &str, span: &Span) -> Self {
|
pub fn unexpected_str(got: &Token, expected: &str, span: &Span) -> Self {
|
||||||
Self::new_from_span(format!("expected '{}', got '{}'", expected, got.to_string()), span)
|
Self::new_from_span(format!("expected '{}', got '{}'", expected, got.to_string()), span)
|
||||||
}
|
}
|
||||||
|
@ -102,11 +102,20 @@ impl ParserContext {
|
|||||||
let end_span;
|
let end_span;
|
||||||
let arguments = if self.eat(Token::LeftParen).is_some() {
|
let arguments = if self.eat(Token::LeftParen).is_some() {
|
||||||
let mut args = Vec::new();
|
let mut args = Vec::new();
|
||||||
|
let mut comma = false;
|
||||||
loop {
|
loop {
|
||||||
if let Some(end) = self.eat(Token::RightParen) {
|
if let Some(end) = self.eat(Token::RightParen) {
|
||||||
|
if comma {
|
||||||
|
return Err(SyntaxError::unexpected(
|
||||||
|
&Token::RightParen,
|
||||||
|
&[Token::Ident("identifier".into()), Token::Int("number".into())],
|
||||||
|
&end.span,
|
||||||
|
));
|
||||||
|
}
|
||||||
end_span = end.span;
|
end_span = end.span;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
comma = false;
|
||||||
if let Some(ident) = self.eat_identifier() {
|
if let Some(ident) = self.eat_identifier() {
|
||||||
args.push(ident.name);
|
args.push(ident.name);
|
||||||
} else if let Some((int, _)) = self.eat_int() {
|
} else if let Some((int, _)) = self.eat_int() {
|
||||||
@ -115,10 +124,11 @@ impl ParserContext {
|
|||||||
let token = self.peek()?;
|
let token = self.peek()?;
|
||||||
return Err(SyntaxError::unexpected_str(&token.token, "ident or int", &token.span));
|
return Err(SyntaxError::unexpected_str(&token.token, "ident or int", &token.span));
|
||||||
}
|
}
|
||||||
if self.eat(Token::Comma).is_none() {
|
if self.eat(Token::Comma).is_none() && !comma {
|
||||||
end_span = self.expect(Token::RightParen)?;
|
end_span = self.expect(Token::RightParen)?;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
comma = true;
|
||||||
}
|
}
|
||||||
args
|
args
|
||||||
} else {
|
} else {
|
||||||
|
@ -185,7 +185,17 @@ impl ParserContext {
|
|||||||
self.fuzzy_struct_state = false;
|
self.fuzzy_struct_state = false;
|
||||||
let body = self.parse_block()?;
|
let body = self.parse_block()?;
|
||||||
let next = if self.eat(Token::Else).is_some() {
|
let next = if self.eat(Token::Else).is_some() {
|
||||||
Some(Box::new(self.parse_statement()?))
|
let s = self.parse_statement()?;
|
||||||
|
match s {
|
||||||
|
Statement::Block(_) | Statement::Conditional(_) => Some(Box::new(s)),
|
||||||
|
s => {
|
||||||
|
return Err(SyntaxError::unexpected_statement(
|
||||||
|
s.to_string(),
|
||||||
|
"Block or Conditional",
|
||||||
|
s.span(),
|
||||||
|
));
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
None
|
None
|
||||||
};
|
};
|
||||||
|
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
namespace: Parse
|
||||||
|
expectation: Fail
|
||||||
|
outputs:
|
||||||
|
- " --> test:3:12\n |\n 3 | @test(test,)\n | ^\n |\n = expected 'identifier', 'number' -- got ')'"
|
5
tests/expectations/parser/parser/import/invalid.leo.out
Normal file
5
tests/expectations/parser/parser/import/invalid.leo.out
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
namespace: Parse
|
||||||
|
expectation: Fail
|
||||||
|
outputs:
|
||||||
|
- " --> test:3:18\n |\n 3 | import foo as bar;\n | ^\n |\n = expected '.' -- got ';'"
|
9
tests/parser/functions/annotated_fail.leo
Normal file
9
tests/parser/functions/annotated_fail.leo
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
/*
|
||||||
|
namespace: Parse
|
||||||
|
expectation: Fail
|
||||||
|
*/
|
||||||
|
|
||||||
|
@test(test,)
|
||||||
|
function x() {
|
||||||
|
return ();
|
||||||
|
}
|
7
tests/parser/import/invalid.leo
Normal file
7
tests/parser/import/invalid.leo
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
/*
|
||||||
|
namespace: Parse
|
||||||
|
expectation: Fail
|
||||||
|
*/
|
||||||
|
|
||||||
|
import foo as bar;
|
||||||
|
import *;
|
Loading…
Reference in New Issue
Block a user