Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Aleo ins parser] Fix array type anomaly. #2017

Merged
merged 2 commits into from
Oct 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions console/program/src/data_types/array_type/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ impl<N: Network> Parser for ArrayType<N> {
Ok((string, length))
}

// Parse the whitespace and comments from the string.
let (string, _) = Sanitizer::parse(string)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this parse is justified. At the least we should have parse_whitespaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This array type parser is called by the plaintext type parser, which expects whitespace to have been skipped over already: the other two alternative parsers for plaintext type, namely literal type and identifier, do not skip over whitespace.

This is an instance of a general pattern in the Aleo instructions parser:

  1. Some (larger) constructs are parsed by first skipping over white space (and sometimes also comments).
  2. Other (smaller) constructs are parsed without any such initial skipping, i.e. they are expected to start right there. Their callers skip white space already.

Plaintext types, including array types, belong to the second group of constructs above.

Copy link
Member

@howardwu howardwu Oct 8, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see your perspective, we should make sure its parent callers have some notion of parse_whitespace since we don't invoke string.trim() before calling the parser.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, definitely. I believe that to be the case. I don't have a formal proof yet, but here's an argument. As you may know, I've derived the ABNF grammar of Aleo instructions directly from the parser. So under the (good) assumption that the grammar is consistent with the parser, we can see the following in the grammar:

  • Every occurrence of plaintext-type not at the start of the right side of a rule is preceded by ws, which corresponds to Sanitizer::parse_whitespaces.
  • plainttext-type occurs at the start of the right sides of value-type, mapping-type, finalize-type, and entry-type.
  • We repeat the same argument for value-type, ..., entry-type, recursively, and find that there is always a preceding ws.

(That was actually a sketch of a formal proof.)

// Parse the opening brackets and validate the number of dimensions.
let (string, dimensions) = map_res(many0_count(pair(tag("["), Sanitizer::parse_whitespaces)), |dimensions| {
if dimensions.is_zero() {
Expand Down