Skip to main content

Ast

Struct Ast 

Source
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

Source

pub fn root(&self) -> NodeId

Source

pub fn node(&self, id: NodeId) -> &Node

Source

pub fn span(&self, id: NodeId) -> Span

Source

pub fn get(&self, list: List) -> &[NodeId]

The nodes in list.

Source

pub fn len(&self) -> usize

How many nodes there are.

Source

pub fn is_empty(&self) -> bool

Source

pub fn ids(&self) -> impl Iterator<Item = NodeId> + '_

Every node, in the order they were made: children before their parents, and the program last.

Source

pub fn comments(&self) -> &[Comment]

The comments, in source order.

Source

pub fn warnings(&self) -> &[Diagnostic]

What the parse noticed that isn’t an error, like a legacy octal literal in sloppy code.

Source

pub fn unparenthesized(&self, id: NodeId) -> NodeId

The node inside any parentheses around id.

Source§

impl Ast

Source

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""#));
Source

pub fn to_estree_with(&self, src: &str, options: EstreeOptions) -> String

Source§

impl Ast

Source

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));

Trait Implementations§

Source§

impl Clone for Ast

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Ast

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Ast

§

impl Freeze for Ast

§

impl Move for Ast

§

impl RefUnwindSafe for Ast

§

impl Send for Ast

§

impl Sync for Ast

§

impl Unpin for Ast

§

impl UnsafeUnpin for Ast

§

impl UnwindSafe for Ast

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.