pub struct Parser {
pub source_file: SourceFile,
pub tokens: Vec<Token>,
pub current: usize,
pub ast_arena: Ast,
pub record_names: HashSet<String>,
pub tag_names: HashSet<String>,
}Expand description
Parses a flat list of tokens and produces a Vec<Statement>.
The parser owns the token stream and advances through it with a single
current index cursor. All sub-parsers borrow self mutably and call
the cursor primitives (peek, advance, match_type, etc.) defined
in this module.
Construct via Parser::parse - there is no public new.
Fields§
§source_file: SourceFilethe source file (text + name) carried for error reports
tokens: Vec<Token>the full token list produced by the lexer, including the terminal [TokenType::Eof]
current: usizeindex of the token currently being examined (the “read head”)
ast_arena: Ast§record_names: HashSet<String>Names of record types declared so far, used to disambiguate
Name { ... } struct literals from block bodies (e.g. if x { }).
tag_names: HashSet<String>Names of tag (enum) types declared so far, used to recognize
Name.Variant as an enum variant reference rather than a field access.
Implementations§
Source§impl Parser
impl Parser
Sourcepub fn parse_comparsion(&mut self) -> Result<ExprId, Error>
pub fn parse_comparsion(&mut self) -> Result<ExprId, Error>
Parses comparison and compound-assignment operators:
<, <=, >, >=, +=, -=, *=, /=.
Compound-assignment operators (+= etc.) are desugared in-place: when
the left-hand side is a plain ExpressionKind::Identifier, they expand
to an ExpressionKind::Assign whose value is the corresponding
ExpressionKind::Binary. If the LHS is not a simple identifier the
operator is treated as a plain binary expression (the checker will reject
it later if needed).
Source§impl Parser
impl Parser
Sourcepub fn parse_equality(&mut self) -> Result<ExprId, Error>
pub fn parse_equality(&mut self) -> Result<ExprId, Error>
Parses == and != binary expressions (lowest precedence).
Left-associative: a == b != c is (a == b) != c.
Source§impl Parser
impl Parser
Sourcepub fn parse_factor(&mut self) -> Result<ExprId, Error>
pub fn parse_factor(&mut self) -> Result<ExprId, Error>
Parses multiplicative expressions: * and /.
Left-associative: a * b / c is (a * b) / c.
Source§impl Parser
impl Parser
Sourcepub fn parse_postfix(
&mut self,
expr: ExprId,
start: Span,
) -> Result<ExprId, Error>
pub fn parse_postfix( &mut self, expr: ExprId, start: Span, ) -> Result<ExprId, Error>
Parses zero or more postfix method-call chains on expr.
After any primary or sub-expression is built, this function consumes
repeated .method(args) suffixes, producing a left-associative chain of
ExpressionKind::MethodCall nodes. Method names may themselves be
namespaced (obj.module::method()).
Returns expr unchanged when the next token is not ..
§Errors
Returns an error if . is not followed by a valid identifier, or if the
argument list is not opened with (.
Source§impl Parser
impl Parser
Sourcepub fn parse_primary(&mut self) -> Result<ExprId, Error>
pub fn parse_primary(&mut self) -> Result<ExprId, Error>
Parses a primary expression - the highest-precedence, non-recursive forms.
Handles (in order):
- Identifiers - plain names, module paths (
a::b::c), function calls (f(args)), variable assignments (x = expr), index access (arr[i]), chained index access (arr[i][j]), index-assign (arr[i] = expr), and call-on-index (fns[0](args)). - Array literals -
[a, b, c] - Integer literals -
42 - Byte literals -
0bstyle byte values - String literals -
"hello" - Character literals -
'x' - Boolean literals -
true/false - Float literals -
3.14 - Null - the
nullkeyword - Grouped expressions -
(expr) - Lambda expressions -
fn(params) -> T { body }
Every matched form is then passed through parse_postfix to handle
any trailing method-call chains.
§Errors
Returns an error with the message "expected expression" if none of the
above forms match the current token.
Source§impl Parser
impl Parser
Sourcepub fn parse_term(&mut self) -> Result<ExprId, Error>
pub fn parse_term(&mut self) -> Result<ExprId, Error>
Parses additive expressions: + and -.
Left-associative: a + b - c is (a + b) - c.
Source§impl Parser
impl Parser
Sourcepub fn parse_unary(&mut self) -> Result<ExprId, Error>
pub fn parse_unary(&mut self) -> Result<ExprId, Error>
Parses unary prefix expressions: ! (logical not) and - (negation).
Right-associative by recursion: --x parses as -(-(x)).
Falls through to parse_primary when no prefix operator is present.
Source§impl Parser
impl Parser
Sourcepub fn parse_expression(&mut self) -> Result<ExprId, Error>
pub fn parse_expression(&mut self) -> Result<ExprId, Error>
Entry point for expression parsing. Delegates to parse_equality.
Source§impl Parser
impl Parser
Sourcepub fn parse(
tokens: Vec<Token>,
source_file: SourceFile,
) -> Result<(Ast, Vec<Statement>), Error>
pub fn parse( tokens: Vec<Token>, source_file: SourceFile, ) -> Result<(Ast, Vec<Statement>), Error>
Entry point: consumes tokens and returns a fully-parsed statement list.
Drives the top-level parse loop, calling parse_statement_to_ast until
[TokenType::Eof] is reached.
§Errors
Returns the first Error encountered; parsing stops immediately.
Source§impl Parser
impl Parser
Sourcepub fn parse_const_declartion(
&mut self,
start: Span,
) -> Result<Statement, Error>
pub fn parse_const_declartion( &mut self, start: Span, ) -> Result<Statement, Error>
Parses a CONST constant declaration.
Called after CONST has been consumed. Handles two forms:
-
Constant array -
CONST array[T] NAME = [items]orCONST array[T] NAME = expr- detected by thearraykeyword followed by[. ProducesStatementKind::ConstantArrayfor inline literals orStatementKind::ConstantDeclarationwithTypeAnnotation::CArrayfor expression initialisers. -
Scalar constant -
const T NAME = expr- producesStatementKind::ConstantDeclarationwith the correspondingC*annotation viaparse_type(false).
§Errors
Returns an error if the type, name, =, or initialiser expression is
missing or malformed.
fn parse_const_declartion_scalar( &mut self, start: Span, ) -> Result<Statement, Error>
Source§impl Parser
impl Parser
Sourcepub fn parse_for(&mut self, start: Span) -> Result<Statement, Error>
pub fn parse_for(&mut self, start: Span) -> Result<Statement, Error>
Parses a for statement in one of its three forms.
Called after for has been consumed. Dispatches on the next token:
-
[- C-style loop. Reads[T i = init, condition, increment]then a body block. ProducesStatementKind::For. -
identifier - inspects the token after the identifier name:
- followed by
in N..Morin [items]->StatementKind::ForRangewith a pre-evaluatedStatementKind::Rangeofi64values. - followed by
in <identifier>->StatementKind::ForEach, iterating over an array expression at runtime.
- followed by
§Errors
Returns an error for invalid range elements (non-integer), missing
commas in C-style headers, or unrecognised for syntax.
Source§impl Parser
impl Parser
Sourcepub fn parse_function(
&mut self,
start: Span,
attribute: Option<FunctionAttribute>,
) -> Result<Statement, Error>
pub fn parse_function( &mut self, start: Span, attribute: Option<FunctionAttribute>, ) -> Result<Statement, Error>
Parses a named function declaration.
Called after fn has been consumed (either by parse_statement_to_ast
or parse_entry_attribute). Reads:
- The function name (identifier).
- A
(-delimited, comma-separated parameter list ofT namepairs. - An optional
-> Treturn type annotation (defaults toTypeAnnotation::Nullwhen absent). - The function body via
parse_block.
Produces StatementKind::FunctionDeclaration.
§Parameters
start- the span of thefntoken, used as the start of the overall span.is_entry-truewhen the function was marked with!#[entry].
§Errors
Returns an error if the function name, a parameter name, or the body block are missing or malformed.
Source§impl Parser
impl Parser
Sourcepub fn parse_if(&mut self, start: Span) -> Result<Statement, Error>
pub fn parse_if(&mut self, start: Span) -> Result<Statement, Error>
Parses an if statement, including any else if / else tail.
Called after if has been consumed. Reads:
- The condition expression.
- The
{-delimited if-body viaparse_block. - An optional
elsetail - either anotherif(recursed) or a plainelse { … }block (condition isNone).
Blank lines between the closing } and else are skipped.
Produces StatementKind::Conditional with one or two
StatementKind::ConditionalBranch children.
Source§impl Parser
impl Parser
Sourcepub fn parse_import(&mut self, start: Span) -> Result<Statement, Error>
pub fn parse_import(&mut self, start: Span) -> Result<Statement, Error>
Parses a get import statement.
Called after get has been consumed. Dispatches on the tokens that
follow the first identifier:
-
get mod(no::, nofrom) - single-segment file import. ProducesStatementKind::ImportFile{ path: [mod] }. -
get mod::sub::…- multi-segment path. If the first segment isstd, the last segment is treated as the function name and the rest as the namespace path ->StatementKind::Import. Otherwise the whole path is a file module ->StatementKind::ImportFile. -
get name, name from path- named imports. Ifpathstarts withstd->StatementKind::Import{ names, path }. Otherwise ->StatementKind::ImportFileNamed{ path, names }.
§Errors
Returns an error if an identifier is missing after get, ::, ,, or
from, or if from itself is absent in the named-import form.
Source§impl Parser
impl Parser
Sourcepub fn parse_record_declaration(
&mut self,
start: Span,
) -> Result<Statement, Error>
pub fn parse_record_declaration( &mut self, start: Span, ) -> Result<Statement, Error>
Parses a record declaration.
Called after record has been consumed. Reads the record name,
then a {-delimited, comma-separated list of T name field pairs.
Produces StatementKind::RecordDeclaration.
§Errors
Returns an error if the record name, a field type/name, or the
closing } are missing or malformed.
Source§impl Parser
impl Parser
Sourcepub fn parse_tag_declaration(&mut self, start: Span) -> Result<Statement, Error>
pub fn parse_tag_declaration(&mut self, start: Span) -> Result<Statement, Error>
Parses a tag declaration.
Called after tag has been consumed. Reads the tag name, then a
{-delimited, comma-separated list of variant names.
Produces StatementKind::TagDeclaration.
§Errors
Returns an error if the tag name, a variant name, or the closing
} are missing or malformed.
Source§impl Parser
impl Parser
Sourcepub fn parse_variable_declartion(
&mut self,
start: Span,
) -> Result<Statement, Error>
pub fn parse_variable_declartion( &mut self, start: Span, ) -> Result<Statement, Error>
Parses a dec variable declaration.
Called after dec has been consumed. Handles two forms:
-
Array declaration -
dec array[T] name = [items]ordec array[T] name = expr- detected by thearraykeyword followed by[. ProducesStatementKind::Arrayfor inline literals orStatementKind::VariableDeclarationwithTypeAnnotation::Arrayfor expression initialisers. -
Scalar declaration -
dec T name = expr- producesStatementKind::VariableDeclaration.
§Errors
Returns an error if the type, name, =, or initialiser expression is
missing or malformed.
fn parse_variable_declartion_scalar( &mut self, start: Span, ) -> Result<Statement, Error>
Source§impl Parser
impl Parser
Sourcepub fn parse_while(&mut self, start: Span) -> Result<Statement, Error>
pub fn parse_while(&mut self, start: Span) -> Result<Statement, Error>
Parses a while loop.
Called after while has been consumed. Reads the loop condition as an
expression, then the loop body via parse_block, and produces
StatementKind::While.
Source§impl Parser
impl Parser
Sourcepub fn parse_statement_to_ast(&mut self) -> Result<Statement, Error>
pub fn parse_statement_to_ast(&mut self) -> Result<Statement, Error>
Dispatches the current token to the appropriate statement sub-parser.
| Token | Action |
|---|---|
Newline | skip - emits a no-op Expression(0) placeholder |
get | parse_import |
dec | parse_variable_declaration |
CONST | parse_const_declaration |
while | parse_while |
for | parse_for |
if | parse_if |
fn | parse_function |
!# | parse_entry_attribute |
return | inline - parses optional return value |
break | inline - emits StatementKind::Break |
continue | inline - emits StatementKind::Continue |
| anything else | parse_expression wrapped in StatementKind::Expression |
Sourcepub fn parse_block(&mut self) -> Result<Vec<Statement>, Error>
pub fn parse_block(&mut self) -> Result<Vec<Statement>, Error>
Parses a brace-delimited block { stmts* } into a Vec<Statement>.
Consumes the opening {, then repeatedly calls parse_statement_to_ast
until } or TokenType::Eof is reached. Blank lines (newlines) inside
the block are skipped.
§Errors
Returns an error if the opening { is missing.
Sourcefn parse_entry_attribute(&mut self, start: Span) -> Result<Statement, Error>
fn parse_entry_attribute(&mut self, start: Span) -> Result<Statement, Error>
Parses a !#[entry] attribute and the fn declaration that follows it.
The !# token has already been consumed by [parse_statement_to_ast]
before this is called. Expects [entry] then a function declaration,
which is forwarded to parse_function with is_entry = true.
§Errors
Returns an error if [, the entry identifier, ], or fn are missing
or in the wrong order.
Source§impl Parser
impl Parser
Sourcepub fn parse_type(&mut self, is_mut: bool) -> Result<TypeAnnotation, Error>
pub fn parse_type(&mut self, is_mut: bool) -> Result<TypeAnnotation, Error>
Parses a type keyword into a TypeAnnotation.
The is_mut flag controls which annotation variant is produced:
is_mut | produced variant |
|---|---|
true | Int, Float, Bool, String, Byte, Char, Fn, Array(T) |
false | CInt, CFloat, CBool, CString, CByte, CChar, Fn, CArray(T) |
The C* variants represent constant (immutable) bindings and are used by
the const declaration path.
For array[T] the inner element type is parsed recursively.
§Errors
Returns an error if the current token is not a recognised type keyword.
Source§impl Parser
impl Parser
Sourcepub fn peek_span(&self) -> Span
pub fn peek_span(&self) -> Span
Returns the Span of the token at the current read head without consuming it.
Sourcepub fn previous_span(&self) -> Span
pub fn previous_span(&self) -> Span
Sourcepub fn is_at_end(&self) -> bool
pub fn is_at_end(&self) -> bool
Returns true when the read head sits on TokenType::Eof, indicating
the end of the token stream.
Sourcepub fn advance(&mut self)
pub fn advance(&mut self)
Advances the read head by one token, unless already at TokenType::Eof.
Sourcepub fn peek(&self) -> TokenType
pub fn peek(&self) -> TokenType
Returns the TokenType at the current read head without consuming it.
Sourcepub fn check(&self, token_type: &TokenType) -> bool
pub fn check(&self, token_type: &TokenType) -> bool
Returns true if the token at the read head matches token_type.
Literal-carrying variants (NumberLiteral, StringLiteral, etc.) are
matched by variant tag only - the inner value is ignored. Does not
consume the token.
Sourcepub fn match_type(&mut self, types: &[TokenType]) -> bool
pub fn match_type(&mut self, types: &[TokenType]) -> bool
Sourcepub fn parse_param_type(&mut self) -> Result<TypeAnnotation, Error>
pub fn parse_param_type(&mut self) -> Result<TypeAnnotation, Error>
Parses a single type keyword (or array[T]) as a function/lambda parameter
type annotation.
Called when a type is expected in a parameter list position. For the
array keyword the inner element type is parsed recursively via
nested_array_type.
§Errors
Returns an error if the current token is not a recognised type keyword.
Sourcepub fn nested_array_type(&mut self) -> Result<Box<TypeAnnotation>, Error>
pub fn nested_array_type(&mut self) -> Result<Box<TypeAnnotation>, Error>
Parses the [T] suffix of an array[T] type annotation, returning a
boxed TypeAnnotation::Array.
Expects the array keyword to have already been consumed. Reads
[, the inner element type (via parse_param_type), then ].
§Errors
Returns an error if [ or ] are missing, or if the inner type is invalid.
pub fn nested_map_type(&mut self) -> Result<Box<TypeAnnotation>, Error>
pub fn set_type(&mut self) -> Result<Box<TypeAnnotation>, Error>
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> 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§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);