no empty import lists, star is a span, grammar fix

This commit is contained in:
gluax 2021-05-05 16:10:51 -04:00
parent 7e83de8df4
commit 29ca0d9a12
6 changed files with 51 additions and 42 deletions

View File

@ -105,7 +105,7 @@ fn resolve_import_package_access(
package: &PackageAccess, package: &PackageAccess,
) { ) {
match package { match package {
PackageAccess::Star(span) => { PackageAccess::Star { span } => {
output.push((package_segments, ImportSymbol::All, span.clone())); output.push((package_segments, ImportSymbol::All, span.clone()));
} }
PackageAccess::SubPackage(subpackage) => { PackageAccess::SubPackage(subpackage) => {
@ -414,7 +414,7 @@ pub fn reform_ast<'a>(program: &Program<'a>) -> leo_ast::Program {
.map(|(module, _)| leo_ast::ImportStatement { .map(|(module, _)| leo_ast::ImportStatement {
package_or_packages: leo_ast::PackageOrPackages::Package(leo_ast::Package { package_or_packages: leo_ast::PackageOrPackages::Package(leo_ast::Package {
name: Identifier::new(module.clone().into()), name: Identifier::new(module.clone().into()),
access: leo_ast::PackageAccess::Star(Span::default()), access: leo_ast::PackageAccess::Star { span: Span::default() },
span: Default::default(), span: Default::default(),
}), }),
span: Span::default(), span: Span::default(),

View File

@ -21,7 +21,7 @@ use std::fmt;
#[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)] #[derive(Clone, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum PackageAccess { pub enum PackageAccess {
Star(Span), Star { span: Span },
SubPackage(Box<Package>), SubPackage(Box<Package>),
Symbol(ImportSymbol), Symbol(ImportSymbol),
Multiple(Packages), Multiple(Packages),
@ -30,7 +30,7 @@ pub enum PackageAccess {
impl Node for PackageAccess { impl Node for PackageAccess {
fn span(&self) -> &Span { fn span(&self) -> &Span {
match self { match self {
PackageAccess::Star(span) => span, PackageAccess::Star { span } => span,
PackageAccess::SubPackage(package) => &package.span, PackageAccess::SubPackage(package) => &package.span,
PackageAccess::Symbol(package) => &package.span, PackageAccess::Symbol(package) => &package.span,
PackageAccess::Multiple(package) => &package.span, PackageAccess::Multiple(package) => &package.span,
@ -39,7 +39,7 @@ impl Node for PackageAccess {
fn set_span(&mut self, span: Span) { fn set_span(&mut self, span: Span) {
match self { match self {
PackageAccess::Star(package) => *package = span, PackageAccess::Star { span } => *span = span.clone(),
PackageAccess::SubPackage(package) => package.span = span, PackageAccess::SubPackage(package) => package.span = span,
PackageAccess::Symbol(package) => package.span = span, PackageAccess::Symbol(package) => package.span = span,
PackageAccess::Multiple(package) => package.span = span, PackageAccess::Multiple(package) => package.span = span,
@ -50,7 +50,7 @@ impl Node for PackageAccess {
impl PackageAccess { impl PackageAccess {
fn format(&self, f: &mut fmt::Formatter) -> fmt::Result { fn format(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self { match self {
PackageAccess::Star(ref _span) => write!(f, "*"), PackageAccess::Star { .. } => write!(f, "*"),
PackageAccess::SubPackage(ref package) => write!(f, "{}", package), PackageAccess::SubPackage(ref package) => write!(f, "{}", package),
PackageAccess::Symbol(ref symbol) => write!(f, "{}", symbol), PackageAccess::Symbol(ref symbol) => write!(f, "{}", symbol),
PackageAccess::Multiple(ref packages) => { PackageAccess::Multiple(ref packages) => {

View File

@ -494,7 +494,7 @@ the ones used in the Java language reference.
comment = block-comment / end-of-line-comment comment = block-comment / end-of-line-comment
``` ```
Go to: _[block-comment](#user-content-block-comment), [end-of-line-comment](#user-content-end-of-line-comment)_; Go to: _[end-of-line-comment](#user-content-end-of-line-comment), [block-comment](#user-content-block-comment)_;
<a name="block-comment"></a> <a name="block-comment"></a>
@ -511,7 +511,7 @@ rest-of-block-comment = "*" rest-of-block-comment-after-star
/ not-star rest-of-block-comment / not-star rest-of-block-comment
``` ```
Go to: _[rest-of-block-comment](#user-content-rest-of-block-comment), [rest-of-block-comment-after-star](#user-content-rest-of-block-comment-after-star), [not-star](#user-content-not-star)_; Go to: _[rest-of-block-comment-after-star](#user-content-rest-of-block-comment-after-star), [not-star](#user-content-not-star), [rest-of-block-comment](#user-content-rest-of-block-comment)_;
<a name="rest-of-block-comment-after-star"></a> <a name="rest-of-block-comment-after-star"></a>
@ -521,7 +521,7 @@ rest-of-block-comment-after-star = "/"
/ not-star-or-slash rest-of-block-comment / not-star-or-slash rest-of-block-comment
``` ```
Go to: _[rest-of-block-comment](#user-content-rest-of-block-comment), [not-star-or-slash](#user-content-not-star-or-slash), [rest-of-block-comment-after-star](#user-content-rest-of-block-comment-after-star)_; Go to: _[not-star-or-slash](#user-content-not-star-or-slash), [rest-of-block-comment-after-star](#user-content-rest-of-block-comment-after-star), [rest-of-block-comment](#user-content-rest-of-block-comment)_;
<a name="end-of-line-comment"></a> <a name="end-of-line-comment"></a>
@ -596,7 +596,7 @@ lowercase-letter = %x61-7A ; a-z
letter = uppercase-letter / lowercase-letter letter = uppercase-letter / lowercase-letter
``` ```
Go to: _[lowercase-letter](#user-content-lowercase-letter), [uppercase-letter](#user-content-uppercase-letter)_; Go to: _[uppercase-letter](#user-content-uppercase-letter), [lowercase-letter](#user-content-lowercase-letter)_;
An identifier is a non-empty sequence of letters, digits, and underscores, An identifier is a non-empty sequence of letters, digits, and underscores,
@ -642,7 +642,7 @@ format-string-element = not-double-quote-or-open-brace
/ format-string-container / format-string-container
``` ```
Go to: _[format-string-container](#user-content-format-string-container), [not-double-quote-or-open-brace](#user-content-not-double-quote-or-open-brace), [not-double-quote-or-close-brace](#user-content-not-double-quote-or-close-brace)_; Go to: _[not-double-quote-or-close-brace](#user-content-not-double-quote-or-close-brace), [format-string-container](#user-content-format-string-container), [not-double-quote-or-open-brace](#user-content-not-double-quote-or-open-brace)_;
<a name="format-string"></a> <a name="format-string"></a>
@ -767,7 +767,7 @@ atomic-literal = untyped-literal
/ address-literal / address-literal
``` ```
Go to: _[product-group-literal](#user-content-product-group-literal), [signed-literal](#user-content-signed-literal), [unsigned-literal](#user-content-unsigned-literal), [field-literal](#user-content-field-literal), [boolean-literal](#user-content-boolean-literal), [untyped-literal](#user-content-untyped-literal), [address-literal](#user-content-address-literal)_; Go to: _[product-group-literal](#user-content-product-group-literal), [untyped-literal](#user-content-untyped-literal), [address-literal](#user-content-address-literal), [unsigned-literal](#user-content-unsigned-literal), [boolean-literal](#user-content-boolean-literal), [signed-literal](#user-content-signed-literal), [field-literal](#user-content-field-literal)_;
After defining the (mostly) alphanumeric tokens above, After defining the (mostly) alphanumeric tokens above,
@ -812,7 +812,7 @@ token = keyword
/ symbol / symbol
``` ```
Go to: _[package-name](#user-content-package-name), [annotation-name](#user-content-annotation-name), [format-string](#user-content-format-string), [keyword](#user-content-keyword), [atomic-literal](#user-content-atomic-literal), [identifier](#user-content-identifier), [symbol](#user-content-symbol)_; Go to: _[package-name](#user-content-package-name), [format-string](#user-content-format-string), [annotation-name](#user-content-annotation-name), [identifier](#user-content-identifier), [keyword](#user-content-keyword), [symbol](#user-content-symbol), [atomic-literal](#user-content-atomic-literal)_;
@ -869,7 +869,7 @@ group-type = %s"group"
arithmetic-type = integer-type / field-type / group-type arithmetic-type = integer-type / field-type / group-type
``` ```
Go to: _[integer-type](#user-content-integer-type), [field-type](#user-content-field-type), [group-type](#user-content-group-type)_; Go to: _[integer-type](#user-content-integer-type), [group-type](#user-content-group-type), [field-type](#user-content-field-type)_;
The arithmetic types, along with the boolean and address types, The arithmetic types, along with the boolean and address types,
@ -890,7 +890,7 @@ address-type = %s"address"
scalar-type = boolean-type / arithmetic-type / address-type scalar-type = boolean-type / arithmetic-type / address-type
``` ```
Go to: _[arithmetic-type](#user-content-arithmetic-type), [address-type](#user-content-address-type), [boolean-type](#user-content-boolean-type)_; Go to: _[address-type](#user-content-address-type), [arithmetic-type](#user-content-arithmetic-type), [boolean-type](#user-content-boolean-type)_;
Circuit types are denoted by identifiers and the keyword `Self`. Circuit types are denoted by identifiers and the keyword `Self`.
@ -951,7 +951,7 @@ i.e. types whose values contain (sub-)values
aggregate-type = tuple-type / array-type / circuit-type aggregate-type = tuple-type / array-type / circuit-type
``` ```
Go to: _[tuple-type](#user-content-tuple-type), [array-type](#user-content-array-type), [circuit-type](#user-content-circuit-type)_; Go to: _[circuit-type](#user-content-circuit-type), [array-type](#user-content-array-type), [tuple-type](#user-content-tuple-type)_;
Scalar and aggregate types form all the types. Scalar and aggregate types form all the types.
@ -961,7 +961,7 @@ Scalar and aggregate types form all the types.
type = scalar-type / aggregate-type type = scalar-type / aggregate-type
``` ```
Go to: _[aggregate-type](#user-content-aggregate-type), [scalar-type](#user-content-scalar-type)_; Go to: _[scalar-type](#user-content-scalar-type), [aggregate-type](#user-content-aggregate-type)_;
The lexical grammar given earlier defines product group literals. The lexical grammar given earlier defines product group literals.
@ -1039,7 +1039,7 @@ primary-expression = identifier
/ circuit-expression / circuit-expression
``` ```
Go to: _[tuple-expression](#user-content-tuple-expression), [identifier](#user-content-identifier), [expression](#user-content-expression), [array-expression](#user-content-array-expression), [circuit-expression](#user-content-circuit-expression), [literal](#user-content-literal)_; Go to: _[circuit-expression](#user-content-circuit-expression), [expression](#user-content-expression), [literal](#user-content-literal), [tuple-expression](#user-content-tuple-expression), [identifier](#user-content-identifier), [array-expression](#user-content-array-expression)_;
Tuple expressions construct tuples. Tuple expressions construct tuples.
@ -1100,7 +1100,7 @@ Go to: _[expression](#user-content-expression), [array-dimensions](#user-content
array-construction = array-inline-construction / array-repeat-construction array-construction = array-inline-construction / array-repeat-construction
``` ```
Go to: _[array-repeat-construction](#user-content-array-repeat-construction), [array-inline-construction](#user-content-array-inline-construction)_; Go to: _[array-inline-construction](#user-content-array-inline-construction), [array-repeat-construction](#user-content-array-repeat-construction)_;
<a name="array-expression"></a> <a name="array-expression"></a>
@ -1128,7 +1128,7 @@ circuit-construction = circuit-type "{"
"}" "}"
``` ```
Go to: _[circuit-inline-element](#user-content-circuit-inline-element), [circuit-type](#user-content-circuit-type)_; Go to: _[circuit-type](#user-content-circuit-type), [circuit-inline-element](#user-content-circuit-inline-element)_;
<a name="circuit-inline-element"></a> <a name="circuit-inline-element"></a>
@ -1136,7 +1136,7 @@ Go to: _[circuit-inline-element](#user-content-circuit-inline-element), [circuit
circuit-inline-element = identifier ":" expression / identifier circuit-inline-element = identifier ":" expression / identifier
``` ```
Go to: _[expression](#user-content-expression), [identifier](#user-content-identifier)_; Go to: _[identifier](#user-content-identifier), [expression](#user-content-expression)_;
<a name="circuit-expression"></a> <a name="circuit-expression"></a>
@ -1187,7 +1187,7 @@ postfix-expression = primary-expression
/ postfix-expression "[" [expression] ".." [expression] "]" / postfix-expression "[" [expression] ".." [expression] "]"
``` ```
Go to: _[function-arguments](#user-content-function-arguments), [postfix-expression](#user-content-postfix-expression), [natural](#user-content-natural), [circuit-type](#user-content-circuit-type), [expression](#user-content-expression), [identifier](#user-content-identifier), [primary-expression](#user-content-primary-expression)_; Go to: _[natural](#user-content-natural), [circuit-type](#user-content-circuit-type), [function-arguments](#user-content-function-arguments), [primary-expression](#user-content-primary-expression), [postfix-expression](#user-content-postfix-expression), [identifier](#user-content-identifier), [expression](#user-content-expression)_;
Unary operators have the highest operator precedence. Unary operators have the highest operator precedence.
@ -1239,7 +1239,7 @@ additive-expression = multiplicative-expression
/ additive-expression "-" multiplicative-expression / additive-expression "-" multiplicative-expression
``` ```
Go to: _[multiplicative-expression](#user-content-multiplicative-expression), [additive-expression](#user-content-additive-expression)_; Go to: _[additive-expression](#user-content-additive-expression), [multiplicative-expression](#user-content-multiplicative-expression)_;
Next in the precedence order are ordering relations. Next in the precedence order are ordering relations.
@ -1302,7 +1302,7 @@ conditional-expression = disjunctive-expression
":" conditional-expression ":" conditional-expression
``` ```
Go to: _[expression](#user-content-expression), [conditional-expression](#user-content-conditional-expression), [disjunctive-expression](#user-content-disjunctive-expression)_; Go to: _[expression](#user-content-expression), [disjunctive-expression](#user-content-disjunctive-expression), [conditional-expression](#user-content-conditional-expression)_;
Those above are all the expressions. Those above are all the expressions.
@ -1335,7 +1335,7 @@ statement = expression-statement
/ block / block
``` ```
Go to: _[expression-statement](#user-content-expression-statement), [return-statement](#user-content-return-statement), [assignment-statement](#user-content-assignment-statement), [block](#user-content-block), [loop-statement](#user-content-loop-statement), [constant-declaration](#user-content-constant-declaration), [conditional-statement](#user-content-conditional-statement), [console-statement](#user-content-console-statement), [variable-declaration](#user-content-variable-declaration)_; Go to: _[return-statement](#user-content-return-statement), [block](#user-content-block), [expression-statement](#user-content-expression-statement), [assignment-statement](#user-content-assignment-statement), [constant-declaration](#user-content-constant-declaration), [conditional-statement](#user-content-conditional-statement), [console-statement](#user-content-console-statement), [variable-declaration](#user-content-variable-declaration), [loop-statement](#user-content-loop-statement)_;
<a name="block"></a> <a name="block"></a>
@ -1378,7 +1378,7 @@ variable-declaration = %s"let" identifier-or-identifiers [ ":" type ]
"=" expression ";" "=" expression ";"
``` ```
Go to: _[type](#user-content-type), [identifier-or-identifiers](#user-content-identifier-or-identifiers), [expression](#user-content-expression)_; Go to: _[identifier-or-identifiers](#user-content-identifier-or-identifiers), [type](#user-content-type), [expression](#user-content-expression)_;
<a name="constant-declaration"></a> <a name="constant-declaration"></a>
@ -1387,7 +1387,7 @@ constant-declaration = %s"const" identifier-or-identifiers [ ":" type ]
"=" expression ";" "=" expression ";"
``` ```
Go to: _[expression](#user-content-expression), [type](#user-content-type), [identifier-or-identifiers](#user-content-identifier-or-identifiers)_; Go to: _[type](#user-content-type), [expression](#user-content-expression), [identifier-or-identifiers](#user-content-identifier-or-identifiers)_;
<a name="identifier-or-identifiers"></a> <a name="identifier-or-identifiers"></a>
@ -1420,7 +1420,7 @@ conditional-statement = branch
/ branch %s"else" conditional-statement / branch %s"else" conditional-statement
``` ```
Go to: _[block](#user-content-block), [branch](#user-content-branch), [conditional-statement](#user-content-conditional-statement)_; Go to: _[conditional-statement](#user-content-conditional-statement), [block](#user-content-block), [branch](#user-content-branch)_;
A loop statement implicitly defines a loop variable A loop statement implicitly defines a loop variable
@ -1476,7 +1476,7 @@ console-call = assert-call
/ print-call / print-call
``` ```
Go to: _[print-call](#user-content-print-call), [assert-call](#user-content-assert-call)_; Go to: _[assert-call](#user-content-assert-call), [print-call](#user-content-print-call)_;
<a name="assert-call"></a> <a name="assert-call"></a>
@ -1505,7 +1505,7 @@ Go to: _[format-string](#user-content-format-string)_;
print-call = print-function print-arguments print-call = print-function print-arguments
``` ```
Go to: _[print-function](#user-content-print-function), [print-arguments](#user-content-print-arguments)_; Go to: _[print-arguments](#user-content-print-arguments), [print-function](#user-content-print-function)_;
An annotation consists of an annotation name (which starts with `@`) An annotation consists of an annotation name (which starts with `@`)
@ -1518,7 +1518,7 @@ annotation = annotation-name
[ "(" identifier *( "," identifier ) ")" ] [ "(" identifier *( "," identifier ) ")" ]
``` ```
Go to: _[annotation-name](#user-content-annotation-name), [identifier](#user-content-identifier)_; Go to: _[identifier](#user-content-identifier), [annotation-name](#user-content-annotation-name)_;
A function declaration defines a function. A function declaration defines a function.
@ -1566,7 +1566,7 @@ Go to: _[function-input](#user-content-function-input)_;
function-input = [ %s"const" ] identifier ":" type function-input = [ %s"const" ] identifier ":" type
``` ```
Go to: _[type](#user-content-type), [identifier](#user-content-identifier)_; Go to: _[identifier](#user-content-identifier), [type](#user-content-type)_;
A circuit member variable declaration consists of an identifier and a type. A circuit member variable declaration consists of an identifier and a type.
@ -1586,7 +1586,7 @@ Go to: _[member-function-declaration](#user-content-member-function-declaration)
member-variable-declaration = identifier ":" type member-variable-declaration = identifier ":" type
``` ```
Go to: _[identifier](#user-content-identifier), [type](#user-content-type)_; Go to: _[type](#user-content-type), [identifier](#user-content-identifier)_;
<a name="member-function-declaration"></a> <a name="member-function-declaration"></a>
@ -1606,7 +1606,7 @@ circuit-declaration = *annotation %s"circuit" identifier
"{" member-declaration *( "," member-declaration ) "}" "{" member-declaration *( "," member-declaration ) "}"
``` ```
Go to: _[identifier](#user-content-identifier), [member-declaration](#user-content-member-declaration)_; Go to: _[member-declaration](#user-content-member-declaration), [identifier](#user-content-identifier)_;
An import declaration consists of the `import` keyword An import declaration consists of the `import` keyword
@ -1621,10 +1621,10 @@ to be followed by a comma, for convenience.
<a name="import-declaration"></a> <a name="import-declaration"></a>
```abnf ```abnf
import-declaration = %s"import" package-path ";" import-declaration = %s"import" package-name "." package-path ";"
``` ```
Go to: _[package-path](#user-content-package-path)_; Go to: _[package-name](#user-content-package-name), [package-path](#user-content-package-path)_;
<a name="package-path"></a> <a name="package-path"></a>
@ -1635,7 +1635,7 @@ package-path = "*"
/ "(" package-path *( "," package-path ) [","] ")" / "(" package-path *( "," package-path ) [","] ")"
``` ```
Go to: _[identifier](#user-content-identifier), [package-path](#user-content-package-path), [package-name](#user-content-package-name)_; Go to: _[identifier](#user-content-identifier), [package-name](#user-content-package-name), [package-path](#user-content-package-path)_;
Finally, we define a file as a sequence of zero or more declarations. Finally, we define a file as a sequence of zero or more declarations.
@ -1650,7 +1650,7 @@ declaration = import-declaration
/ constant-declaration / constant-declaration
``` ```
Go to: _[constant-declaration](#user-content-constant-declaration), [import-declaration](#user-content-import-declaration), [function-declaration](#user-content-function-declaration), [circuit-declaration](#user-content-circuit-declaration)_; Go to: _[circuit-declaration](#user-content-circuit-declaration), [constant-declaration](#user-content-constant-declaration), [import-declaration](#user-content-import-declaration), [function-declaration](#user-content-function-declaration)_;
<a name="file"></a> <a name="file"></a>

View File

@ -968,7 +968,7 @@ circuit-declaration = *annotation %s"circuit" identifier
; Note that we allow the last element of the parenthesized list ; Note that we allow the last element of the parenthesized list
; to be followed by a comma, for convenience. ; to be followed by a comma, for convenience.
import-declaration = %s"import" package-path ";" import-declaration = %s"import" package-name "." package-path ";"
package-path = "*" package-path = "*"
/ identifier [ %s"as" identifier ] / identifier [ %s"as" identifier ]

View File

@ -48,6 +48,10 @@ impl SyntaxError {
SyntaxError::Error(FormattedError::new_from_span(message, span)) SyntaxError::Error(FormattedError::new_from_span(message, span))
} }
pub fn invalid_import_list(span: &Span) -> Self {
Self::new_from_span("Cannot import empty list".to_string(), span)
}
pub fn unexpected_eof(span: &Span) -> Self { pub fn unexpected_eof(span: &Span) -> Self {
Self::new_from_span("unexpected EOF".to_string(), span) Self::new_from_span("unexpected EOF".to_string(), span)
} }

View File

@ -136,7 +136,7 @@ impl ParserContext {
/// Returns a vector of [`PackageAccess`] AST nodes if the next tokens represent package access /// Returns a vector of [`PackageAccess`] AST nodes if the next tokens represent package access
/// expressions within an import statement. /// expressions within an import statement.
/// ///
pub fn parse_package_accesses(&mut self) -> SyntaxResult<Vec<PackageAccess>> { pub fn parse_package_accesses(&mut self, span: &Span) -> SyntaxResult<Vec<PackageAccess>> {
let mut out = Vec::new(); let mut out = Vec::new();
self.expect(Token::LeftParen)?; self.expect(Token::LeftParen)?;
while self.eat(Token::RightParen).is_none() { while self.eat(Token::RightParen).is_none() {
@ -147,6 +147,11 @@ impl ParserContext {
break; break;
} }
} }
if out.is_empty() {
return Err(SyntaxError::invalid_import_list(span));
}
Ok(out) Ok(out)
} }
@ -156,7 +161,7 @@ impl ParserContext {
/// ///
pub fn parse_package_access(&mut self) -> SyntaxResult<PackageAccess> { pub fn parse_package_access(&mut self) -> SyntaxResult<PackageAccess> {
if let Some(SpannedToken { span, .. }) = self.eat(Token::Mul) { if let Some(SpannedToken { span, .. }) = self.eat(Token::Mul) {
Ok(PackageAccess::Star(span)) Ok(PackageAccess::Star { span })
} else { } else {
let name = self.expect_ident()?; let name = self.expect_ident()?;
if self.peek_token().as_ref() == &Token::Dot { if self.peek_token().as_ref() == &Token::Dot {
@ -247,7 +252,7 @@ impl ParserContext {
let package_name = self.parse_package_name()?; let package_name = self.parse_package_name()?;
self.expect(Token::Dot)?; self.expect(Token::Dot)?;
if self.peek()?.token == Token::LeftParen { if self.peek()?.token == Token::LeftParen {
let accesses = self.parse_package_accesses()?; let accesses = self.parse_package_accesses(&package_name.span)?;
Ok(PackageOrPackages::Packages(Packages { Ok(PackageOrPackages::Packages(Packages {
span: &package_name.span + accesses.last().map(|x| x.span()).unwrap_or(&package_name.span), span: &package_name.span + accesses.last().map(|x| x.span()).unwrap_or(&package_name.span),
name: package_name, name: package_name,