[−][src]Struct rslint_parser::Parser
An extremely fast, error tolerant, completely lossless JavaScript parser
The Parser yields lower level events instead of nodes.
These events are then processed into a syntax tree through a TreeSink
implementation.
use rslint_parser::{ Parser, syntax::expr, tokenize, TokenSource, ast::GroupingExpr, LosslessTreeSink, SyntaxNode, process, AstNode }; let source = "(delete b)"; // File id is used for the labels inside parser errors to report them, the file id // is used to look up a file's source code and path inside of a codespan `Files` implementation. let (tokens, lexer_errors) = tokenize(source, 0); assert!(lexer_errors.is_empty()); // The parser uses a token source which manages yielding non-whitespace tokens, // and giving raw tokens to allow the parser to turn completed markers into syntax nodes let token_source = TokenSource::new(source, &tokens); let mut parser = Parser::new(token_source, 0); // Use one of the syntax parsing functions to parse an expression. // This adds node and token events to the parser which are then used to make a node. // A completed marker marks the start and end indices in the events vec which signify // the Start event, and the Finish event. // Completed markers can be turned into an ast node with parse_marker on the parser let completed_marker = expr::expr(&mut parser).unwrap(); parser.parse_marker::<GroupingExpr>(&completed_marker); // Make a new text tree sink, its job is assembling events into a rowan GreenNode. // At each point (Start, Token, Finish, Error) it also consumes whitespace. // Other abstractions can also yield lossy syntax nodes if whitespace is not wanted. // Swap this for a LossyTreeSink for a lossy AST result. let mut sink = LosslessTreeSink::new(source, &tokens); // Consume the parser and get its events, then apply them to a tree sink with `process`. process(&mut sink, parser.finish()); let (green, errors) = sink.finish(); assert!(errors.is_empty()); // Make a new SyntaxNode from the green node let untyped_node = SyntaxNode::new_root(green); assert!(GroupingExpr::can_cast(untyped_node.kind())); // Convert the untyped SyntaxNode into a typed AST node let typed_expr = GroupingExpr::cast(untyped_node).unwrap(); assert_eq!(typed_expr.inner().unwrap().syntax().text(), "delete b");
Fields
file_id: usize
state: ParserState
Implementations
impl<'t> Parser<'t>
[src]
pub fn new(tokens: TokenSource<'t>, file_id: usize) -> Parser<'t>
[src]
Make a new parser configured to parse a script
pub fn new_module(tokens: TokenSource<'t>, file_id: usize) -> Parser<'t>
[src]
Make a new parser configured to parse a module
pub fn token_src(&self, token: &Token) -> &str
[src]
Get the source code of a token
pub fn finish(self) -> Vec<Event>
[src]
Consume the parser and return the list of events it produced
pub fn cur(&self) -> SyntaxKind
[src]
Get the current token kind of the parser
pub fn cur_tok(&self) -> Token
[src]
Get the current token of the parser
pub fn nth(&self, n: usize) -> SyntaxKind
[src]
Look ahead at a token and get its kind, The max lookahead is 4.
Panics
This method panics if the lookahead is higher than 4
,
or if the parser has run this method more than 10m times, as it is a sign of infinite recursion
pub fn nth_tok(&self, n: usize) -> Token
[src]
Look ahead at a token, The max lookahead is 4.
Panics
This method panics if the lookahead is higher than 4
,
or if the parser has run this method more than 10m times, as it is a sign of infinite recursion
pub fn at(&self, kind: SyntaxKind) -> bool
[src]
Check if the parser is currently at a specific token
pub fn nth_at(&self, n: usize, kind: SyntaxKind) -> bool
[src]
Check if a token lookahead is something, n
must be smaller or equal to 4
pub fn eat(&mut self, kind: SyntaxKind) -> bool
[src]
Consume the next token if kind
matches.
pub fn err_recover(
&mut self,
error: impl Into<ParserError>,
recovery: TokenSet,
include_braces: bool
)
[src]
&mut self,
error: impl Into<ParserError>,
recovery: TokenSet,
include_braces: bool
)
Recover from an error with a recovery set or by using a {
or }
.
pub fn err_recover_no_err(&mut self, recovery: TokenSet, include_braces: bool)
[src]
Recover from an error but don't add an error to the events
pub fn start(&mut self) -> Marker
[src]
Starts a new node in the syntax tree. All nodes and tokens
consumed between the start
and the corresponding Marker::complete
belong to the same node.
pub fn has_linebreak_before_n(&self, n: usize) -> bool
[src]
Check if there was a linebreak before a lookahead
pub fn bump(&mut self, kind: SyntaxKind)
[src]
Consume the next token if kind
matches.
pub fn bump_remap(&mut self, kind: SyntaxKind)
[src]
Consume any token but cast it as a different kind
pub fn bump_any(&mut self)
[src]
Advances the parser by one token
pub fn err_builder(&self, message: &str) -> ErrorBuilder
[src]
Make a new error builder with error
severity
pub fn error(&mut self, err: impl Into<ParserError>)
[src]
Add an error event
pub fn at_ts(&self, kinds: TokenSet) -> bool
[src]
Check if the parser's current token is contained in a token set
pub fn cur_src(&self) -> &str
[src]
Get the source code of the parser's current token.
Panics
This method panics if the token range and source code range mismatch
pub fn expect(&mut self, kind: SyntaxKind) -> bool
[src]
Try to eat a specific token kind, if the kind is not there then add an error to the events stack.
pub fn marker_range(&self, marker: &CompletedMarker) -> Range<usize>
[src]
Get the byte index range of a completed marker for error reporting.
pub fn parse_marker<T: AstNode>(&self, marker: &CompletedMarker) -> T
[src]
Parse a completed marker into an ast node.
Panics
Panics if the AST node represented by the marker does not match the generic
pub fn source(&self, range: TextRange) -> &str
[src]
Get the source code of a range
pub fn rewind(&mut self, pos: usize)
[src]
Rewind the token position back to a former position
pub fn cur_event_pos(&self) -> usize
[src]
Get the current index of the last event
pub fn drain_events(&mut self, amount: usize)
[src]
Remove amount
events from the parser
pub fn token_pos(&self) -> usize
[src]
Get the current token position
pub fn warning_builder(&self, message: &str) -> ErrorBuilder
[src]
Make a new error builder with warning severity
pub fn err_and_bump(&mut self, err: impl Into<ParserError>)
[src]
Bump and add an error event
impl<'t> Parser<'t>
[src]
pub fn with_state<'a>(&'a mut self, state: ParserState) -> StateGuard<'a, 't>
[src]
Auto Trait Implementations
impl<'t> !RefUnwindSafe for Parser<'t>
impl<'t> Send for Parser<'t>
impl<'t> !Sync for Parser<'t>
impl<'t> Unpin for Parser<'t>
impl<'t> UnwindSafe for Parser<'t>
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> Erasable for T
unsafe fn unerase(this: NonNull<Erased>) -> NonNull<T>
const ACK_1_1_0: bool
fn erase(this: NonNull<Self>) -> NonNull<Erased>
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,