pub struct Ast { /* private fields */ }Expand description
A parsed source: its nodes, the comments beside them, and the warnings the parse had.
Implementations§
Source§impl Ast
impl Ast
Sourcepub fn root(&self) -> NodeId
pub fn root(&self) -> NodeId
The Node::Program.
pub fn node(&self, id: NodeId) -> &Node
pub fn span(&self, id: NodeId) -> Span
pub fn is_empty(&self) -> bool
Sourcepub fn ids(&self) -> impl Iterator<Item = NodeId> + '_
pub fn ids(&self) -> impl Iterator<Item = NodeId> + '_
Every node, in the order they were made: children before their parents, and the program last.
Sourcepub fn warnings(&self) -> &[Diagnostic]
pub fn warnings(&self) -> &[Diagnostic]
What the parse noticed that isn’t an error, like a legacy octal literal in sloppy code.
Sourcepub fn unparenthesized(&self, id: NodeId) -> NodeId
pub fn unparenthesized(&self, id: NodeId) -> NodeId
The node inside any parentheses around id.
Source§impl Ast
impl Ast
Sourcepub fn to_estree(&self, src: &str) -> String
pub fn to_estree(&self, src: &str) -> String
The tree as ESTree JSON. src is the source it was parsed from.
let src = "a?.b";
let ast = parsec_js::parse_script(src).unwrap();
let json = ast.to_estree(src);
assert!(json.starts_with(r#"{"type":"Program","start":0,"end":4,"body":[{"type":"ExpressionStatement""#));
assert!(json.contains(r#""type":"ChainExpression""#));pub fn to_estree_with(&self, src: &str, options: EstreeOptions) -> String
Source§impl Ast
impl Ast
Sourcepub fn reparse(
&mut self,
src: &str,
edit: &Edit<'_>,
options: &Options,
) -> Result<Reparsed, Diagnostic>
pub fn reparse( &mut self, src: &str, edit: &Edit<'_>, options: &Options, ) -> Result<Reparsed, Diagnostic>
Makes edit to src, the source this tree was parsed from, and
brings the tree up to date with it: the smallest function body or
statement around the edit is parsed again and the rest kept, or
where that can’t be sure to give what parse would, the new
source is parsed whole. The tree’s source type is kept, and
options gives the limits.
A syntax error in the new source is the Err, and the tree is
left as it was, of src.
use parsec_js::{Edit, Options, Span, parse_script};
let src = "function f() {\n return 1;\n}\nf();\n";
let mut ast = parse_script(src).unwrap();
let edit = Edit { range: Span::new(24, 25), text: "42" };
let done = ast.reparse(src, &edit, &Options::default()).unwrap();
assert_eq!(done.source, "function f() {\n return 42;\n}\nf();\n");
// Only the `return` was parsed again.
assert_eq!(done.reparsed, Some(Span::new(17, 27)));
assert_eq!(ast.to_estree(&done.source), parse_script(&done.source).unwrap().to_estree(&done.source));