Struct leo_parser::parser::context::ParserContext

source ·
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>

source

pub fn new( handler: &'a Handler, node_builder: &'a NodeBuilder, tokens: Vec<SpannedToken>, ) -> Self

Returns a new ParserContext type given a vector of tokens.

source

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].

source

pub(super) fn check(&self, tok: &Token) -> bool

Checks whether the current token is tok.

source

pub(super) fn check_int(&self) -> bool

Checks whether the current token is a Token::Integer(_).

source

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.

source

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.

source

pub(super) fn emit_err(&self, err: ParserError)

Emit the error err.

source

pub(super) fn emit_warning(&self, warning: ParserWarning)

Emit the warning warning.

source

pub(crate) fn has_next(&self) -> bool

Returns true if the next token exists.

source

fn mk_ident_prev(&self, name: Symbol) -> Identifier

At the previous token, return and make an identifier with name.

source

pub(super) fn eat_identifier(&mut self) -> Option<Identifier>

Eats the next token if it is an identifier and returns it.

source

pub(super) fn expect_identifier(&mut self) -> Result<Identifier>

Expects an Identifier, or errors.

source

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.

source

pub(super) fn eat_any(&mut self, tokens: &[Token]) -> bool

Eats any of the given tokens, returning true if anything was eaten.

source

pub(super) fn unexpected<T>(&self, expected: impl Display) -> Result<T>

Returns an unexpected error at the current token.

source

pub(super) fn expect(&mut self, token: &Token) -> Result<Span>

Eats the expected token, or errors.

source

pub(super) fn expect_any(&mut self, tokens: &[Token]) -> Result<Span>

Eats one of the expected tokens, or errors.

source

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 Ts 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.

source

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.

source

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.

source

pub(super) fn peek_is_left_par(&self) -> bool

Returns true if the current token is (.

source§

impl<N: Network> ParserContext<'_, N>

source

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.

source

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].

source

fn bin_expr( node_builder: &NodeBuilder, left: Expression, right: Expression, op: BinaryOperation, ) -> Expression

Constructs a binary expression left op right.

source

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.

source

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].

source

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].

source

fn eat_bin_op(&mut self, tokens: &[Token]) -> Option<BinaryOperation>

Eats one of binary operators matching any in tokens.

source

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].

source

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].

source

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].

source

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].

source

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].

source

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].

source

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].

source

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].

source

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].

source

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].

source

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].

source

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.

source

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.

source

pub(crate) fn parse_expr_tuple( &mut self, ) -> Result<(Vec<Expression>, bool, Span)>

Parses a tuple of Expression AST nodes.

source

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.

source

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.

source

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.

source

fn parse_array_expression(&mut self) -> Result<Expression>

Returns an Expression AST node if the next tokens represent an array initialization expression.

source

fn peek_group_coordinate(&self, dist: &mut usize) -> Option<GroupCoordinate>

Returns a reference to the next token if it is a GroupCoordinate, or None if the next token is not a GroupCoordinate.

source

fn eat_group_partial(&mut self) -> Option<Result<GroupTuple>>

Attempts to parse an affine group literal, if present. If absent, returns None.

source

fn parse_struct_member(&mut self) -> Result<StructVariableInitializer>

source

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 };

source

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>

source

pub fn parse_program(&mut self) -> Result<Program>

Returns a Program AST if all tokens can be consumed and represent a valid Leo program.

source

fn unexpected_item(token: &SpannedToken, expected: &[Token]) -> ParserError

source

pub(super) fn parse_import(&mut self) -> Result<(Symbol, (Program, Span))>

Parses an import statement import foo.leo;.

source

fn parse_program_scope(&mut self) -> Result<ProgramScope>

Parses a program scope program foo.aleo { ... }.

source

fn parse_struct_members(&mut self) -> Result<(Vec<Member>, Span)>

Returns a Vec<Member> AST node if the next tokens represent a struct member.

source

pub(super) fn parse_typed_ident(&mut self) -> Result<(Identifier, Type, Span)>

Parses IDENT: TYPE.

source

fn parse_member_variable_declaration(&mut self) -> Result<Member>

Returns a Member AST node if the next tokens represent a struct member variable.

source

pub(super) fn parse_struct(&mut self) -> Result<(Symbol, Composite)>

Parses a struct or record definition, e.g., struct Foo { ... } or record Foo { ... }.

source

pub(super) fn parse_mapping(&mut self) -> Result<(Symbol, Mapping)>

Parses a mapping declaration, e.g. mapping balances: address => u128.

source

pub(super) fn parse_mode(&mut self) -> Result<Mode>

Returns a [ParamMode] AST node if the next tokens represent a function parameter mode.

source

fn parse_input(&mut self) -> Result<Input>

Returns an Input AST node if the next tokens represent a function input.

source

fn parse_output(&mut self) -> Result<Output>

Returns an Output AST node if the next tokens represent a function output.

source

fn parse_annotation(&mut self) -> Result<Annotation>

Returns an Annotation AST node if the next tokens represent an annotation.

source

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>

source

pub(crate) fn parse_statement(&mut self) -> Result<Statement>

Returns a Statement AST node if the next tokens represent a statement.

source

fn parse_assert_statement(&mut self) -> Result<Statement>

Returns an AssertStatement AST node if the next tokens represent an assertion statement.

source

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.

source

pub(super) fn parse_block(&mut self) -> Result<Block>

Returns a Block AST node if the next tokens represent a block of statements.

source

fn parse_return_statement(&mut self) -> Result<ReturnStatement>

Returns a ReturnStatement AST node if the next tokens represent a return statement.

source

fn parse_conditional_statement(&mut self) -> Result<ConditionalStatement>

Returns a ConditionalStatement AST node if the next tokens represent a conditional statement.

source

fn parse_loop_statement(&mut self) -> Result<IterationStatement>

Returns an IterationStatement AST node if the next tokens represent an iteration statement.

source

fn parse_console_statement(&mut self) -> Result<ConsoleStatement>

Returns a ConsoleStatement AST node if the next tokens represent a console statement.

source

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.

source

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>

source

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.

source

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.

source

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> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more