pub struct Parser {
pub tokens: Vec<Token>,
pub cursor: usize,
pub current_token: Token,
pub base: Option<String>,
pub spans: Vec<Span>,
pub remapper: Remapper,
pub(crate) loop_variables: Vec<String>,
}Expand description
The Parser
Fields§
§tokens: Vec<Token>Vector of the tokens
cursor: usizeCurrent position
current_token: TokenCurrent token
base: Option<String>Current base path for resolving imports
spans: Vec<Span>A collection of current spans
remapper: RemapperOur remapper
loop_variables: Vec<String>Stack of active for loop variable names (for nested loops)
Implementations§
Source§impl Parser
impl Parser
Sourcepub fn new(tokens: Vec<Token>, base: Option<String>) -> Self
pub fn new(tokens: Vec<Token>, base: Option<String>) -> Self
Public associated function that instantiates a Parser.
Sourcepub fn reset(&mut self)
pub fn reset(&mut self)
Resets the current token and cursor to the first token in the parser’s token vec
PANICS if the tokens vec is empty!
Sourcepub fn parse_imports(&mut self) -> Result<FilePath, ParserError>
pub fn parse_imports(&mut self) -> Result<FilePath, ParserError>
Parses Contract Imports
Sourcepub fn match_kind(&mut self, kind: TokenKind) -> Result<TokenKind, ParserError>
pub fn match_kind(&mut self, kind: TokenKind) -> Result<TokenKind, ParserError>
Match current token to a type.
Sourcepub fn check(&mut self, kind: TokenKind) -> bool
pub fn check(&mut self, kind: TokenKind) -> bool
Check the current token’s type against the given type.
Sourcepub(crate) fn check_duplicate_label(
&self,
contract: &mut Contract,
macro_name: &str,
label: String,
span: Span,
) -> Result<(), ParserError>
pub(crate) fn check_duplicate_label( &self, contract: &mut Contract, macro_name: &str, label: String, span: Span, ) -> Result<(), ParserError>
Checks whether the input label is unique in a macro.
Sourcepub fn consume_all(&mut self, kinds: Vec<TokenKind>)
pub fn consume_all(&mut self, kinds: Vec<TokenKind>)
Consumes following tokens until not contained in the kinds vec of TokenKinds.
Sourcepub fn peek_behind(&self) -> Option<Token>
pub fn peek_behind(&self) -> Option<Token>
Take a look at the previous token.
Sourcepub fn parse_function(&mut self) -> Result<FunctionDefinition, ParserError>
pub fn parse_function(&mut self) -> Result<FunctionDefinition, ParserError>
Parses a function. Adheres to https://github.com/huff-language/huffc/blob/master/src/parser/high-level.ts#L87-L111
Sourcepub fn parse_event(&mut self) -> Result<EventDefinition, ParserError>
pub fn parse_event(&mut self) -> Result<EventDefinition, ParserError>
Parse an event.
Sourcepub fn parse_constant(&mut self) -> Result<ConstantDefinition, ParserError>
pub fn parse_constant(&mut self) -> Result<ConstantDefinition, ParserError>
Parse a constant.
Sourcepub fn parse_custom_error(&mut self) -> Result<ErrorDefinition, ParserError>
pub fn parse_custom_error(&mut self) -> Result<ErrorDefinition, ParserError>
Parse a custom error definition.
Sourcepub fn parse_decorator(&mut self) -> Result<Decorator, ParserError>
pub fn parse_decorator(&mut self) -> Result<Decorator, ParserError>
Parses the decorator blocks above a macro definition.
Several #[...] blocks may be stacked; their flags are merged in source order. Flag names
and arguments are only checked for syntax here, the test runner decides what they mean.
Sourcepub(crate) fn parse_decorator_flag(
&mut self,
) -> Result<DecoratorFlag, ParserError>
pub(crate) fn parse_decorator_flag( &mut self, ) -> Result<DecoratorFlag, ParserError>
Parses a single decorator flag: name or name(arg, key = arg, ...).
Sourcepub(crate) fn parse_decorator_arg(
&mut self,
flag: &str,
) -> Result<DecoratorArg, ParserError>
pub(crate) fn parse_decorator_arg( &mut self, flag: &str, ) -> Result<DecoratorArg, ParserError>
Parses a decorator flag argument, optionally prefixed with key =.
Sourcepub(crate) fn span_from(&self, start: &Span) -> Span
pub(crate) fn span_from(&self, start: &Span) -> Span
The source range from start through the last consumed token.
Sourcepub(crate) fn decorator_error(&self, hint: String) -> ParserError
pub(crate) fn decorator_error(&self, hint: String) -> ParserError
Builds a decorator syntax error at the current token.
Sourcepub fn parse_macro(
&mut self,
contract: &mut Contract,
) -> Result<MacroDefinition, ParserError>
pub fn parse_macro( &mut self, contract: &mut Contract, ) -> Result<MacroDefinition, ParserError>
Parses a macro.
It should parse the following : macro MACRO_NAME(args…) = takes (x) returns (n) {…}
Sourcepub(crate) fn at_opcode_operand(&self) -> bool
pub(crate) fn at_opcode_operand(&self) -> bool
Returns true if the current token can start an opcode operand.
Sourcepub(crate) fn takes_opcode_operands(&self, o: Opcode) -> bool
pub(crate) fn takes_opcode_operands(&self, o: Opcode) -> bool
Returns true if the opcode just consumed is followed by operands resolved at compile time,
e.g. dupn 17 or push2 [SIZE]. A push followed by a hex literal keeps the plain form.
Sourcepub(crate) fn parse_opcode_operands(
&mut self,
o: Opcode,
opcode_span: Span,
) -> Result<Statement, ParserError>
pub(crate) fn parse_opcode_operands( &mut self, o: Opcode, opcode_span: Span, ) -> Result<Statement, ParserError>
Parses the operands of an opcode that carries immediate data.
Covers DUPN, SWAPN and EXCHANGE (dupn 17, exchange 1 2) as well as PUSH1..PUSH32 with a
non-literal value (push2 [SIZE]). Each operand is a compile-time expression: a decimal or
hex literal, [CONSTANT], <arg>, a loop variable, or a parenthesized expression.
Literal operands are validated immediately; all others once resolved during codegen.
Sourcepub fn parse_body(
&mut self,
macro_name: &str,
contract: &mut Contract,
) -> Result<Vec<Statement>, ParserError>
pub fn parse_body( &mut self, macro_name: &str, contract: &mut Contract, ) -> Result<Vec<Statement>, ParserError>
Parse the body of a macro.
Only HEX, OPCODES, labels, builtins, and MACRO calls should be authorized.
Sourcepub fn parse_for_loop(
&mut self,
macro_name: &str,
) -> Result<Statement, ParserError>
pub fn parse_for_loop( &mut self, macro_name: &str, ) -> Result<Statement, ParserError>
Parse a for loop statement
Syntax: for(var in start..end) { body } or for(var in start..end step N) { body }
Sourcepub fn parse_if_statement(
&mut self,
macro_name: &str,
) -> Result<Statement, ParserError>
pub fn parse_if_statement( &mut self, macro_name: &str, ) -> Result<Statement, ParserError>
Parse a compile-time if statement
Sourcepub(crate) fn parse_if_body(
&mut self,
macro_name: &str,
) -> Result<Vec<Statement>, ParserError>
pub(crate) fn parse_if_body( &mut self, macro_name: &str, ) -> Result<Vec<Statement>, ParserError>
Parse the body of an if statement (reuses for loop body parsing logic)
Sourcepub(crate) fn parse_for_body(
&mut self,
macro_name: &str,
) -> Result<Vec<Statement>, ParserError>
pub(crate) fn parse_for_body( &mut self, macro_name: &str, ) -> Result<Vec<Statement>, ParserError>
Parse the body of a for loop (similar to parse_body but handles interpolation)
Sourcepub fn parse_label(
&mut self,
parent_macro_name: String,
) -> Result<Vec<Statement>, ParserError>
pub fn parse_label( &mut self, parent_macro_name: String, ) -> Result<Vec<Statement>, ParserError>
Parse the body of a label.
§Examples
Below is an example of a label that contains a Macro Invocation, Literals, and Opcodes.
error:
TRANSFER()
0x20 0x00 returnSourcepub fn parse_newline(&mut self) -> Result<(), ParserError>
pub fn parse_newline(&mut self) -> Result<(), ParserError>
Parse new lines.
No-return since newlines are non-essential.
Sourcepub(crate) fn at_builtin_expression_arg(&self) -> bool
pub(crate) fn at_builtin_expression_arg(&self) -> bool
Returns true if the current builtin argument is a compile-time expression rather than a
single literal, constant, identifier, or builtin call, e.g. <arg> or [BASE] + 0x20.
Sourcepub fn parse_builtin_args(
&mut self,
) -> Result<Vec<BuiltinFunctionArg>, ParserError>
pub fn parse_builtin_args( &mut self, ) -> Result<Vec<BuiltinFunctionArg>, ParserError>
Parse the argument of a builtin function call.
Sourcepub fn parse_args(
&mut self,
select_name: bool,
select_type: bool,
has_indexed: bool,
) -> Result<Vec<Argument>, ParserError>
pub fn parse_args( &mut self, select_name: bool, select_type: bool, has_indexed: bool, ) -> Result<Vec<Argument>, ParserError>
Parse abi arguments
Arguments can be typed or not. Between parenthesis. Works for both inputs and outputs. It should parse the following : (uint256 a, bool b, …)
Sourcepub fn parse_single_arg(&mut self) -> Result<usize, ParserError>
pub fn parse_single_arg(&mut self) -> Result<usize, ParserError>
Parses the following : (x)
Sourcepub(crate) fn parse_single_macro_argument(
&mut self,
) -> Result<MacroArg, ParserError>
pub(crate) fn parse_single_macro_argument( &mut self, ) -> Result<MacroArg, ParserError>
Parse a single macro argument
Sourcepub fn parse_macro_call_args(
&mut self,
macro_name: String,
) -> Result<Vec<MacroArg>, ParserError>
pub fn parse_macro_call_args( &mut self, macro_name: String, ) -> Result<Vec<MacroArg>, ParserError>
Parse the arguments of a macro call.
Sourcepub fn parse_table(&mut self) -> Result<TableDefinition, ParserError>
pub fn parse_table(&mut self) -> Result<TableDefinition, ParserError>
Parses a table (JumpTable, JumpTablePacked, or CodeTable).
It should parse the following : (jumptable|jumptable__packed|table) NAME() {…}
Sourcepub fn parse_table_body(
&mut self,
is_code_table: bool,
) -> Result<Vec<Statement>, ParserError>
pub fn parse_table_body( &mut self, is_code_table: bool, ) -> Result<Vec<Statement>, ParserError>
Parse the body of a table.
Only LabelCall and Code Statements should be authorized.
Sourcepub fn parse_constant_push(&mut self) -> Result<(String, Span), ParserError>
pub fn parse_constant_push(&mut self) -> Result<(String, Span), ParserError>
Parses a constant push.
Sourcepub fn parse_arg_call(&mut self) -> Result<(String, Span), ParserError>
pub fn parse_arg_call(&mut self) -> Result<(String, Span), ParserError>
Parses an argument call.
§Examples
When an argument is called in Huff, it is wrapped in angle brackets like so:
#define macro EXAMPLE_FUNCTION(error) = takes (0) returns (0) {
<error> jumpi
}Sourcepub(crate) fn parse_arg_call_or_invocation(
&mut self,
parent_macro_name: String,
) -> Result<Statement, ParserError>
pub(crate) fn parse_arg_call_or_invocation( &mut self, parent_macro_name: String, ) -> Result<Statement, ParserError>
Parse an arg call or arg macro invocation: <arg> or <arg>(...)
Returns either an ArgCall or ArgMacroInvocation statement
Sourcepub(crate) fn parse_arg_call_or_invocation_after_angle(
&mut self,
parent_macro_name: String,
angle_span: Span,
) -> Result<Statement, ParserError>
pub(crate) fn parse_arg_call_or_invocation_after_angle( &mut self, parent_macro_name: String, angle_span: Span, ) -> Result<Statement, ParserError>
Parse an arg call or arg macro invocation after < has already been consumed
This is used in for loop bodies where we check for loop variables first
Sourcepub fn parse_nl_or_whitespace(&mut self) -> Result<(), ParserError>
pub fn parse_nl_or_whitespace(&mut self) -> Result<(), ParserError>
Parses whitespaces and newlines until none are left.
Sourcepub fn parse_arg_type(&mut self) -> Result<String, ParserError>
pub fn parse_arg_type(&mut self) -> Result<String, ParserError>
Parses the type of an argument as its canonical ABI name, such as uint256[] or
(address,uint256)[2].
Sourcepub(crate) fn parse_tuple_component(&mut self) -> Result<String, ParserError>
pub(crate) fn parse_tuple_component(&mut self) -> Result<String, ParserError>
Parses a component of a tuple type, whose optional name is not part of the type
Sourcepub fn parse_primitive_type(
&mut self,
prim: PrimitiveEVMType,
) -> Result<TokenKind, ParserError>
pub fn parse_primitive_type( &mut self, prim: PrimitiveEVMType, ) -> Result<TokenKind, ParserError>
Parses a primitive EVM type. Arrays of primitive types are not considered as primitive types themselves.
Sourcepub(crate) fn is_expression_context(&mut self) -> bool
pub(crate) fn is_expression_context(&mut self) -> bool
Check if the current context indicates we’re parsing an expression (i.e., the next token is an operator)
Sourcepub(crate) fn parse_constant_expression(
&mut self,
) -> Result<Expression, ParserError>
pub(crate) fn parse_constant_expression( &mut self, ) -> Result<Expression, ParserError>
Parse an arithmetic expression (entry point for constant expressions).
Implements operator precedence:
- Lowest: ==, !=, <, >, <=, >= (comparison)
- Higher: + - (additive)
- Higher: * / % (multiplicative)
- Highest: unary -, !, literals, parentheses
Sourcepub(crate) fn parse_comparison_expression(
&mut self,
) -> Result<Expression, ParserError>
pub(crate) fn parse_comparison_expression( &mut self, ) -> Result<Expression, ParserError>
Parse comparison operators (lowest precedence binary operators).
Sourcepub(crate) fn parse_additive_expression(
&mut self,
) -> Result<Expression, ParserError>
pub(crate) fn parse_additive_expression( &mut self, ) -> Result<Expression, ParserError>
Parse addition and subtraction.
Sourcepub(crate) fn parse_multiplicative_expression(
&mut self,
) -> Result<Expression, ParserError>
pub(crate) fn parse_multiplicative_expression( &mut self, ) -> Result<Expression, ParserError>
Parse multiplication, division, and modulo (higher precedence).
Sourcepub(crate) fn parse_unary_expression(
&mut self,
) -> Result<Expression, ParserError>
pub(crate) fn parse_unary_expression( &mut self, ) -> Result<Expression, ParserError>
Parse unary operators (negation and logical NOT).
Sourcepub(crate) fn parse_primary_expression(
&mut self,
) -> Result<Expression, ParserError>
pub(crate) fn parse_primary_expression( &mut self, ) -> Result<Expression, ParserError>
Parse primary expressions: literals, bracketed constants, and parenthesized expressions.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Parser
impl RefUnwindSafe for Parser
impl Send for Parser
impl Sync for Parser
impl Unpin for Parser
impl UnsafeUnpin for Parser
impl UnwindSafe for Parser
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> 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 moreSource§impl<S, T> Upcast<T> for S
impl<S, T> Upcast<T> for S
§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘwhere
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘwhere
S: Into<Dispatch>,
§fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ
fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ
Layout§
Note: Most layout information is completely unstable and may even differ between compilations. The only exception is types with certain repr(...) attributes. Please see the Rust Reference's “Type Layout” chapter for details on type layout guarantees.
Size: 240 bytes