pub struct Parser<'a> {
pub file_id: SourceFileId,
pub diagnostics: Vec<Diagnostic>,
/* private fields */
}
Expand description
Parser
maintains the parsing state, such as the token stream,
“enclosure” (paren, brace, ..) stack, diagnostics, etc.
Syntax parsing logic is in the crate::grammar
module.
See [BTParser
] if you need backtrackable parser.
Fields§
§file_id: SourceFileId
§diagnostics: Vec<Diagnostic>
The diagnostics (errors and warnings) emitted during parsing.
Implementations§
Source§impl<'a> Parser<'a>
impl<'a> Parser<'a>
Sourcepub fn new(file_id: SourceFileId, content: &'a str) -> Self
pub fn new(file_id: SourceFileId, content: &'a str) -> Self
Create a new parser for a source code string and associated file id.
Sourcepub fn as_bt_parser<'b>(&'b mut self) -> BTParser<'a, 'b>
pub fn as_bt_parser<'b>(&'b mut self) -> BTParser<'a, 'b>
Returns back tracking parser.
Sourcepub fn next(&mut self) -> ParseResult<Token<'a>>
pub fn next(&mut self) -> ParseResult<Token<'a>>
Return the next token, or an error if we’ve reached the end of the file.
Sourcepub fn peek_or_err(&mut self) -> ParseResult<TokenKind>
pub fn peek_or_err(&mut self) -> ParseResult<TokenKind>
Take a peek at the next token kind without consuming it, or return an error if we’ve reached the end of the file.
Sourcepub fn peek(&mut self) -> Option<TokenKind>
pub fn peek(&mut self) -> Option<TokenKind>
Take a peek at the next token kind. Returns None
if we’ve reached the
end of the file.
Sourcepub fn split_next(&mut self) -> ParseResult<Token<'a>>
pub fn split_next(&mut self) -> ParseResult<Token<'a>>
Split the next token into two tokens, returning the first. Only supports
splitting the >>
token into two >
tokens, specifically for
parsing the closing angle bracket of a generic type argument list
(Map<x, Map<y, z>>
).
§Panics
Panics if the next token isn’t >>
pub fn eat_newlines(&mut self)
Sourcepub fn assert(&mut self, tk: TokenKind) -> Token<'a>
pub fn assert(&mut self, tk: TokenKind) -> Token<'a>
Assert that the next token kind it matches the expected token kind, and return it. This should be used in cases where the next token kind is expected to have been checked already.
§Panics
Panics if the next token kind isn’t tk
.
Sourcepub fn expect<S: Into<String>>(
&mut self,
expected: TokenKind,
message: S,
) -> ParseResult<Token<'a>>
pub fn expect<S: Into<String>>( &mut self, expected: TokenKind, message: S, ) -> ParseResult<Token<'a>>
If the next token matches the expected kind, return it. Otherwise emit an error diagnostic with the given message and return an error.
Sourcepub fn expect_with_notes<Str, NotesFn>(
&mut self,
expected: TokenKind,
message: Str,
notes_fn: NotesFn,
) -> ParseResult<Token<'a>>
pub fn expect_with_notes<Str, NotesFn>( &mut self, expected: TokenKind, message: Str, notes_fn: NotesFn, ) -> ParseResult<Token<'a>>
Like Parser::expect
, but with additional notes to be appended to the
bottom of the diagnostic message. The notes are provided by a
function that returns a Vec<String>
, to avoid allocations in the
case where the token is as expected.
Sourcepub fn optional(&mut self, kind: TokenKind) -> Option<Token<'a>>
pub fn optional(&mut self, kind: TokenKind) -> Option<Token<'a>>
If the next token matches the expected kind, return it. Otherwise return None.
Sourcepub fn unexpected_token_error<S: Into<String>>(
&mut self,
tok: &Token<'_>,
message: S,
notes: Vec<String>,
)
pub fn unexpected_token_error<S: Into<String>>( &mut self, tok: &Token<'_>, message: S, notes: Vec<String>, )
Emit an “unexpected token” error diagnostic with the given message.
Sourcepub fn enter_block(
&mut self,
context_span: Span,
context_name: &str,
) -> ParseResult<()>
pub fn enter_block( &mut self, context_span: Span, context_name: &str, ) -> ParseResult<()>
Enter a “block”, which is a brace-enclosed list of statements,
separated by newlines and/or semicolons.
This checks for and consumes the {
that precedes the block.
Sourcepub fn expect_stmt_end(&mut self, context_name: &str) -> ParseResult<()>
pub fn expect_stmt_end(&mut self, context_name: &str) -> ParseResult<()>
Consumes newlines and semicolons. Returns Ok if one or more newlines or
semicolons are consumed, or if the next token is a }
.