[][src]Function rslint_parser::parse_text_lossy

pub fn parse_text_lossy(text: &str, file_id: usize) -> Parse<Script>

Lossly parse text into a Parse which can then be turned into an untyped root SyntaxNode. Or turned into a typed Script with tree.

Unlike parse_text, the final parse result includes no whitespace, it does however include errors.

Note however that the ranges and text of nodes still includes whitespace! Therefore you should trim text before rendering it.
The util module has utility functions for dealing with this easily.

use rslint_parser::{ast::BracketExpr, parse_text_lossy, AstNode, SyntaxToken, SyntaxNodeExt, util};

let parse = parse_text_lossy("foo. bar[2]", 0);
// The untyped syntax node of `foo.bar[2]`, the root node is `Script`.
let untyped_expr_node = parse.syntax().first_child().unwrap();

// SyntaxNodes can be turned into a nice string representation.
println!("{:#?}", untyped_expr_node);

// You can then cast syntax nodes into a typed AST node.
let typed_ast_node = BracketExpr::cast(untyped_expr_node.first_child().unwrap()).unwrap();

// Everything on every ast node is optional because of error recovery.
let prop = typed_ast_node.prop().unwrap();

// You can then go back to an untyped SyntaxNode and get its range, text, parents, children, etc.
assert_eq!(prop.syntax().text(), "2");

// Util has a function for yielding all tokens of a node.
let tokens = untyped_expr_node.tokens();

// End result does not include whitespace because the parsing is lossy in this case
assert_eq!(&util::concat_tokens(&tokens), "foo.bar[2]")