pub(crate) struct ParserContext<'a, N: Network> {
pub(crate) handler: &'a Handler,
pub(crate) node_builder: &'a NodeBuilder,
tokens: Vec<SpannedToken>,
pub(crate) token: SpannedToken,
pub(crate) prev_token: SpannedToken,
pub(crate) disallow_struct_construction: bool,
pub(crate) program_name: Option<Symbol>,
phantom: PhantomData<N>,
}
Expand description
Stores a program in tokenized format plus additional context.
May be converted into a Program
AST by parsing all tokens.
Fields§
§handler: &'a Handler
Handler used to side-channel emit errors from the parser.
node_builder: &'a NodeBuilder
Counter used to generate unique node ids.
tokens: Vec<SpannedToken>
All un-bumped tokens.
token: SpannedToken
The current token, i.e., if p.tokens = ['3', *, '4']
,
then after a p.bump()
, we’ll have p.token = '3'
.
prev_token: SpannedToken
The previous token, i.e., if p.tokens = ['3', *, '4']
,
then after two p.bump()
s, we’ll have p.token = '*'
and p.prev_token = '3'
.
disallow_struct_construction: bool
True if parsing an expression for if and loop statements – means struct inits are not legal.
program_name: Option<Symbol>
The name of the program being parsed.
phantom: PhantomData<N>
Implementations§
Source§impl<'a, N: Network> ParserContext<'a, N>
impl<'a, N: Network> ParserContext<'a, N>
Sourcepub fn new(
handler: &'a Handler,
node_builder: &'a NodeBuilder,
tokens: Vec<SpannedToken>,
) -> Self
pub fn new( handler: &'a Handler, node_builder: &'a NodeBuilder, tokens: Vec<SpannedToken>, ) -> Self
Returns a new ParserContext
type given a vector of tokens.
Sourcepub(crate) fn bump(&mut self)
pub(crate) fn bump(&mut self)
Advances the parser cursor by one token.
So e.g., if we had previous = A
, current = B
, and tokens = [C, D, E]
,
then after p.bump()
, the state will be previous = B
, current = C
, and tokens = [D, E]
.
Sourcepub(super) fn check_int(&self) -> bool
pub(super) fn check_int(&self) -> bool
Checks whether the current token is a Token::Integer(_)
.
Sourcepub(super) fn eat(&mut self, token: &Token) -> bool
pub(super) fn eat(&mut self, token: &Token) -> bool
Returns true
if the next token is equal to the given token.
Advances the parser to the next token.
Sourcepub(super) fn look_ahead<'s, R>(
&'s self,
dist: usize,
looker: impl FnOnce(&'s SpannedToken) -> R,
) -> R
pub(super) fn look_ahead<'s, R>( &'s self, dist: usize, looker: impl FnOnce(&'s SpannedToken) -> R, ) -> R
Look-ahead dist
tokens of self.token
and get access to that token there.
When dist == 0
then the current token is looked at.
Sourcepub(super) fn emit_err(&self, err: ParserError)
pub(super) fn emit_err(&self, err: ParserError)
Emit the error err
.
Sourcepub(super) fn emit_warning(&self, warning: ParserWarning)
pub(super) fn emit_warning(&self, warning: ParserWarning)
Emit the warning warning
.
Sourcefn mk_ident_prev(&self, name: Symbol) -> Identifier
fn mk_ident_prev(&self, name: Symbol) -> Identifier
At the previous token, return and make an identifier with name
.
Sourcepub(super) fn eat_identifier(&mut self) -> Option<Identifier>
pub(super) fn eat_identifier(&mut self) -> Option<Identifier>
Eats the next token if it is an identifier and returns it.
Sourcepub(super) fn expect_identifier(&mut self) -> Result<Identifier>
pub(super) fn expect_identifier(&mut self) -> Result<Identifier>
Expects an Identifier
, or errors.
Sourcepub fn eat_whole_number(&mut self) -> Result<(NonNegativeNumber, Span)>
pub fn eat_whole_number(&mut self) -> Result<(NonNegativeNumber, Span)>
Removes the next token if it is a [Token::Integer(_)
] and returns it, or None if
the next token is not a [Token::Integer(_)
] or if the next token does not exist.
Sourcepub(super) fn eat_any(&mut self, tokens: &[Token]) -> bool
pub(super) fn eat_any(&mut self, tokens: &[Token]) -> bool
Eats any of the given tokens
, returning true
if anything was eaten.
Sourcepub(super) fn unexpected<T>(&self, expected: impl Display) -> Result<T>
pub(super) fn unexpected<T>(&self, expected: impl Display) -> Result<T>
Returns an unexpected error at the current token.
Sourcepub(super) fn expect(&mut self, token: &Token) -> Result<Span>
pub(super) fn expect(&mut self, token: &Token) -> Result<Span>
Eats the expected token
, or errors.
Sourcepub(super) fn expect_any(&mut self, tokens: &[Token]) -> Result<Span>
pub(super) fn expect_any(&mut self, tokens: &[Token]) -> Result<Span>
Eats one of the expected tokens
, or errors.
Sourcepub(super) fn parse_list<T>(
&mut self,
delimiter: Delimiter,
sep: Option<Token>,
inner: impl FnMut(&mut Self) -> Result<Option<T>>,
) -> Result<(Vec<T>, bool, Span)>
pub(super) fn parse_list<T>( &mut self, delimiter: Delimiter, sep: Option<Token>, inner: impl FnMut(&mut Self) -> Result<Option<T>>, ) -> Result<(Vec<T>, bool, Span)>
Parses a list of T
s using inner
The opening and closing delimiters are specified in delimiter
,
and elements in the list are optionally separated by sep
.
When (list, true)
is returned, sep
was a terminator.
Sourcepub(super) fn parse_paren_comma_list<T>(
&mut self,
f: impl FnMut(&mut Self) -> Result<Option<T>>,
) -> Result<(Vec<T>, bool, Span)>
pub(super) fn parse_paren_comma_list<T>( &mut self, f: impl FnMut(&mut Self) -> Result<Option<T>>, ) -> Result<(Vec<T>, bool, Span)>
Parse a list separated by ,
and delimited by parens.
Sourcepub(super) fn parse_bracket_comma_list<T>(
&mut self,
f: impl FnMut(&mut Self) -> Result<Option<T>>,
) -> Result<(Vec<T>, bool, Span)>
pub(super) fn parse_bracket_comma_list<T>( &mut self, f: impl FnMut(&mut Self) -> Result<Option<T>>, ) -> Result<(Vec<T>, bool, Span)>
Parse a list separated by ,
and delimited by brackets.
Sourcepub(super) fn peek_is_left_par(&self) -> bool
pub(super) fn peek_is_left_par(&self) -> bool
Returns true if the current token is (
.
Sourcepub(crate) fn check_identifier(&self, identifier: &Identifier)
pub(crate) fn check_identifier(&self, identifier: &Identifier)
Error on identifiers that are longer than SnarkVM allows.
Source§impl<N: Network> ParserContext<'_, N>
impl<N: Network> ParserContext<'_, N>
Sourcepub(crate) fn parse_expression(&mut self) -> Result<Expression>
pub(crate) fn parse_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next token is an expression.
Includes struct init expressions.
Sourcepub(super) fn parse_conditional_expression(&mut self) -> Result<Expression>
pub(super) fn parse_conditional_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent
a ternary expression. May or may not include struct init expressions.
Otherwise, tries to parse the next token using [parse_boolean_or_expression
].
Sourcefn bin_expr(
node_builder: &NodeBuilder,
left: Expression,
right: Expression,
op: BinaryOperation,
) -> Expression
fn bin_expr( node_builder: &NodeBuilder, left: Expression, right: Expression, op: BinaryOperation, ) -> Expression
Constructs a binary expression left op right
.
Sourcefn parse_bin_expr(
&mut self,
tokens: &[Token],
f: impl FnMut(&mut Self) -> Result<Expression>,
) -> Result<Expression>
fn parse_bin_expr( &mut self, tokens: &[Token], f: impl FnMut(&mut Self) -> Result<Expression>, ) -> Result<Expression>
Parses a left-associative binary expression <left> token <right>
using f
for left/right.
The token
is translated to op
in the AST.
Sourcefn parse_boolean_or_expression(&mut self) -> Result<Expression>
fn parse_boolean_or_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent
a binary OR expression.
Otherwise, tries to parse the next token using [parse_boolean_and_expression
].
Sourcefn parse_boolean_and_expression(&mut self) -> Result<Expression>
fn parse_boolean_and_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
binary AND expression.
Otherwise, tries to parse the next token using [parse_equality_expression
].
fn eat_unary_op(&mut self) -> Option<UnaryOperation>
Sourcefn eat_bin_op(&mut self, tokens: &[Token]) -> Option<BinaryOperation>
fn eat_bin_op(&mut self, tokens: &[Token]) -> Option<BinaryOperation>
Eats one of binary operators matching any in tokens
.
Sourcefn parse_ordering_expression(&mut self) -> Result<Expression>
fn parse_ordering_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
binary relational expression: less than, less than or equals, greater than, greater than or equals.
Otherwise, tries to parse the next token using [parse_additive_expression
].
Sourcefn parse_equality_expression(&mut self) -> Result<Expression>
fn parse_equality_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
binary equals or not equals expression.
Otherwise, tries to parse the next token using [parse_ordering_expression
].
Sourcefn parse_bitwise_exclusive_or_expression(&mut self) -> Result<Expression>
fn parse_bitwise_exclusive_or_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
bitwise exclusive or expression.
Otherwise, tries to parse the next token using [parse_bitwise_inclusive_or_expression
].
Sourcefn parse_bitwise_inclusive_or_expression(&mut self) -> Result<Expression>
fn parse_bitwise_inclusive_or_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
bitwise inclusive or expression.
Otherwise, tries to parse the next token using [parse_bitwise_and_expression
].
Sourcefn parse_bitwise_and_expression(&mut self) -> Result<Expression>
fn parse_bitwise_and_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
bitwise and expression.
Otherwise, tries to parse the next token using [parse_shift_expression
].
Sourcefn parse_shift_expression(&mut self) -> Result<Expression>
fn parse_shift_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
shift left or a shift right expression.
Otherwise, tries to parse the next token using [parse_additive_expression
].
Sourcefn parse_additive_expression(&mut self) -> Result<Expression>
fn parse_additive_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
binary addition or subtraction expression.
Otherwise, tries to parse the next token using [parse_mul_div_pow_expression
].
Sourcefn parse_multiplicative_expression(&mut self) -> Result<Expression>
fn parse_multiplicative_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
binary multiplication, division, or a remainder expression.
Otherwise, tries to parse the next token using [parse_exponential_expression
].
Sourcefn parse_exponential_expression(&mut self) -> Result<Expression>
fn parse_exponential_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
binary exponentiation expression.
Otherwise, tries to parse the next token using [parse_cast_expression
].
Sourcefn parse_cast_expression(&mut self) -> Result<Expression>
fn parse_cast_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
cast expression.
Otherwise, tries to parse the next token using [parse_unary_expression
].
Sourcepub(super) fn parse_unary_expression(&mut self) -> Result<Expression>
pub(super) fn parse_unary_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
unary not, negate, or bitwise not expression.
Otherwise, tries to parse the next token using [parse_postfix_expression
].
Sourcefn parse_method_call_expression(
&mut self,
receiver: Expression,
method: Identifier,
) -> Result<Expression>
fn parse_method_call_expression( &mut self, receiver: Expression, method: Identifier, ) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
method call expression.
Sourcefn parse_associated_access_expression(
&mut self,
module_name: Expression,
) -> Result<Expression>
fn parse_associated_access_expression( &mut self, module_name: Expression, ) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
static access expression.
Sourcepub(crate) fn parse_expr_tuple(
&mut self,
) -> Result<(Vec<Expression>, bool, Span)>
pub(crate) fn parse_expr_tuple( &mut self, ) -> Result<(Vec<Expression>, bool, Span)>
Parses a tuple of Expression
AST nodes.
Sourcefn parse_external_resource(
&mut self,
expr: Expression,
network_span: Span,
) -> Result<Expression>
fn parse_external_resource( &mut self, expr: Expression, network_span: Span, ) -> Result<Expression>
Parses an external function call credits.aleo/transfer()
or locator token.aleo/accounts
.
In the ABNF grammar, an external function call is one of the two kinds of free function calls, namely the one that uses a locator to designate the function; a locator is a kind of primary expression.
Sourcefn parse_postfix_expression(&mut self) -> Result<Expression>
fn parse_postfix_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent an
array access, struct member access, tuple access, or method call expression.
Otherwise, tries to parse the next token using [parse_primary_expression
].
Note that, as mentioned in [parse_primary_expression
],
this function also completes the parsing of some primary expressions
(as defined in the ABNF grammar),
which [parse_primary_expression
] only starts to parse.
Sourcefn parse_tuple_expression(&mut self) -> Result<Expression>
fn parse_tuple_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent
a parenthesized expression or a unit expression
or a tuple initialization expression or an affine group literal.
Sourcefn parse_array_expression(&mut self) -> Result<Expression>
fn parse_array_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent an array initialization expression.
fn parse_struct_member(&mut self) -> Result<StructVariableInitializer>
Sourcepub fn parse_struct_init_expression(
&mut self,
identifier: Identifier,
) -> Result<Expression>
pub fn parse_struct_init_expression( &mut self, identifier: Identifier, ) -> Result<Expression>
Returns an Expression
AST node if the next tokens represent a
struct initialization expression.
let foo = Foo { x: 1u8 };
Sourcefn parse_primary_expression(&mut self) -> Result<Expression>
fn parse_primary_expression(&mut self) -> Result<Expression>
Returns an Expression
AST node if the next token is a primary expression:
- Literals: field, group, unsigned integer, signed integer, boolean, address, string
- Aggregate type constructors: array, tuple, structs
- Identifiers: variables, keywords
This function only parses some of the primary expressions defined in the ABNF grammar;
for the others, it parses their initial parts,
leaving it to the [self.parse_postfix_expression] function to complete the parsing.
For example, of the primary expression u8::c
, this function only parses the u8
part,
leaving it to [self.parse_postfix_expression] to parse the ::c
part.
So technically the expression returned by this function may not quite be
an expression as defined in the ABNF grammar,
but it is only a temporary expression that is combined into a larger one
by [self.parse_postfix_expression], yielding an actual expression according to the grammar.
Returns an expression error if the token cannot be matched.
Source§impl<N: Network> ParserContext<'_, N>
impl<N: Network> ParserContext<'_, N>
Sourcepub fn parse_program(&mut self) -> Result<Program>
pub fn parse_program(&mut self) -> Result<Program>
Returns a Program
AST if all tokens can be consumed and represent a valid Leo program.
fn unexpected_item(token: &SpannedToken, expected: &[Token]) -> ParserError
Sourcepub(super) fn parse_import(&mut self) -> Result<(Symbol, (Program, Span))>
pub(super) fn parse_import(&mut self) -> Result<(Symbol, (Program, Span))>
Parses an import statement import foo.leo;
.
Sourcefn parse_program_scope(&mut self) -> Result<ProgramScope>
fn parse_program_scope(&mut self) -> Result<ProgramScope>
Parses a program scope program foo.aleo { ... }
.
Sourcefn parse_struct_members(&mut self) -> Result<(Vec<Member>, Span)>
fn parse_struct_members(&mut self) -> Result<(Vec<Member>, Span)>
Returns a Vec<Member>
AST node if the next tokens represent a struct member.
Sourcepub(super) fn parse_typed_ident(&mut self) -> Result<(Identifier, Type, Span)>
pub(super) fn parse_typed_ident(&mut self) -> Result<(Identifier, Type, Span)>
Parses IDENT: TYPE
.
Sourcefn parse_member_variable_declaration(&mut self) -> Result<Member>
fn parse_member_variable_declaration(&mut self) -> Result<Member>
Returns a Member
AST node if the next tokens represent a struct member variable.
Sourcepub(super) fn parse_struct(&mut self) -> Result<(Symbol, Composite)>
pub(super) fn parse_struct(&mut self) -> Result<(Symbol, Composite)>
Parses a struct or record definition, e.g., struct Foo { ... }
or record Foo { ... }
.
Sourcepub(super) fn parse_mapping(&mut self) -> Result<(Symbol, Mapping)>
pub(super) fn parse_mapping(&mut self) -> Result<(Symbol, Mapping)>
Parses a mapping declaration, e.g. mapping balances: address => u128
.
Sourcepub(super) fn parse_mode(&mut self) -> Result<Mode>
pub(super) fn parse_mode(&mut self) -> Result<Mode>
Returns a [ParamMode
] AST node if the next tokens represent a function parameter mode.
Sourcefn parse_input(&mut self) -> Result<Input>
fn parse_input(&mut self) -> Result<Input>
Returns an Input
AST node if the next tokens represent a function input.
Sourcefn parse_output(&mut self) -> Result<Output>
fn parse_output(&mut self) -> Result<Output>
Returns an Output
AST node if the next tokens represent a function output.
Sourcefn parse_annotation(&mut self) -> Result<Annotation>
fn parse_annotation(&mut self) -> Result<Annotation>
Returns an Annotation
AST node if the next tokens represent an annotation.
Sourcefn parse_function(&mut self) -> Result<(Symbol, Function)>
fn parse_function(&mut self) -> Result<(Symbol, Function)>
Returns an [(Identifier, Function)
] AST node if the next tokens represent a function name
and function definition.
Source§impl<N: Network> ParserContext<'_, N>
impl<N: Network> ParserContext<'_, N>
Sourcepub(crate) fn parse_statement(&mut self) -> Result<Statement>
pub(crate) fn parse_statement(&mut self) -> Result<Statement>
Returns a Statement
AST node if the next tokens represent a statement.
Sourcefn parse_assert_statement(&mut self) -> Result<Statement>
fn parse_assert_statement(&mut self) -> Result<Statement>
Returns an AssertStatement
AST node if the next tokens represent an assertion statement.
Sourcefn parse_assign_statement(&mut self) -> Result<Statement>
fn parse_assign_statement(&mut self) -> Result<Statement>
Returns an AssignStatement
AST node if the next tokens represent an assignment, otherwise expects an expression statement.
Sourcepub(super) fn parse_block(&mut self) -> Result<Block>
pub(super) fn parse_block(&mut self) -> Result<Block>
Returns a Block
AST node if the next tokens represent a block of statements.
Sourcefn parse_return_statement(&mut self) -> Result<ReturnStatement>
fn parse_return_statement(&mut self) -> Result<ReturnStatement>
Returns a ReturnStatement
AST node if the next tokens represent a return statement.
Sourcefn parse_conditional_statement(&mut self) -> Result<ConditionalStatement>
fn parse_conditional_statement(&mut self) -> Result<ConditionalStatement>
Returns a ConditionalStatement
AST node if the next tokens represent a conditional statement.
Sourcefn parse_loop_statement(&mut self) -> Result<IterationStatement>
fn parse_loop_statement(&mut self) -> Result<IterationStatement>
Returns an IterationStatement
AST node if the next tokens represent an iteration statement.
Sourcefn parse_console_statement(&mut self) -> Result<ConsoleStatement>
fn parse_console_statement(&mut self) -> Result<ConsoleStatement>
Returns a ConsoleStatement
AST node if the next tokens represent a console statement.
Sourcepub(super) fn parse_const_declaration_statement(
&mut self,
) -> Result<ConstDeclaration>
pub(super) fn parse_const_declaration_statement( &mut self, ) -> Result<ConstDeclaration>
Returns a ConstDeclaration
AST node if the next tokens represent a const declaration statement.
Sourcepub(super) fn parse_definition_statement(
&mut self,
) -> Result<DefinitionStatement>
pub(super) fn parse_definition_statement( &mut self, ) -> Result<DefinitionStatement>
Returns a DefinitionStatement
AST node if the next tokens represent a definition statement.
Source§impl<N: Network> ParserContext<'_, N>
impl<N: Network> ParserContext<'_, N>
Sourcepub(super) fn token_to_int_type(token: &Token) -> Option<IntegerType>
pub(super) fn token_to_int_type(token: &Token) -> Option<IntegerType>
Returns a IntegerType
AST node if the given token is a supported integer type, or None
.
Sourcepub fn parse_primitive_type(&mut self) -> Result<(Type, Span)>
pub fn parse_primitive_type(&mut self) -> Result<(Type, Span)>
Returns a [(Type, Span)
] tuple of AST nodes if the next token represents a primitive type.
Also returns the span of the parsed token.
These correspond to what the ABNF grammar calls ‘named primitive types’; the ‘primitive types’ according to the ABNF grammar include also the unit type.
Sourcepub fn parse_type(&mut self) -> Result<(Type, Span)>
pub fn parse_type(&mut self) -> Result<(Type, Span)>
Returns a [(Type, Span)
] tuple of AST nodes if the next token represents a type.
Also returns the span of the parsed token.
Auto Trait Implementations§
impl<'a, N> Freeze for ParserContext<'a, N>
impl<'a, N> !RefUnwindSafe for ParserContext<'a, N>
impl<'a, N> !Send for ParserContext<'a, N>
impl<'a, N> !Sync for ParserContext<'a, N>
impl<'a, N> Unpin for ParserContext<'a, N>where
N: Unpin,
impl<'a, N> !UnwindSafe for ParserContext<'a, N>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more