mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-12-01 02:35:37 +01:00
With Options::es2015, vectorcraft-js reads and runs two ECMAScript 2020 forms: optional chains (a?.b, a?.[key], f?.(args) and the accesses and calls after them) and ??. - An optional step that finds undefined or null ends the whole chain with undefined, without evaluating its other keys and arguments. Parentheses end a chain. a?.b() and a.b?.() call with a as this, (a?.b)() does too, eval?.(code) is an indirect eval, and delete a?.b is true when a is undefined or null. - ?? keeps any left side other than undefined and null. - ?. before a digit still reads as ? and a number (a?.5:1). - Mixing ?? with && or || without parentheses, assigning to or updating an optional chain, a tagged template after one and new a?.b() are syntax errors. Two new jump instructions test for undefined and null, and the verifier checks both of their paths. A chain of any length compiles with a loop. .jsx files and source strings stay ES5.1, where ?. and ?? are syntax errors. The test262 ES2015 subset now also reads expressions/optional-chaining and expressions/coalesce: 426 of 426 negative parse runs and 591 of 615 other runs pass with the option. The 24 failing runs are the same as before this change.
494 lines
13 KiB
Rust
494 lines
13 KiB
Rust
//! The syntax tree: expressions, statements and functions kept in arenas ([`Ast`]) and referred to
|
|
//! by index, each with its source span. Nodes hold indices rather than boxes, so dropping a deeply
|
|
//! nested tree does not recurse.
|
|
|
|
use crate::token::Span;
|
|
|
|
/// An expression's index in [`Ast::exprs`].
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub struct ExprId(pub u32);
|
|
|
|
/// A statement's index in [`Ast::stmts`].
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub struct StmtId(pub u32);
|
|
|
|
/// A function's index in [`Ast::functions`].
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
|
pub struct FunctionId(pub u32);
|
|
|
|
/// An identifier as written, with its span.
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
|
pub struct Name {
|
|
pub name: String,
|
|
pub span: Span,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum UnaryOp {
|
|
Delete,
|
|
Void,
|
|
Typeof,
|
|
Plus,
|
|
Minus,
|
|
BitNot,
|
|
Not,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum UpdateOp {
|
|
Increment,
|
|
Decrement,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum BinaryOp {
|
|
Mul,
|
|
Div,
|
|
Mod,
|
|
Add,
|
|
Sub,
|
|
Shl,
|
|
Shr,
|
|
UShr,
|
|
Lt,
|
|
Gt,
|
|
LtEq,
|
|
GtEq,
|
|
Instanceof,
|
|
In,
|
|
Eq,
|
|
NotEq,
|
|
StrictEq,
|
|
StrictNotEq,
|
|
BitAnd,
|
|
BitXor,
|
|
BitOr,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum LogicalOp {
|
|
And,
|
|
Or,
|
|
/// `??`: the right side only when the left is `undefined` or `null`.
|
|
Coalesce,
|
|
}
|
|
|
|
/// `=` (`None`) or a compound assignment's operator (`+=` is `Some(BinaryOp::Add)`).
|
|
pub type AssignOp = Option<BinaryOp>;
|
|
|
|
/// A property name in an object literal.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum PropertyKey {
|
|
Ident(String),
|
|
String(Vec<u16>),
|
|
Number(f64),
|
|
}
|
|
|
|
impl PropertyKey {
|
|
/// The key as the string a property is stored under (a number through ToString).
|
|
pub fn to_utf16(&self) -> Vec<u16> {
|
|
match self {
|
|
PropertyKey::Ident(name) => name.encode_utf16().collect(),
|
|
PropertyKey::String(s) => s.clone(),
|
|
PropertyKey::Number(n) => crate::number::to_string(*n).encode_utf16().collect(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum PropertyValue {
|
|
Init(ExprId),
|
|
Get(FunctionId),
|
|
Set(FunctionId),
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Property {
|
|
pub key: PropertyKey,
|
|
pub value: PropertyValue,
|
|
pub span: Span,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum Expr {
|
|
This,
|
|
Ident(String),
|
|
Null,
|
|
Bool(bool),
|
|
Number(f64),
|
|
String(Vec<u16>),
|
|
RegExp {
|
|
pattern: Vec<u16>,
|
|
flags: String,
|
|
},
|
|
/// An array literal; `None` is a hole (`[1, , 3]`).
|
|
Array(Vec<Option<ExprId>>),
|
|
Object(Vec<Property>),
|
|
Function(FunctionId),
|
|
/// `object.property`.
|
|
Member {
|
|
object: ExprId,
|
|
property: Name,
|
|
},
|
|
/// `object[index]`.
|
|
Index {
|
|
object: ExprId,
|
|
index: ExprId,
|
|
},
|
|
Call {
|
|
callee: ExprId,
|
|
args: Vec<ExprId>,
|
|
},
|
|
New {
|
|
callee: ExprId,
|
|
args: Vec<ExprId>,
|
|
},
|
|
Update {
|
|
op: UpdateOp,
|
|
prefix: bool,
|
|
target: ExprId,
|
|
},
|
|
Unary {
|
|
op: UnaryOp,
|
|
arg: ExprId,
|
|
},
|
|
Binary {
|
|
op: BinaryOp,
|
|
left: ExprId,
|
|
right: ExprId,
|
|
},
|
|
Logical {
|
|
op: LogicalOp,
|
|
left: ExprId,
|
|
right: ExprId,
|
|
},
|
|
Conditional {
|
|
test: ExprId,
|
|
consequent: ExprId,
|
|
alternate: ExprId,
|
|
},
|
|
Assign {
|
|
op: AssignOp,
|
|
target: ExprId,
|
|
value: ExprId,
|
|
},
|
|
/// Comma-separated expressions.
|
|
Sequence(Vec<ExprId>),
|
|
/// A parenthesized expression, kept so a rule about the bare form (a label, a directive) can
|
|
/// tell them apart.
|
|
Paren(ExprId),
|
|
/// An XML literal (`<a b="c">d</a>`), or with `list` an XMLList literal (`<>…</>`).
|
|
XmlLiteral {
|
|
parts: Vec<XmlPart>,
|
|
list: bool,
|
|
},
|
|
/// An E4X name (`@a`, `*`, `ns::a`), the key of an [`Expr::Index`] or the operand of
|
|
/// [`Expr::XmlScopeName`] and [`Expr::XmlDescendants`].
|
|
XmlName {
|
|
attribute: bool,
|
|
qualifier: Option<XmlQualifier>,
|
|
name: XmlNamePart,
|
|
},
|
|
/// An E4X name standing alone (`@a` in a filter): looked up on the nearest XML value in scope
|
|
/// that holds it, and also an assignment, update or `delete` target.
|
|
XmlScopeName(ExprId),
|
|
/// `object..name`.
|
|
XmlDescendants {
|
|
object: ExprId,
|
|
name: ExprId,
|
|
},
|
|
/// `object.(predicate)`.
|
|
XmlFilter {
|
|
object: ExprId,
|
|
predicate: ExprId,
|
|
},
|
|
/// An arrow function.
|
|
Arrow(FunctionId),
|
|
/// A template literal: its pieces (one more than its substitutions) and its substitutions,
|
|
/// in source order.
|
|
Template {
|
|
quasis: Vec<TemplatePiece>,
|
|
exprs: Vec<ExprId>,
|
|
},
|
|
/// A tagged template (`` tag`a${b}c` ``): `tag` called with the template object and the
|
|
/// substitutions.
|
|
TaggedTemplate {
|
|
tag: ExprId,
|
|
quasis: Vec<TemplatePiece>,
|
|
exprs: Vec<ExprId>,
|
|
},
|
|
/// `...expression` in an array literal or an argument list, and as the last of an arrow
|
|
/// function's parenthesized parameters while they are read.
|
|
Spread(ExprId),
|
|
/// An optional chain (`a?.b.c`, `a?.[k]`, `f?.()`): its accesses and calls, the
|
|
/// [`Expr::Optional`] steps among them included. It is `undefined` when one of those steps
|
|
/// finds `undefined` or `null`, and the rest of the chain is skipped.
|
|
OptionalChain(ExprId),
|
|
/// The object or function before `?.` in an [`Expr::OptionalChain`].
|
|
Optional(ExprId),
|
|
}
|
|
|
|
/// A piece of a template literal's text between its substitutions.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct TemplatePiece {
|
|
/// The text with its escapes read.
|
|
pub cooked: Vec<u16>,
|
|
/// The text as written.
|
|
pub raw: Vec<u16>,
|
|
}
|
|
|
|
/// Which lexical declaration a name has.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum LexKind {
|
|
Let,
|
|
Const,
|
|
}
|
|
|
|
/// A piece of an XML literal, in source order.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum XmlPart {
|
|
/// Markup kept as written.
|
|
Text(Vec<u16>),
|
|
/// `{expression}` in element content: XML values keep their markup, other values are escaped.
|
|
Content(ExprId),
|
|
/// `{expression}` as an attribute's value.
|
|
Attribute(ExprId),
|
|
/// `{expression}` as an element or attribute name, its string checked as an XML name.
|
|
Name(ExprId),
|
|
/// `{expression}` as a run of attributes, its string checked as `name="value"` attributes.
|
|
Attributes(ExprId),
|
|
}
|
|
|
|
/// What stands before `::` in a qualified name.
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum XmlQualifier {
|
|
/// `*::name`: any namespace.
|
|
Any,
|
|
Expr(ExprId),
|
|
}
|
|
|
|
/// The name in an E4X name.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum XmlNamePart {
|
|
Ident(String),
|
|
/// `*`.
|
|
Star,
|
|
/// `[expression]`.
|
|
Computed(ExprId),
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct VarDecl {
|
|
pub name: Name,
|
|
pub init: Option<ExprId>,
|
|
}
|
|
|
|
/// What a `for (… in …)` statement assigns each property name to.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum ForInTarget {
|
|
/// `for (var x in o)`, with the declaration's initializer if it has one.
|
|
Var(VarDecl),
|
|
Expr(ExprId),
|
|
/// `for (let x in o)` or `for (const x in o)`: a new binding for each name.
|
|
Lexical(LexKind, Name),
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum ForInit {
|
|
Var(Vec<VarDecl>),
|
|
Expr(ExprId),
|
|
/// `let` or `const` declarations, in a scope of the loop's own with a new copy of each `let`
|
|
/// binding for each iteration.
|
|
Lexical(LexKind, Vec<VarDecl>),
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct SwitchCase {
|
|
/// `None` for `default`.
|
|
pub test: Option<ExprId>,
|
|
pub body: Vec<StmtId>,
|
|
pub span: Span,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct CatchClause {
|
|
pub param: Name,
|
|
pub body: Vec<StmtId>,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub enum Stmt {
|
|
Var(Vec<VarDecl>),
|
|
/// `let` or `const` declarations (a `const` always has an initializer).
|
|
Lexical(LexKind, Vec<VarDecl>),
|
|
FunctionDecl(FunctionId),
|
|
Expr(ExprId),
|
|
Block(Vec<StmtId>),
|
|
Empty,
|
|
If {
|
|
test: ExprId,
|
|
consequent: StmtId,
|
|
alternate: Option<StmtId>,
|
|
},
|
|
DoWhile {
|
|
body: StmtId,
|
|
test: ExprId,
|
|
},
|
|
While {
|
|
test: ExprId,
|
|
body: StmtId,
|
|
},
|
|
For {
|
|
init: Option<ForInit>,
|
|
test: Option<ExprId>,
|
|
update: Option<ExprId>,
|
|
body: StmtId,
|
|
},
|
|
/// `for (… in …)`, or with `each` the E4X `for each (… in …)`, which assigns the values.
|
|
ForIn {
|
|
target: ForInTarget,
|
|
object: ExprId,
|
|
body: StmtId,
|
|
each: bool,
|
|
},
|
|
/// `default xml namespace = expression;`.
|
|
DefaultXmlNamespace(ExprId),
|
|
Continue(Option<Name>),
|
|
Break(Option<Name>),
|
|
Return(Option<ExprId>),
|
|
With {
|
|
object: ExprId,
|
|
body: StmtId,
|
|
},
|
|
Switch {
|
|
discriminant: ExprId,
|
|
cases: Vec<SwitchCase>,
|
|
},
|
|
Labeled {
|
|
label: Name,
|
|
body: StmtId,
|
|
},
|
|
Throw(ExprId),
|
|
Try {
|
|
block: Vec<StmtId>,
|
|
handler: Option<CatchClause>,
|
|
finalizer: Option<Vec<StmtId>>,
|
|
},
|
|
Debugger,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
|
pub enum FunctionKind {
|
|
Declaration,
|
|
Expression,
|
|
Getter,
|
|
Setter,
|
|
/// An arrow function; one with an expression body has a body of one `return` statement.
|
|
Arrow,
|
|
}
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Function {
|
|
pub kind: FunctionKind,
|
|
pub name: Option<Name>,
|
|
/// The parameters before a rest parameter.
|
|
pub params: Vec<Name>,
|
|
/// Each parameter's default value, in the order of `params` (empty when none has one).
|
|
pub defaults: Vec<Option<ExprId>>,
|
|
/// The rest parameter (`...name`).
|
|
pub rest: Option<Name>,
|
|
pub body: Vec<StmtId>,
|
|
/// The function's code is strict mode code: its own directive prologue holds `"use strict"`,
|
|
/// or the code around it is strict.
|
|
pub strict: bool,
|
|
pub span: Span,
|
|
}
|
|
|
|
impl Function {
|
|
/// The parameters are plain names: no default value and no rest parameter.
|
|
pub fn simple_params(&self) -> bool {
|
|
self.rest.is_none() && self.defaults.iter().all(Option::is_none)
|
|
}
|
|
}
|
|
|
|
/// The arenas of a parsed script.
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
pub struct Ast {
|
|
pub exprs: Vec<(Expr, Span)>,
|
|
pub stmts: Vec<(Stmt, Span)>,
|
|
pub functions: Vec<Function>,
|
|
}
|
|
|
|
impl Ast {
|
|
pub fn expr(&self, id: ExprId) -> Option<&Expr> {
|
|
self.exprs.get(id.0 as usize).map(|(e, _)| e)
|
|
}
|
|
|
|
pub fn expr_span(&self, id: ExprId) -> Span {
|
|
self.exprs.get(id.0 as usize).map(|(_, s)| *s).unwrap_or_default()
|
|
}
|
|
|
|
pub fn stmt(&self, id: StmtId) -> Option<&Stmt> {
|
|
self.stmts.get(id.0 as usize).map(|(s, _)| s)
|
|
}
|
|
|
|
pub fn stmt_span(&self, id: StmtId) -> Span {
|
|
self.stmts.get(id.0 as usize).map(|(_, s)| *s).unwrap_or_default()
|
|
}
|
|
|
|
pub fn function(&self, id: FunctionId) -> Option<&Function> {
|
|
self.functions.get(id.0 as usize)
|
|
}
|
|
|
|
/// The expression with any parentheses around it removed.
|
|
pub fn unparenthesized(&self, mut id: ExprId) -> ExprId {
|
|
while let Some(Expr::Paren(inner)) = self.expr(id) {
|
|
id = *inner;
|
|
}
|
|
id
|
|
}
|
|
|
|
pub(crate) fn push_expr(&mut self, expr: Expr, span: Span) -> ExprId {
|
|
self.exprs.push((expr, span));
|
|
ExprId(u32::try_from(self.exprs.len() - 1).unwrap_or(u32::MAX))
|
|
}
|
|
|
|
pub(crate) fn push_stmt(&mut self, stmt: Stmt, span: Span) -> StmtId {
|
|
self.stmts.push((stmt, span));
|
|
StmtId(u32::try_from(self.stmts.len() - 1).unwrap_or(u32::MAX))
|
|
}
|
|
|
|
pub(crate) fn push_function(&mut self, function: Function) -> FunctionId {
|
|
self.functions.push(function);
|
|
FunctionId(u32::try_from(self.functions.len() - 1).unwrap_or(u32::MAX))
|
|
}
|
|
}
|
|
|
|
/// A parsed script.
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
pub struct Program {
|
|
pub ast: Ast,
|
|
pub body: Vec<StmtId>,
|
|
/// The script is strict mode code (its directive prologue holds `"use strict"`, or it was
|
|
/// parsed with [`crate::Options::strict`]).
|
|
pub strict: bool,
|
|
/// Its preprocessor lines, in source order.
|
|
pub directives: Vec<crate::token::Directive>,
|
|
pub(crate) lines: crate::LineTable,
|
|
/// It was read with [`crate::Options::es2015`].
|
|
pub(crate) es2015: bool,
|
|
}
|
|
|
|
impl Program {
|
|
/// The line and column (both from 1, the column in UTF-16 code units) of a source offset.
|
|
pub fn line_column(&self, offset: u32) -> (u32, u32) {
|
|
self.lines.line_column(offset)
|
|
}
|
|
|
|
/// The number of lines in the source: its line terminators plus one for a last line without
|
|
/// one.
|
|
pub fn line_count(&self) -> u32 {
|
|
self.lines.line_count()
|
|
}
|
|
}
|