Skip to main content

JsParser

Struct JsParser 

Source
pub struct JsParser;
Expand description

JavaScript parser using PEG grammar.

Parses ES6 JavaScript source code into an ESTree-compliant AST. The parser is built on Pest and follows the ECMAScript 2015 specification.

§Examples

use just::parser::JsParser;

let code = "var x = 5 + 3;";
let ast = JsParser::parse_to_ast_from_str(code).unwrap();
assert_eq!(ast.body.len(), 1);

Implementations§

Source§

impl JsParser

Source

pub fn parse_to_token_tree(script: &str) -> Result<String, String>

Parse JavaScript source into a debug token tree.

Returns a formatted string representation of the parse tree, useful for debugging the parser grammar.

§Examples
use just::parser::JsParser;

let code = "var x = 5;";
let tree = JsParser::parse_to_token_tree(code).unwrap();
assert!(tree.contains("script"));
Source

pub fn parse_to_ast(script: Rc<String>) -> Result<ProgramData, JsError<Rule>>

Parse JavaScript source into an AST.

Takes an Rc<String> to allow efficient sharing of the source text across AST nodes for error reporting and formatting.

§Examples
use just::parser::JsParser;
use std::rc::Rc;

let code = Rc::new("var x = 5 + 3;".to_string());
let ast = JsParser::parse_to_ast(code).unwrap();
assert_eq!(ast.body.len(), 1);
Source

pub fn parse_to_ast_from_str(script: &str) -> Result<ProgramData, JsError<Rule>>

Parse JavaScript source string into an AST.

Convenience method that wraps the source in an Rc<String>. This is the most commonly used parsing method.

§Examples
use just::parser::JsParser;

let code = "function add(a, b) { return a + b; }";
let ast = JsParser::parse_to_ast_from_str(code).unwrap();
assert_eq!(ast.body.len(), 1);
Source

pub fn parse_to_ast_formatted_string( script: &str, ) -> Result<String, JsError<Rule>>

Parse JavaScript and return a formatted AST string.

Useful for debugging and visualizing the AST structure.

§Examples
use just::parser::JsParser;

let code = "var x = 5;";
let formatted = JsParser::parse_to_ast_formatted_string(code).unwrap();
assert!(formatted.contains("VariableDeclaration"));
Source

pub fn parse_numeric_string( s: &String, is_error_on_empty: bool, ) -> Result<ExtendedNumberLiteralType, JsError<Rule>>

Parse a numeric literal string.

Supports decimal, hexadecimal (0x), binary (0b), octal (0o), floating point, and scientific notation.

§Arguments
  • s - The numeric string to parse
  • is_error_on_empty - Whether to return an error on empty string
§Examples
use just::parser::JsParser;

let hex = "0xFF".to_string();
let result = JsParser::parse_numeric_string(&hex, true).unwrap();

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.