Struct rslint_parser::Parser[][src]

pub struct Parser<'t> {
    pub file_id: usize,
    pub state: ParserState,
    pub syntax: Syntax,
    pub errors: Vec<ParserError>,
    // some fields omitted
}
Expand description

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,
    Syntax
};

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, Syntax::default());

// 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`.
let (events, errors) = parser.finish();
process(&mut sink, events, errors);

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: usizestate: ParserStatesyntax: Syntaxerrors: Vec<ParserError>

Implementations

Make a new parser

Get the source code of a token

Consume the parser and return the list of events it produced

Get the current token kind of the parser

Get the current token of the parser

Look ahead at a token and get its kind, The max lookahead is 4.

Look ahead at a token, The max lookahead is 4.

Check if the parser is currently at a specific token

Check if a token lookahead is something, n must be smaller or equal to 4

Consume the next token if kind matches.

Recover from an error with a recovery set or by using a { or }.

Recover from an error but don’t add an error to the events

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.

Check if there was a linebreak before a lookahead

Consume the next token if kind matches.

Consume any token but cast it as a different kind

Advances the parser by one token

Make a new error builder with error severity

Add an error

Check if the parser’s current token is contained in a token set

Get the source code of the parser’s current token.

Panics

This method panics if the token range and source code range mismatch

Try to eat a specific token kind, if the kind is not there then add an error to the events stack.

Get the byte index range of a completed marker for error reporting.

Parse a completed marker into an ast node.

Panics

Panics if the AST node represented by the marker does not match the generic

Get the source code of a range

Rewind the parser back to a previous position in time

Get a checkpoint representing the progress of the parser at this point in time

Get the current token position

Make a new error builder with warning severity

Bump and add an error event

Try running a parser function and backtrack if any errors occured

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Unerase this erased pointer. Read more

Whether this implementor has acknowledged the 1.1.0 update to unerase’s documented implementation requirements. Read more

Turn this erasable pointer into an erased pointer. Read more

Performs the conversion.

Performs the conversion.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.