pub struct Parser<'sess, 'ast> {
pub sess: &'sess Session,
pub arena: &'ast Arena,
pub token: Token,
pub prev_token: Token,
/* private fields */
}Expand description
Solidity and Yul parser.
§Examples
use solar::{
ast,
interface::{Session, diagnostics::EmittedDiagnostics},
parse::Parser,
};
use std::path::Path;
#[test]
fn main() -> Result<(), EmittedDiagnostics> {
let path = Path::new("src/Counter.sol");
// Create a new session with a buffer emitter.
// This is required to capture the emitted diagnostics and to return them at the end.
let sess = Session::builder().with_buffer_emitter(solar::interface::ColorChoice::Auto).build();
// Enter the context and parse the file.
let _ = sess.enter(|| -> solar::interface::Result<()> {
// Set up the parser.
let arena = ast::Arena::new();
let mut parser = Parser::from_file(&sess, &arena, path)?;
// Parse the file.
let ast = parser.parse_file().map_err(|e| e.emit())?;
println!("parsed {path:?}: {ast:#?}");
Ok(())
});
// Return the emitted diagnostics as a `Result<(), _>`.
// If any errors were emitted, this returns `Err(_)`, otherwise `Ok(())`.
// Note that this discards warnings and other non-error diagnostics.
sess.emitted_errors().unwrap()
}Fields§
§sess: &'sess SessionThe parser session.
arena: &'ast ArenaThe arena where the AST nodes are allocated.
token: TokenThe current token.
prev_token: TokenThe previous token.
Implementations§
Source§impl<'sess, 'ast> Parser<'sess, 'ast>
impl<'sess, 'ast> Parser<'sess, 'ast>
Sourcepub fn parse_expr(&mut self) -> PResult<'sess, Box<'ast, Expr<'ast>>>
pub fn parse_expr(&mut self) -> PResult<'sess, Box<'ast, Expr<'ast>>>
Parses an expression.
Source§impl<'sess, 'ast> Parser<'sess, 'ast>
impl<'sess, 'ast> Parser<'sess, 'ast>
Sourcepub fn parse_file(&mut self) -> PResult<'sess, SourceUnit<'ast>>
pub fn parse_file(&mut self) -> PResult<'sess, SourceUnit<'ast>>
Parses a source unit.
Sourcepub fn parse_item(&mut self) -> PResult<'sess, Option<Item<'ast>>>
pub fn parse_item(&mut self) -> PResult<'sess, Option<Item<'ast>>>
Parses an item.
Sourcepub fn parse_semver_req(&mut self) -> PResult<'sess, SemverReq<'ast>>
pub fn parse_semver_req(&mut self) -> PResult<'sess, SemverReq<'ast>>
Parses a SemVer version requirement.
See crates/ast/src/ast/semver.rs for more details on the implementation.
Source§impl<'sess, 'ast> Parser<'sess, 'ast>
impl<'sess, 'ast> Parser<'sess, 'ast>
Sourcepub fn parse_lit(
&mut self,
with_subdenomination: bool,
) -> PResult<'sess, (Lit<'ast>, Option<SubDenomination>)>
pub fn parse_lit( &mut self, with_subdenomination: bool, ) -> PResult<'sess, (Lit<'ast>, Option<SubDenomination>)>
Parses a literal with an optional subdenomination.
Note that the subdenomination gets applied to the literal directly, and is returned just for display reasons.
Returns None if no subdenomination was parsed or if the literal is not a number or rational.
Sourcepub fn parse_subdenomination(&mut self) -> Option<SubDenomination>
pub fn parse_subdenomination(&mut self) -> Option<SubDenomination>
Parses a subdenomination.
Source§impl<'sess, 'ast> Parser<'sess, 'ast>
impl<'sess, 'ast> Parser<'sess, 'ast>
Sourcepub fn parse_stmt(&mut self) -> PResult<'sess, Stmt<'ast>>
pub fn parse_stmt(&mut self) -> PResult<'sess, Stmt<'ast>>
Parses a statement.
Sourcepub fn parse_stmt_boxed(&mut self) -> PResult<'sess, Box<'ast, Stmt<'ast>>>
pub fn parse_stmt_boxed(&mut self) -> PResult<'sess, Box<'ast, Stmt<'ast>>>
Parses a statement into a new allocation.
Source§impl<'sess, 'ast> Parser<'sess, 'ast>
impl<'sess, 'ast> Parser<'sess, 'ast>
Sourcepub fn parse_type(&mut self) -> PResult<'sess, Type<'ast>>
pub fn parse_type(&mut self) -> PResult<'sess, Type<'ast>>
Parses a type.
Source§impl<'sess, 'ast> Parser<'sess, 'ast>
impl<'sess, 'ast> Parser<'sess, 'ast>
Sourcepub fn parse_yul_file_object(&mut self) -> PResult<'sess, Object<'ast>>
pub fn parse_yul_file_object(&mut self) -> PResult<'sess, Object<'ast>>
Parses a Yul object or plain block.
The plain block gets returned as a Yul object named “object”, with a single code block.
See: https://github.com/argotorg/solidity/blob/eff410eb746f202fe756a2473fd0c8a718348457/libyul/ObjectParser.cpp#L50
Sourcepub fn parse_yul_object(
&mut self,
docs: DocComments<'ast>,
) -> PResult<'sess, Object<'ast>>
pub fn parse_yul_object( &mut self, docs: DocComments<'ast>, ) -> PResult<'sess, Object<'ast>>
Parses a Yul object.
Reference: https://docs.soliditylang.org/en/latest/yul.html#specification-of-yul-object
Sourcepub fn parse_yul_stmt(&mut self) -> PResult<'sess, Stmt<'ast>>
pub fn parse_yul_stmt(&mut self) -> PResult<'sess, Stmt<'ast>>
Parses a Yul statement.
Sourcepub fn parse_yul_stmt_unchecked(&mut self) -> PResult<'sess, Stmt<'ast>>
pub fn parse_yul_stmt_unchecked(&mut self) -> PResult<'sess, Stmt<'ast>>
Parses a Yul statement, without setting in_yul.
Sourcepub fn parse_yul_block(&mut self) -> PResult<'sess, Block<'ast>>
pub fn parse_yul_block(&mut self) -> PResult<'sess, Block<'ast>>
Parses a Yul block.
Sourcepub fn parse_yul_block_unchecked(&mut self) -> PResult<'sess, Block<'ast>>
pub fn parse_yul_block_unchecked(&mut self) -> PResult<'sess, Block<'ast>>
Parses a Yul block, without setting in_yul.
Source§impl<'sess, 'ast> Parser<'sess, 'ast>
impl<'sess, 'ast> Parser<'sess, 'ast>
Sourcepub fn new(sess: &'sess Session, arena: &'ast Arena, tokens: Vec<Token>) -> Self
pub fn new(sess: &'sess Session, arena: &'ast Arena, tokens: Vec<Token>) -> Self
Creates a new parser.
Sourcepub fn from_source_code(
sess: &'sess Session,
arena: &'ast Arena,
filename: FileName,
src: impl Into<String>,
) -> Result<Self>
pub fn from_source_code( sess: &'sess Session, arena: &'ast Arena, filename: FileName, src: impl Into<String>, ) -> Result<Self>
Creates a new parser from a source code string.
Sourcepub fn from_file(
sess: &'sess Session,
arena: &'ast Arena,
path: &Path,
) -> Result<Self>
pub fn from_file( sess: &'sess Session, arena: &'ast Arena, path: &Path, ) -> Result<Self>
Creates a new parser from a file.
The file will not be read if it has already been added into the source map.
Sourcepub fn from_lazy_source_code(
sess: &'sess Session,
arena: &'ast Arena,
filename: FileName,
get_src: impl FnOnce() -> Result<String>,
) -> Result<Self>
pub fn from_lazy_source_code( sess: &'sess Session, arena: &'ast Arena, filename: FileName, get_src: impl FnOnce() -> Result<String>, ) -> Result<Self>
Creates a new parser from a source code closure.
The closure will not be called if the file name has already been added into the source map.
Sourcepub fn from_source_file(
sess: &'sess Session,
arena: &'ast Arena,
file: &SourceFile,
) -> Self
pub fn from_source_file( sess: &'sess Session, arena: &'ast Arena, file: &SourceFile, ) -> Self
Creates a new parser from a source file.
Note that the source file must be added to the source map before calling this function.
Prefer using from_source_code or from_file
instead.
Sourcepub fn from_lexer(arena: &'ast Arena, lexer: Lexer<'sess, '_>) -> Self
pub fn from_lexer(arena: &'ast Arena, lexer: Lexer<'sess, '_>) -> Self
Creates a new parser from a lexer.
Sourcepub fn alloc_path(&self, segments: &[Ident]) -> AstPath<'ast>
pub fn alloc_path(&self, segments: &[Ident]) -> AstPath<'ast>
Sourcepub fn alloc_vec<T>(&self, values: Vec<T>) -> BoxSlice<'ast, T>
pub fn alloc_vec<T>(&self, values: Vec<T>) -> BoxSlice<'ast, T>
Allocates a list of objects on the AST arena.
Sourcepub fn alloc_smallvec<A: Array>(
&self,
values: SmallVec<A>,
) -> BoxSlice<'ast, A::Item>
pub fn alloc_smallvec<A: Array>( &self, values: SmallVec<A>, ) -> BoxSlice<'ast, A::Item>
Allocates a list of objects on the AST arena.
Sourcepub fn unexpected<T>(&mut self) -> PResult<'sess, T>
pub fn unexpected<T>(&mut self) -> PResult<'sess, T>
Returns an “unexpected token” error in a PResult for the current token.
Sourcepub fn unexpected_error(&mut self) -> PErr<'sess>
pub fn unexpected_error(&mut self) -> PErr<'sess>
Returns an “unexpected token” error for the current token.
Sourcepub fn expect(&mut self, tok: TokenKind) -> PResult<'sess, Recovered>
pub fn expect(&mut self, tok: TokenKind) -> PResult<'sess, Recovered>
Expects and consumes the token t. Signals an error if the next token is not t.
Sourcepub fn expect_one_of(
&mut self,
edible: &[TokenKind],
inedible: &[TokenKind],
) -> PResult<'sess, Recovered>
pub fn expect_one_of( &mut self, edible: &[TokenKind], inedible: &[TokenKind], ) -> PResult<'sess, Recovered>
Expect next token to be edible or inedible token. If edible, then consume it; if inedible, then return without consuming anything. Signal a fatal error if next token is unexpected.
Sourcepub fn eat_noexpect(&mut self, tok: TokenKind) -> bool
pub fn eat_noexpect(&mut self, tok: TokenKind) -> bool
Consumes a token ‘tok’ if it exists. Returns whether the given token was present.
the main purpose of this function is to reduce the cluttering of the suggestions list which using the normal eat method could introduce in some cases.
Sourcepub fn eat(&mut self, tok: TokenKind) -> bool
pub fn eat(&mut self, tok: TokenKind) -> bool
Consumes a token ‘tok’ if it exists. Returns whether the given token was present.
Sourcepub fn eat_keyword(&mut self, kw: Symbol) -> bool
pub fn eat_keyword(&mut self, kw: Symbol) -> bool
If the next token is the given keyword, eats it and returns true.
Otherwise, returns false. An expectation is also added for diagnostics purposes.
Sourcepub fn bump_with(&mut self, next: Token)
pub fn bump_with(&mut self, next: Token)
Advance the parser by one token using provided token as the next one.
§Panics
Panics if the provided token is a comment.
Sourcepub fn look_ahead(&self, dist: usize) -> Token
pub fn look_ahead(&self, dist: usize) -> Token
Returns the token dist tokens ahead of the current one.
Eof will be returned if the look-ahead is any distance past the end of the
tokens.
Sourcepub fn look_ahead_with<R>(&self, dist: usize, f: impl FnOnce(Token) -> R) -> R
pub fn look_ahead_with<R>(&self, dist: usize, f: impl FnOnce(Token) -> R) -> R
Calls f with the token dist tokens ahead of the current one.
See look_ahead for more information.
Source§impl<'sess, 'ast> Parser<'sess, 'ast>
Common parsing methods.
impl<'sess, 'ast> Parser<'sess, 'ast>
Common parsing methods.
Sourcepub fn parse_spanned<T>(
&mut self,
f: impl FnOnce(&mut Self) -> PResult<'sess, T>,
) -> PResult<'sess, (Span, T)>
pub fn parse_spanned<T>( &mut self, f: impl FnOnce(&mut Self) -> PResult<'sess, T>, ) -> PResult<'sess, (Span, T)>
Provides a spanned parser.
Sourcepub fn parse_doc_comments(&mut self) -> DocComments<'ast>
pub fn parse_doc_comments(&mut self) -> DocComments<'ast>
Parses contiguous doc comments. Can be empty.
Sourcepub fn parse_path(&mut self) -> PResult<'sess, AstPath<'ast>>
pub fn parse_path(&mut self) -> PResult<'sess, AstPath<'ast>>
Parses a qualified identifier: foo.bar.baz.
Sourcepub fn parse_path_with(&mut self, first: Ident) -> PResult<'sess, AstPath<'ast>>
pub fn parse_path_with(&mut self, first: Ident) -> PResult<'sess, AstPath<'ast>>
Parses a qualified identifier starting with the given identifier.
Sourcepub fn parse_path_any(&mut self) -> PResult<'sess, AstPath<'ast>>
pub fn parse_path_any(&mut self) -> PResult<'sess, AstPath<'ast>>
Parses a qualified identifier: foo.bar.baz.
Sourcepub fn parse_ident(&mut self) -> PResult<'sess, Ident>
pub fn parse_ident(&mut self) -> PResult<'sess, Ident>
Parses an identifier.
Sourcepub fn parse_ident_any(&mut self) -> PResult<'sess, Ident>
pub fn parse_ident_any(&mut self) -> PResult<'sess, Ident>
Parses an identifier. Does not check if the identifier is a reserved keyword.
Sourcepub fn parse_ident_opt(&mut self) -> PResult<'sess, Option<Ident>>
pub fn parse_ident_opt(&mut self) -> PResult<'sess, Option<Ident>>
Parses an optional identifier.
Auto Trait Implementations§
impl<'sess, 'ast> Freeze for Parser<'sess, 'ast>
impl<'sess, 'ast> !RefUnwindSafe for Parser<'sess, 'ast>
impl<'sess, 'ast> !Send for Parser<'sess, 'ast>
impl<'sess, 'ast> !Sync for Parser<'sess, 'ast>
impl<'sess, 'ast> Unpin for Parser<'sess, 'ast>
impl<'sess, 'ast> !UnwindSafe for Parser<'sess, 'ast>
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, R> CollectAndApply<T, R> for T
impl<T, R> CollectAndApply<T, R> for T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§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 moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> WithSubscriber for T
impl<T> WithSubscriber for T
Source§fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
Source§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: 152 bytes