mirror of https://github.com/boa-dev/boa.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
108 lines
3.3 KiB
108 lines
3.3 KiB
2 years ago
|
//! Boa's **`boa_ast`** crate implements an ECMAScript abstract syntax tree.
|
||
2 years ago
|
//!
|
||
2 years ago
|
//! # Crate Overview
|
||
2 years ago
|
//! **`boa_ast`** contains representations of [**Parse Nodes**][grammar] as defined by the ECMAScript
|
||
2 years ago
|
//! spec. Some `Parse Node`s are not represented by Boa's AST, because a lot of grammar productions
|
||
|
//! are only used to throw [**Early Errors**][early], and don't influence the evaluation of the AST
|
||
|
//! itself.
|
||
2 years ago
|
//!
|
||
|
//! Boa's AST is mainly split in three main components: [`Declaration`]s, [`Expression`]s and
|
||
|
//! [`Statement`]s, with [`StatementList`] being the primordial Parse Node that combines
|
||
|
//! all of them to create a proper AST.
|
||
|
//!
|
||
|
//! [grammar]: https://tc39.es/ecma262/#sec-syntactic-grammar
|
||
|
//! [early]: https://tc39.es/ecma262/#sec-static-semantic-rules
|
||
1 year ago
|
#![doc = include_str!("../ABOUT.md")]
|
||
2 years ago
|
#![doc(
|
||
|
html_logo_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg",
|
||
|
html_favicon_url = "https://raw.githubusercontent.com/boa-dev/boa/main/assets/logo.svg"
|
||
|
)]
|
||
2 years ago
|
#![cfg_attr(not(test), forbid(clippy::unwrap_used))]
|
||
2 years ago
|
#![allow(
|
||
|
clippy::module_name_repetitions,
|
||
|
clippy::too_many_lines,
|
||
2 years ago
|
clippy::option_if_let_else
|
||
2 years ago
|
)]
|
||
2 years ago
|
|
||
2 years ago
|
mod module_item_list;
|
||
2 years ago
|
mod position;
|
||
|
mod punctuator;
|
||
2 years ago
|
mod source;
|
||
2 years ago
|
mod statement_list;
|
||
5 years ago
|
|
||
2 years ago
|
pub mod declaration;
|
||
|
pub mod expression;
|
||
|
pub mod function;
|
||
6 years ago
|
pub mod keyword;
|
||
2 years ago
|
pub mod operations;
|
||
2 years ago
|
pub mod pattern;
|
||
|
pub mod property;
|
||
|
pub mod statement;
|
||
2 years ago
|
pub mod visitor;
|
||
2 years ago
|
|
||
2 years ago
|
use boa_interner::{Interner, ToIndentedString, ToInternedString};
|
||
5 years ago
|
|
||
|
pub use self::{
|
||
2 years ago
|
declaration::Declaration,
|
||
|
expression::Expression,
|
||
5 years ago
|
keyword::Keyword,
|
||
2 years ago
|
module_item_list::{ModuleItem, ModuleItemList},
|
||
5 years ago
|
position::{Position, Span},
|
||
|
punctuator::Punctuator,
|
||
2 years ago
|
source::{Module, Script},
|
||
2 years ago
|
statement::Statement,
|
||
|
statement_list::{StatementList, StatementListItem},
|
||
5 years ago
|
};
|
||
2 years ago
|
|
||
|
/// Utility to join multiple Nodes into a single string.
|
||
|
fn join_nodes<N>(interner: &Interner, nodes: &[N]) -> String
|
||
|
where
|
||
|
N: ToInternedString,
|
||
|
{
|
||
|
let mut first = true;
|
||
|
let mut buf = String::new();
|
||
|
for e in nodes {
|
||
|
if first {
|
||
|
first = false;
|
||
|
} else {
|
||
|
buf.push_str(", ");
|
||
|
}
|
||
|
buf.push_str(&e.to_interned_string(interner));
|
||
|
}
|
||
|
buf
|
||
|
}
|
||
|
|
||
|
/// Displays the body of a block or statement list.
|
||
|
///
|
||
|
/// This includes the curly braces at the start and end. This will not indent the first brace,
|
||
|
/// but will indent the last brace.
|
||
|
fn block_to_string(body: &StatementList, interner: &Interner, indentation: usize) -> String {
|
||
|
if body.statements().is_empty() {
|
||
|
"{}".to_owned()
|
||
|
} else {
|
||
|
format!(
|
||
|
"{{\n{}{}}}",
|
||
|
body.to_indented_string(interner, indentation + 1),
|
||
|
" ".repeat(indentation)
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
/// Utility trait that adds a `UTF-16` escaped representation to every [`[u16]`][slice].
|
||
|
trait ToStringEscaped {
|
||
|
/// Decodes `self` as an `UTF-16` encoded string, escaping any unpaired surrogates by its
|
||
|
/// codepoint value.
|
||
|
fn to_string_escaped(&self) -> String;
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
impl ToStringEscaped for [u16] {
|
||
|
fn to_string_escaped(&self) -> String {
|
||
|
char::decode_utf16(self.iter().copied())
|
||
|
.map(|r| match r {
|
||
|
Ok(c) => String::from(c),
|
||
|
Err(e) => format!("\\u{:04X}", e.unpaired_surrogate()),
|
||
|
})
|
||
|
.collect()
|
||
2 years ago
|
}
|
||
|
}
|