diff --git a/Cargo.lock b/Cargo.lock index 0a414a2b7..42ee76986 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -391,6 +391,13 @@ dependencies = [ name = "clazel-js-regexp" version = "0.0.0" +[[package]] +name = "clazel-js-syntax" +version = "0.0.0" +dependencies = [ + "vstd", +] + [[package]] name = "clazel-js-vm" version = "0.0.0" diff --git a/crates/clazel-js-syntax/Cargo.toml b/crates/clazel-js-syntax/Cargo.toml new file mode 100644 index 000000000..787d18904 --- /dev/null +++ b/crates/clazel-js-syntax/Cargo.toml @@ -0,0 +1,24 @@ +[package] +name = "clazel-js-syntax" +version = "0.0.0" +edition.workspace = true +license.workspace = true +publish.workspace = true +description = "The JavaScript parser for Clazel's Phase 7 browser (phase-7.md item 7.3): the AST, the specification of what a correct parse is (ECMA-262 section 13 onward, ASI, early errors) and the parser's contract." + +[dependencies] +vstd = "=0.0.0-2026-10-04-0306" + +# Verification tier (docs/VERIFICATION.md). Tier 0: this is the first half of +# item 7.3 (CLAUDE.md rule 3): the AST, the specification and the contract, +# with a few proven examples. There is no parser yet, so nothing about shipped +# code is proven. `target-tier` is 1 (phase-7.md, Crates). +[package.metadata.clazel] +tier = 0 +target-tier = 1 + +[package.metadata.verus] +verify = true + +[lints] +workspace = true diff --git a/crates/clazel-js-syntax/mutations.json b/crates/clazel-js-syntax/mutations.json new file mode 100644 index 000000000..3804e7b1c --- /dev/null +++ b/crates/clazel-js-syntax/mutations.json @@ -0,0 +1,43 @@ +{ + "mutations": [ + { + "id": "coalesce-mixes-with-or", + "file": "src/spec.rs", + "find": "NodeKind::LogicalORExpression => lvl(a, l) >= 1 && lvl(a, r) >= 2,", + "replace": "NodeKind::LogicalORExpression => lvl(a, l) >= 0 && lvl(a, r) >= 2,", + "expect": "killed", + "reason": "Lets the left operand of `||` be a CoalesceExpression, so `a ?? b || c` would parse, which §13.13 forbids: example_coalesce_does_not_mix_with_or is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_coalesce_does_not_mix_with_or()", + "witness": "binary_operands_ok(AstV { nodes: seq![node(NodeKind::IdentifierReference, Op::None, 0, 1, Seq::empty()), node(NodeKind::IdentifierReference, Op::None, 5, 6, Seq::empty()), node(NodeKind::CoalesceExpression, Op::Coalesce, 0, 6, seq![0int, 1int]), node(NodeKind::IdentifierReference, Op::None, 10, 11, Seq::empty())], root: 3 }, NodeKind::LogicalORExpression, Op::LogicalOr, 2, 3)" + } + }, + { + "id": "exponent-base-allows-unary", + "file": "src/spec.rs", + "find": "NodeKind::ExponentiationExpression => lvl(a, l) >= 13 && lvl(a, r) >= 11,", + "replace": "NodeKind::ExponentiationExpression => lvl(a, l) >= 12 && lvl(a, r) >= 11,", + "expect": "killed", + "reason": "Lets a UnaryExpression be the base of `**`, so `-a ** b` would parse, which §13.6 forbids: example_unary_is_not_an_exponentiation_base is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_unary_is_not_an_exponentiation_base()", + "witness": "binary_operands_ok(AstV { nodes: seq![node(NodeKind::IdentifierReference, Op::None, 1, 2, Seq::empty()), node(NodeKind::UnaryExpression, Op::Minus, 0, 2, seq![0int]), node(NodeKind::IdentifierReference, Op::None, 6, 7, Seq::empty())], root: 2 }, NodeKind::ExponentiationExpression, Op::Exp, 1, 2)" + } + }, + { + "id": "do-while-insertion-anywhere", + "file": "src/spec.rs", + "find": "(dw && ts.gaps[m] == Ins::DoWhile)", + "replace": "ts.gaps[m] == Ins::DoWhile", + "expect": "killed", + "reason": "Accepts the `do`-`while` insertion (§12.10.1 rule 1, third condition) as the `;` of any statement, not only after the `)` of a do-while: example_inserted_semicolon_consumes_no_token is false.", + "certificate": { + "file": "src/examples.rs", + "lemma": "example_inserted_semicolon_consumes_no_token()", + "witness": "semi(Stream { toks: Seq::empty(), gaps: seq![Ins::DoWhile] }, 0, 0, false)" + } + } + ] +} diff --git a/crates/clazel-js-syntax/src/api.rs b/crates/clazel-js-syntax/src/api.rs new file mode 100644 index 000000000..ea9e2568b --- /dev/null +++ b/crates/clazel-js-syntax/src/api.rs @@ -0,0 +1,62 @@ +//! The parser's public types and its contract-only API. +//! +//! The function is in [`JsParser`], a trait with no implementation: the method +//! states its `requires` and `ensures` by the predicates of +//! [`super::contract`], and no body is written, so Verus accepts it with no +//! escape hatch (CLAUDE.md rule 4), the pattern of `clazel-js-lexer`'s `api.rs`. +//! The implementation (item 7.3, second half) implements it, or replaces it +//! with a free function whose `requires` and `ensures` are the same predicates. +//! +//! The parser drives a lexer (§12: the syntactic grammar chooses the goal symbol +//! of every token), so the trait is generic in the lexer's specification +//! [`LexerSpec`]: what the parser promises is relative to what the lexer +//! promises. The implementation names `clazel-js-lexer`'s as the model of +//! [`LexerSpec`]; this crate does not depend on it. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Why a source text was not parsed. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum ParseError { + /// The source text is not a valid `Script` or `Module`: it has a lexical error, + /// is not derived by the grammar, or has an early error (§16.1.1, §16.2.1.1 and + /// the rules they name). Raised as a `SyntaxError` by `eval`, `new Function`, + /// `import()` and the like. + Syntax, + /// The source text is nested more deeply than [`MAX_DEPTH`] (the deviation + /// of phase-7.md item 7.3): no parse of it is that low. The embedder reports it + /// as a `SyntaxError` too. + TooDeep, +} + +/// The parser's contract (CLAUDE.md rule 3): the function is total: for every +/// source text and goal symbol the result is determined by [`super::spec`], +/// and there is no precondition. +pub trait JsParser { + /// Parses `src` (code points, §11.1) as a `Script` or `Module`. + /// + /// - **Totality:** every `src` and `goal` has a result, and nothing it does + /// depends on anything but them. + /// - **Spans inside the input:** the arena is well formed (the root is the last + /// node, every child is before its parent), and every span, and every child's + /// span in its parent's, is inside `0..src.len()`. + /// - **Ok is a parse:** the arena is a derivation of a token stream of `src` from + /// the goal symbol (semicolon insertion included), with no early error, and at + /// most `MAX_DEPTH` high. + /// - **Err is a syntax error or the depth limit:** `Err` only when there is no + /// parse of `src` that is within the limit, and `Syntax` only when there is no + /// parse of it at all. + fn parse(src: &[u32], goal: SourceGoal) -> (r: Result) + requires + crate::contract::parse_requires(src@, goal), + ensures + crate::contract::parse_ensures::(src@, goal, r), + ; +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/ast.rs b/crates/clazel-js-syntax/src/ast.rs new file mode 100644 index 000000000..a9a295a4a --- /dev/null +++ b/crates/clazel-js-syntax/src/ast.rs @@ -0,0 +1,700 @@ +//! The arena AST of ECMAScript scripts and modules (phase-7.md item 7.3). +//! +//! An [`Ast`] is a `Vec` of [`Node`]s. A node names its children by index +//! into that `Vec` (a [`NodeId`]); the root is the last node, and every +//! child has a smaller index than its parent (the arena is built bottom-up, +//! as a parser finishes a production after its parts). So the tree needs no +//! pointers, no `Box` and no recursion to drop, and a walk can go forward or +//! backward over the arena without a stack. The checks that an `Ast` is a +//! well-formed arena are in `spec::ast_wf`, and what it takes for one to be +//! a parse of a source text is in `spec::is_parse`. +//! +//! # Shape +//! +//! Every node has a [`NodeKind`] named after the ECMA-262 production it is +//! an instance of, an [`Op`] and a `flag` for the few facts that a production +//! has alternatives for (the operator of `AdditiveExpression`, `var` against +//! `let` against `const`, `static` in a `ClassElement`), a span into the +//! source text, and an ordered list of children. The variant docs give each +//! kind's children layout; `spec::derives` is the normative statement, and +//! the two agree. +//! +//! - **A span** is `start..end` in code points of the source text: `start` +//! is the first code point of the node's first token and `end` is one past +//! the last code point of its last token (white space and comments between +//! tokens are inside a node's span, and outside it at the ends). A node +//! with no token has the empty span at the end of the token before it. +//! - **An absent optional symbol** (`Expression?`, `BindingIdentifier?`) is a +//! child of kind [`NodeKind::Absent`], whose span is empty, so a kind's +//! children layout has a fixed length where the production has fixed +//! positions. A list (statements, arguments, properties) has one child +//! per item. +//! - **Names, values and literal text** are not stored: they are the source +//! text of a node's span, and a consumer asks the lexer for them (an +//! identifier's code points, a string's value, a number's value). Nothing +//! in the AST can disagree with the source text. +//! - **Cover grammars** (§13.2.5.1 and friends) are not in the AST: it holds +//! the refined production the parse selected, for example an +//! [`NodeKind::ObjectAssignmentPattern`] where the text is the left side of +//! an `=`. + +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// The index of a node in [`Ast::nodes`]. +pub type NodeId = u32; + +/// The production a node is an instance of. A kind's doc comment gives the +/// ECMA-262 production(s) and the children layout. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum NodeKind { + // ----- Roots and statements (§16, §14) --------------------------------- + /// §16.1 `Script`: `ScriptBody?`. Children: the `StatementListItem`s. + Script, + /// §16.2 `Module`: `ModuleBody?`. Children: the `ModuleItem`s. + Module, + /// §14.2 `Block`: `{ StatementList? }`. Children: the statements. + BlockStatement, + /// §14.3.2 `VariableStatement`: `var VariableDeclarationList ;`. + /// Children: the `VariableDeclaration`s. + VariableStatement, + /// §14.3.2 `VariableDeclarationList` as the head of a `for ( var ...` + /// statement: `var` and the declarations, without the `;`. The span + /// starts at `var`. Children: the `VariableDeclaration`s. + VariableDeclarationList, + /// §14.3.2 `VariableDeclaration`: `BindingIdentifier Initializer?` or + /// `BindingPattern Initializer`. Children: the target, the initializer + /// (an `AssignmentExpression`) or [`NodeKind::Absent`]. + VariableDeclaration, + /// §14.3.1 `LexicalDeclaration`: `LetOrConst BindingList ;`. [`Op`] + /// `Let` or `Const`. Children: the `LexicalBinding`s. + LexicalDeclaration, + /// §14.3.1 `LexicalBinding`: as `VariableDeclaration`. Children: the + /// target, the initializer or [`NodeKind::Absent`]. + LexicalBinding, + /// §14.4 `EmptyStatement`: `;`. No children. + EmptyStatement, + /// §14.5 `ExpressionStatement`: `Expression ;`. Children: the expression. + ExpressionStatement, + /// §14.6 `IfStatement`. Children: the test, the consequent, the + /// alternate or [`NodeKind::Absent`]. + IfStatement, + /// §14.7.2 `DoWhileStatement`. Children: the body, the test. + DoWhileStatement, + /// §14.7.3 `WhileStatement`. Children: the test, the body. + WhileStatement, + /// §14.7.4 `ForStatement`. Children: the initializer (an `Expression`, + /// a [`NodeKind::VariableDeclarationList`], a + /// [`NodeKind::LexicalDeclaration`] or [`NodeKind::Absent`]), the test, + /// the update (each an `Expression` or [`NodeKind::Absent`]), the body. + ForStatement, + /// §14.7.5 `for ( ... in ... )`. Children: the head (a + /// [`NodeKind::ForDeclaration`] or a `LeftHandSideExpression` or + /// assignment pattern), the expression, the body. + ForInStatement, + /// §14.7.5 `for ( ... of ... )` and `for await ( ... of ... )` (`flag`). + /// Children: as `ForInStatement`. + ForOfStatement, + /// §14.7.5 `ForDeclaration`: `LetOrConst ForBinding`, and the `var + /// ForBinding` of `for ( var ForBinding in ...`. [`Op`] `Var`, `Let` or + /// `Const`. Children: the binding (a `BindingIdentifier` or + /// `BindingPattern`), or, for Annex B.3.5's `for (var a = 1 in x)`, a + /// [`NodeKind::VariableDeclaration`]. + ForDeclaration, + /// §14.8 `ContinueStatement`. Children: the `LabelIdentifier` or + /// [`NodeKind::Absent`]. + ContinueStatement, + /// §14.9 `BreakStatement`. Children: the `LabelIdentifier` or + /// [`NodeKind::Absent`]. + BreakStatement, + /// §14.10 `ReturnStatement`. Children: the expression or + /// [`NodeKind::Absent`]. + ReturnStatement, + /// §14.11 `WithStatement`. Children: the object, the body. + WithStatement, + /// §14.12 `SwitchStatement`. Children: the discriminant, then the + /// `CaseClause`s and `DefaultClause` of the `CaseBlock`, in order. + SwitchStatement, + /// §14.12 `CaseClause`: `case Expression : StatementList?`. Children: + /// the test, then the statements. + CaseClause, + /// §14.12 `DefaultClause`: `default : StatementList?`. Children: the + /// statements. + DefaultClause, + /// §14.13 `LabelledStatement`. Children: the `LabelIdentifier`, the + /// body (a statement, or a function declaration under Annex B.3.2). + LabelledStatement, + /// §14.14 `ThrowStatement`. Children: the expression. + ThrowStatement, + /// §14.15 `TryStatement`. Children: the block, the `Catch` or + /// [`NodeKind::Absent`], the `Finally` or [`NodeKind::Absent`]. + TryStatement, + /// §14.15 `Catch`. Children: the parameter (a `CatchParameter`) or + /// [`NodeKind::Absent`], the block. + Catch, + /// §14.15 `Finally`. Children: the block. + Finally, + /// §14.16 `DebuggerStatement`. No children. + DebuggerStatement, + + // ----- Functions and classes (§15) ------------------------------------ + /// §15.2 `FunctionDeclaration`. Children: the `BindingIdentifier` (or + /// [`NodeKind::Absent`] for `export default function`), the + /// `FormalParameters`, the `FunctionBody`. + FunctionDeclaration, + /// §15.2 `FunctionExpression`. Children: as `FunctionDeclaration`. + FunctionExpression, + /// §15.5 `GeneratorDeclaration`. Children: as `FunctionDeclaration`. + GeneratorDeclaration, + /// §15.5 `GeneratorExpression`. Children: as `FunctionDeclaration`. + GeneratorExpression, + /// §15.8 `AsyncFunctionDeclaration`. Children: as `FunctionDeclaration`. + AsyncFunctionDeclaration, + /// §15.8 `AsyncFunctionExpression`. Children: as `FunctionDeclaration`. + AsyncFunctionExpression, + /// §15.6 `AsyncGeneratorDeclaration`. Children: as `FunctionDeclaration`. + AsyncGeneratorDeclaration, + /// §15.6 `AsyncGeneratorExpression`. Children: as `FunctionDeclaration`. + AsyncGeneratorExpression, + /// §15.1 `FormalParameters` (and `UniqueFormalParameters`, + /// `ArrowFormalParameters`, `PropertySetParameterList`): its span runs + /// from the `(` to the `)`, both included. Children: the `BindingElement` + /// per parameter, then a `FunctionRestParameter` if any. + FormalParameters, + /// §15.1 `FunctionRestParameter`: `... BindingRestElement`. Children: + /// the target (a `BindingIdentifier` or `BindingPattern`). + FunctionRestParameter, + /// §15.1 `FunctionBody` (and `GeneratorBody`, `AsyncFunctionBody`, + /// `AsyncGeneratorBody`, `ConciseBody`'s braced form, `ClassStaticBlockBody`): + /// its span runs from the `{` to the `}`, both included. Children: the + /// statements. + FunctionBody, + /// §15.3 `ArrowFunction`. Children: the parameters (a `BindingIdentifier` + /// or `FormalParameters`), the body (an `AssignmentExpression` or a + /// `FunctionBody`). + ArrowFunction, + /// §15.9 `AsyncArrowFunction`. Children: as `ArrowFunction`. + AsyncArrowFunction, + /// §15.4 `MethodDefinition` (and `GeneratorMethod`, `AsyncMethod`, + /// `AsyncGeneratorMethod`). [`Op`] `Method`, `Getter`, `Setter`, + /// `GeneratorMethod`, `AsyncMethod` or `AsyncGeneratorMethod`; `flag` is + /// `static` for a `ClassElement`. Children: the `ClassElementName` or + /// `PropertyName`, the `FormalParameters`, the `FunctionBody`. + MethodDefinition, + /// §15.7 `ClassDeclaration`. Children: the `BindingIdentifier` (or + /// [`NodeKind::Absent`] for `export default class`), the + /// `ClassHeritage`'s `LeftHandSideExpression` or [`NodeKind::Absent`], + /// the `ClassBody`. + ClassDeclaration, + /// §15.7 `ClassExpression`. Children: as `ClassDeclaration`. + ClassExpression, + /// §15.7 `ClassBody`: `{ ClassElementList? }`. Children: the + /// `ClassElement`s that are not `;`: `MethodDefinition`, + /// `FieldDefinition`, `ClassStaticBlock`. + ClassBody, + /// §15.7 `FieldDefinition` as a `ClassElement`: `static? FieldDefinition + /// ;`. `flag` is `static`. Children: the `ClassElementName`, the + /// initializer or [`NodeKind::Absent`]. + FieldDefinition, + /// §15.7 `ClassStaticBlock`: `static { ClassStaticBlockBody }`. + /// Children: the `FunctionBody` (the `ClassStaticBlockBody`). + ClassStaticBlock, + + // ----- Patterns (§13.15.5, §14.3.3) -------------------------------------- + /// §14.3.3 `ObjectBindingPattern`. Children: the `BindingProperty`s, then + /// a `BindingRestProperty` if any. + ObjectBindingPattern, + /// §14.3.3 `ArrayBindingPattern`. Children: the `BindingElement`s and + /// [`NodeKind::Elision`]s, then a `BindingRestElement` if any. + ArrayBindingPattern, + /// §14.3.3 `BindingProperty`: `SingleNameBinding` or `PropertyName : + /// BindingElement`. [`Op`] `Shorthand` (children: the `BindingElement`, + /// whose target is a `BindingIdentifier`) or `KeyValue` (children: the + /// `PropertyName`, the `BindingElement`). + BindingProperty, + /// §14.3.3 `BindingElement` (and `SingleNameBinding`). Children: the + /// target (a `BindingIdentifier` or `BindingPattern`), the initializer or + /// [`NodeKind::Absent`]. + BindingElement, + /// §14.3.3 `BindingRestElement`: `... BindingIdentifier` or `... + /// BindingPattern`. Children: the target. + BindingRestElement, + /// §14.3.3 `BindingRestProperty`: `... BindingIdentifier`. Children: the + /// `BindingIdentifier`. + BindingRestProperty, + /// §13.15.5 `ObjectAssignmentPattern`. Children: the `AssignmentProperty`s, + /// then an `AssignmentRestProperty` if any. + ObjectAssignmentPattern, + /// §13.15.5 `ArrayAssignmentPattern`. Children: the `AssignmentElement`s + /// and [`NodeKind::Elision`]s, then an `AssignmentRestElement` if any. + ArrayAssignmentPattern, + /// §13.15.5 `AssignmentProperty`. [`Op`] `Shorthand` (children: the + /// `IdentifierReference`, the initializer or [`NodeKind::Absent`]) or + /// `KeyValue` (children: the `PropertyName`, the `AssignmentElement`). + AssignmentProperty, + /// §13.15.5 `AssignmentElement`: `DestructuringAssignmentTarget + /// Initializer?`. Children: the target (a `LeftHandSideExpression` or an + /// assignment pattern), the initializer or [`NodeKind::Absent`]. + AssignmentElement, + /// §13.15.5 `AssignmentRestElement`: `... DestructuringAssignmentTarget`. + /// Children: the target. + AssignmentRestElement, + /// §13.15.5 `AssignmentRestProperty`: `... DestructuringAssignmentTarget`. + /// Children: the target. + AssignmentRestProperty, + /// §13.2.4 `Elision` for one hole, `,`: the span is that comma. + Elision, + + // ----- Expressions (§13) ------------------------------------------------- + /// An optional symbol that is not there. The span is empty. + Absent, + /// §13.1 `IdentifierReference`. No children; the name is the source text. + IdentifierReference, + /// §13.1 `BindingIdentifier`. + BindingIdentifier, + /// §13.1 `LabelIdentifier`. + LabelIdentifier, + /// §12.7 `IdentifierName` in a `PropertyName`, a member access or an + /// import/export name. + IdentifierName, + /// §12.7 `PrivateIdentifier`. + PrivateIdentifier, + /// §13.2.1 `this`. + ThisExpression, + /// §13.2.3 `NullLiteral`. + NullLiteral, + /// §13.2.3 `BooleanLiteral`. + BooleanLiteral, + /// §13.2.3 `NumericLiteral`. + NumericLiteral, + /// §13.2.3 `StringLiteral`. + StringLiteral, + /// §13.2.7 `RegularExpressionLiteral`. + RegularExpressionLiteral, + /// §13.2.8 `TemplateLiteral`: a `NoSubstitutionTemplate`, or a + /// `TemplateHead`, the expressions and `TemplateMiddle`s, and a + /// `TemplateTail`. Children: `TemplateElement`s and `Expression`s, + /// alternating, starting and ending with a `TemplateElement`. + TemplateLiteral, + /// §13.2.8 one of `NoSubstitutionTemplate`, `TemplateHead`, + /// `TemplateMiddle`, `TemplateTail`: the span includes its delimiters. + TemplateElement, + /// §13.2.4 `ArrayLiteral`. Children: `AssignmentExpression`s, + /// `SpreadElement`s and [`NodeKind::Elision`]s. + ArrayLiteral, + /// §13.2.5 `ObjectLiteral`. Children: `PropertyDefinition`s, + /// `CoverInitializedName`s and `MethodDefinition`s. + ObjectLiteral, + /// §13.2.5 `PropertyDefinition`. [`Op`] `Shorthand` (children: the + /// `IdentifierReference`), `KeyValue` (children: the `PropertyName`, the + /// `AssignmentExpression`) or `Spread` (children: the + /// `AssignmentExpression`). + PropertyDefinition, + /// §13.2.5 `CoverInitializedName`: `IdentifierReference Initializer`. + /// Children: the `IdentifierReference`, the initializer. Only an + /// assignment pattern may contain it (§13.2.5.1's early error). + CoverInitializedName, + /// §13.2.5 `ComputedPropertyName`: `[ AssignmentExpression ]`. Children: + /// the expression. + ComputedPropertyName, + /// §13.2.9 `CoverParenthesizedExpressionAndArrowParameterList` read as a + /// `ParenthesizedExpression`. Children: the `Expression`. + ParenthesizedExpression, + /// §13.3.2 `MemberExpression . IdentifierName` and `[ Expression ]`, and + /// `. PrivateIdentifier`. [`Op`] `Dot`, `Bracket` or `PrivateDot`. + /// Children: the object, the property (an `IdentifierName`, an + /// `Expression` or a `PrivateIdentifier`). + MemberExpression, + /// §13.3.7 `SuperProperty`. [`Op`] `Dot` or `Bracket`. Children: the + /// property. + SuperProperty, + /// §13.3.7 `SuperCall`. Children: the `Arguments`. + SuperCall, + /// §13.3.10 `ImportCall`. Children: the specifier, the options or + /// [`NodeKind::Absent`]. + ImportCall, + /// §13.3.12 `ImportMeta`. No children. + ImportMeta, + /// §13.3.12 `NewTarget`. No children. + NewTarget, + /// §13.3.5 `NewExpression`: `new MemberExpression Arguments`, and `new + /// NewExpression` (the arguments absent). Children: the callee, the + /// `Arguments` or [`NodeKind::Absent`]. + NewExpression, + /// §13.3.6 `CallExpression`. Children: the callee, the `Arguments`. + CallExpression, + /// §13.3.8 `Arguments`: its span runs from the `(` to the `)`. Children: + /// `AssignmentExpression`s and `SpreadElement`s. + Arguments, + /// §13.3.11 `TaggedTemplate`: `MemberExpression TemplateLiteral[+Tagged]`. + /// Children: the tag, the `TemplateLiteral`. + TaggedTemplate, + /// §13.3.9 `OptionalExpression`. Children: the base expression, then the + /// `OptionalChain` links in order. + OptionalExpression, + /// §13.3.9 one link of an `OptionalChain`. [`Op`] `ChainDot`, + /// `ChainBracket`, `ChainCall`, `ChainPrivateDot` or `ChainTemplate`; + /// `flag` is true when the link starts with `?.`. Children: the + /// `IdentifierName`, `Expression`, `Arguments`, `PrivateIdentifier` or + /// `TemplateLiteral`. + OptionalChain, + /// §13.4 `UpdateExpression`. [`Op`] `PrefixIncrement`, + /// `PrefixDecrement`, `PostfixIncrement` or `PostfixDecrement`. Children: + /// the operand. + UpdateExpression, + /// §13.5 `UnaryExpression` (`delete void typeof + - ~ !`). [`Op`] names + /// the operator. Children: the operand. + UnaryExpression, + /// §15.8 `AwaitExpression`. Children: the operand. + AwaitExpression, + /// §13.6 `ExponentiationExpression`: `UpdateExpression ** + /// ExponentiationExpression`. Children: left, right. + ExponentiationExpression, + /// §13.7 `MultiplicativeExpression`. [`Op`] `Mul`, `Div` or `Rem`. + /// Children: left, right. + MultiplicativeExpression, + /// §13.8 `AdditiveExpression`. [`Op`] `Add` or `Sub`. + AdditiveExpression, + /// §13.9 `ShiftExpression`. [`Op`] `Shl`, `Shr` or `UShr`. + ShiftExpression, + /// §13.10 `RelationalExpression`. [`Op`] `Lt`, `Gt`, `Le`, `Ge`, + /// `InstanceOf` or `In`; the left child may be a `PrivateIdentifier` for + /// `#x in obj`. + RelationalExpression, + /// §13.11 `EqualityExpression`. [`Op`] `Eq`, `Ne`, `StrictEq` or + /// `StrictNe`. + EqualityExpression, + /// §13.12 `BitwiseANDExpression`. + BitwiseANDExpression, + /// §13.12 `BitwiseXORExpression`. + BitwiseXORExpression, + /// §13.12 `BitwiseORExpression`. + BitwiseORExpression, + /// §13.13 `LogicalANDExpression`. + LogicalANDExpression, + /// §13.13 `LogicalORExpression`. + LogicalORExpression, + /// §13.13 `CoalesceExpression`. + CoalesceExpression, + /// §13.14 `ConditionalExpression`. Children: the test, the consequent, + /// the alternate. + ConditionalExpression, + /// §13.15 `AssignmentExpression`. [`Op`] `Assign` or a compound + /// assignment operator. Children: the target (a `LeftHandSideExpression`, + /// or for `=` an assignment pattern), the value. + AssignmentExpression, + /// §15.5 `YieldExpression`. [`Op`] `YieldPlain` or `YieldDelegate`. + /// Children: the operand or [`NodeKind::Absent`]. + YieldExpression, + /// §13.16 `Expression`: `Expression , AssignmentExpression`. Children: + /// two or more `AssignmentExpression`s. + Expression, + /// §13.2.4, §13.3.8 `SpreadElement`: `... AssignmentExpression`. + /// Children: the expression. + SpreadElement, + + // ----- Modules (§16.2) --------------------------------------------------- + /// §16.2.2 `ImportDeclaration`. Children: the `ImportClause` or + /// [`NodeKind::Absent`] (`import "m";`), the `ModuleSpecifier` (a + /// `StringLiteral`), the `WithClause` or [`NodeKind::Absent`]. + ImportDeclaration, + /// §16.2.2 `ImportClause`. Children: the `ImportedDefaultBinding` (a + /// `BindingIdentifier`) or [`NodeKind::Absent`], then the + /// `NameSpaceImport` or `NamedImports` or [`NodeKind::Absent`]. + ImportClause, + /// §16.2.2 `NameSpaceImport`: `* as ImportedBinding`. Children: the + /// `BindingIdentifier`. + NameSpaceImport, + /// §16.2.2 `NamedImports`. Children: the `ImportSpecifier`s. + NamedImports, + /// §16.2.2 `ImportSpecifier`. Children: the imported name (an + /// `IdentifierName` or `StringLiteral`) or [`NodeKind::Absent`] when the + /// binding is the name, the `BindingIdentifier`. + ImportSpecifier, + /// §16.2.2 `WithClause`: `with { WithEntries? }`. Children: the + /// `ImportAttribute`s. + WithClause, + /// §16.2.2 `ImportAttribute`: `AttributeKey : StringLiteral`. Children: + /// the key (an `IdentifierName` or `StringLiteral`), the `StringLiteral`. + ImportAttribute, + /// §16.2.3 `ExportDeclaration`. [`Op`] `ExportAll` (`export * from`; + /// children: the specifier, the `WithClause` or [`NodeKind::Absent`]), + /// `ExportAllAs` (`export * as n from`; children: the `ModuleExportName`, + /// the specifier, the `WithClause`), `ExportFrom` (`export {..} from`; + /// children: the `NamedExports`, the specifier, the `WithClause`), + /// `ExportNamed` (`export {..};`; children: the `NamedExports`), + /// `ExportVar`, `ExportDecl` (children: the `VariableStatement`, + /// `Declaration`), `ExportDefaultHoistable` (`export default + /// HoistableDeclaration`), `ExportDefaultClass`, `ExportDefaultExpression` + /// (`export default AssignmentExpression ;`; children: the expression). + ExportDeclaration, + /// §16.2.3 `NamedExports`. Children: the `ExportSpecifier`s. + NamedExports, + /// §16.2.3 `ExportSpecifier`. Children: the local name (an + /// `IdentifierName` or `StringLiteral`), the exported name or + /// [`NodeKind::Absent`]. + ExportSpecifier, +} + +/// The few facts a production's alternatives distinguish, shared by every +/// kind that has them. `None` for a kind that has no such fact. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum Op { + /// No operator or mode. + None, + // ----- UnaryExpression ---- + /// `delete`. + Delete, + /// `void`. + Void, + /// `typeof`. + Typeof, + /// Unary `+`. + Plus, + /// Unary `-`. + Minus, + /// `~`. + BitNot, + /// `!`. + LogicalNot, + // ----- UpdateExpression ---- + /// Prefix `++`. + PrefixIncrement, + /// Prefix `--`. + PrefixDecrement, + /// Postfix `++`. + PostfixIncrement, + /// Postfix `--`. + PostfixDecrement, + // ----- Binary operators ---- + /// `**`. + Exp, + /// `*`. + Mul, + /// `/`. + Div, + /// `%`. + Rem, + /// `+`. + Add, + /// `-`. + Sub, + /// `<<`. + Shl, + /// `>>`. + Shr, + /// `>>>`. + UShr, + /// `<`. + Lt, + /// `>`. + Gt, + /// `<=`. + Le, + /// `>=`. + Ge, + /// `instanceof`. + InstanceOf, + /// `in`. + In, + /// `==`. + Eq, + /// `!=`. + Ne, + /// `===`. + StrictEq, + /// `!==`. + StrictNe, + /// `&`. + BitAnd, + /// `^`. + BitXor, + /// `|`. + BitOr, + /// `&&`. + LogicalAnd, + /// `||`. + LogicalOr, + /// `??`. + Coalesce, + // ----- AssignmentOperator ---- + /// `=`. + Assign, + /// `*=`. + MulAssign, + /// `/=`. + DivAssign, + /// `%=`. + RemAssign, + /// `+=`. + AddAssign, + /// `-=`. + SubAssign, + /// `<<=`. + ShlAssign, + /// `>>=`. + ShrAssign, + /// `>>>=`. + UShrAssign, + /// `&=`. + BitAndAssign, + /// `^=`. + BitXorAssign, + /// `|=`. + BitOrAssign, + /// `**=`. + ExpAssign, + /// `&&=`. + LogicalAndAssign, + /// `||=`. + LogicalOrAssign, + /// `??=`. + CoalesceAssign, + // ----- Member access ---- + /// `. IdentifierName`. + Dot, + /// `[ Expression ]`. + Bracket, + /// `. PrivateIdentifier`. + PrivateDot, + /// `?. IdentifierName` or `. IdentifierName` in a chain. + ChainDot, + /// `?. [ Expression ]` or `[ Expression ]` in a chain. + ChainBracket, + /// `?. Arguments` or `Arguments` in a chain. + ChainCall, + /// `?. PrivateIdentifier` or `. PrivateIdentifier` in a chain. + ChainPrivateDot, + /// `?. TemplateLiteral` or a `TemplateLiteral` in a chain: derived by the + /// grammar, so that automatic semicolon insertion does not apply to it, + /// and a Syntax Error always (§13.3.1.1). + ChainTemplate, + // ----- Declarations ---- + /// `var`. + Var, + /// `let`. + Let, + /// `const`. + Const, + // ----- MethodDefinition ---- + /// `PropertyName ( ... ) { ... }`. + Method, + /// `get PropertyName ( ) { ... }`. + Getter, + /// `set PropertyName ( PropertySetParameterList ) { ... }`. + Setter, + /// `* PropertyName ( ... ) { ... }`. + GeneratorMethod, + /// `async PropertyName ( ... ) { ... }`. + AsyncMethod, + /// `async * PropertyName ( ... ) { ... }`. + AsyncGeneratorMethod, + // ----- Properties and yield ---- + /// A shorthand property or binding. + Shorthand, + /// `PropertyName : value`. + KeyValue, + /// `... value`. + Spread, + /// `yield` with an optional operand. + YieldPlain, + /// `yield *` with an operand. + YieldDelegate, + // ----- ExportDeclaration ---- + /// `export * from "m";`. + ExportAll, + /// `export * as n from "m";`. + ExportAllAs, + /// `export { ... } from "m";`. + ExportFrom, + /// `export { ... };`. + ExportNamed, + /// `export VariableStatement`. + ExportVar, + /// `export Declaration`. + ExportDecl, + /// `export default HoistableDeclaration`. + ExportDefaultHoistable, + /// `export default ClassDeclaration`. + ExportDefaultClass, + /// `export default AssignmentExpression ;`. + ExportDefaultExpression, +} + +/// One node of the arena. +pub struct Node { + /// The production. + pub kind: NodeKind, + /// The alternative of the production, where it has more than one fact. + pub op: Op, + /// A flag some kinds have: `static`, `await` of `for await`, `?.` of an + /// optional chain link. False elsewhere. + pub flag: bool, + /// The first code point of the node's first token. + pub start: usize, + /// One past the last code point of the node's last token. + pub end: usize, + /// The children, in source order, each with a smaller index than this + /// node's. + pub children: Vec, +} + +/// An arena AST: nodes, and the root's index. +pub struct Ast { + /// The nodes. The root is `nodes[root]`, and is the last node. + pub nodes: Vec, + /// The root, a [`NodeKind::Script`] or [`NodeKind::Module`]. + pub root: NodeId, +} + +/// The specification's view of a [`Node`]: the same facts as mathematical +/// values. +pub struct NodeV { + /// See [`Node::kind`]. + pub kind: NodeKind, + /// See [`Node::op`]. + pub op: Op, + /// See [`Node::flag`]. + pub flag: bool, + /// See [`Node::start`]. + pub start: int, + /// See [`Node::end`]. + pub end: int, + /// See [`Node::children`]. + pub children: Seq, +} + +/// The specification's view of an [`Ast`]. +pub struct AstV { + /// See [`Ast::nodes`]. + pub nodes: Seq, + /// See [`Ast::root`]. + pub root: int, +} + +impl View for Node { + type V = NodeV; + + open spec fn view(&self) -> NodeV { + NodeV { + kind: self.kind, + op: self.op, + flag: self.flag, + start: self.start as int, + end: self.end as int, + children: self.children@.map_values(|c: u32| c as int), + } + } +} + +impl View for Ast { + type V = AstV; + + open spec fn view(&self) -> AstV { + AstV { nodes: self.nodes@.map_values(|n: Node| n@), root: self.root as int } + } +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/contract.rs b/crates/clazel-js-syntax/src/contract.rs new file mode 100644 index 000000000..67dcd0e2d --- /dev/null +++ b/crates/clazel-js-syntax/src/contract.rs @@ -0,0 +1,66 @@ +//! Contracts the executable parser must be proven to meet. The method of +//! [`super::api::JsParser`] has exactly one `requires` predicate and one +//! `ensures` predicate here, written in terms of [`super::spec`], so reviewing +//! these and the spec is enough to know what the implementation guarantees +//! (docs/SPEC-STYLE.md). +//! +//! The properties of phase-7.md item 7.3, where they are stated: +//! +//! - **Totality:** the `requires` is `true`; the `ensures` determines the result +//! (`Ok`, or one of the two errors) for every source text and goal: `Ok` +//! exactly when a valid parse within the limit exists ([`parse_ensures`]: +//! `Ok` implies one, `Err` implies none). +//! - **Spans inside the input:** [`spans_wf`] and [`ast_wf`] of an `Ok`. +//! - **An `Ok` result satisfies the spec relation:** [`is_valid_parse`], at most +//! [`MAX_DEPTH`] high. +//! - **An `Err` is a syntax error or the depth limit:** `Syntax` means no valid +//! parse of any height ([`has_valid_parse`] is false); `TooDeep` means none +//! within the limit ([`has_valid_parse_within`] is false) and is allowed +//! only if a deeper one may exist, which the contract does not require. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// Precondition of `parse`: none. Any `[u32]` (including values above U+10FFFF, +/// which are lexical errors) and either goal symbol is allowed. +pub open spec fn parse_requires(src: Seq, goal: SourceGoal) -> bool { + true +} + +/// There is a valid parse of `src` as `goal` (a parse by the grammar with no +/// early error), whatever its height. +pub open spec fn has_valid_parse(src: Seq, goal: SourceGoal) -> bool { + exists|a: AstV| is_valid_parse::(src, goal, a) +} + +/// There is a valid parse of `src` as `goal` whose height is at most the limit. +pub open spec fn has_valid_parse_within(src: Seq, goal: SourceGoal) -> bool { + exists|a: AstV| is_valid_parse::(src, goal, a) && height(a, a.root) <= MAX_DEPTH as int +} + +/// Contract for `parse`: the result is the one the grammar, the early errors, +/// and the depth limit determine. +pub open spec fn parse_ensures( + src: Seq, + goal: SourceGoal, + r: Result, +) -> bool { + match r { + // A parse: a well-formed arena with every span inside the source text, which is a + // derivation of a token stream of the source text with no early error, within the limit. + Ok(ast) => ast_wf(ast@) && spans_wf(ast@, src.len() as int) && is_valid_parse::( + src, + goal, + ast@, + ) && height(ast@, ast@.root) <= MAX_DEPTH as int, + // An error: no valid parse within the limit; `Syntax` is no valid parse at all. + Err(e) => !has_valid_parse_within::(src, goal) && (e == crate::api::ParseError::Syntax + ==> !has_valid_parse::(src, goal)), + } +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/examples.rs b/crates/clazel-js-syntax/src/examples.rs new file mode 100644 index 000000000..5f4dcd1d6 --- /dev/null +++ b/crates/clazel-js-syntax/src/examples.rs @@ -0,0 +1,253 @@ +//! A few small facts about the specification, proven: evidence that the +//! transcription accepts and rejects what the standard says, on inputs small +//! enough to write out. Positive and negative; the larger evidence is the +//! conformance suite and, in the second half of 7.3, the proofs about the parser. +//! +//! This is a module of its own for the reason `clazel-js-lexer`'s is. + +#[allow(unused_imports)] +use super::spec::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// A node with no children and no tokens of its own recorded: a leaf. +pub open spec fn node(kind: NodeKind, op: Op, start: int, end: int, children: Seq) -> NodeV { + NodeV { kind: kind, op: op, flag: false, start: start, end: end, children: children } +} + +/// A punctuator token `text` of one code point at `start`. +pub open spec fn punct(text: u32, start: int) -> Tok { + Tok { + kind: TokKind::Punctuator, + start: start, + end: start + 1, + text: seq![text], + name: Seq::empty(), + escaped: false, + nl_before: false, + legacy: false, + bigint: false, + invalid_escape: false, + regexp_valid: false, + } +} + +/// §16.1 `Script : ScriptBody?`: the empty source text is a `Script`, with no +/// statements and no early error. +pub proof fn example_empty_script() + ensures + ({ + let a = AstV { + nodes: seq![node(NodeKind::Script, Op::None, 0, 0, Seq::empty())], + root: 0, + }; + let ts = Stream { toks: Seq::empty(), gaps: seq![Ins::None] }; + derives_root(a, ts, SourceGoal::Script) && early_errors_ok(a, ts, SourceGoal::Script) + }), +{ + let a = AstV { nodes: seq![node(NodeKind::Script, Op::None, 0, 0, Seq::empty())], root: 0 }; + let ts = Stream { toks: Seq::empty(), gaps: seq![Ins::None] }; + assert(ast_wf(a)); + assert(d(a, ts, 0, root_ctx(SourceGoal::Script), 0, 0)); + assert(derives_root(a, ts, SourceGoal::Script)); +} + +/// §14.4 `EmptyStatement : ;`: the source text `;` is a `Script` of one +/// `EmptyStatement`, whose span is the `;`. +pub proof fn example_empty_statement() + ensures + ({ + let a = AstV { + nodes: seq![ + node(NodeKind::EmptyStatement, Op::None, 0, 1, Seq::empty()), + node(NodeKind::Script, Op::None, 0, 1, seq![0int]), + ], + root: 1, + }; + let ts = Stream { toks: seq![punct(0x3Bu32, 0)], gaps: seq![Ins::None, Ins::None] }; + derives_root(a, ts, SourceGoal::Script) + }), +{ + let a = AstV { + nodes: seq![ + node(NodeKind::EmptyStatement, Op::None, 0, 1, Seq::empty()), + node(NodeKind::Script, Op::None, 0, 1, seq![0int]), + ], + root: 1, + }; + let ts = Stream { toks: seq![punct(0x3Bu32, 0)], gaps: seq![Ins::None, Ins::None] }; + reveal_strlit(";"); + assert(";"@ =~= seq![';']); + assert(has_parent(a, 0)) by { + assert(a.nodes[1].children[0] == 0); + } + assert(ast_wf(a)); + assert(exists|x: int| 0 <= x < tn(ts) && #[trigger] ts.toks[x].end == a.nodes[0].end) by { + assert(ts.toks[0].end == 1); + } + assert(ed(a, ts, 0, 0) == 1); + assert(is_p(ts, 0, ";"@)); + assert(d(a, ts, 0, root_ctx(SourceGoal::Script), 0, 1)); + assert(dc(a, ts, 1, 0, root_ctx(SourceGoal::Script), 0, 1)); + let ks = a.nodes[1].children; + assert(all_items(a, ks)) by { + assert(ks[0] == 0); + } + reveal_with_fuel(dl, 2); + assert(dl(a, ts, 1, ks, 0, root_ctx(SourceGoal::Script), 0, 1)); + assert(d(a, ts, 1, root_ctx(SourceGoal::Script), 0, 1)); + assert(derives_root(a, ts, SourceGoal::Script)); +} + +/// §13.13 `CoalesceExpression`: `a ?? b || c` is not derived. The left operand of +/// `||` is a `LogicalORExpression` or tighter, and a `CoalesceExpression` is not. +pub proof fn example_coalesce_does_not_mix_with_or() + ensures + ({ + let a = AstV { + nodes: seq![ + node(NodeKind::IdentifierReference, Op::None, 0, 1, Seq::empty()), + node(NodeKind::IdentifierReference, Op::None, 5, 6, Seq::empty()), + node(NodeKind::CoalesceExpression, Op::Coalesce, 0, 6, seq![0int, 1int]), + node(NodeKind::IdentifierReference, Op::None, 10, 11, Seq::empty()), + ], + root: 3, + }; + !binary_operands_ok(a, NodeKind::LogicalORExpression, Op::LogicalOr, 2, 3) + && binary_operands_ok(a, NodeKind::CoalesceExpression, Op::Coalesce, 0, 1) + }), +{ + let a = AstV { + nodes: seq![ + node(NodeKind::IdentifierReference, Op::None, 0, 1, Seq::empty()), + node(NodeKind::IdentifierReference, Op::None, 5, 6, Seq::empty()), + node(NodeKind::CoalesceExpression, Op::Coalesce, 0, 6, seq![0int, 1int]), + node(NodeKind::IdentifierReference, Op::None, 10, 11, Seq::empty()), + ], + root: 3, + }; + assert(lvl(a, 2) == 0); + assert(lvl(a, 0) == 17); + assert(lvl(a, 3) == 17); +} + +/// §13.6 `ExponentiationExpression`: `-a ** b` is not derived: the left operand +/// of `**` is an `UpdateExpression`, and a `UnaryExpression` is not. +pub proof fn example_unary_is_not_an_exponentiation_base() + ensures + ({ + let a = AstV { + nodes: seq![ + node(NodeKind::IdentifierReference, Op::None, 1, 2, Seq::empty()), + node(NodeKind::UnaryExpression, Op::Minus, 0, 2, seq![0int]), + node(NodeKind::IdentifierReference, Op::None, 6, 7, Seq::empty()), + ], + root: 2, + }; + !binary_operands_ok(a, NodeKind::ExponentiationExpression, Op::Exp, 1, 2) + }), +{ + let a = AstV { + nodes: seq![ + node(NodeKind::IdentifierReference, Op::None, 1, 2, Seq::empty()), + node(NodeKind::UnaryExpression, Op::Minus, 0, 2, seq![0int]), + node(NodeKind::IdentifierReference, Op::None, 6, 7, Seq::empty()), + ], + root: 2, + }; + assert(lvl(a, 1) == 12); +} + +/// §14.9.1: a `break` that is not in an iteration statement or a `switch` is an +/// early error, in a program with no enclosing statement. +pub proof fn example_break_outside_loop_is_an_early_error() + ensures + ({ + let a = AstV { + nodes: seq![ + node(NodeKind::Absent, Op::None, 5, 5, Seq::empty()), + node(NodeKind::BreakStatement, Op::None, 0, 5, seq![0int]), + node(NodeKind::Script, Op::None, 0, 5, seq![1int]), + ], + root: 2, + }; + let ts = Stream { toks: Seq::empty(), gaps: seq![Ins::None] }; + !early_local(a, ts, 1, root_env(a, ts, SourceGoal::Script)) + }), +{ + let a = AstV { + nodes: seq![ + node(NodeKind::Absent, Op::None, 5, 5, Seq::empty()), + node(NodeKind::BreakStatement, Op::None, 0, 5, seq![0int]), + node(NodeKind::Script, Op::None, 0, 5, seq![1int]), + ], + root: 2, + }; + let ts = Stream { toks: Seq::empty(), gaps: seq![Ins::None] }; + let env = root_env(a, ts, SourceGoal::Script); + assert(!env.in_breakable); +} + +/// §14.11.1 `with` in strict mode code is an early error; the same statement in +/// sloppy mode code is not. +pub proof fn example_with_is_an_early_error_in_strict_mode_only() + ensures + ({ + let a = AstV { + nodes: seq![ + node(NodeKind::EmptyStatement, Op::None, 0, 1, Seq::empty()), + node(NodeKind::EmptyStatement, Op::None, 1, 2, Seq::empty()), + node(NodeKind::WithStatement, Op::None, 0, 2, seq![0int, 1int]), + ], + root: 2, + }; + let ts = Stream { toks: Seq::empty(), gaps: seq![Ins::None] }; + let strict = EarlyEnv { strict: true, ..root_env(a, ts, SourceGoal::Script) }; + let sloppy = EarlyEnv { strict: false, ..root_env(a, ts, SourceGoal::Script) }; + !early_local(a, ts, 2, strict) && early_local(a, ts, 2, sloppy) + }), +{ + let a = AstV { + nodes: seq![ + node(NodeKind::EmptyStatement, Op::None, 0, 1, Seq::empty()), + node(NodeKind::EmptyStatement, Op::None, 1, 2, Seq::empty()), + node(NodeKind::WithStatement, Op::None, 0, 2, seq![0int, 1int]), + ], + root: 2, + }; + let ts = Stream { toks: Seq::empty(), gaps: seq![Ins::None] }; + let strict = EarlyEnv { strict: true, ..root_env(a, ts, SourceGoal::Script) }; + let sloppy = EarlyEnv { strict: false, ..root_env(a, ts, SourceGoal::Script) }; + assert(!is_labelled_function(a, 1)); +} + +/// §12.10.1 rule 1 and §14.7.2: a `;` inserted after the `)` of a `do`-`while` +/// ends the statement in a range that has no token for it. +pub proof fn example_inserted_semicolon_consumes_no_token() + ensures + ({ + let ts = Stream { toks: Seq::empty(), gaps: seq![Ins::DoWhile] }; + semi(ts, 0, 0, true) && !semi(ts, 0, 0, false) + }), +{ +} + +/// The limit of item 7.3 is a number, and a leaf is one node high. +pub proof fn example_depth_limit() + ensures + MAX_DEPTH == 1024, + ({ + let a = AstV { + nodes: seq![node(NodeKind::NullLiteral, Op::None, 0, 4, Seq::empty())], + root: 0, + }; + height(a, 0) == 1 + }), +{ + let a = AstV { nodes: seq![node(NodeKind::NullLiteral, Op::None, 0, 4, Seq::empty())], root: 0 }; + assert(max_height(a, 0, a.nodes[0].children, 0) == 0); +} + +} // verus! diff --git a/crates/clazel-js-syntax/src/lib.rs b/crates/clazel-js-syntax/src/lib.rs new file mode 100644 index 000000000..d7738cd55 --- /dev/null +++ b/crates/clazel-js-syntax/src/lib.rs @@ -0,0 +1,27 @@ +//! The JavaScript parser for Clazel's Phase 7 browser (phase-7.md item 7.3): the +//! AST, the specification of what a correct parse is, and the parser's contract. +//! +//! Spec: ECMA-262, 2026 edition, §12.10 and §13 to §16 (and the early errors and +//! Annex B.3 syntax they bring), at commit +//! `0248456c758431e4bb8e5d26333ff1865123c9cd` (`SPEC-PIN.md` of +//! `clazel-js-lexer`). Tier: see `[package.metadata.clazel]` in `Cargo.toml` +//! and `docs/VERIFICATION.md`. +//! +//! This is the first half of item 7.3 (CLAUDE.md rule 3, contracts before code): +//! there is no parser yet. +//! +//! - [`ast`]: the arena AST, with spans, its node kinds named after the +//! productions of the standard. +//! - [`spec`]: a transcription of the syntactic grammar as a relation between an +//! AST and a token stream, automatic semicolon insertion, the early errors, and the +//! recursion limit. What it covers and leaves out is listed at the top of the +//! file. +//! - [`contract`]: what the executable parser must be proven to satisfy. +//! - [`api`]: the contract-only trait [`api::JsParser`]. +//! - [`examples`]: a few small proven facts about the spec. + +pub mod api; +pub mod ast; +pub mod contract; +pub mod examples; +pub mod spec; diff --git a/crates/clazel-js-syntax/src/spec.rs b/crates/clazel-js-syntax/src/spec.rs new file mode 100644 index 000000000..236c4f964 --- /dev/null +++ b/crates/clazel-js-syntax/src/spec.rs @@ -0,0 +1,3945 @@ +//! Specification of what a correct parse of an ECMAScript `Script` or `Module` +//! is, transcribed from ECMA-262, 2026 edition (`es2026`), §12.10 "Automatic +//! Semicolon Insertion", §13 to §16 (the syntactic grammar and its static +//! semantics: early errors, BoundNames, LexicallyDeclaredNames, ...), and Annex B.3 +//! (the syntax that web browsers add), at commit +//! `0248456c758431e4bb8e5d26333ff1865123c9cd` of +//! (31 March 2026; `SPEC-PIN.md` of +//! `clazel-js-lexer`). Section numbers are those `spec.html` numbers at that +//! commit, and every rule quoted is checked against that text. Nothing here +//! runs; it defines what "correct" means. Every definition cites the standard. +//! +//! # What this file covers +//! +//! - **The syntactic grammar** (§13.1 to §16.2), for both goal symbols, as a +//! relation `d` between nodes of an arena AST ([`crate::ast`]) and the token +//! stream they derive: identifiers, literals (templates included), array and +//! object literals, function, generator, async and async generator expressions +//! and declarations, arrow and async arrow functions, methods, classes (fields, +//! private names, static blocks), every operator with its precedence (the +//! exponent, `??` and optional chaining rules included), assignment and its +//! patterns, binding patterns, `yield`, `await`, every statement of §14, +//! parameter lists and bodies, and the module items: `import` and `export` in +//! all their forms, with import attributes. The grammar parameters `[Yield]`, +//! `[Await]`, `[In]`, `[Return]` and `[Default]` are `Ctx`; `[Tagged]` is +//! `EarlyEnv::tagged` (it affects only early errors); the lookaheads and the +//! `[no LineTerminator here]` restrictions are written where the standard has +//! them. Annex B.3.1 (labelled functions), B.3.3 (functions as `if` clauses) and +//! B.3.5 (`for (var a = 1 in x)`) are in the grammar; Annex B's HTML-like +//! comments and legacy octal syntax are in the lexer. +//! - **Automatic semicolon insertion** (§12.10.1): rules 1 to 4 (`gap_justified`). +//! - **Early errors** (`early`, `early_local`): the rules of §12.9.3.1, §12.9.4.1, +//! §13.1.1, §13.2.5.1, §13.2.7.1, §13.2.8.1, §13.3.1.1, §13.4.1, §13.5.1.1, §13.15.1, +//! §13.15.5.1, §14.2.1, §14.3.1.1, §14.6.1, §14.7.2.1 to §14.7.5.1, §14.8.1, +//! §14.9.1, §14.11.1, §14.12.1, §14.13.1, §14.15.1, §15.1.1 to §15.9.1, §15.7.1, +//! §16.1.1, §16.2.1.1 and §16.2.2.1 (names declared twice, names that are both +//! lexical and var, labels and the targets of `break` and `continue`, strict +//! mode, the targets of assignments, `super`, `new.target`, `import.meta`, +//! `yield` and `await` in parameters, private names and the constructors of +//! classes, directive prologues, duplicate `__proto__`, `arguments` in field +//! initializers), and §16.2.3.1's rule on the local names of `export {..};`, +//! with Annex B.3.2 (duplicate sloppy function declarations in a block), B.3.3, +//! B.3.4 (`var` of a `catch` parameter's name), B.3.5 and B.3.9 (calls as +//! assignment targets: `~web-compat~`). +//! - **The recursion limit** ([`MAX_DEPTH`]), as the listed deviation of item 7.3. +//! +//! # What it leaves out +//! +//! - **Early errors not yet specified.** §13.2.8.1's limit of 2^32 template strings; +//! §16.2.1.1's rules on ExportedNames (no duplicate), ExportedBindings (each is +//! declared) and `IsStringWellFormedUnicode` of module export names; the +//! exception of Annex B.3.4 for a `for (var e of ...)` inside a `catch (e)`; +//! the rules about the *contents* of a regular expression (§22.2.1.1, Annex +//! B.1.2), which are `regexp_valid` of its token, supplied by `clazel-js-regexp`; +//! the additional rules of §16.1.1 for code that a direct `eval` processes (the +//! embedder's, item 7.19); and `for await` outside an `async` +//! function, which the grammar parameter `[+Await]` already excludes. +//! - **Everything about values**: the numeric value of a literal, the SV of a +//! string other than for `__proto__` and a directive (`Tok::name` for a +//! `StringLiteral`), and TV and TRV, which the lexer item has, and run-time +//! semantics. The parser does not compute them. +//! - **Cover grammars as a technique.** See the modelling choices. +//! - **The lexer.** Tokens come from the lexer's specification through the +//! [`LexerSpec`] trait, and `lexes_as` states only how a parser drives it. +//! +//! # Modelling choices +//! +//! Each one is a deliberate difference in form, never in meaning, except where +//! it is listed under *Deviations*. +//! +//! - **An AST is a derivation.** The standard has a grammar; a parser's output +//! is a derivation, here an arena of nodes whose kinds name the productions +//! (`NodeKind`), whose children are in the productions' order, and whose spans +//! are in the source text. `d(a, ts, n, cx, i, j)` says node `n` derives exactly the +//! tokens `ts[i..j)` in context `cx`: each `NodeKind` has one arm, written from the +//! production(s) it is named after, in the standard's order and with its +//! alternatives' numbers where there are several. The relation is checked from +//! the root, which derives the whole stream. Since a node's span says where it +//! ends, no arm guesses where a child ends: `ed` reads it off the span, and +//! `span_ok` checks that the span is the range of tokens derived. +//! - **Optional symbols are `Absent` nodes**, with an empty span where they +//! would be (anywhere in the white space before the next token, so that it +//! lies inside the parent whether it comes first or last: `span_ok`), so a kind's children are at fixed positions, as the production's symbols +//! are. A list is one child per item. A comma-separated list with an optional +//! trailing `,` is `dsep`, and an `ElementList` with `Elision`s is `darray`. +//! - **Precedence is `lvl`.** The chain of productions from `Expression` down to +//! `PrimaryExpression` (`X : Y | X op Z`) is one number per node kind; a binary +//! node says which operands it takes by comparing them (`binary_operands_ok`), +//! which is the same as writing each level's production, in a few lines instead of +//! one production per level. `ExponentiationExpression`, `RelationalExpression`'s `#x in y` +//! and `CoalesceExpression` are written out as the standard has them. +//! - **Cover grammars** (§13.2.9, §13.3.6 and §15.9 CoverCallExpressionAndAsyncArrowHead, +//! §13.2.5.1's `CoverInitializedName`, §13.15.5's `AssignmentPattern`) are +//! specified by the refined production that the text is read as, not by the +//! cover: the standard says "the interpretation of [the cover] is refined using +//! the following grammar", and a derivation by the refined grammar is such a +//! refinement. A `ParenthesizedExpression` and an `ArrowFunction`'s parameters +//! are different nodes, an `AssignmentPattern` is its own kind of node (so +//! "`LeftHandSideExpression` must cover an `AssignmentPattern`" is that the +//! target of `=`, of a `for` head, or of a pattern element is one, and an +//! `ObjectLiteral` or `ArrayLiteral` is not), and a `CoverInitializedName` is +//! an `ObjectLiteral`'s child that is always an early error. Two nodes can derive +//! the same text (`({a = 1} = x)` and `({a = 1})`), as two productions can. +//! - **Tokens are chosen with the grammar.** The lexer's goal symbol is the +//! syntactic grammar's choice (§12, "the syntactic grammar chooses the goal +//! symbol"): `lexes_as` lets each token be a step of the lexer with *some* goal. +//! Every position of the grammar accepts only the token kinds of one goal (a +//! `/` is `DivPunctuator` after an operand, the start of a +//! `RegularExpressionLiteral` before one), so the existential does not admit a +//! second parse. Trivia are the lexer's: `Tok::nl_before` is the one fact of +//! them the parser reads. +//! - **Automatic semicolon insertion is the one rule that looks at what could +//! come next.** The standard says a `;` is inserted before a token that is "not +//! allowed by any production of the grammar" (rule 1), or at the end if the +//! tokens do not make a `Script` (rule 2), left to right. The `;` is in the +//! stream, in the gap where it goes (`Stream::gaps`), and in no token's range, +//! so spans never include text that is not in the source; the production that +//! ends with `;` ends at the gap (`semi`). It is justified (`gap_justified`) if no +//! sentence of the grammar *without* semicolon insertion begins with the tokens +//! before it (inserted `;`s included) and the offending token, under any +//! goal symbol the text there can lex with (`viable_free`). That is the standard's +//! test and nothing else: the grammar itself has no insertion, so the +//! definition is not circular. The restricted productions of §12.10.1 are +//! `[no LineTerminator here]` in the grammar, so a token after a line terminator +//! there is "not allowed", as the standard says rule 3 amounts to. The overriding +//! condition (no `;` as an empty statement or in a `for` header) is that those +//! productions accept a real `;` only. The `;` after a `do`-`while` is `Ins::DoWhile`. +//! - **Context is a parameter, strictness is a position.** `Ctx` is the +//! grammar's parameters. `EarlyEnv` is what the early errors read of a node's +//! position, passed down the tree as `child_env` (mirroring `d`): `IsStrict` +//! (§11.2.2), the labels in scope, whether a `break` or `continue` is allowed, +//! whether `super` and `new.target` are, and so on. "Contains" is not +//! recursion into the node, but the flag of the position (`super_call`), which +//! a function, class or `static` block boundary resets and an arrow function +//! does not. +//! - **Names are code points.** The StringValue of an identifier is `Tok::name` +//! (§13.1.2, IdentifierCodePoints, escapes resolved), and a name is a +//! `Seq`. A `PrivateIdentifier`'s name is the part after the `#`. +//! - **Strictness has no grammar effect.** The grammar does not read the strict +//! mode (the parse of `yield` as an identifier is by `[Yield]` alone); every +//! strict mode difference of the standard is an early error here, and a Use +//! Strict Directive makes the whole function, parameters and name included, +//! strict (§11.2.2, §15.2.1's note). +//! - **Reserved words.** `Identifier : IdentifierName but not ReservedWord` is +//! `is_reserved_token`: an `IdentifierName` written without escapes that is a +//! `ReservedWord` is not an `Identifier`, and one with escapes whose StringValue +//! is one is the early error of §13.1.1. +//! +//! # Deviations from the standard +//! +//! - **Recursion depth.** A program whose AST is higher than [`MAX_DEPTH`] is +//! rejected (item 7.3: "a listed deviation with its number"). `height` counts every +//! node, so a chain of 1024 binary operators is too high. The standard has no +//! limit; the contract (`crate::contract`) says the parser accepts every valid +//! program that has a parse within the limit. +//! - **Annex B is on.** The web legacy compatibility syntax and semantics that +//! the standard marks as normative optional is specified as present, as a +//! browser needs it (Annex B is required of a web browser). + +/// The arena AST, whose kinds the grammar below is written over, re-exported so +/// that the other modules reach it with `use super::spec::*`. +pub use crate::ast::*; +#[allow(unused_imports)] +use vstd::prelude::*; + +verus! { + +/// §11.1 source text: a sequence of code points. +pub type Src = Seq; + +/// The code points of an ASCII word or punctuator, for comparing with a +/// token's text. +pub open spec fn cps(s: Seq) -> Seq { + s.map_values(|c: char| c as u32) +} + +// --------------------------------------------------------------------- +// The lexer, as the parser sees it +// --------------------------------------------------------------------- + +/// The lexical goal symbols of §12, the argument of every lexer step. The same +/// five as `clazel_js_lexer::spec::Goal`; this crate depends on `vstd` only, +/// so it names them itself, and the bridge to the lexer crate is the +/// [`LexerSpec`] trait. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum LexGoal { + /// `InputElementDiv`. + InputElementDiv, + /// `InputElementRegExp`. + InputElementRegExp, + /// `InputElementRegExpOrTemplateTail`. + InputElementRegExpOrTemplateTail, + /// `InputElementTemplateTail`. + InputElementTemplateTail, + /// `InputElementHashbangOrRegExp`: the first token of a `Script` or + /// `Module`. + InputElementHashbangOrRegExp, +} + +/// The kind of a template literal lexical component (§12.9.6). +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum TemplateKind { + /// `NoSubstitutionTemplate`. + NoSubstitution, + /// `TemplateHead`. + Head, + /// `TemplateMiddle`. + Middle, + /// `TemplateTail`. + Tail, +} + +/// The kinds of token the syntactic grammar sees (the input elements of §12 +/// that are not trivia). +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum TokKind { + /// `IdentifierName`, a `ReservedWord` or not. + IdentifierName, + /// `PrivateIdentifier`. + PrivateIdentifier, + /// `Punctuator`, `DivPunctuator` or `RightBracePunctuator`. + Punctuator, + /// `NumericLiteral`. + NumericLiteral, + /// `StringLiteral`. + StringLiteral, + /// A template component. + Template(TemplateKind), + /// `RegularExpressionLiteral`. + RegularExpressionLiteral, +} + +/// What automatic semicolon insertion (§12.10) puts in the gap before a token +/// (or before the end of the input): nothing, or a `;` that is not in the +/// source text. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum Ins { + /// Nothing is inserted. + None, + /// A `;` inserted by §12.10.1 rule 1 or rule 2 (an offending token, or the + /// end of the input). + Semicolon, + /// A `;` inserted by §12.10.1 rule 1, third condition: after the `)` that ends a `do`-`while` + /// statement, whatever follows. + DoWhile, +} + +/// A token: what the parser reads. The fields are the lexer's output +/// (`clazel_js_lexer::api::Element` and `Token`). +pub struct Tok { + /// The kind. + pub kind: TokKind, + /// The index of the first code point. + pub start: int, + /// One past the last code point. + pub end: int, + /// The code points `src[start..end)`. + pub text: Seq, + /// For an `IdentifierName` or `PrivateIdentifier`, its + /// IdentifierCodePoints (§12.7.1.2), escapes resolved; for a `StringLiteral`, + /// its SV (§12.9.4.2) as UTF-16 code units. Empty otherwise. + pub name: Seq, + /// For an `IdentifierName` or `PrivateIdentifier`, whether it was written + /// with a `UnicodeEscapeSequence`. + pub escaped: bool, + /// Whether a line terminator precedes the token since the previous one + /// (the flag §12.10 needs). + pub nl_before: bool, + /// For a `NumericLiteral`, a `LegacyOctalIntegerLiteral` or + /// `NonOctalDecimalIntegerLiteral` (§12.9.3.1); for a `StringLiteral`, one + /// that holds a `LegacyOctalEscapeSequence` or + /// `NonOctalDecimalEscapeSequence` (§12.9.4.1). False otherwise. + pub legacy: bool, + /// For a `NumericLiteral`, a BigInt literal. + pub bigint: bool, + /// For a template component, that its TV is *undefined*: it holds a + /// `NotEscapeSequence` (§12.9.6.1). + pub invalid_escape: bool, + /// For a `RegularExpressionLiteral`, whether `IsValidRegularExpressionLiteral` + /// (§13.2.7.1) holds. + pub regexp_valid: bool, +} + +/// What the parser reads: the tokens of the source text (trivia dropped), and +/// for each gap before a token, and the one before the end of the input, what +/// §12.10 inserts there. `gaps[k]` is before `toks[k]`, and `gaps[toks.len()]` +/// is at the end, so `gaps.len() == toks.len() + 1`. An inserted `;` is in no +/// token's range: a production that ends with `;` ends at the gap, consuming +/// no token (`semi`), so a node's span never includes anything that is not in +/// the source text. +pub struct Stream { + /// The tokens of the source text, in order. + pub toks: Seq, + /// What is inserted before each token and before the end. + pub gaps: Seq, +} + +/// The number of tokens. +pub open spec fn tn(ts: Stream) -> int { + ts.toks.len() as int +} + +/// The result of one lexer step, `clazel_js_lexer::api::JsLexer::next_token`. +pub enum LexStep { + /// Only trivia remain. + Eof, + /// A lexical error (`LexError`). + Error, + /// The next token. + Token(Tok), +} + +/// What the parser assumes of the lexer: the lexer's own specification +/// (`clazel_js_lexer::spec`, through `next_token_ensures`) says what +/// `next_token` is, and `clazel-js-regexp`'s says what +/// `regexp_literal_valid` is. The parser's contract is stated for any lexer +/// that is a model of this trait, so this crate need not depend on the lexer +/// crate; the implementation of 7.3's second half instantiates it. +pub trait LexerSpec { + /// The step at `pos` (`0 <= pos <= src.len()`) with `goal`: skip trivia, + /// then the longest input element of `goal` (§12), as + /// `clazel_js_lexer::contract::next_token_ensures`. + spec fn next_token(src: Src, pos: int, goal: LexGoal) -> LexStep; + + /// `IsValidRegularExpressionLiteral` (§13.2.7.1) of the regular + /// expression literal with this text, `/ body / flags`. + spec fn regexp_literal_valid(text: Seq) -> bool; +} + +/// The end of token `k - 1`, or 0 before the first token: where the lexer step +/// for token `k` starts. +pub open spec fn prev_end(ts: Stream, k: int) -> int { + if k <= 0 || k > tn(ts) { + 0 + } else { + ts.toks[k - 1].end + } +} + +/// `toks` is a lexing of `src`, as a parser drives the lexer (§12: "the +/// syntactic grammar chooses the goal symbol"): each token is the result of +/// a lexer step from the end of the one before, with some goal (the first with +/// `InputElementHashbangOrRegExp`), and only trivia follow the last. The goal +/// is existential, so which of the lexical grammars applied at each token +/// is the grammar's choice (a `/` the grammar reads as a division is +/// `DivPunctuator`, as a regular expression is `RegularExpressionLiteral`): a +/// token sequence the syntactic grammar derives is a lexing with the +/// right goals at once, because each position of the grammar accepts only +/// the token kinds of one goal. +pub open spec fn lexes_as(src: Src, toks: Seq) -> bool { + let last_end = if toks.len() == 0 { + 0 + } else { + toks[toks.len() - 1].end + }; + (exists|goals: Seq| + #![auto] + goals.len() == toks.len() && (toks.len() > 0 ==> goals[0] + == LexGoal::InputElementHashbangOrRegExp) && (forall|k: int| + 0 <= k < toks.len() ==> L::next_token( + src, + if k == 0 { + 0 + } else { + toks[k - 1].end + }, + #[trigger] goals[k], + ) == LexStep::Token(toks[k]) && (toks[k].kind == TokKind::RegularExpressionLiteral + ==> toks[k].regexp_valid == L::regexp_literal_valid(toks[k].text)))) + && (exists|g: LexGoal| + L::next_token(src, last_end, g) == LexStep::Eof && (toks.len() == 0 ==> g == LexGoal::InputElementHashbangOrRegExp)) +} + +// --------------------------------------------------------------------- +// Token predicates +// --------------------------------------------------------------------- + +/// Token `i` exists and is the punctuator `lit`. +pub open spec fn is_p(ts: Stream, i: int, lit: Seq) -> bool { + 0 <= i < tn(ts) && ts.toks[i].kind == TokKind::Punctuator && ts.toks[i].text =~= cps(lit) +} + +/// Token `i` is the keyword or contextual keyword `w`: an `IdentifierName` +/// written without escapes (§12.7.2: a keyword is matched as source +/// characters; a contextual keyword the same way). +pub open spec fn is_kw(ts: Stream, i: int, w: Seq) -> bool { + 0 <= i < tn(ts) && ts.toks[i].kind == TokKind::IdentifierName && !ts.toks[i].escaped + && ts.toks[i].text =~= cps(w) +} + +/// Token `i` is an `IdentifierName` token. +pub open spec fn is_name(ts: Stream, i: int) -> bool { + 0 <= i < tn(ts) && ts.toks[i].kind == TokKind::IdentifierName +} + +/// Token `i` is a token of kind `k`. +pub open spec fn is_kind(ts: Stream, i: int, k: TokKind) -> bool { + 0 <= i < tn(ts) && ts.toks[i].kind == k +} + +/// A line terminator separates token `i` from the one before. +pub open spec fn nl(ts: Stream, i: int) -> bool { + 0 <= i < tn(ts) && ts.toks[i].nl_before +} + +/// §12.7.2 `ReservedWord`. +pub open spec fn reserved_words() -> Seq> { + seq![ + "await"@, "break"@, "case"@, "catch"@, "class"@, "const"@, "continue"@, "debugger"@, + "default"@, "delete"@, "do"@, "else"@, "enum"@, "export"@, "extends"@, "false"@, + "finally"@, "for"@, "function"@, "if"@, "import"@, "in"@, "instanceof"@, "new"@, + "null"@, "return"@, "super"@, "switch"@, "this"@, "throw"@, "true"@, "try"@, + "typeof"@, "var"@, "void"@, "while"@, "with"@, "yield"@, + ] +} + +/// The code points `text` are a `ReservedWord`. +pub open spec fn is_reserved_text(text: Seq) -> bool { + exists|w: int| 0 <= w < reserved_words().len() && text =~= cps(#[trigger] reserved_words()[w]) +} + +/// §12.7.2, §13.1 `Identifier :: IdentifierName but not ReservedWord`: the +/// `but not` excludes a token whose source characters are a `ReservedWord`. +/// An identifier with an escape is never excluded here: §13.1.1 makes one +/// whose StringValue is a reserved word an early error +/// ([`early_identifier_ok`]). +pub open spec fn is_reserved_token(t: Tok) -> bool { + !t.escaped && is_reserved_text(t.text) +} + +// --------------------------------------------------------------------- +// The parameters of the syntactic grammar (§5.1.5) +// --------------------------------------------------------------------- + +/// The grammar parameters `[Yield]`, `[Await]`, `[In]`, `[Return]` and +/// `[Default]` (§5.1.5, §13.1, §14.7, §15, §16.2), as booleans (`+Yield` is +/// `true`, `~Yield` is `false`). `[Tagged]` is [`EarlyEnv::tagged`]. +pub struct Ctx { + /// `[Yield]`. + pub yield_: bool, + /// `[Await]`. + pub await_: bool, + /// `[In]`. + pub in_: bool, + /// `[Return]`. + pub return_: bool, + /// `[Default]`: this declaration is the one `export default` names, so its + /// `BindingIdentifier` may be absent. + pub deflt: bool, +} + +/// The same context with `[In]` set to `b` and `[Default]` cleared. +pub open spec fn cx_in(cx: Ctx, b: bool) -> Ctx { + Ctx { yield_: cx.yield_, await_: cx.await_, in_: b, return_: cx.return_, deflt: false } +} + +/// A context for the parts of a function, generator, async function or async +/// generator (`yield_`: a generator, `await_`: async): `[+Yield]`, `[+Await]` +/// as the kind of function says, `[+In]`, and `[Return]` as given. +pub open spec fn cx_fn(yield_: bool, await_: bool, return_: bool) -> Ctx { + Ctx { yield_: yield_, await_: await_, in_: true, return_: return_, deflt: false } +} + +} // verus! + +verus! { + +// --------------------------------------------------------------------- +// Early errors (every "It is a Syntax Error if" rule of the clauses named) +// --------------------------------------------------------------------- +// +// A rule says what a *valid* program is, not how to find out. They are +// stated over the AST and the tokens, by the static semantics the standard +// names (BoundNames, LexicallyDeclaredNames, VarDeclaredNames, +// AssignmentTargetType, IsStrict, Contains, ...), written as spec fns that +// keep the standard's names. The context that the standard's rules read off +// the position of a production ("is contained in strict mode code", "is not +// nested within an IterationStatement", "has a [Yield] parameter") is carried +// down the tree in an `EarlyEnv`, as `d` carries the grammar parameters. + +/// The facts about the position of a node that early errors read. +pub struct EarlyEnv { + /// `IsStrict` (§11.2.2): the node is in strict mode code. + pub strict: bool, + /// The goal symbol is `Module`. + pub module: bool, + /// The `[Yield]` parameter of the production the node is derived with. + pub yield_: bool, + /// The `[Await]` parameter. + pub await_: bool, + /// The labels of the enclosing `LabelledStatement`s, not crossing a function or + /// `static` initialization block boundary, and whether each labels an iteration + /// statement (§14.8.1, §14.9.1, ContainsDuplicateLabels). + pub labels: Seq, + /// The node is nested in an `IterationStatement` (not crossing a function + /// boundary): `continue` is allowed. + pub in_loop: bool, + /// The node is nested in an `IterationStatement` or a `SwitchStatement`: + /// an unlabelled `break` is allowed. + pub in_breakable: bool, + /// `SuperProperty` is allowed: in a method, or a field initializer or static + /// block, or an arrow function in one (§13.3.7.1). + pub super_prop: bool, + /// `SuperCall` is allowed: in a derived class's constructor, or an arrow + /// function in one (§13.3.7.1). + pub super_call: bool, + /// `new.target` is allowed (§13.3.12.1): in a function other than an arrow + /// function, or a field initializer or static block, or an arrow function in one. + pub new_target: bool, + /// `arguments` is not allowed (§15.7.1 ContainsArguments): in a field + /// initializer or static block, or an arrow function in one. + pub no_arguments: bool, + /// A `YieldExpression` or `AwaitExpression` is not allowed: in the formal + /// parameters of a generator, async function or arrow function (§15.5.1 and + /// the other "Contains YieldExpression / AwaitExpression" rules), and in a + /// class static block, where `for await` is not allowed either (§15.7.1 + /// ContainsAwait; `[~Yield]` already leaves no `YieldExpression` there). + pub no_yield_await: bool, + /// The `[Tagged]` parameter of a `TemplateLiteral` (§13.2.8). + pub tagged: bool, + /// The `#names` that class bodies containing the node declare + /// (AllPrivateIdentifiersValid, §15.7.1, §16.1.1). + pub privates: Seq>, + /// The nearest enclosing class has a `ClassHeritage`. + pub derived: bool, +} + +/// A label in scope: its name, and whether it labels an iteration statement. +pub struct EarlyLabel { + /// The name (its StringValue). + pub name: Seq, + /// The labelled statement is an `IterationStatement`, possibly through + /// other labels (ContainsUndefinedContinueTarget). + pub iteration: bool, +} + +/// A name bound by a declaration, and whether the declaration is a +/// `FunctionDeclaration` (not a generator or async function), which Annex B.3.2 +/// lets repeat in sloppy mode code. +pub struct DeclName { + /// The name. + pub name: Seq, + /// A plain `FunctionDeclaration`. + pub plain_function: bool, +} + +/// The first token of node `c`: the token whose start is the start of its span. +pub open spec fn node_tok(a: AstV, ts: Stream, c: int) -> Tok { + ts.toks[choose|x: int| 0 <= x < tn(ts) && ts.toks[x].start == a.nodes[c].start] +} + +/// The StringValue of an identifier node (§13.1.2, §12.7.1.2 +/// IdentifierCodePoints): the name of its token. For a string literal, its SV; +/// for a `PrivateIdentifier`, the name after the `#`. +pub open spec fn name_of(a: AstV, ts: Stream, c: int) -> Seq { + node_tok(a, ts, c).name +} + +/// A string of the standard, as the code points StringValue returns. +pub open spec fn sv(s: Seq) -> Seq { + cps(s) +} + +/// The names that §13.1.1 forbids as identifiers in strict mode code. +pub open spec fn is_strict_reserved(n: Seq) -> bool { + n =~= sv("implements"@) || n =~= sv("interface"@) || n =~= sv("let"@) || n =~= sv("package"@) + || n =~= sv("private"@) || n =~= sv("protected"@) || n =~= sv("public"@) || n + =~= sv("static"@) || n =~= sv("yield"@) +} + +// --------------------------------------------------------------------- +// Names (§8.2.? BoundNames, §14.2.? LexicallyDeclaredNames, VarDeclaredNames) +// --------------------------------------------------------------------- + +/// The names in `ns` has a name twice. +pub open spec fn has_duplicate(ns: Seq>) -> bool { + exists|x: int, y: int| 0 <= x < y < ns.len() && #[trigger] ns[x] =~= #[trigger] ns[y] +} + +/// Some name is in both. +pub open spec fn intersects(xs: Seq>, ys: Seq>) -> bool { + exists|x: int, y: int| 0 <= x < xs.len() && 0 <= y < ys.len() && #[trigger] xs[x] =~= #[trigger] ys[y] +} + +/// `ns` contains the name `n`. +pub open spec fn has_name(ns: Seq>, n: Seq) -> bool { + exists|x: int| 0 <= x < ns.len() && #[trigger] ns[x] =~= n +} + +/// BoundNames (§8.2.1) of the binding, parameter or pattern node `c`: the +/// `BindingIdentifier`s in it, in order. A `BindingElement`'s initializer binds +/// nothing. +pub open spec fn bound_names(a: AstV, ts: Stream, c: int) -> Seq> + decreases c, 1int, 0int, +{ + if c < 0 || c >= a.nodes.len() { + Seq::empty() + } else { + let nd = a.nodes[c]; + match nd.kind { + NodeKind::BindingIdentifier => seq![name_of(a, ts, c)], + // The binding of these is their first child, the last, or each child. + NodeKind::BindingElement | NodeKind::BindingRestElement | NodeKind::BindingRestProperty + | NodeKind::FunctionRestParameter | NodeKind::VariableDeclaration + | NodeKind::LexicalBinding | NodeKind::ForDeclaration => if nd.children.len() > 0 + && 0 <= nd.children[0] < c { + bound_names(a, ts, nd.children[0]) + } else { + Seq::empty() + }, + NodeKind::BindingProperty => if nd.children.len() > 0 && 0 <= nd.children[nd.children.len() + - 1] < c { + bound_names(a, ts, nd.children[nd.children.len() - 1]) + } else { + Seq::empty() + }, + NodeKind::ObjectBindingPattern | NodeKind::ArrayBindingPattern | NodeKind::FormalParameters + | NodeKind::VariableStatement | NodeKind::LexicalDeclaration + | NodeKind::VariableDeclarationList => bound_names_list(a, ts, c, nd.children, 0), + _ => Seq::empty(), + } + } +} + +/// The concatenation of the BoundNames of the children `ks[k..]` of node `n`. +pub open spec fn bound_names_list(a: AstV, ts: Stream, n: int, ks: Seq, k: int) -> Seq> + decreases n, 0int, ks.len() - k, +{ + if k < 0 || k >= ks.len() { + Seq::empty() + } else { + let here = if 0 <= ks[k] < n { + bound_names(a, ts, ks[k]) + } else { + Seq::empty() + }; + here + bound_names_list(a, ts, n, ks, k + 1) + } +} + +/// LexicallyDeclaredNames (§8.2.? ) of one `StatementListItem` `c`, or, with `top`, +/// TopLevelLexicallyDeclaredNames, where a function declaration is var-scoped. A +/// `LexicalDeclaration` and a `ClassDeclaration` declare their bound names; a +/// function declaration (inside a block, or a module) its name; a labelled function +/// the name of the function; the import and export declarations of a module what +/// they declare (§16.2.1.5). +pub open spec fn lex_names_item(a: AstV, ts: Stream, c: int, top: bool) -> Seq + decreases c, +{ + if c < 0 || c >= a.nodes.len() { + Seq::empty() + } else { + let nd = a.nodes[c]; + let k0 = kid(nd.children, 0); + let k1 = kid(nd.children, 1); + match nd.kind { + NodeKind::LexicalDeclaration => bound_names(a, ts, c).map_values(|n: Seq| DeclName { + name: n, + plain_function: false, + }), + NodeKind::ClassDeclaration => if kd(a, k0) == NodeKind::BindingIdentifier { + seq![DeclName { name: name_of(a, ts, k0), plain_function: false }] + } else { + Seq::empty() + }, + NodeKind::FunctionDeclaration | NodeKind::GeneratorDeclaration + | NodeKind::AsyncFunctionDeclaration | NodeKind::AsyncGeneratorDeclaration => if !top + && kd(a, k0) == NodeKind::BindingIdentifier { + seq![DeclName { name: name_of(a, ts, k0), plain_function: nd.kind + == NodeKind::FunctionDeclaration }] + } else { + Seq::empty() + }, + // `LabelledItem : FunctionDeclaration` declares the function (§14.13.? ). + NodeKind::LabelledStatement => if !top && 0 <= k1 < c { + lex_names_item(a, ts, k1, top) + } else { + Seq::empty() + }, + NodeKind::ImportDeclaration => if 0 <= k0 < c { + bound_names_import(a, ts, k0) + } else { + Seq::empty() + }, + NodeKind::ExportDeclaration => if (nd.op == Op::ExportDecl || nd.op + == Op::ExportDefaultHoistable || nd.op == Op::ExportDefaultClass) && 0 <= k0 < c { + lex_names_item(a, ts, k0, false) + } else { + Seq::empty() + }, + _ => Seq::empty(), + } + } +} + +/// The names an `ImportClause` binds (§16.2.2.? BoundNames of ImportDeclaration). +pub open spec fn bound_names_import(a: AstV, ts: Stream, c: int) -> Seq { + // The default binding, the namespace import, and each import specifier's binding. + let nd = a.nodes[c]; + let k0 = kid(nd.children, 0); + let k1 = kid(nd.children, 1); + let def = if kd(a, k0) == NodeKind::BindingIdentifier { + seq![DeclName { name: name_of(a, ts, k0), plain_function: false }] + } else { + Seq::empty() + }; + let rest = if kd(a, k1) == NodeKind::NameSpaceImport { + seq![DeclName { + name: name_of(a, ts, kid(kids(a, k1), 0)), + plain_function: false, + }] + } else if kd(a, k1) == NodeKind::NamedImports { + kids(a, k1).map_values(|s: int| DeclName { + name: name_of(a, ts, kid(kids(a, s), 1)), + plain_function: false, + }) + } else { + Seq::empty() + }; + def + rest +} + +/// The LexicallyDeclaredNames (or top-level ones) of the items `ks[k..]`. +pub open spec fn lex_names_list(a: AstV, ts: Stream, n: int, ks: Seq, k: int, top: bool) -> Seq + decreases n, 1int, ks.len() - k, +{ + if k < 0 || k >= ks.len() { + Seq::empty() + } else { + let here = if 0 <= ks[k] < n { + lex_names_item(a, ts, ks[k], top) + } else { + Seq::empty() + }; + here + lex_names_list(a, ts, n, ks, k + 1, top) + } +} + +/// The plain names of a sequence of declared names. +pub open spec fn plain_names(ds: Seq) -> Seq> { + ds.map_values(|d: DeclName| d.name) +} + +/// §14.2.1 (and the other lists of lexical declarations): a name declared twice, +/// "unless the host is a web browser ... and both of the following conditions are +/// true: IsStrict(this production) is false, and the duplicate entries are only +/// bound by FunctionDeclarations" (Annex B.3.2). +pub open spec fn has_forbidden_duplicate(ds: Seq, strict: bool) -> bool { + exists|x: int, y: int| + 0 <= x < y < ds.len() && #[trigger] decl_name(ds, x) =~= #[trigger] decl_name(ds, y) + && !(!strict && ds[x].plain_function && ds[y].plain_function) +} + +/// The name of declaration `x` in `ds` (the trigger of a quantifier). +pub open spec fn decl_name(ds: Seq, x: int) -> Seq { + ds[x].name +} + +/// VarDeclaredNames (§8.2.? ) of one statement `c`, or, with `top`, of one +/// `StatementListItem` at the top level of a function body, script or static block, +/// where a function declaration is var-scoped (TopLevelVarDeclaredNames). `var` +/// declarations are found in nested statements, but not in nested functions +/// or classes; Annex B.3.5 adds `for (var a = 1 in x)`. +pub open spec fn var_names_item(a: AstV, ts: Stream, c: int, top: bool) -> Seq> + decreases c, 1int, 0int, +{ + if c < 0 || c >= a.nodes.len() { + Seq::empty() + } else { + let nd = a.nodes[c]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let k3 = kid(ks, 3); + match nd.kind { + NodeKind::VariableStatement | NodeKind::VariableDeclarationList => bound_names(a, ts, c), + NodeKind::BlockStatement => var_names_list(a, ts, c, ks, 0), + NodeKind::IfStatement => var_names_sub(a, ts, c, k1) + var_names_sub(a, ts, c, k2), + NodeKind::DoWhileStatement => var_names_sub(a, ts, c, k0), + NodeKind::WhileStatement => var_names_sub(a, ts, c, k1), + NodeKind::ForStatement => var_names_sub(a, ts, c, k0) + var_names_sub(a, ts, c, k3), + NodeKind::ForInStatement | NodeKind::ForOfStatement => (if 0 <= k0 < c && kd(a, k0) + == NodeKind::ForDeclaration && op_of(a, k0) == Op::Var { + bound_names(a, ts, k0) + } else { + Seq::empty() + }) + var_names_sub(a, ts, c, k2), + NodeKind::WithStatement => var_names_sub(a, ts, c, k1), + NodeKind::SwitchStatement => var_names_list(a, ts, c, ks, 1), + NodeKind::CaseClause => var_names_list(a, ts, c, ks, 1), + NodeKind::DefaultClause => var_names_list(a, ts, c, ks, 0), + NodeKind::LabelledStatement => var_names_sub_top(a, ts, c, k1, top), + NodeKind::TryStatement => var_names_sub(a, ts, c, k0) + var_names_sub(a, ts, c, k1) + + var_names_sub(a, ts, c, k2), + NodeKind::Catch => var_names_sub(a, ts, c, k1), + NodeKind::Finally => var_names_sub(a, ts, c, k0), + NodeKind::FunctionDeclaration | NodeKind::GeneratorDeclaration + | NodeKind::AsyncFunctionDeclaration | NodeKind::AsyncGeneratorDeclaration => if top + && kd(a, k0) == NodeKind::BindingIdentifier { + seq![name_of(a, ts, k0)] + } else { + Seq::empty() + }, + NodeKind::ExportDeclaration => if nd.op == Op::ExportVar { + var_names_sub(a, ts, c, k0) + } else { + Seq::empty() + }, + _ => Seq::empty(), + } + } +} + +/// `var_names_item` of a child `s` of `c`, not at the top level of a body. +pub open spec fn var_names_sub(a: AstV, ts: Stream, c: int, s: int) -> Seq> + decreases c, 0int, 0int, +{ + if 0 <= s < c { + var_names_item(a, ts, s, false) + } else { + Seq::empty() + } +} + +/// `var_names_item` of a child `s` of `c`, passing `top` through: the item of +/// a `LabelledStatement` at the top level of a body. +pub open spec fn var_names_sub_top(a: AstV, ts: Stream, c: int, s: int, top: bool) -> Seq> + decreases c, 0int, 0int, +{ + if 0 <= s < c { + var_names_item(a, ts, s, top) + } else { + Seq::empty() + } +} + +/// The VarDeclaredNames of the statements `ks[k..]` of node `n`, none at the top +/// level (a block, a case clause). +pub open spec fn var_names_list(a: AstV, ts: Stream, n: int, ks: Seq, k: int) -> Seq> + decreases n, 0int, ks.len() - k, +{ + if k < 0 || k >= ks.len() { + Seq::empty() + } else { + var_names_sub(a, ts, n, ks[k]) + var_names_list(a, ts, n, ks, k + 1) + } +} + +/// The TopLevelVarDeclaredNames of the items `ks[k..]` of node `n`, a function body +/// or script (§8.2.? ): function declarations count. +pub open spec fn top_var_names_list(a: AstV, ts: Stream, n: int, ks: Seq, k: int) -> Seq> + decreases n, 0int, ks.len() - k, +{ + if k < 0 || k >= ks.len() { + Seq::empty() + } else { + var_names_sub_top(a, ts, n, ks[k], true) + top_var_names_list(a, ts, n, ks, k + 1) + } +} + +} // verus! + +verus! { + +/// §8.6.4 AssignmentTargetType: whether a `LeftHandSideExpression` may be +/// assigned to. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum TargetType { + /// `~simple~`. + Simple, + /// `~web-compat~`: a call, in sloppy mode code, under Annex B.3.9 (a + /// `ReferenceError` at run time, no early error). + WebCompat, + /// `~invalid~`. + Invalid, +} + +/// §8.6.4 AssignmentTargetType of the expression node `c`, in strict mode code or +/// not. +pub open spec fn target_type(a: AstV, ts: Stream, c: int, strict: bool) -> TargetType + decreases c, +{ + if c < 0 || c >= a.nodes.len() { + TargetType::Invalid + } else { + let nd = a.nodes[c]; + let k0 = kid(nd.children, 0); + match nd.kind { + // `IdentifierReference : Identifier`: `eval` and `arguments` are not + // assignable in strict mode code. + NodeKind::IdentifierReference => if strict && (name_of(a, ts, c) =~= sv("eval"@) + || name_of(a, ts, c) =~= sv("arguments"@)) { + TargetType::Invalid + } else { + TargetType::Simple + }, + // `MemberExpression : MemberExpression [ Expression ]`, `. IdentifierName`, + // `. PrivateIdentifier`, `SuperProperty`, and the `CallExpression` forms. + NodeKind::MemberExpression | NodeKind::SuperProperty => TargetType::Simple, + // `PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList` + NodeKind::ParenthesizedExpression => if 0 <= k0 < c { + target_type(a, ts, k0, strict) + } else { + TargetType::Invalid + }, + // `CallExpression : CoverCallExpressionAndAsyncArrowHead | CallExpression Arguments` + NodeKind::CallExpression => if !strict { + TargetType::WebCompat + } else { + TargetType::Invalid + }, + _ => TargetType::Invalid, + } + } +} + +/// `c` with the `ParenthesizedExpression`s around it removed (§13.5.1.1: "this rule +/// is recursively applied"). +pub open spec fn strip_parens(a: AstV, c: int) -> int + decreases c, +{ + if 0 <= c < a.nodes.len() && a.nodes[c].kind == NodeKind::ParenthesizedExpression { + let k0 = kid(a.nodes[c].children, 0); + if 0 <= k0 < c { + strip_parens(a, k0) + } else { + c + } + } else { + c + } +} + +/// An `IterationStatement` (§14.7), possibly under labels. +pub open spec fn is_iteration_stmt(a: AstV, c: int) -> bool + decreases c, +{ + if 0 <= c < a.nodes.len() { + let k = a.nodes[c].kind; + k == NodeKind::DoWhileStatement || k == NodeKind::WhileStatement || k + == NodeKind::ForStatement || k == NodeKind::ForInStatement || k + == NodeKind::ForOfStatement || (k == NodeKind::LabelledStatement && { + let k1 = kid(a.nodes[c].children, 1); + 0 <= k1 < c && is_iteration_stmt(a, k1) + }) + } else { + false + } +} + +/// §8.6.? IsLabelledFunction(`c`): a `LabelledStatement` whose item, through any +/// labels, is a function declaration. +pub open spec fn is_labelled_function(a: AstV, c: int) -> bool + decreases c, +{ + if 0 <= c < a.nodes.len() && a.nodes[c].kind == NodeKind::LabelledStatement { + let k1 = kid(a.nodes[c].children, 1); + kd(a, k1) == NodeKind::FunctionDeclaration || (0 <= k1 < c && is_labelled_function(a, k1)) + } else { + false + } +} + +/// §11.2.1 a `Directive Prologue` item: an `ExpressionStatement` that consists +/// entirely of a `StringLiteral` token. +pub open spec fn is_directive(a: AstV, c: int) -> bool { + kd(a, c) == NodeKind::ExpressionStatement && kd(a, kid(kids(a, c), 0)) == NodeKind::StringLiteral +} + +/// §11.2.1 a `Use Strict Directive`: a directive whose `StringLiteral` is exactly +/// `"use strict"` or `'use strict'`, with no escape or line continuation. +pub open spec fn is_use_strict_directive(a: AstV, ts: Stream, c: int) -> bool { + is_directive(a, c) && ({ + let t = node_tok(a, ts, kid(kids(a, c), 0)); + t.text =~= cps("\"use strict\""@) || t.text =~= cps("'use strict'"@) + }) +} + +/// The items `ks` begin with a Directive Prologue that holds a Use Strict Directive: +/// §11.2.1, FunctionBodyContainsUseStrict (§15.2.1). +pub open spec fn has_use_strict(a: AstV, ts: Stream, ks: Seq) -> bool { + exists|k: int| + 0 <= k < ks.len() && is_use_strict_directive(a, ts, #[trigger] ks[k]) && (forall|m: int| + 0 <= m <= k ==> is_directive(a, #[trigger] ks[m])) +} + +/// §15.1.1 IsSimpleParameterList of a `FormalParameters` node: each parameter is +/// a plain `BindingIdentifier` without an initializer. +pub open spec fn is_simple_params(a: AstV, params: int) -> bool { + forall|k: int| + 0 <= k < kids(a, params).len() ==> { + let p = #[trigger] kids(a, params)[k]; + kd(a, p) == NodeKind::BindingElement && first_kid_kind(a, p) == NodeKind::BindingIdentifier + && kd(a, kid(kids(a, p), 1)) == NodeKind::Absent + } +} + +/// Whether the key `c` of a class element or property, not computed, has the +/// name `w` (§8.6.? PropName). +pub open spec fn prop_name_is(a: AstV, ts: Stream, c: int, w: Seq) -> bool { + (kd(a, c) == NodeKind::IdentifierName || kd(a, c) == NodeKind::StringLiteral) && name_of(a, ts, c) + =~= sv(w) +} + +/// A `MethodDefinition` that is `constructor` by name (§15.7.3 ConstructorMethod, +/// `PropName` is "constructor", not `static`). +pub open spec fn is_constructor_named(a: AstV, ts: Stream, c: int) -> bool { + kd(a, c) == NodeKind::MethodDefinition && !flag_of(a, c) && prop_name_is( + a, + ts, + kid(kids(a, c), 0), + "constructor"@, + ) +} + +/// §14.2.1, §15.2.1, §16.1.1: the early errors of a list of statements: no name +/// declared twice by the lexical declarations, and none that is also var-declared. +/// `top` is for a function body, script or static block, where function declarations +/// are var-scoped. +pub open spec fn list_early(a: AstV, ts: Stream, n: int, ks: Seq, strict: bool, top: bool) -> bool { + let lex = lex_names_list(a, ts, n, ks, 0, top); + let vars = if top { + top_var_names_list(a, ts, n, ks, 0) + } else { + var_names_list(a, ts, n, ks, 0) + }; + !has_forbidden_duplicate(lex, strict) && !intersects(plain_names(lex), vars) +} + +/// The lexical declarations of the statement lists of the clauses `ks[k..]` of a +/// `CaseBlock` (§14.12.1 LexicallyDeclaredNames of CaseBlock). +pub open spec fn clause_lex_names(a: AstV, ts: Stream, n: int, ks: Seq, k: int) -> Seq + decreases n, 0int, ks.len() - k, +{ + if k < 1 || k >= ks.len() { + Seq::empty() + } else { + let c = ks[k]; + let here = if 0 <= c < n { + lex_names_list( + a, + ts, + c, + kids(a, c), + if kd(a, c) == NodeKind::CaseClause { + 1 + } else { + 0 + }, + false, + ) + } else { + Seq::empty() + }; + here + clause_lex_names(a, ts, n, ks, k + 1) + } +} + +/// §15.1.1, §15.2.1, §15.3.1, ...: the early errors shared by the function-like +/// productions. `params` is the `FormalParameters` node, `body_ks` the statements of +/// the body (empty for an arrow function with an expression body), `unique` says the +/// `UniqueFormalParameters` rules apply (methods, arrow functions: duplicates +/// are always an error), `strict` is `IsStrict` of the function code. +pub open spec fn function_early( + a: AstV, + ts: Stream, + params: int, + body_ks: Seq, + unique: bool, + strict: bool, +) -> bool { + let names = bound_names(a, ts, params); + // "FunctionBodyContainsUseStrict is true and IsSimpleParameterList is false" + (has_use_strict(a, ts, body_ks) ==> is_simple_params(a, params)) + // "any element of BoundNames of FormalParameters also occurs in LexicallyDeclaredNames of FunctionBody" + && !intersects(names, plain_names(lex_names_list(a, ts, a.nodes.len() as int, body_ks, 0, true))) + // 15.1.1: "BoundNames contains any duplicate elements", when strict, non-simple, or unique + && ((strict || unique || !is_simple_params(a, params)) ==> !has_duplicate(names)) +} + +/// §15.7.1 the early errors of a `ClassBody`'s elements `ks`. +pub open spec fn class_body_early(a: AstV, ts: Stream, ks: Seq) -> bool { + // "the PrototypePropertyNameList of ClassElementList contains more than one occurrence of "constructor"" + (!exists|x: int, y: int| + 0 <= x < y < ks.len() && is_constructor_named(a, ts, #[trigger] ks[x]) && is_constructor_named( + a, + ts, + #[trigger] ks[y], + )) && (forall|x: int| + 0 <= x < ks.len() ==> { + let c = #[trigger] ks[x]; + let key = kid(kids(a, c), 0); + let is_static = flag_of(a, c); + // "PropName of MethodDefinition is "constructor" and SpecialMethod is true" + (is_constructor_named(a, ts, c) ==> op_of(a, c) == Op::Method) + // "the PropName of MethodDefinition is "prototype"" (static) + && !(kd(a, c) == NodeKind::MethodDefinition && is_static && prop_name_is( + a, + ts, + key, + "prototype"@, + )) + // "the PropName of FieldDefinition is "constructor"" / "prototype" or "constructor" (static) + && !(kd(a, c) == NodeKind::FieldDefinition && (prop_name_is( + a, + ts, + key, + "constructor"@, + ) || (is_static && prop_name_is(a, ts, key, "prototype"@)))) + // "the StringValue of PrivateIdentifier is "#constructor"" + && !(kd(a, key) == NodeKind::PrivateIdentifier && name_of(a, ts, key) + =~= sv("constructor"@)) + }) && (forall|x: int, y: int| + 0 <= x < y < ks.len() ==> { + let cx_ = #[trigger] ks[x]; + let cy = #[trigger] ks[y]; + let kx = kid(kids(a, cx_), 0); + let ky = kid(kids(a, cy), 0); + // "PrivateBoundIdentifiers of ClassElementList contains any duplicate entries, + // unless the name is used once for a getter and once for a setter and in no + // other entries, and the getter and setter are either both static or both non-static" + (kd(a, kx) == NodeKind::PrivateIdentifier && kd(a, ky) == NodeKind::PrivateIdentifier + && name_of(a, ts, kx) =~= name_of(a, ts, ky)) ==> (kd(a, cx_) + == NodeKind::MethodDefinition && kd(a, cy) == NodeKind::MethodDefinition && ( + (op_of(a, cx_) == Op::Getter && op_of(a, cy) == Op::Setter) || (op_of(a, cx_) + == Op::Setter && op_of(a, cy) == Op::Getter)) && flag_of(a, cx_) == flag_of(a, cy)) + }) +} + +} // verus! + +verus! { + +/// The names of the labels in `ls`. +pub open spec fn label_names(ls: Seq) -> Seq> { + ls.map_values(|l: EarlyLabel| l.name) +} + +/// The `#names` that the elements `ks` of a `ClassBody` declare (§15.7.8 +/// PrivateBoundIdentifiers). +pub open spec fn class_private_names(a: AstV, ts: Stream, ks: Seq) -> Seq> { + ks.filter(|c: int| kd(a, kid(kids(a, c), 0)) == NodeKind::PrivateIdentifier).map_values( + |c: int| name_of(a, ts, kid(kids(a, c), 0)), + ) +} + +/// §13.1.1 the early errors of an `IdentifierReference`, `BindingIdentifier` or +/// `LabelIdentifier` `c`. +pub open spec fn early_identifier_ok(a: AstV, ts: Stream, c: int, env: EarlyEnv) -> bool { + let nm = name_of(a, ts, c); + let is_yield = nm =~= sv("yield"@); + let is_await = nm =~= sv("await"@); + // `BindingIdentifier : Identifier`: strict mode `arguments` or `eval`. + !(kd(a, c) == NodeKind::BindingIdentifier && env.strict && (nm =~= sv("arguments"@) || nm + =~= sv("eval"@))) + // `Identifier : IdentifierName but not ReservedWord`: strict mode reserved words, and `yield` + // (`IdentifierReference : yield` and the others). + && !(env.strict && is_strict_reserved(nm)) + // "the goal symbol of the syntactic grammar is Module and the StringValue is await" + && !(env.module && is_await) + // "this production has a [Yield] parameter and the StringValue of Identifier is yield", and [Await] + && !(env.yield_ && is_yield) && !(env.await_ && is_await) + // "the StringValue of IdentifierName is the StringValue of any ReservedWord except for yield or await" + && !(is_reserved_text(nm) && !is_yield && !is_await) + // §15.7.1 ContainsArguments: `arguments` in a field initializer or static block + && !(kd(a, c) == NodeKind::IdentifierReference && env.no_arguments && nm + =~= sv("arguments"@)) +} + +/// §13.5.1.1 the `delete` rule: in strict mode code, `delete` of an +/// `IdentifierReference` or of a private member, through parentheses. +pub open spec fn delete_error(a: AstV, ts: Stream, operand: int, strict: bool) -> bool { + let o = strip_parens(a, operand); + let last = kid(kids(a, o), kids(a, o).len() as int - 1); + strict && (kd(a, o) == NodeKind::IdentifierReference || (kd(a, o) == NodeKind::MemberExpression + && op_of(a, o) == Op::PrivateDot) || (kd(a, o) == NodeKind::OptionalExpression && kd(a, last) + == NodeKind::OptionalChain && op_of(a, last) == Op::ChainPrivateDot)) +} + +/// §13.2.5.1 `PropertyDefinitionList`: the child `c` is a +/// `PropertyDefinition : PropertyName : AssignmentExpression` whose name is `__proto__`. +pub open spec fn is_proto_entry(a: AstV, ts: Stream, c: int) -> bool { + kd(a, c) == NodeKind::PropertyDefinition && op_of(a, c) == Op::KeyValue && prop_name_is( + a, + ts, + kid(kids(a, c), 0), + "__proto__"@, + ) +} + +/// §13.15.5.1 `DestructuringAssignmentTarget` and the rule for `[[ ]]` elements: an +/// assignment pattern, or a `LeftHandSideExpression` whose AssignmentTargetType is +/// `~simple~`. +pub open spec fn destructuring_target_ok(a: AstV, ts: Stream, c: int, strict: bool) -> bool { + is_assignment_pattern_kind(kd(a, c)) || target_type(a, ts, c, strict) == TargetType::Simple +} + +/// A private name `nm` is declared by an enclosing class (§15.7.? AllPrivateIdentifiersValid). +pub open spec fn private_declared(env: EarlyEnv, nm: Seq) -> bool { + has_name(env.privates, nm) +} + +/// The early errors of node `n` itself, in the environment `env`: every "It is a +/// Syntax Error if" rule of the clauses cited, for the production `n` is. +pub open spec fn early_local(a: AstV, ts: Stream, n: int, env: EarlyEnv) -> bool { + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let k3 = kid(ks, 3); + let tok = node_tok(a, ts, n); + match nd.kind { + // 13.1.1 Identifiers + NodeKind::IdentifierReference | NodeKind::BindingIdentifier | NodeKind::LabelIdentifier => + early_identifier_ok(a, ts, n, env), + // 12.9.3.1: "IsStrict(this production) is true" for `LegacyOctalIntegerLiteral` + // and `NonOctalDecimalIntegerLiteral`; 12.9.4.1 the same for the legacy escape + // sequences in a `StringLiteral`. + NodeKind::NumericLiteral | NodeKind::StringLiteral => !(env.strict && tok.legacy), + // 13.2.7.1: "IsValidRegularExpressionLiteral(RegularExpressionLiteral) is false" + NodeKind::RegularExpressionLiteral => tok.regexp_valid, + // 13.2.8.1: "the [Tagged] parameter was not set and ... Contains NotEscapeSequence" + NodeKind::TemplateElement => env.tagged || !tok.invalid_escape, + // 13.2.5.1: duplicate `__proto__`, and "PrivateBoundIdentifiers of MethodDefinition is not empty" + NodeKind::ObjectLiteral => (!exists|x: int, y: int| + 0 <= x < y < ks.len() && is_proto_entry(a, ts, #[trigger] ks[x]) && is_proto_entry( + a, + ts, + #[trigger] ks[y], + )) && (forall|x: int| + 0 <= x < ks.len() ==> !(kd(a, #[trigger] ks[x]) == NodeKind::MethodDefinition + && kd(a, kid(kids(a, ks[x]), 0)) == NodeKind::PrivateIdentifier)), + // 13.2.5.1: "CoverInitializedName : IdentifierReference Initializer: It is a Syntax Error + // if any source text is matched by this production" (an assignment pattern is its own node). + NodeKind::CoverInitializedName => false, + // 13.3.7.1: `super.x` only where `SuperProperty` is allowed, and `super()` in a derived constructor. + NodeKind::SuperProperty => env.super_prop, + NodeKind::SuperCall => env.super_call, + // 13.3.12.1 `NewTarget`, `ImportMeta` + NodeKind::NewTarget => env.new_target, + NodeKind::ImportMeta => env.module, + // 13.3.? private names: `a.#x`, `a?.#x`, `#x in a` need `#x` declared by an enclosing class. + NodeKind::MemberExpression => op_of(a, n) != Op::PrivateDot || private_declared(env, name_of( + a, + ts, + k1, + )), + // 13.3.1.1: "OptionalChain : ?. TemplateLiteral: It is a Syntax Error if any source text is matched". + NodeKind::OptionalChain => nd.op != Op::ChainTemplate && (nd.op != Op::ChainPrivateDot + || private_declared(env, name_of(a, ts, k0))), + NodeKind::RelationalExpression => !(kd(a, k0) == NodeKind::PrivateIdentifier) + || private_declared(env, name_of(a, ts, k0)), + // 13.5.1.1 `delete` + NodeKind::UnaryExpression => !(nd.op == Op::Delete && delete_error(a, ts, k0, env.strict)), + // 13.4.1: "the AssignmentTargetType of LeftHandSideExpression is ~invalid~" + NodeKind::UpdateExpression => target_type(a, ts, k0, env.strict) != TargetType::Invalid, + // 13.15.1: `=` and the other operators need a target that is not ~invalid~; `&&=`, `||=` and + // `??=` need ~simple~. An `ObjectAssignmentPattern` or `ArrayAssignmentPattern` is a valid + // target of `=`. + NodeKind::AssignmentExpression => (nd.op == Op::Assign && is_assignment_pattern_kind(kd(a, k0))) + || (target_type(a, ts, k0, env.strict) != TargetType::Invalid && ((nd.op + == Op::LogicalAndAssign || nd.op == Op::LogicalOrAssign || nd.op == Op::CoalesceAssign) + ==> target_type(a, ts, k0, env.strict) == TargetType::Simple)), + // 13.15.5.1 + NodeKind::AssignmentProperty => nd.op != Op::Shorthand || target_type( + a, + ts, + k0, + env.strict, + ) == TargetType::Simple, + NodeKind::AssignmentElement | NodeKind::AssignmentRestElement => destructuring_target_ok( + a, + ts, + k0, + env.strict, + ), + NodeKind::AssignmentRestProperty => !is_assignment_pattern_kind(kd(a, k0)) + && destructuring_target_ok(a, ts, k0, env.strict), + // 15.5.1 and the like: `yield` and `await` expressions are not allowed in parameters; + // 15.7.1 ContainsAwait: nor `await` in a static block. + NodeKind::YieldExpression | NodeKind::AwaitExpression => !env.no_yield_await, + // 14.2.1 Block + NodeKind::BlockStatement => list_early(a, ts, n, ks, env.strict, false), + // 14.3.1.1 LexicalDeclaration: no `let`, no duplicates, `const` has an initializer + NodeKind::LexicalDeclaration => { + let bn = bound_names(a, ts, n); + !has_name(bn, sv("let"@)) && !has_duplicate(bn) && (nd.op == Op::Const ==> forall|k: int| + 0 <= k < ks.len() ==> kd(a, kid(kids(a, #[trigger] ks[k]), 1)) != NodeKind::Absent) + }, + // 14.6.1, Annex B.3.3: `if` bodies are not labelled functions, and not functions in strict mode + NodeKind::IfStatement => !is_labelled_function(a, k1) && !is_labelled_function(a, k2) + && !(env.strict && (kd(a, k1) == NodeKind::FunctionDeclaration || kd(a, k2) + == NodeKind::FunctionDeclaration)), + // 14.7.2.1, 14.7.3.1 + NodeKind::DoWhileStatement => !is_labelled_function(a, k0), + NodeKind::WhileStatement => !is_labelled_function(a, k1), + // 14.7.4.1: "any element of the BoundNames of LexicalDeclaration also occurs in the + // VarDeclaredNames of Statement" + NodeKind::ForStatement => !is_labelled_function(a, k3) && (kd(a, k0) + == NodeKind::LexicalDeclaration ==> !intersects( + bound_names(a, ts, k0), + var_names_sub(a, ts, n, k3), + )), + // 14.7.5.1 and Annex B.3.5; 15.7.1 ContainsAwait: no `for await` in a static block. + NodeKind::ForInStatement | NodeKind::ForOfStatement => { + let bn = bound_names(a, ts, k0); + !(nd.kind == NodeKind::ForOfStatement && nd.flag && env.no_yield_await) + && !is_labelled_function(a, k2) && (if kd(a, k0) == NodeKind::ForDeclaration { + (op_of(a, k0) != Op::Var ==> (!has_name(bn, sv("let"@)) && !has_duplicate(bn) + && !intersects(bn, var_names_sub(a, ts, n, k2)))) && (first_kid_kind(a, k0) + == NodeKind::VariableDeclaration ==> (!env.strict && first_kid_kind( + a, + kid(kids(a, k0), 0), + ) == NodeKind::BindingIdentifier && kd(a, kid(kids(a, kid(kids(a, k0), 0)), 1)) + != NodeKind::Absent)) + } else { + is_assignment_pattern_kind(kd(a, k0)) || target_type(a, ts, k0, env.strict) + != TargetType::Invalid + }) + }, + // 14.8.1, 14.9.1: a `continue` is nested in an iteration statement, with that label; a `break` + // in an iteration or switch statement, or labelled. + NodeKind::ContinueStatement => if kd(a, k0) == NodeKind::LabelIdentifier { + exists|x: int| + 0 <= x < env.labels.len() && env.labels[x].name =~= name_of(a, ts, k0) + && env.labels[x].iteration + } else { + env.in_loop + }, + NodeKind::BreakStatement => if kd(a, k0) == NodeKind::LabelIdentifier { + has_name(label_names(env.labels), name_of(a, ts, k0)) + } else { + env.in_breakable + }, + // 14.11.1: `with` in strict mode code, a labelled function as its body + NodeKind::WithStatement => !env.strict && !is_labelled_function(a, k1), + // 14.12.1 CaseBlock + NodeKind::SwitchStatement => { + let lex = clause_lex_names(a, ts, n, ks, 1); + !has_forbidden_duplicate(lex, env.strict) && !intersects( + plain_names(lex), + var_names_list(a, ts, n, ks, 1), + ) + }, + // 14.13.1, ContainsDuplicateLabels, Annex B.3.1: a label is not repeated in a nest of labels; + // `LabelledItem : FunctionDeclaration` is not allowed in strict mode code. + NodeKind::LabelledStatement => !has_name(label_names(env.labels), name_of(a, ts, k0)) + && !(env.strict && kd(a, k1) == NodeKind::FunctionDeclaration), + // 14.15.1 Catch, Annex B.3.4 + NodeKind::Catch => { + let bn = bound_names(a, ts, k0); + let bk = kids(a, k1); + let nb = a.nodes.len() as int; + !has_duplicate(bn) && !intersects(bn, plain_names(lex_names_list(a, ts, nb, bk, 0, false))) + && (kd(a, k0) != NodeKind::BindingIdentifier ==> !intersects( + bn, + var_names_list(a, ts, nb, bk, 0), + )) + }, + // 15.2.1 and the like: the lists of statements of a function body, a static block + NodeKind::FunctionBody => list_early(a, ts, n, ks, env.strict, true), + // 15.2.1, 15.5.1, 15.6.1, 15.8.1: the function productions. `IsStrict` of the function + // code includes a Use Strict Directive in its own body. + NodeKind::FunctionDeclaration | NodeKind::FunctionExpression | NodeKind::GeneratorDeclaration + | NodeKind::GeneratorExpression | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncFunctionExpression | NodeKind::AsyncGeneratorDeclaration + | NodeKind::AsyncGeneratorExpression => function_early( + a, + ts, + k1, + kids(a, k2), + false, + env.strict || has_use_strict(a, ts, kids(a, k2)), + ), + // 15.3.1, 15.9.1: arrow functions (`UniqueFormalParameters` rules); a body that is an + // expression has no directive and no declarations. + NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction => { + let body_ks = if kd(a, k1) == NodeKind::FunctionBody { + kids(a, k1) + } else { + Seq::empty() + }; + function_early(a, ts, k0, body_ks, true, env.strict || has_use_strict(a, ts, body_ks)) + }, + // 15.4.1, 15.5.1, 15.6.1, 15.8.1: methods (`UniqueFormalParameters`). + NodeKind::MethodDefinition => function_early( + a, + ts, + k1, + kids(a, k2), + true, + env.strict || has_use_strict(a, ts, kids(a, k2)), + ), + // 15.7.1 ClassBody + NodeKind::ClassBody => class_body_early(a, ts, ks), + // 16.1.1 Script, 16.2.1.1 Module: names, and for a module its exports (16.2.3.1) + NodeKind::Script => list_early(a, ts, n, ks, env.strict, true), + NodeKind::Module => list_early(a, ts, n, ks, true, false), + // 16.2.2.1: "WithClauseToAttributes of WithClause has two different entries with the same [[Key]]" + NodeKind::WithClause => !has_duplicate( + ks.map_values(|c: int| name_of(a, ts, kid(kids(a, c), 0))), + ), + // 16.2.3.1: "ReferencedBindings of NamedExports contains any StringLiteral", or a reserved word + NodeKind::ExportDeclaration => nd.op != Op::ExportNamed || forall|x: int| + 0 <= x < kids(a, k0).len() ==> { + let loc = kid(kids(a, #[trigger] kids(a, k0)[x]), 0); + kd(a, loc) != NodeKind::StringLiteral && !is_reserved_text(name_of(a, ts, loc)) + && !is_strict_reserved(name_of(a, ts, loc)) + }, + _ => true, + } +} + +} // verus! + +verus! { + +/// The environment of the code of a function-like construct: `strict`, `y`ield and +/// `aw`ait as the kind says; no label, no loop; `sp`: `SuperProperty` allowed, +/// `sc`: `SuperCall` allowed, `nt`: `new.target` allowed, `na`: `arguments` +/// not allowed, `nya`: no yield or await expression (the parameters of +/// a generator, async function or arrow function). +pub open spec fn env_fn( + env: EarlyEnv, + strict: bool, + y: bool, + aw: bool, + sp: bool, + sc: bool, + nt: bool, + na: bool, + nya: bool, +) -> EarlyEnv { + EarlyEnv { + strict: strict, + module: env.module, + yield_: y, + await_: aw, + labels: Seq::empty(), + in_loop: false, + in_breakable: false, + super_prop: sp, + super_call: sc, + new_target: nt, + no_arguments: na, + no_yield_await: nya, + tagged: false, + privates: env.privates, + derived: env.derived, + } +} + +/// A generator or async generator function kind. +pub open spec fn is_generator_kind(k: NodeKind) -> bool { + k == NodeKind::GeneratorDeclaration || k == NodeKind::GeneratorExpression || k + == NodeKind::AsyncGeneratorDeclaration || k == NodeKind::AsyncGeneratorExpression +} + +/// An async function or async generator function kind. +pub open spec fn is_async_kind(k: NodeKind) -> bool { + k == NodeKind::AsyncFunctionDeclaration || k == NodeKind::AsyncFunctionExpression || k + == NodeKind::AsyncGeneratorDeclaration || k == NodeKind::AsyncGeneratorExpression +} + +/// A function expression kind (not a declaration). +pub open spec fn is_function_expression_kind(k: NodeKind) -> bool { + k == NodeKind::FunctionExpression || k == NodeKind::GeneratorExpression || k + == NodeKind::AsyncFunctionExpression || k == NodeKind::AsyncGeneratorExpression +} + +/// The environment in which child `k` of node `n` is checked, given `n`'s own. +/// It mirrors how [`d`] passes the grammar parameters, and adds what early +/// errors read of the position. +pub open spec fn child_env(a: AstV, ts: Stream, n: int, k: int, env: EarlyEnv) -> EarlyEnv { + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let c = kid(ks, k); + let kind = nd.kind; + let g = is_generator_kind(kind); + let aw = is_async_kind(kind); + // Strictness of a function's code: the enclosing code, or a Use Strict Directive of its body. + let fn_strict = env.strict || has_use_strict(a, ts, kids(a, k2)); + let arrow_strict = env.strict || (kd(a, k1) == NodeKind::FunctionBody && has_use_strict( + a, + ts, + kids(a, k1), + )); + match kind { + NodeKind::FunctionDeclaration | NodeKind::FunctionExpression | NodeKind::GeneratorDeclaration + | NodeKind::GeneratorExpression | NodeKind::AsyncFunctionDeclaration + | NodeKind::AsyncFunctionExpression | NodeKind::AsyncGeneratorDeclaration + | NodeKind::AsyncGeneratorExpression => if k == 0 { + // The name: part of the function code for `IsStrict`; a declaration's name has the + // enclosing `[Yield]` and `[Await]`. + EarlyEnv { + strict: fn_strict, + yield_: if is_function_expression_kind(kind) { + g + } else { + env.yield_ + }, + await_: if is_function_expression_kind(kind) { + aw + } else { + env.await_ + }, + ..env + } + } else { + env_fn(env, fn_strict, g, aw, false, false, true, false, k == 1) + }, + // 15.3 the parameters have the enclosing `[Yield]` and `[Await]`, the body neither. + NodeKind::ArrowFunction => if k == 0 { + env_fn(env, arrow_strict, env.yield_, env.await_, env.super_prop, env.super_call, env.new_target, env.no_arguments, true) + } else { + env_fn(env, arrow_strict, false, false, env.super_prop, env.super_call, env.new_target, env.no_arguments, false) + }, + // 15.9: `[~Yield, +Await]`. + NodeKind::AsyncArrowFunction => if k == 0 { + env_fn(env, arrow_strict, if kd(a, k0) == NodeKind::BindingIdentifier { + env.yield_ + } else { + false + }, true, env.super_prop, env.super_call, env.new_target, env.no_arguments, true) + } else { + env_fn(env, arrow_strict, false, true, env.super_prop, env.super_call, env.new_target, env.no_arguments, false) + }, + // 15.4 and the like: the name has the enclosing parameters; the function's code is + // `SuperProperty`-capable, and `SuperCall`-capable in a derived class's constructor. + NodeKind::MethodDefinition => if k == 0 { + env + } else { + let mg = nd.op == Op::GeneratorMethod || nd.op == Op::AsyncGeneratorMethod; + let ma = nd.op == Op::AsyncMethod || nd.op == Op::AsyncGeneratorMethod; + env_fn(env, fn_strict, mg, ma, true, env.derived && is_constructor_named(a, ts, n) + && nd.op == Op::Method, true, false, k == 1) + }, + // 15.7: a field initializer has the enclosing `[Yield]` and `[Await]`; `super.x` and `new.target`, not `arguments`. + NodeKind::FieldDefinition => if k == 1 { + env_fn(env, env.strict, env.yield_, env.await_, true, false, true, true, false) + } else { + env + }, + // 15.7 a static block: `[~Yield, +Await]`. 15.7.1 `ClassStaticBlockBody`: "It is a + // Syntax Error if ContainsAwait of ClassStaticBlockStatementList is true", and + // ContainsAwait, like `nya`, does not look inside functions or async arrow bodies. + NodeKind::ClassStaticBlock => env_fn(env, env.strict, false, true, true, false, true, true, true), + // 15.7: every part of a class is strict mode code; the heritage is outside the class's own + // private names, and a body is `derived` when there is a heritage. + NodeKind::ClassDeclaration | NodeKind::ClassExpression => EarlyEnv { + strict: true, + derived: if k == 2 { + kd(a, k1) != NodeKind::Absent + } else { + env.derived + }, + ..env + }, + NodeKind::ClassBody => EarlyEnv { + privates: env.privates + class_private_names(a, ts, ks), + ..env + }, + // 13.2.5: an object literal is not a class body; `super()` is not allowed in its methods. + NodeKind::ObjectLiteral => EarlyEnv { derived: false, ..env }, + // 14.7: the body of a loop. + NodeKind::DoWhileStatement => if k == 0 { + EarlyEnv { in_loop: true, in_breakable: true, ..env } + } else { + env + }, + NodeKind::WhileStatement => if k == 1 { + EarlyEnv { in_loop: true, in_breakable: true, ..env } + } else { + env + }, + NodeKind::ForStatement => if k == 3 { + EarlyEnv { in_loop: true, in_breakable: true, ..env } + } else { + env + }, + NodeKind::ForInStatement | NodeKind::ForOfStatement => if k == 2 { + EarlyEnv { in_loop: true, in_breakable: true, ..env } + } else { + env + }, + // 14.12: the clauses of a switch. + NodeKind::SwitchStatement => if k >= 1 { + EarlyEnv { in_breakable: true, ..env } + } else { + env + }, + // 14.13: the body of a labelled statement. + NodeKind::LabelledStatement => if k == 1 { + EarlyEnv { + labels: env.labels.push( + EarlyLabel { name: name_of(a, ts, k0), iteration: is_iteration_stmt(a, k1) }, + ), + ..env + } + } else { + env + }, + // 13.3.11: the template of a tagged template is `[+Tagged]`; the tag is not. + NodeKind::TaggedTemplate => EarlyEnv { tagged: k == 1, ..env }, + // 13.2.8: a substitution is not `[+Tagged]`, whatever the literal is. + NodeKind::TemplateLiteral => if kd(a, c) == NodeKind::TemplateElement { + env + } else { + EarlyEnv { tagged: false, ..env } + }, + _ => EarlyEnv { tagged: false, ..env }, + } +} + +/// Node `n` and every node below it meet the early errors, in the environment `env`. +pub open spec fn early(a: AstV, ts: Stream, n: int, env: EarlyEnv) -> bool + decreases n, +{ + 0 <= n < a.nodes.len() && early_local(a, ts, n, env) && (forall|k: int| + 0 <= k < a.nodes[n].children.len() ==> { + let c = #[trigger] a.nodes[n].children[k]; + 0 <= c < n && early(a, ts, c, child_env(a, ts, n, k, env)) + }) +} + +/// The environment of the goal symbol: a `Script` is strict if its Directive +/// Prologue has a Use Strict Directive; a `Module` always is (§11.2.2). +pub open spec fn root_env(a: AstV, ts: Stream, g: SourceGoal) -> EarlyEnv { + EarlyEnv { + strict: g == SourceGoal::Module || has_use_strict(a, ts, kids(a, a.root)), + module: g == SourceGoal::Module, + yield_: false, + await_: g == SourceGoal::Module, + labels: Seq::empty(), + in_loop: false, + in_breakable: false, + super_prop: false, + super_call: false, + new_target: false, + no_arguments: false, + no_yield_await: false, + tagged: false, + privates: Seq::empty(), + derived: false, + } +} + +/// The program `a`, derived from the token stream `ts`, has no early error. +pub open spec fn early_errors_ok(a: AstV, ts: Stream, g: SourceGoal) -> bool { + early(a, ts, a.root, root_env(a, ts, g)) +} + +/// `a` is a parse of the source text `src` as `g`, and has no early error: the +/// program is valid. This is the relation the parser's contract is stated by. +pub open spec fn is_valid_parse(src: Src, g: SourceGoal, a: AstV) -> bool { + exists|ts: Stream| + tokens_of::(src, ts, g) && derives_root(a, ts, g) && spans_wf(a, src.len() as int) + && early_errors_ok(a, ts, g) +} + +} // verus! + +verus! { + +// --------------------------------------------------------------------- +// The arena, read totally +// --------------------------------------------------------------------- + +/// Which goal symbol of §16 the source text is parsed as. +#[derive(Clone, Copy, PartialEq, Eq)] +pub enum SourceGoal { + /// `Script` (§16.1), with Annex B's HTML-like comments in the lexer. + Script, + /// `Module` (§16.2). + Module, +} + +/// The kind of node `c`; `Script` for an index outside the arena (a value no +/// positional check accepts, since [`ast_wf`] makes `Script` the root only). +pub open spec fn kd(a: AstV, c: int) -> NodeKind { + if 0 <= c < a.nodes.len() { + a.nodes[c].kind + } else { + NodeKind::Script + } +} + +/// The [`Op`] of node `c`, `Op::None` outside the arena. +pub open spec fn op_of(a: AstV, c: int) -> Op { + if 0 <= c < a.nodes.len() { + a.nodes[c].op + } else { + Op::None + } +} + +/// The `flag` of node `c`, `false` outside the arena. +pub open spec fn flag_of(a: AstV, c: int) -> bool { + 0 <= c < a.nodes.len() && a.nodes[c].flag +} + +/// The children of node `n`, none outside the arena. +pub open spec fn kids(a: AstV, n: int) -> Seq { + if 0 <= n < a.nodes.len() { + a.nodes[n].children + } else { + Seq::empty() + } +} + +/// `ks[k]`, or `-1` (no node) when `k` is out of range. +pub open spec fn kid(ks: Seq, k: int) -> int { + if 0 <= k < ks.len() { + ks[k] + } else { + -1 + } +} + +/// The kind of the first child of node `c`, `Script` if there is none. +pub open spec fn first_kid_kind(a: AstV, c: int) -> NodeKind { + kd(a, kid(kids(a, c), 0)) +} + +/// §14 `Statement`: the kinds a `Statement` is. +pub open spec fn is_statement_kind(k: NodeKind) -> bool { + k == NodeKind::BlockStatement || k == NodeKind::VariableStatement || k + == NodeKind::EmptyStatement || k == NodeKind::ExpressionStatement || k + == NodeKind::IfStatement || k == NodeKind::DoWhileStatement || k + == NodeKind::WhileStatement || k == NodeKind::ForStatement || k == NodeKind::ForInStatement + || k == NodeKind::ForOfStatement || k == NodeKind::ContinueStatement || k + == NodeKind::BreakStatement || k == NodeKind::ReturnStatement || k + == NodeKind::WithStatement || k == NodeKind::SwitchStatement || k + == NodeKind::LabelledStatement || k == NodeKind::ThrowStatement || k + == NodeKind::TryStatement || k == NodeKind::DebuggerStatement +} + +/// §15.2 `HoistableDeclaration`. +pub open spec fn is_hoistable_kind(k: NodeKind) -> bool { + k == NodeKind::FunctionDeclaration || k == NodeKind::GeneratorDeclaration || k + == NodeKind::AsyncFunctionDeclaration || k == NodeKind::AsyncGeneratorDeclaration +} + +/// §14 `Declaration`. +pub open spec fn is_declaration_kind(k: NodeKind) -> bool { + is_hoistable_kind(k) || k == NodeKind::ClassDeclaration || k == NodeKind::LexicalDeclaration +} + +/// §14.2 `StatementListItem`. +pub open spec fn is_item_kind(k: NodeKind) -> bool { + is_statement_kind(k) || is_declaration_kind(k) +} + +/// §16.2 `ModuleItem`. +pub open spec fn is_module_item_kind(k: NodeKind) -> bool { + is_item_kind(k) || k == NodeKind::ImportDeclaration || k == NodeKind::ExportDeclaration +} + +/// §14.3.3 `BindingPattern`. +pub open spec fn is_binding_pattern_kind(k: NodeKind) -> bool { + k == NodeKind::ObjectBindingPattern || k == NodeKind::ArrayBindingPattern +} + +/// A `BindingIdentifier` or a `BindingPattern`. +pub open spec fn is_binding_target_kind(k: NodeKind) -> bool { + k == NodeKind::BindingIdentifier || is_binding_pattern_kind(k) +} + +/// §13.15.5 `AssignmentPattern`. +pub open spec fn is_assignment_pattern_kind(k: NodeKind) -> bool { + k == NodeKind::ObjectAssignmentPattern || k == NodeKind::ArrayAssignmentPattern +} + +/// §13.2.5 `PropertyName`. +pub open spec fn is_prop_name_kind(k: NodeKind) -> bool { + k == NodeKind::IdentifierName || k == NodeKind::StringLiteral || k == NodeKind::NumericLiteral + || k == NodeKind::ComputedPropertyName +} + +/// §15.7 `ClassElementName`. +pub open spec fn is_class_element_name_kind(k: NodeKind) -> bool { + is_prop_name_kind(k) || k == NodeKind::PrivateIdentifier +} + +/// A kind that ends a list and takes no comma after it: `...rest`. +pub open spec fn is_rest_kind(k: NodeKind) -> bool { + k == NodeKind::FunctionRestParameter || k == NodeKind::BindingRestElement || k + == NodeKind::BindingRestProperty || k == NodeKind::AssignmentRestElement || k + == NodeKind::AssignmentRestProperty +} + +/// The binding strength of an expression node, the standard's chain of +/// productions in one number: how many of the productions between +/// `Expression` and `PrimaryExpression` the node is an instance of. +/// +/// `-1` `Expression` (comma); `0` `AssignmentExpression` and the +/// productions at its level, and `CoalesceExpression`; `1` `LogicalORExpression`; +/// `2` `LogicalANDExpression`; `3` `BitwiseORExpression`; `4` +/// `BitwiseXORExpression`; `5` `BitwiseANDExpression`; `6` +/// `EqualityExpression`; `7` `RelationalExpression`; `8` `ShiftExpression`; +/// `9` `AdditiveExpression`; `10` `MultiplicativeExpression`; `11` +/// `ExponentiationExpression`; `12` `UnaryExpression`; `13` +/// `UpdateExpression`; `14` `OptionalExpression`; `15` `CallExpression`; `16` +/// `NewExpression` without arguments; `17` `MemberExpression` and +/// `PrimaryExpression`. A node that is not an expression is `-100`. +/// +/// A production `X : Y | X op Z` allows an operand of level at least `Y`'s; +/// §13.6 to §13.13, §13.3 and §13.14 are written this way, and the checks in +/// [`d_expr`] read the levels off them. A `MemberExpression` or +/// `TaggedTemplate` node is a `MemberExpression` (17) when its object is, and +/// a `CallExpression` (15) when its object is a `CallExpression`. +pub open spec fn lvl(a: AstV, n: int) -> int + decreases n, +{ + if n < 0 || n >= a.nodes.len() { + -100 + } else { + let nd = a.nodes[n]; + match nd.kind { + NodeKind::Expression => -1, + NodeKind::ConditionalExpression | NodeKind::AssignmentExpression + | NodeKind::ArrowFunction | NodeKind::AsyncArrowFunction | NodeKind::YieldExpression + | NodeKind::CoalesceExpression => 0, + NodeKind::LogicalORExpression => 1, + NodeKind::LogicalANDExpression => 2, + NodeKind::BitwiseORExpression => 3, + NodeKind::BitwiseXORExpression => 4, + NodeKind::BitwiseANDExpression => 5, + NodeKind::EqualityExpression => 6, + NodeKind::RelationalExpression => 7, + NodeKind::ShiftExpression => 8, + NodeKind::AdditiveExpression => 9, + NodeKind::MultiplicativeExpression => 10, + NodeKind::ExponentiationExpression => 11, + NodeKind::UnaryExpression | NodeKind::AwaitExpression => 12, + NodeKind::UpdateExpression => 13, + NodeKind::OptionalExpression => 14, + NodeKind::CallExpression | NodeKind::SuperCall | NodeKind::ImportCall => 15, + NodeKind::NewExpression => if nd.children.len() == 2 && kd(a, nd.children[1]) + == NodeKind::Absent { + 16 + } else { + 17 + }, + NodeKind::MemberExpression | NodeKind::TaggedTemplate => if nd.children.len() > 0 + && 0 <= nd.children[0] < n && lvl(a, nd.children[0]) == 17 { + 17 + } else { + 15 + }, + NodeKind::IdentifierReference | NodeKind::ThisExpression | NodeKind::NullLiteral + | NodeKind::BooleanLiteral | NodeKind::NumericLiteral | NodeKind::StringLiteral + | NodeKind::RegularExpressionLiteral | NodeKind::TemplateLiteral + | NodeKind::ArrayLiteral | NodeKind::ObjectLiteral | NodeKind::ParenthesizedExpression + | NodeKind::FunctionExpression | NodeKind::GeneratorExpression + | NodeKind::AsyncFunctionExpression | NodeKind::AsyncGeneratorExpression + | NodeKind::ClassExpression | NodeKind::SuperProperty | NodeKind::ImportMeta + | NodeKind::NewTarget => 17, + _ => -100, + } + } +} + +/// An `AssignmentExpression` (level 0 or above). +pub open spec fn is_assign_expr(a: AstV, c: int) -> bool { + lvl(a, c) >= 0 +} + +/// A `MemberExpression` or a `CallExpression`: what can be the object of a +/// property access, the callee of a call or the tag of a template. +pub open spec fn is_member_or_call(a: AstV, c: int) -> bool { + lvl(a, c) == 17 || lvl(a, c) == 15 +} + +/// A `LeftHandSideExpression` (level 14 or above). +pub open spec fn is_lhs_expr(a: AstV, c: int) -> bool { + lvl(a, c) >= 14 +} + +/// §13.15.5 `DestructuringAssignmentTarget` and the target of an +/// assignment with `=`: a `LeftHandSideExpression` that is not an +/// `ObjectLiteral` or `ArrayLiteral` (those are covers, read as the +/// pattern), or an `AssignmentPattern`. +pub open spec fn is_destructuring_target(a: AstV, c: int) -> bool { + (is_lhs_expr(a, c) && kd(a, c) != NodeKind::ObjectLiteral && kd(a, c) != NodeKind::ArrayLiteral) + || is_assignment_pattern_kind(kd(a, c)) +} + +// --------------------------------------------------------------------- +// The shape of an arena +// --------------------------------------------------------------------- + +/// The arena is well formed: the root is the last node and the only +/// `Script` or `Module`; every child has a smaller index than its parent +/// (so the arena is a forest in post-order, and recursion on the index is +/// well-founded); every node but the root is a child of some node (no garbage). +pub open spec fn ast_wf(a: AstV) -> bool { + a.nodes.len() > 0 && a.root == a.nodes.len() - 1 && (forall|m: int| + 0 <= m < a.nodes.len() ==> { + let nd = #[trigger] a.nodes[m]; + (forall|k: int| + 0 <= k < nd.children.len() ==> 0 <= #[trigger] nd.children[k] < m) && ((nd.kind + == NodeKind::Script || nd.kind == NodeKind::Module) <==> m == a.root) + }) && (forall|m: int| 0 <= m < a.root ==> #[trigger] has_parent(a, m)) +} + +/// Node `m` is a child of some node. +pub open spec fn has_parent(a: AstV, m: int) -> bool { + exists|p: int, k: int| + m < p < a.nodes.len() && 0 <= k < a.nodes[p].children.len() + && #[trigger] a.nodes[p].children[k] == m +} + +/// Every span is within `0..len`, `start <= end`, and a child's span is within its +/// parent's: spans inside the input. +pub open spec fn spans_wf(a: AstV, len: int) -> bool { + forall|m: int| + 0 <= m < a.nodes.len() ==> { + let nd = #[trigger] a.nodes[m]; + 0 <= nd.start <= nd.end <= len && (forall|k: int| + 0 <= k < nd.children.len() ==> nd.start <= a.nodes[#[trigger] nd.children[k]].start + && a.nodes[nd.children[k]].end <= nd.end) + } +} + +/// The height of the tree under node `n`, in nodes: 1 for a leaf. The +/// recursion limit (item 7.3, "a listed deviation") is on this number. +pub open spec fn height(a: AstV, n: int) -> int + decreases n, 1int, +{ + if n < 0 || n >= a.nodes.len() { + 0 + } else { + 1 + max_height(a, n, a.nodes[n].children, 0) + } +} + +/// The largest height among `ks[k..]`, each of which is a child of `n`. +pub open spec fn max_height(a: AstV, n: int, ks: Seq, k: int) -> int + decreases n, 0int, ks.len() - k, +{ + if k < 0 || k >= ks.len() { + 0 + } else { + let c = ks[k]; + let h = if 0 <= c < n { + height(a, c) + } else { + 0 + }; + let rest = max_height(a, n, ks, k + 1); + if h > rest { + h + } else { + rest + } + } +} + +/// The deviation of phase-7.md item 7.3 ("Recursion: depth is bounded by an +/// explicit limit; deeper nesting is a `SyntaxError`, a listed deviation with its +/// number"): the parser rejects a program whose AST is higher than this. The +/// standard has no such limit; every program it accepts and whose parse is +/// at most this high is accepted, and the others get [`ParseError::TooDeep`] +/// or [`ParseError::Syntax`]. The height counts every node, so a chain of +/// binary operators `a + b + c + ...` is as high as it is long. +pub const MAX_DEPTH: usize = 1024; + +} // verus! + +verus! { + +// --------------------------------------------------------------------- +// The grammar, as a relation between nodes and tokens +// --------------------------------------------------------------------- +// +// `d(a, ts, n, cx, i, j)`: node `n` of the arena `a` is an instance of its +// production, in context `cx`, deriving exactly the tokens `ts.toks[i..j)`. +// Each node kind has one arm, written from the production(s) it is named +// after. A child is checked with `dc` at the range the production gives it, +// which starts where the production puts it and ends where the child's own +// span says (`ed`), so no arm guesses a position: where the production has +// fixed positions for optional symbols, an absent one is an `Absent` child +// with the empty range at its place. + +/// §13.1, §13.2.2 `Identifier`, `IdentifierReference[?Yield, ?Await]`, +/// `BindingIdentifier[?Yield, ?Await]` and `LabelIdentifier[?Yield, ?Await]` +/// at token `i`: an `IdentifierName` that is not a `ReservedWord`, or +/// `yield` when `[~Yield]`, or `await` when `[~Await]`. +pub open spec fn ident_tok(ts: Stream, i: int, cx: Ctx) -> bool { + is_name(ts, i) && (!is_reserved_token(ts.toks[i]) || (is_kw(ts, i, "yield"@) && !cx.yield_) + || (is_kw(ts, i, "await"@) && !cx.await_)) +} + +/// The text of the punctuator or keyword of an operator. +pub open spec fn op_text(op: Op) -> Seq { + match op { + Op::Delete => "delete"@, + Op::Void => "void"@, + Op::Typeof => "typeof"@, + Op::Plus => "+"@, + Op::Minus => "-"@, + Op::BitNot => "~"@, + Op::LogicalNot => "!"@, + Op::PrefixIncrement | Op::PostfixIncrement => "++"@, + Op::PrefixDecrement | Op::PostfixDecrement => "--"@, + Op::Exp => "**"@, + Op::Mul => "*"@, + Op::Div => "/"@, + Op::Rem => "%"@, + Op::Add => "+"@, + Op::Sub => "-"@, + Op::Shl => "<<"@, + Op::Shr => ">>"@, + Op::UShr => ">>>"@, + Op::Lt => "<"@, + Op::Gt => ">"@, + Op::Le => "<="@, + Op::Ge => ">="@, + Op::InstanceOf => "instanceof"@, + Op::In => "in"@, + Op::Eq => "=="@, + Op::Ne => "!="@, + Op::StrictEq => "==="@, + Op::StrictNe => "!=="@, + Op::BitAnd => "&"@, + Op::BitXor => "^"@, + Op::BitOr => "|"@, + Op::LogicalAnd => "&&"@, + Op::LogicalOr => "||"@, + Op::Coalesce => "??"@, + Op::Assign => "="@, + Op::MulAssign => "*="@, + Op::DivAssign => "/="@, + Op::RemAssign => "%="@, + Op::AddAssign => "+="@, + Op::SubAssign => "-="@, + Op::ShlAssign => "<<="@, + Op::ShrAssign => ">>="@, + Op::UShrAssign => ">>>="@, + Op::BitAndAssign => "&="@, + Op::BitXorAssign => "^="@, + Op::BitOrAssign => "|="@, + Op::ExpAssign => "**="@, + Op::LogicalAndAssign => "&&="@, + Op::LogicalOrAssign => "||="@, + Op::CoalesceAssign => "??="@, + _ => ""@, + } +} + +/// The operator `op` is at token `i`: a keyword for `delete`, `void`, +/// `typeof`, `instanceof` and `in`, a punctuator for the others. +pub open spec fn is_op_tok(ts: Stream, i: int, op: Op) -> bool { + if op == Op::Delete || op == Op::Void || op == Op::Typeof || op == Op::InstanceOf || op + == Op::In { + is_kw(ts, i, op_text(op)) + } else { + is_p(ts, i, op_text(op)) + } +} + +/// §13.5 the `UnaryOperator`s. +pub open spec fn is_unary_op(op: Op) -> bool { + op == Op::Delete || op == Op::Void || op == Op::Typeof || op == Op::Plus || op == Op::Minus + || op == Op::BitNot || op == Op::LogicalNot +} + +/// §13.15 `AssignmentOperator`, and `=`. +pub open spec fn is_assign_op(op: Op) -> bool { + op == Op::Assign || op == Op::MulAssign || op == Op::DivAssign || op == Op::RemAssign || op + == Op::AddAssign || op == Op::SubAssign || op == Op::ShlAssign || op == Op::ShrAssign + || op == Op::UShrAssign || op == Op::BitAndAssign || op == Op::BitXorAssign || op + == Op::BitOrAssign || op == Op::ExpAssign || op == Op::LogicalAndAssign || op + == Op::LogicalOrAssign || op == Op::CoalesceAssign +} + +/// §13.6 to §13.13: the operators of a binary expression production. +pub open spec fn bin_op_ok(k: NodeKind, op: Op) -> bool { + match k { + NodeKind::ExponentiationExpression => op == Op::Exp, + NodeKind::MultiplicativeExpression => op == Op::Mul || op == Op::Div || op == Op::Rem, + NodeKind::AdditiveExpression => op == Op::Add || op == Op::Sub, + NodeKind::ShiftExpression => op == Op::Shl || op == Op::Shr || op == Op::UShr, + NodeKind::RelationalExpression => op == Op::Lt || op == Op::Gt || op == Op::Le || op + == Op::Ge || op == Op::InstanceOf || op == Op::In, + NodeKind::EqualityExpression => op == Op::Eq || op == Op::Ne || op == Op::StrictEq || op + == Op::StrictNe, + NodeKind::BitwiseANDExpression => op == Op::BitAnd, + NodeKind::BitwiseXORExpression => op == Op::BitXor, + NodeKind::BitwiseORExpression => op == Op::BitOr, + NodeKind::LogicalANDExpression => op == Op::LogicalAnd, + NodeKind::LogicalORExpression => op == Op::LogicalOr, + NodeKind::CoalesceExpression => op == Op::Coalesce, + _ => false, + } +} + +/// The operands of a binary expression, by the productions of §13.6 to +/// §13.13: `X : Y | X op Z` (left associative) takes a left operand that is +/// an `X` and a right operand that is a `Z`; the levels are [`lvl`]'s. +/// §13.6 `ExponentiationExpression : UpdateExpression ** ExponentiationExpression` +/// (the left operand is not a `UnaryExpression`, and the operator is right +/// associative). §13.10 `RelationalExpression : [+In] PrivateIdentifier in +/// ShiftExpression`. §13.13 `CoalesceExpression : CoalesceExpressionHead ?? +/// BitwiseORExpression`, where the head is a `CoalesceExpression` or a +/// `BitwiseORExpression`: `??` never mixes with `&&` or `||` unparenthesized. +pub open spec fn binary_operands_ok(a: AstV, k: NodeKind, op: Op, l: int, r: int) -> bool { + match k { + NodeKind::ExponentiationExpression => lvl(a, l) >= 13 && lvl(a, r) >= 11, + NodeKind::MultiplicativeExpression => lvl(a, l) >= 10 && lvl(a, r) >= 11, + NodeKind::AdditiveExpression => lvl(a, l) >= 9 && lvl(a, r) >= 10, + NodeKind::ShiftExpression => lvl(a, l) >= 8 && lvl(a, r) >= 9, + NodeKind::RelationalExpression => (lvl(a, l) >= 7 || (op == Op::In && kd(a, l) + == NodeKind::PrivateIdentifier)) && lvl(a, r) >= 8, + NodeKind::EqualityExpression => lvl(a, l) >= 6 && lvl(a, r) >= 7, + NodeKind::BitwiseANDExpression => lvl(a, l) >= 5 && lvl(a, r) >= 6, + NodeKind::BitwiseXORExpression => lvl(a, l) >= 4 && lvl(a, r) >= 5, + NodeKind::BitwiseORExpression => lvl(a, l) >= 3 && lvl(a, r) >= 4, + NodeKind::LogicalANDExpression => lvl(a, l) >= 2 && lvl(a, r) >= 3, + NodeKind::LogicalORExpression => lvl(a, l) >= 1 && lvl(a, r) >= 2, + NodeKind::CoalesceExpression => (lvl(a, l) >= 3 || kd(a, l) == NodeKind::CoalesceExpression) + && lvl(a, r) >= 3, + _ => false, + } +} + +/// The index just past the tokens of child `c` of a node, when the child starts +/// at token `p`: the token whose end is the end of `c`'s span, if `c` has a +/// span; `p` itself if it is empty. The span of a node is part of the AST, +/// so this is not a guess: the grammar's checks apply to the range it gives, +/// and a node whose span is not the range of tokens it derives is not a +/// derivation (`span_ok`). +pub open spec fn ed(a: AstV, ts: Stream, c: int, p: int) -> int { + if c < 0 || c >= a.nodes.len() || a.nodes[c].start >= a.nodes[c].end { + p + } else { + 1 + choose|x: int| p <= x < tn(ts) && ts.toks[x].end == a.nodes[c].end + } +} + +/// The source range of node `n` is the range of tokens `ts[i..j)`: it starts at +/// the first token and ends at the last; a node with no tokens is an empty +/// range in the gap before token `i`, from the end of the token before to the +/// start of token `i` (or at the end of the last token when `i` is past it). +/// The gap, not one point in it: an `Absent` first child must sit inside its +/// parent, which starts at token `i`, and an `Absent` last child inside its +/// parent, which ends at token `i - 1`; with white space between them no one +/// point is in both (`spans_wf`). +pub open spec fn span_ok(a: AstV, ts: Stream, n: int, i: int, j: int) -> bool { + if i == j { + a.nodes[n].end == a.nodes[n].start && prev_end(ts, i) <= a.nodes[n].start && (if 0 <= i + < tn(ts) { + a.nodes[n].start <= ts.toks[i].start + } else { + a.nodes[n].start == prev_end(ts, i) + }) + } else { + a.nodes[n].start == ts.toks[i].start && a.nodes[n].end == ts.toks[j - 1].end + } +} + +/// §12.10 the `;` that ends a production, whose last child ends at token `m` +/// (the production's range ends at `j`): an explicit `;` at `m` (`j == m + 1`), +/// or one inserted in the gap before token `m` (`j == m`, nothing consumed). `dw` +/// also accepts the `;` of §12.10.1 rule 1 (third condition), inserted after a `do`-`while`. +pub open spec fn semi(ts: Stream, m: int, j: int, dw: bool) -> bool { + (is_p(ts, m, ";"@) && j == m + 1) || (0 <= m < ts.gaps.len() && j == m && (ts.gaps[m] + == Ins::Semicolon || (dw && ts.gaps[m] == Ins::DoWhile))) +} + +/// Node `n` derives `ts[i..j)` in context `cx`. +pub open spec fn d(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 4int, 0int, 0int, +{ + 0 <= n < a.nodes.len() && 0 <= i <= j <= tn(ts) && span_ok(a, ts, n, i, j) && (d_stmt( + a, + ts, + n, + cx, + i, + j, + ) || d_func(a, ts, n, cx, i, j) || d_pat(a, ts, n, cx, i, j) || d_expr(a, ts, n, cx, i, j) + || d_module(a, ts, n, cx, i, j)) +} + +/// Child `c` of a node whose index is `bound` derives `ts[i..j)`. +pub open spec fn dc(a: AstV, ts: Stream, bound: int, c: int, cx: Ctx, i: int, j: int) -> bool + decreases bound, 0int, 0int, 0int, +{ + 0 <= c < bound && d(a, ts, c, cx, i, j) +} + +/// The children `ks[k..]` derive consecutive ranges that make `ts[i..j)`, +/// with nothing between them (the statements of a `StatementList`). +pub open spec fn dl( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + j: int, +) -> bool + decreases bound, 1int, ks.len() - k, 0int, +{ + if k >= ks.len() { + i == j + } else { + let m = ed(a, ts, ks[k], i); + dc(a, ts, bound, ks[k], cx, i, m) && dl(a, ts, bound, ks, k + 1, cx, m, j) + } +} + +/// The index after the last child of the list `ks[k..]` of [`dsep`] when its +/// first starts at `i`, before any trailing `,`; `i` for an empty list. +pub open spec fn list_next(a: AstV, ts: Stream, ks: Seq, k: int, i: int) -> int + decreases ks.len() - k, +{ + if k >= ks.len() { + i + } else if k + 1 == ks.len() { + ed(a, ts, ks[k], i) + } else { + list_next(a, ts, ks, k + 1, ed(a, ts, ks[k], i) + 1) + } +} + +/// The children `ks[k..]` are separated by `,` and, when `trail` and the last is +/// not a `...rest`, may be followed by one more `,`: the shape of +/// `ArgumentList`, `PropertyDefinitionList`, `FormalParameterList`, `BindingList` +/// and the like. An empty list derives the empty range. +pub open spec fn dsep( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + j: int, + trail: bool, +) -> bool + decreases bound, 1int, ks.len() - k, 0int, +{ + if k >= ks.len() { + i == j + } else { + let m = ed(a, ts, ks[k], i); + dc(a, ts, bound, ks[k], cx, i, m) && (if k + 1 < ks.len() { + is_p(ts, m, ","@) && dsep(a, ts, bound, ks, k + 1, cx, m + 1, j, trail) + } else { + j == m || (trail && !is_rest_kind(kd(a, ks[k])) && is_p(ts, m, ","@) && j == m + 1) + }) + } +} + +/// §13.2.4 `ElementList` with `Elision`s, and §14.3.3 `BindingElementList`: the +/// children are elements and [`NodeKind::Elision`]s (each one `,`, a hole). +/// After an element comes a `,` if anything follows (the elements' separator, which +/// for a hole is the hole's own comma), or, last, an optional `,`; after +/// an `Elision` comes the next child directly. +pub open spec fn darray( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + j: int, +) -> bool + decreases bound, 1int, ks.len() - k, 0int, +{ + if k >= ks.len() { + i == j + } else { + let m = ed(a, ts, ks[k], i); + dc(a, ts, bound, ks[k], cx, i, m) && (if kd(a, ks[k]) == NodeKind::Elision { + darray(a, ts, bound, ks, k + 1, cx, m, j) + } else if k + 1 < ks.len() { + is_p(ts, m, ","@) && darray(a, ts, bound, ks, k + 1, cx, m + 1, j) + } else { + j == m || (!is_rest_kind(kd(a, ks[k])) && is_p(ts, m, ","@) && j == m + 1) + }) + } +} + +/// §13.2.8 `TemplateLiteral`: the children alternate `TemplateElement`, +/// `Expression`, ..., `TemplateElement`; a template component is one token, +/// whose lexical kind is `Head` first, `Middle` between, and `Tail` last, or a +/// lone `NoSubstitution`. +pub open spec fn dtemplate( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + j: int, +) -> bool + decreases bound, 1int, ks.len() - k, 0int, +{ + let ce = kid(ks, k + 1); + let e = ed(a, ts, ce, i + 1); + if k + 1 == ks.len() { + // The last component: a `NoSubstitutionTemplate` or a `TemplateTail`. + j == i + 1 && kd(a, ks[k]) == NodeKind::TemplateElement && dc(a, ts, bound, ks[k], cx, i, j) + && (if k == 0 { + is_kind(ts, i, TokKind::Template(TemplateKind::NoSubstitution)) + } else { + is_kind(ts, i, TokKind::Template(TemplateKind::Tail)) + }) + } else if k + 1 < ks.len() { + // `TemplateHead` or `TemplateMiddle`, an `Expression`, and the rest. + kd(a, ks[k]) == NodeKind::TemplateElement && dc(a, ts, bound, ks[k], cx, i, i + 1) + && (if k == 0 { + is_kind(ts, i, TokKind::Template(TemplateKind::Head)) + } else { + is_kind(ts, i, TokKind::Template(TemplateKind::Middle)) + }) && lvl(a, ce) >= -1 && dc(a, ts, bound, ce, cx, i + 1, e) && dtemplate( + a, + ts, + bound, + ks, + k + 2, + cx, + e, + j, + ) + } else { + false + } +} + +/// §15.7 `ClassElementList`: the elements `ks[k..]` with any number of `;` +/// (the `ClassElement : ;` alternative) before, between and after them. +pub open spec fn dclass( + a: AstV, + ts: Stream, + bound: int, + ks: Seq, + k: int, + cx: Ctx, + i: int, + j: int, +) -> bool + decreases bound, 1int, ks.len() - k, j - i, +{ + let m = ed(a, ts, kid(ks, k), i); + (k >= ks.len() && i == j) || (i < j && is_p(ts, i, ";"@) && dclass(a, ts, bound, ks, k, cx, i + 1, j)) + || (k < ks.len() && dc(a, ts, bound, ks[k], cx, i, m) && dclass(a, ts, bound, ks, k + 1, cx, m, j)) +} + +/// Every child in `ks` is a `StatementListItem`. +pub open spec fn all_items(a: AstV, ks: Seq) -> bool { + forall|k: int| 0 <= k < ks.len() ==> is_item_kind(kd(a, #[trigger] ks[k])) +} + +/// Every child in `ks` from `from` on is of kind `x` or `y`. +pub open spec fn all_kind2(a: AstV, ks: Seq, from: int, x: NodeKind, y: NodeKind) -> bool { + forall|k: int| + from <= k < ks.len() ==> kd(a, #[trigger] ks[k]) == x || kd(a, ks[k]) == y +} + +/// Every child in `ks` is an `AssignmentExpression` or a `SpreadElement`: the +/// items of an `ArgumentList`. +pub open spec fn all_args(a: AstV, ks: Seq) -> bool { + forall|k: int| + 0 <= k < ks.len() ==> is_assign_expr(a, #[trigger] ks[k]) || kd(a, ks[k]) + == NodeKind::SpreadElement +} + +/// A `...rest` child is the last of its list. +pub open spec fn rest_only_last(a: AstV, ks: Seq) -> bool { + forall|k: int| 0 <= k + 1 < ks.len() ==> !is_rest_kind(kd(a, #[trigger] ks[k])) +} + +/// An optional `Expression`: absent, or an `Expression`. +pub open spec fn opt_expr(a: AstV, c: int) -> bool { + kd(a, c) == NodeKind::Absent || lvl(a, c) >= -1 +} + +/// The optional `Initializer` of node child `c` at token `m`, ending at +/// `j`: `Absent` (when not `req`d) or `= AssignmentExpression`. +pub open spec fn d_init( + a: AstV, + ts: Stream, + bound: int, + c: int, + cx: Ctx, + m: int, + j: int, + req: bool, +) -> bool + decreases bound, 1int, 0int, 0int, +{ + (!req && kd(a, c) == NodeKind::Absent && j == m && dc(a, ts, bound, c, cx, m, m)) || (is_p( + ts, + m, + "="@, + ) && is_assign_expr(a, c) && dc(a, ts, bound, c, cx, m + 1, j)) +} + +} // verus! + +verus! { + +/// §14.7.4 `for ( ...`: the token after the initializer and what ends it: +/// the `;` of `Expression ;`, `;` and `var VariableDeclarationList ;`, or +/// nothing, for a `LexicalDeclaration`, which holds its own `;`. +pub open spec fn for_init_next(a: AstV, ts: Stream, c: int, s: int) -> int { + if kd(a, c) == NodeKind::LexicalDeclaration { + ed(a, ts, c, s) + } else { + ed(a, ts, c, s) + 1 + } +} + +/// §14.7.4 `for ( ...` up to and including what follows the initializer: +/// `LexicalDeclaration` (always a real `;`: §12.10.1 rule 4), `var +/// VariableDeclarationList ;` or `Expression ;` or `;`, with `[~In]`. +pub open spec fn for_init_ok(a: AstV, ts: Stream, bound: int, c: int, cx: Ctx, s: int) -> bool + decreases bound, 1int, 0int, 0int, +{ + let cxi = cx_in(cx, false); + let m = ed(a, ts, c, s); + dc(a, ts, bound, c, cxi, s, m) && (if kd(a, c) == NodeKind::LexicalDeclaration { + is_p(ts, m - 1, ";"@) + } else { + is_p(ts, m, ";"@) && (kd(a, c) == NodeKind::Absent || kd(a, c) + == NodeKind::VariableDeclarationList || (lvl(a, c) >= -1 && !(is_kw(ts, s, "let"@) + && is_p(ts, s + 1, "["@)))) + }) +} + +/// The head of a `for ( ... in/of` loop, at token `s`: a `ForDeclaration`, or +/// a `LeftHandSideExpression` (not an `ObjectLiteral` or `ArrayLiteral`, which +/// are the covers of an assignment pattern) or an `AssignmentPattern`, subject to +/// the lookahead of §14.7.5 (`of`: `[lookahead ∉ { let, async of }]`, `for await`: +/// `[lookahead ≠ let]`, `in`: `[lookahead ≠ let []`). +pub open spec fn for_head_ok(a: AstV, ts: Stream, c: int, s: int, of: bool, is_await: bool) -> bool { + kd(a, c) == NodeKind::ForDeclaration || (is_destructuring_target(a, c) && if of { + if is_await { + !is_kw(ts, s, "let"@) + } else { + !is_kw(ts, s, "let"@) && !(is_kw(ts, s, "async"@) && is_kw(ts, s + 1, "of"@)) + } + } else { + !(is_kw(ts, s, "let"@) && is_p(ts, s + 1, "["@)) + }) +} + +/// §14.12 a `CaseBlock` has at most one `DefaultClause`. +pub open spec fn at_most_one_default(a: AstV, ks: Seq) -> bool { + forall|x: int, y: int| + 1 <= x < y < ks.len() ==> !(kd(a, #[trigger] ks[x]) == NodeKind::DefaultClause && kd( + a, + #[trigger] ks[y], + ) == NodeKind::DefaultClause) +} + +/// A `LabelledItem` or the body of an `if`: a `Statement`, or a +/// `FunctionDeclaration` (§14.13 `LabelledItem`; Annex B.3.3). +pub open spec fn is_stmt_or_function(a: AstV, c: int) -> bool { + is_statement_kind(kd(a, c)) || kd(a, c) == NodeKind::FunctionDeclaration +} + +/// §14 statements, declarations and the roots (§16.1.1, §16.2.1): the arms of +/// [`d`] for `Script`, `Module` and the kinds of §14. +pub open spec fn d_stmt(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 3int, 0int, 0int, +{ + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let k3 = kid(ks, 3); + let cxt = cx_in(cx, true); + match nd.kind { + // 16.1 `Script : ScriptBody?`, `ScriptBody : StatementList[~Yield, ~Await, ~Return]`. + NodeKind::Script => i == 0 && j == tn(ts) && all_items(a, ks) && dl(a, ts, n, ks, 0, cx, i, j), + // 16.2 `Module : ModuleBody?`, `ModuleItemList : ModuleItem | ModuleItemList ModuleItem`. + NodeKind::Module => i == 0 && j == tn(ts) && (forall|k: int| + 0 <= k < ks.len() ==> is_module_item_kind(kd(a, #[trigger] ks[k]))) && dl( + a, + ts, + n, + ks, + 0, + cx, + i, + j, + ), + // 14.2 `Block : { StatementList? }`. + NodeKind::BlockStatement => i + 1 < j && is_p(ts, i, "{"@) && is_p(ts, j - 1, "}"@) + && all_items(a, ks) && dl(a, ts, n, ks, 0, cx, i + 1, j - 1), + // 14.3.2 `VariableStatement : var VariableDeclarationList[+In] ;` + NodeKind::VariableStatement => { + let m = list_next(a, ts, ks, 0, i + 1); + is_kw(ts, i, "var"@) && ks.len() > 0 && (forall|k: int| + 0 <= k < ks.len() ==> kd(a, #[trigger] ks[k]) == NodeKind::VariableDeclaration) + && dsep(a, ts, n, ks, 0, cxt, i + 1, m, false) && semi(ts, m, j, false) + }, + // 14.3.2 as the head of a `for ( var ... )`, `[?In]`. + NodeKind::VariableDeclarationList => is_kw(ts, i, "var"@) && ks.len() > 0 && (forall|k: int| + 0 <= k < ks.len() ==> kd(a, #[trigger] ks[k]) == NodeKind::VariableDeclaration) + && dsep(a, ts, n, ks, 0, cx, i + 1, j, false), + // 14.3.2 `VariableDeclaration : BindingIdentifier Initializer? | BindingPattern Initializer`. + NodeKind::VariableDeclaration => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && is_binding_target_kind(kd(a, k0)) && i < m && dc(a, ts, n, k0, cx, i, m) + && d_init(a, ts, n, k1, cx, m, j, is_binding_pattern_kind(kd(a, k0))) + }, + // 14.3.1 `LexicalDeclaration : LetOrConst BindingList ;` + NodeKind::LexicalDeclaration => { + let m = list_next(a, ts, ks, 0, i + 1); + (if nd.op == Op::Let { + is_kw(ts, i, "let"@) + } else { + nd.op == Op::Const && is_kw(ts, i, "const"@) + }) && ks.len() > 0 && (forall|k: int| + 0 <= k < ks.len() ==> kd(a, #[trigger] ks[k]) == NodeKind::LexicalBinding) + && dsep(a, ts, n, ks, 0, cx, i + 1, m, false) && semi(ts, m, j, false) + }, + // 14.3.1 `LexicalBinding : BindingIdentifier Initializer? | BindingPattern Initializer`. + NodeKind::LexicalBinding => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && is_binding_target_kind(kd(a, k0)) && i < m && dc(a, ts, n, k0, cx, i, m) + && d_init(a, ts, n, k1, cx, m, j, is_binding_pattern_kind(kd(a, k0))) + }, + // 14.4 `EmptyStatement : ;`: a real `;` (12.10.1 rule 4: never an inserted one). + NodeKind::EmptyStatement => ks.len() == 0 && j == i + 1 && is_p(ts, i, ";"@), + // 14.5 `ExpressionStatement : [lookahead ∉ { {, function, async [no LineTerminator + // here] function, class, let [ }] Expression[+In] ;` + NodeKind::ExpressionStatement => { + let m = ed(a, ts, k0, i); + ks.len() == 1 && !is_p(ts, i, "{"@) && !is_kw(ts, i, "function"@) && !is_kw( + ts, + i, + "class"@, + ) && !(is_kw(ts, i, "async"@) && is_kw(ts, i + 1, "function"@) && !nl(ts, i + 1)) + && !(is_kw(ts, i, "let"@) && is_p(ts, i + 1, "["@)) && lvl(a, k0) >= -1 && i < m + && dc(a, ts, n, k0, cxt, i, m) && semi(ts, m, j, false) + }, + // 14.6 `IfStatement : if ( Expression[+In] ) Statement else Statement | if ( Expression[+In] ) Statement`; + // Annex B.3.3: a `FunctionDeclaration` in place of each `Statement`. + NodeKind::IfStatement => { + let m = ed(a, ts, k0, i + 2); + let q = ed(a, ts, k1, m + 1); + ks.len() == 3 && is_kw(ts, i, "if"@) && is_p(ts, i + 1, "("@) && lvl(a, k0) >= -1 + && is_stmt_or_function(a, k1) && dc(a, ts, n, k0, cxt, i + 2, m) && is_p(ts, m, ")"@) + && dc(a, ts, n, k1, cx, m + 1, q) && ((kd(a, k2) == NodeKind::Absent && j == q && dc( + a, + ts, + n, + k2, + cx, + q, + q, + )) || (is_kw(ts, q, "else"@) && is_stmt_or_function(a, k2) && dc( + a, + ts, + n, + k2, + cx, + q + 1, + j, + ))) + }, + // 14.7.2 `do Statement while ( Expression[+In] ) ;`, with §12.10.1 rule 1, third condition. + NodeKind::DoWhileStatement => { + let m = ed(a, ts, k0, i + 1); + let q = ed(a, ts, k1, m + 2); + ks.len() == 2 && is_kw(ts, i, "do"@) && is_statement_kind(kd(a, k0)) && lvl(a, k1) >= -1 + && dc(a, ts, n, k0, cx, i + 1, m) && is_kw(ts, m, "while"@) && is_p(ts, m + 1, "("@) + && dc(a, ts, n, k1, cxt, m + 2, q) && is_p(ts, q, ")"@) && semi(ts, q + 1, j, true) + }, + // 14.7.3 `while ( Expression[+In] ) Statement`. + NodeKind::WhileStatement => { + let m = ed(a, ts, k0, i + 2); + ks.len() == 2 && is_kw(ts, i, "while"@) && is_p(ts, i + 1, "("@) && lvl(a, k0) >= -1 + && is_statement_kind(kd(a, k1)) && dc(a, ts, n, k0, cxt, i + 2, m) && is_p( + ts, + m, + ")"@, + ) && dc(a, ts, n, k1, cx, m + 1, j) + }, + // 14.7.4 `for ( Expression[~In]? ; Expression[+In]? ; Expression[+In]? ) Statement`, + // `for ( var VariableDeclarationList[~In] ; ... )` and + // `for ( LexicalDeclaration[~In] Expression[+In]? ; Expression[+In]? ) Statement`. + NodeKind::ForStatement => { + let p0 = for_init_next(a, ts, k0, i + 2); + let q = ed(a, ts, k1, p0); + let r = ed(a, ts, k2, q + 1); + ks.len() == 4 && is_kw(ts, i, "for"@) && is_p(ts, i + 1, "("@) && is_statement_kind( + kd(a, k3), + ) && for_init_ok(a, ts, n, k0, cx, i + 2) && opt_expr(a, k1) && dc( + a, + ts, + n, + k1, + cxt, + p0, + q, + ) && is_p(ts, q, ";"@) && opt_expr(a, k2) && dc(a, ts, n, k2, cxt, q + 1, r) && is_p( + ts, + r, + ")"@, + ) && dc(a, ts, n, k3, cx, r + 1, j) + }, + // 14.7.5 `for ( [lookahead ≠ let [] LeftHandSideExpression in Expression[+In] ) Statement`, + // `for ( var ForBinding in ... )`, `for ( ForDeclaration in ... )`, and Annex B.3.5 + // `for ( var BindingIdentifier Initializer in Expression ) Statement`. + NodeKind::ForInStatement => { + let m = ed(a, ts, k0, i + 2); + let q = ed(a, ts, k1, m + 1); + ks.len() == 3 && is_kw(ts, i, "for"@) && is_p(ts, i + 1, "("@) && for_head_ok( + a, + ts, + k0, + i + 2, + false, + false, + ) && lvl(a, k1) >= -1 && is_statement_kind(kd(a, k2)) && dc( + a, + ts, + n, + k0, + cx_in(cx, false), + i + 2, + m, + ) && is_kw(ts, m, "in"@) && dc(a, ts, n, k1, cxt, m + 1, q) && is_p(ts, q, ")"@) && dc( + a, + ts, + n, + k2, + cx, + q + 1, + j, + ) + }, + // 14.7.5 `for ( [lookahead ∉ { let, async of }] LeftHandSideExpression of AssignmentExpression[+In] ) Statement`, + // `for ( var ForBinding of ... )`, `for ( ForDeclaration of ... )`, and, with + // `[+Await]`, `for await ( ... of ... )`. + NodeKind::ForOfStatement => { + let s = if nd.flag { + i + 3 + } else { + i + 2 + }; + let m = ed(a, ts, k0, s); + let q = ed(a, ts, k1, m + 1); + ks.len() == 3 && is_kw(ts, i, "for"@) && (nd.flag ==> (cx.await_ && is_kw( + ts, + i + 1, + "await"@, + ))) && is_p(ts, s - 1, "("@) && for_head_ok(a, ts, k0, s, true, nd.flag) + && first_kid_kind(a, k0) != NodeKind::VariableDeclaration && is_assign_expr(a, k1) + && is_statement_kind(kd(a, k2)) && dc(a, ts, n, k0, cx_in(cx, false), s, m) && is_kw( + ts, + m, + "of"@, + ) && dc(a, ts, n, k1, cxt, m + 1, q) && is_p(ts, q, ")"@) && dc(a, ts, n, k2, cx, q + 1, j) + }, + // 14.7.5 `ForDeclaration : LetOrConst ForBinding`, `for ( var ForBinding`. + NodeKind::ForDeclaration => ks.len() == 1 && (if nd.op == Op::Var { + is_kw(ts, i, "var"@) + } else if nd.op == Op::Let { + is_kw(ts, i, "let"@) + } else { + nd.op == Op::Const && is_kw(ts, i, "const"@) + }) && (is_binding_target_kind(kd(a, k0)) || (nd.op == Op::Var && kd(a, k0) + == NodeKind::VariableDeclaration)) && dc(a, ts, n, k0, cx, i + 1, j), + // 14.8 `continue ;` and `continue [no LineTerminator here] LabelIdentifier ;` + NodeKind::ContinueStatement => ks.len() == 1 && is_kw(ts, i, "continue"@) && (if kd(a, k0) + == NodeKind::LabelIdentifier { + !nl(ts, i + 1) && dc(a, ts, n, k0, cx, i + 1, i + 2) && semi(ts, i + 2, j, false) + } else { + kd(a, k0) == NodeKind::Absent && dc(a, ts, n, k0, cx, i + 1, i + 1) && semi( + ts, + i + 1, + j, + false, + ) + }), + // 14.9 `break ;` and `break [no LineTerminator here] LabelIdentifier ;` + NodeKind::BreakStatement => ks.len() == 1 && is_kw(ts, i, "break"@) && (if kd(a, k0) + == NodeKind::LabelIdentifier { + !nl(ts, i + 1) && dc(a, ts, n, k0, cx, i + 1, i + 2) && semi(ts, i + 2, j, false) + } else { + kd(a, k0) == NodeKind::Absent && dc(a, ts, n, k0, cx, i + 1, i + 1) && semi( + ts, + i + 1, + j, + false, + ) + }), + // 14.10 `return ;` and `return [no LineTerminator here] Expression[+In] ;`, `[+Return]`. + NodeKind::ReturnStatement => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 1 && cx.return_ && is_kw(ts, i, "return"@) && (if kd(a, k0) + == NodeKind::Absent { + dc(a, ts, n, k0, cx, i + 1, i + 1) && semi(ts, i + 1, j, false) + } else { + !nl(ts, i + 1) && lvl(a, k0) >= -1 && dc(a, ts, n, k0, cxt, i + 1, m) && semi( + ts, + m, + j, + false, + ) + }) + }, + // 14.11 `with ( Expression[+In] ) Statement` + NodeKind::WithStatement => { + let m = ed(a, ts, k0, i + 2); + ks.len() == 2 && is_kw(ts, i, "with"@) && is_p(ts, i + 1, "("@) && lvl(a, k0) >= -1 + && is_statement_kind(kd(a, k1)) && dc(a, ts, n, k0, cxt, i + 2, m) && is_p( + ts, + m, + ")"@, + ) && dc(a, ts, n, k1, cx, m + 1, j) + }, + // 14.12 `switch ( Expression[+In] ) CaseBlock`, `CaseBlock : { CaseClauses? }` + // and `{ CaseClauses? DefaultClause CaseClauses? }`: at most one `DefaultClause`. + NodeKind::SwitchStatement => { + let m = ed(a, ts, k0, i + 2); + ks.len() >= 1 && is_kw(ts, i, "switch"@) && is_p(ts, i + 1, "("@) && lvl(a, k0) >= -1 + && all_kind2(a, ks, 1, NodeKind::CaseClause, NodeKind::DefaultClause) + && at_most_one_default(a, ks) && dc(a, ts, n, k0, cxt, i + 2, m) && is_p( + ts, + m, + ")"@, + ) && is_p(ts, m + 1, "{"@) && is_p(ts, j - 1, "}"@) && m + 2 <= j - 1 && dl( + a, + ts, + n, + ks, + 1, + cx, + m + 2, + j - 1, + ) + }, + // 14.12 `CaseClause : case Expression[+In] : StatementList?` + NodeKind::CaseClause => { + let m = ed(a, ts, k0, i + 1); + ks.len() >= 1 && is_kw(ts, i, "case"@) && lvl(a, k0) >= -1 && (forall|k: int| + 1 <= k < ks.len() ==> is_item_kind(kd(a, #[trigger] ks[k]))) && dc( + a, + ts, + n, + k0, + cxt, + i + 1, + m, + ) && is_p(ts, m, ":"@) && dl(a, ts, n, ks, 1, cx, m + 1, j) + }, + // 14.12 `DefaultClause : default : StatementList?` + NodeKind::DefaultClause => is_kw(ts, i, "default"@) && is_p(ts, i + 1, ":"@) && all_items( + a, + ks, + ) && dl(a, ts, n, ks, 0, cx, i + 2, j), + // 14.13 `LabelledStatement : LabelIdentifier : LabelledItem`, + // `LabelledItem : Statement | FunctionDeclaration` + NodeKind::LabelledStatement => ks.len() == 2 && kd(a, k0) == NodeKind::LabelIdentifier + && is_p(ts, i + 1, ":"@) && dc(a, ts, n, k0, cx, i, i + 1) && is_stmt_or_function(a, k1) + && dc(a, ts, n, k1, cx, i + 2, j), + // 14.14 `throw [no LineTerminator here] Expression[+In] ;` + NodeKind::ThrowStatement => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 1 && is_kw(ts, i, "throw"@) && !nl(ts, i + 1) && lvl(a, k0) >= -1 && dc( + a, + ts, + n, + k0, + cxt, + i + 1, + m, + ) && semi(ts, m, j, false) + }, + // 14.15 `TryStatement : try Block Catch | try Block Finally | try Block Catch Finally` + NodeKind::TryStatement => { + let m = ed(a, ts, k0, i + 1); + let q = ed(a, ts, k1, m); + ks.len() == 3 && is_kw(ts, i, "try"@) && kd(a, k0) == NodeKind::BlockStatement && (kd( + a, + k1, + ) == NodeKind::Catch || kd(a, k1) == NodeKind::Absent) && (kd(a, k2) + == NodeKind::Finally || kd(a, k2) == NodeKind::Absent) && (kd(a, k1) + == NodeKind::Catch || kd(a, k2) == NodeKind::Finally) && dc(a, ts, n, k0, cx, i + 1, m) + && dc(a, ts, n, k1, cx, m, q) && dc(a, ts, n, k2, cx, q, j) + }, + // 14.15 `Catch : catch ( CatchParameter ) Block | catch Block` + NodeKind::Catch => { + let m = ed(a, ts, k0, i + 2); + ks.len() == 2 && is_kw(ts, i, "catch"@) && kd(a, k1) == NodeKind::BlockStatement && ( + if kd(a, k0) == NodeKind::Absent { + dc(a, ts, n, k0, cx, i + 1, i + 1) && dc(a, ts, n, k1, cx, i + 1, j) + } else { + is_binding_target_kind(kd(a, k0)) && is_p(ts, i + 1, "("@) && dc(a, ts, n, k0, cx, i + 2, m) + && is_p(ts, m, ")"@) && dc(a, ts, n, k1, cx, m + 1, j) + }) + }, + // 14.15 `Finally : finally Block` + NodeKind::Finally => ks.len() == 1 && is_kw(ts, i, "finally"@) && kd(a, k0) + == NodeKind::BlockStatement && dc(a, ts, n, k0, cx, i + 1, j), + // 14.16 `DebuggerStatement : debugger ;` + NodeKind::DebuggerStatement => ks.len() == 0 && is_kw(ts, i, "debugger"@) && semi( + ts, + i + 1, + j, + false, + ), + _ => false, + } +} + +} // verus! + +verus! { + +/// §15.2, §15.5, §15.6, §15.8: the function-like productions, one function +/// for the eight kinds. `is_async` has `async` and no line terminator before +/// `function`; `is_gen` has `*` after it; `is_expr` is the expression form +/// (the name is optional, and has the function's own `[Yield]` and `[Await]`). +/// The parameters get `[Yield]` and `[Await]` as the kind says (`[~Yield, +/// ~Await]` for `FunctionDeclaration` and `FunctionExpression`, and so on), and +/// the body is `[+Return]`. +pub open spec fn d_function( + a: AstV, + ts: Stream, + n: int, + cx: Ctx, + i: int, + j: int, + is_async: bool, + is_gen: bool, + is_expr: bool, +) -> bool + decreases n, 1int, 0int, 0int, +{ + let ks = kids(a, n); + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let s = if is_async { + i + 1 + } else { + i + }; + let g = s + 1; + let nm = if is_gen { + g + 1 + } else { + g + }; + let cxn = if is_expr { + cx_fn(is_gen, is_async, false) + } else { + cx + }; + let m = ed(a, ts, k0, nm); + let q = ed(a, ts, k1, m); + ks.len() == 3 && (!is_async || (is_kw(ts, i, "async"@) && !nl(ts, i + 1))) && is_kw( + ts, + s, + "function"@, + ) && (!is_gen || is_p(ts, g, "*"@)) && ( + // `export default function () {}` has `[+Default]`; an expression's name is optional. + if kd(a, k0) == NodeKind::Absent { + is_expr || cx.deflt + } else { + kd(a, k0) == NodeKind::BindingIdentifier + }) && dc(a, ts, n, k0, cxn, nm, m) && kd(a, k1) == NodeKind::FormalParameters && dc( + a, + ts, + n, + k1, + cx_fn(is_gen, is_async, false), + m, + q, + ) && kd(a, k2) == NodeKind::FunctionBody && dc(a, ts, n, k2, cx_fn(is_gen, is_async, true), q, j) +} + +/// §15.4 `MethodDefinition`, §15.5 `GeneratorMethod`, §15.8 `AsyncMethod`, §15.6 +/// `AsyncGeneratorMethod`, with the `static` of a `ClassElement` (§15.7). +pub open spec fn d_method(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 1int, 0int, 0int, +{ + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let p0 = if nd.flag { + i + 1 + } else { + i + }; + // The index where the `ClassElementName` starts. + let nm = match nd.op { + Op::Method => p0, + Op::Getter | Op::Setter | Op::GeneratorMethod | Op::AsyncMethod => p0 + 1, + _ => p0 + 2, + }; + let is_gen = nd.op == Op::GeneratorMethod || nd.op == Op::AsyncGeneratorMethod; + let is_async = nd.op == Op::AsyncMethod || nd.op == Op::AsyncGeneratorMethod; + let m = ed(a, ts, k0, nm); + let q = ed(a, ts, k1, m); + ks.len() == 3 && (!nd.flag || is_kw(ts, i, "static"@)) && (match nd.op { + Op::Method => true, + Op::Getter => is_kw(ts, p0, "get"@), + Op::Setter => is_kw(ts, p0, "set"@), + Op::GeneratorMethod => is_p(ts, p0, "*"@), + Op::AsyncMethod => is_kw(ts, p0, "async"@) && !nl(ts, p0 + 1), + Op::AsyncGeneratorMethod => is_kw(ts, p0, "async"@) && !nl(ts, p0 + 1) && is_p( + ts, + p0 + 1, + "*"@, + ), + _ => false, + }) && is_class_element_name_kind(kd(a, k0)) && kd(a, k1) == NodeKind::FormalParameters + && kd(a, k2) == NodeKind::FunctionBody && (nd.op == Op::Getter ==> kids(a, k1).len() == 0) + && (nd.op == Op::Setter ==> (kids(a, k1).len() == 1 && kd(a, kid(kids(a, k1), 0)) + == NodeKind::BindingElement)) && dc(a, ts, n, k0, cx, nm, m) && dc( + a, + ts, + n, + k1, + cx_fn(is_gen, is_async, false), + m, + q, + ) && dc(a, ts, n, k2, cx_fn(is_gen, is_async, true), q, j) +} + +/// §15.7 `ClassDeclaration`, `ClassExpression`: `class BindingIdentifier? +/// ClassTail`, `ClassTail : ClassHeritage? { ClassBody? }`, `ClassHeritage : +/// extends LeftHandSideExpression`. +pub open spec fn d_class(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int, is_expr: bool) -> bool + decreases n, 1int, 0int, 0int, +{ + let ks = kids(a, n); + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let m = ed(a, ts, k0, i + 1); + let hs = if kd(a, k1) == NodeKind::Absent { + m + } else { + m + 1 + }; + let h = ed(a, ts, k1, hs); + ks.len() == 3 && is_kw(ts, i, "class"@) && kd(a, k2) == NodeKind::ClassBody && (if kd(a, k0) + == NodeKind::Absent { + is_expr || cx.deflt + } else { + kd(a, k0) == NodeKind::BindingIdentifier + }) && dc(a, ts, n, k0, cx, i + 1, m) && (kd(a, k1) == NodeKind::Absent || (is_lhs_expr(a, k1) + && is_kw(ts, m, "extends"@))) && dc(a, ts, n, k1, cx, hs, h) && dc(a, ts, n, k2, cx, h, j) +} + +/// §15 the arms of [`d`] for functions, classes, parameters and bodies. +pub open spec fn d_func(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 3int, 0int, 0int, +{ + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + match nd.kind { + NodeKind::FunctionDeclaration => d_function(a, ts, n, cx, i, j, false, false, false), + NodeKind::FunctionExpression => d_function(a, ts, n, cx, i, j, false, false, true), + NodeKind::GeneratorDeclaration => d_function(a, ts, n, cx, i, j, false, true, false), + NodeKind::GeneratorExpression => d_function(a, ts, n, cx, i, j, false, true, true), + NodeKind::AsyncFunctionDeclaration => d_function(a, ts, n, cx, i, j, true, false, false), + NodeKind::AsyncFunctionExpression => d_function(a, ts, n, cx, i, j, true, false, true), + NodeKind::AsyncGeneratorDeclaration => d_function(a, ts, n, cx, i, j, true, true, false), + NodeKind::AsyncGeneratorExpression => d_function(a, ts, n, cx, i, j, true, true, true), + // 15.1 `FormalParameters : [empty] | FunctionRestParameter | FormalParameterList + // | FormalParameterList , | FormalParameterList , FunctionRestParameter` + NodeKind::FormalParameters => i + 1 < j && is_p(ts, i, "("@) && is_p(ts, j - 1, ")"@) + && all_kind2(a, ks, 0, NodeKind::BindingElement, NodeKind::FunctionRestParameter) + && rest_only_last(a, ks) && dsep(a, ts, n, ks, 0, cx, i + 1, j - 1, true), + // 15.1 `FunctionRestParameter : BindingRestElement` + NodeKind::FunctionRestParameter => ks.len() == 1 && is_p(ts, i, "..."@) && is_binding_target_kind( + kd(a, k0), + ) && dc(a, ts, n, k0, cx, i + 1, j), + // 15.1 `FunctionBody : FunctionStatementList`, `{ ... }` + NodeKind::FunctionBody => i + 1 < j && is_p(ts, i, "{"@) && is_p(ts, j - 1, "}"@) + && all_items(a, ks) && dl(a, ts, n, ks, 0, cx, i + 1, j - 1), + // 15.3 `ArrowFunction : ArrowParameters[?Yield, ?Await] [no LineTerminator here] => + // ConciseBody[?In]`; `ConciseBody : [lookahead ≠ {] ExpressionBody[?In, ~Await] | + // { FunctionBody[~Yield, ~Await] }`; `ArrowParameters : BindingIdentifier | ArrowFormalParameters`. + NodeKind::ArrowFunction => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && (kd(a, k0) == NodeKind::BindingIdentifier || kd(a, k0) + == NodeKind::FormalParameters) && i < m && dc(a, ts, n, k0, cx, i, m) && is_p( + ts, + m, + "=>"@, + ) && !nl(ts, m) && (if kd(a, k1) == NodeKind::FunctionBody { + dc(a, ts, n, k1, cx_fn(false, false, true), m + 1, j) + } else { + is_assign_expr(a, k1) && !is_p(ts, m + 1, "{"@) && dc( + a, + ts, + n, + k1, + Ctx { yield_: false, await_: false, in_: cx.in_, return_: false, deflt: false }, + m + 1, + j, + ) + }) + }, + // 15.9 `AsyncArrowFunction : async [no LineTerminator here] AsyncArrowBindingIdentifier[?Yield] + // [no LineTerminator here] => AsyncConciseBody[?In]`, `AsyncArrowHead : async [no + // LineTerminator here] ArrowFormalParameters[~Yield, +Await]`. + NodeKind::AsyncArrowFunction => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 2 && is_kw(ts, i, "async"@) && !nl(ts, i + 1) && (kd(a, k0) + == NodeKind::BindingIdentifier || kd(a, k0) == NodeKind::FormalParameters) && i + 1 + < m && dc(a, ts, n, k0, if kd(a, k0) == NodeKind::BindingIdentifier { + Ctx { yield_: cx.yield_, await_: true, in_: true, return_: false, deflt: false } + } else { + cx_fn(false, true, false) + }, i + 1, m) && is_p(ts, m, "=>"@) && !nl(ts, m) && (if kd(a, k1) + == NodeKind::FunctionBody { + dc(a, ts, n, k1, cx_fn(false, true, true), m + 1, j) + } else { + is_assign_expr(a, k1) && !is_p(ts, m + 1, "{"@) && dc( + a, + ts, + n, + k1, + Ctx { yield_: false, await_: true, in_: cx.in_, return_: false, deflt: false }, + m + 1, + j, + ) + }) + }, + NodeKind::MethodDefinition => d_method(a, ts, n, cx, i, j), + NodeKind::ClassDeclaration => d_class(a, ts, n, cx, i, j, false), + NodeKind::ClassExpression => d_class(a, ts, n, cx, i, j, true), + // 15.7 `ClassBody : ClassElementList`, `{ ... }` + NodeKind::ClassBody => i + 1 < j && is_p(ts, i, "{"@) && is_p(ts, j - 1, "}"@) && (forall|k: int| + 0 <= k < ks.len() ==> kd(a, #[trigger] ks[k]) == NodeKind::MethodDefinition || kd( + a, + ks[k], + ) == NodeKind::FieldDefinition || kd(a, ks[k]) == NodeKind::ClassStaticBlock) + && dclass(a, ts, n, ks, 0, cx, i + 1, j - 1), + // 15.7 `FieldDefinition : ClassElementName Initializer[+In, ?Yield, ?Await]?`, + // `ClassElement : static? FieldDefinition ;` + NodeKind::FieldDefinition => { + let s = if nd.flag { + i + 1 + } else { + i + }; + let m = ed(a, ts, k0, s); + let q = if kd(a, k1) == NodeKind::Absent { + m + } else { + ed(a, ts, k1, m + 1) + }; + ks.len() == 2 && (!nd.flag || is_kw(ts, i, "static"@)) && is_class_element_name_kind( + kd(a, k0), + ) && s < m && dc(a, ts, n, k0, cx, s, m) && d_init( + a, + ts, + n, + k1, + cx_in(cx, true), + m, + q, + false, + ) && semi(ts, q, j, false) + }, + // 15.7 `ClassStaticBlock : static { ClassStaticBlockBody }`, + // `ClassStaticBlockStatementList : StatementList[~Yield, +Await, ~Return]?` + NodeKind::ClassStaticBlock => ks.len() == 1 && is_kw(ts, i, "static"@) && kd(a, k0) + == NodeKind::FunctionBody && dc( + a, + ts, + n, + k0, + Ctx { yield_: false, await_: true, in_: true, return_: false, deflt: false }, + i + 1, + j, + ), + _ => false, + } +} + +} // verus! + +verus! { + +/// §13.15.5 and §14.3.3: the arms of [`d`] for binding patterns and assignment +/// patterns. +pub open spec fn d_pat(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 3int, 0int, 0int, +{ + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let cxt = cx_in(cx, true); + match nd.kind { + // 14.3.3 `ObjectBindingPattern : { } | { BindingRestProperty } | { BindingPropertyList } + // | { BindingPropertyList , BindingRestProperty? }` + NodeKind::ObjectBindingPattern => i + 1 < j && is_p(ts, i, "{"@) && is_p(ts, j - 1, "}"@) + && all_kind2(a, ks, 0, NodeKind::BindingProperty, NodeKind::BindingRestProperty) + && rest_only_last(a, ks) && dsep(a, ts, n, ks, 0, cx, i + 1, j - 1, true), + // 14.3.3 `ArrayBindingPattern : [ Elision? BindingRestElement? ]`, `[ BindingElementList ]`, ... + NodeKind::ArrayBindingPattern => i + 1 < j && is_p(ts, i, "["@) && is_p(ts, j - 1, "]"@) + && (forall|k: int| + 0 <= k < ks.len() ==> kd(a, #[trigger] ks[k]) == NodeKind::BindingElement || kd( + a, + ks[k], + ) == NodeKind::Elision || kd(a, ks[k]) == NodeKind::BindingRestElement) + && rest_only_last(a, ks) && darray(a, ts, n, ks, 0, cx, i + 1, j - 1), + // 14.3.3 `BindingProperty : SingleNameBinding | PropertyName : BindingElement` + NodeKind::BindingProperty => if nd.op == Op::Shorthand { + ks.len() == 1 && kd(a, k0) == NodeKind::BindingElement && first_kid_kind(a, k0) + == NodeKind::BindingIdentifier && dc(a, ts, n, k0, cx, i, j) + } else { + let m = ed(a, ts, k0, i); + nd.op == Op::KeyValue && ks.len() == 2 && is_prop_name_kind(kd(a, k0)) && kd(a, k1) + == NodeKind::BindingElement && i < m && dc(a, ts, n, k0, cx, i, m) && is_p( + ts, + m, + ":"@, + ) && dc(a, ts, n, k1, cx, m + 1, j) + }, + // 14.3.3 `BindingElement : SingleNameBinding | BindingPattern Initializer?`, + // `SingleNameBinding : BindingIdentifier Initializer[+In]?` + NodeKind::BindingElement => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && is_binding_target_kind(kd(a, k0)) && i < m && dc(a, ts, n, k0, cx, i, m) + && d_init(a, ts, n, k1, cxt, m, j, false) + }, + // 14.3.3 `BindingRestElement : ... BindingIdentifier | ... BindingPattern` + NodeKind::BindingRestElement => ks.len() == 1 && is_p(ts, i, "..."@) + && is_binding_target_kind(kd(a, k0)) && dc(a, ts, n, k0, cx, i + 1, j), + // 14.3.3 `BindingRestProperty : ... BindingIdentifier` + NodeKind::BindingRestProperty => ks.len() == 1 && is_p(ts, i, "..."@) && kd(a, k0) + == NodeKind::BindingIdentifier && dc(a, ts, n, k0, cx, i + 1, j), + // 13.15.5 `ObjectAssignmentPattern : { } | { AssignmentRestProperty } | + // { AssignmentPropertyList } | { AssignmentPropertyList , AssignmentRestProperty? }` + NodeKind::ObjectAssignmentPattern => i + 1 < j && is_p(ts, i, "{"@) && is_p(ts, j - 1, "}"@) + && all_kind2(a, ks, 0, NodeKind::AssignmentProperty, NodeKind::AssignmentRestProperty) + && rest_only_last(a, ks) && dsep(a, ts, n, ks, 0, cx, i + 1, j - 1, true), + // 13.15.5 `ArrayAssignmentPattern : [ Elision? AssignmentRestElement? ]`, ... + NodeKind::ArrayAssignmentPattern => i + 1 < j && is_p(ts, i, "["@) && is_p(ts, j - 1, "]"@) + && (forall|k: int| + 0 <= k < ks.len() ==> kd(a, #[trigger] ks[k]) == NodeKind::AssignmentElement || kd( + a, + ks[k], + ) == NodeKind::Elision || kd(a, ks[k]) == NodeKind::AssignmentRestElement) + && rest_only_last(a, ks) && darray(a, ts, n, ks, 0, cx, i + 1, j - 1), + // 13.15.5 `AssignmentProperty : IdentifierReference Initializer[+In]? | + // PropertyName : AssignmentElement` + NodeKind::AssignmentProperty => if nd.op == Op::Shorthand { + ks.len() == 2 && kd(a, k0) == NodeKind::IdentifierReference && dc(a, ts, n, k0, cx, i, i + 1) + && d_init(a, ts, n, k1, cxt, i + 1, j, false) + } else { + let m = ed(a, ts, k0, i); + nd.op == Op::KeyValue && ks.len() == 2 && is_prop_name_kind(kd(a, k0)) && kd(a, k1) + == NodeKind::AssignmentElement && i < m && dc(a, ts, n, k0, cx, i, m) && is_p( + ts, + m, + ":"@, + ) && dc(a, ts, n, k1, cx, m + 1, j) + }, + // 13.15.5 `AssignmentElement : DestructuringAssignmentTarget Initializer[+In]?` + NodeKind::AssignmentElement => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && is_destructuring_target(a, k0) && i < m && dc(a, ts, n, k0, cx, i, m) + && d_init(a, ts, n, k1, cxt, m, j, false) + }, + // 13.15.5 `AssignmentRestElement : ... DestructuringAssignmentTarget` + NodeKind::AssignmentRestElement => ks.len() == 1 && is_p(ts, i, "..."@) + && is_destructuring_target(a, k0) && dc(a, ts, n, k0, cx, i + 1, j), + // 13.15.5 `AssignmentRestProperty : ... DestructuringAssignmentTarget` + NodeKind::AssignmentRestProperty => ks.len() == 1 && is_p(ts, i, "..."@) + && is_destructuring_target(a, k0) && dc(a, ts, n, k0, cx, i + 1, j), + // 13.2.4 `Elision : ,` + NodeKind::Elision => ks.len() == 0 && j == i + 1 && is_p(ts, i, ","@), + _ => false, + } +} + +} // verus! + +verus! { + +/// §13.3.9 one link of an `OptionalChain`, starting at token `i`: `?.` (when +/// `flag`) or nothing (the link continues the chain), then the access. +pub open spec fn d_chain_link(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 1int, 0int, 0int, +{ + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + // The token after the `?.`, if the link has one. + let s = if nd.flag { + i + 1 + } else { + i + }; + ks.len() == 1 && (nd.flag ==> is_p(ts, i, "?."@)) && match nd.op { + // `OptionalChain : ?. IdentifierName | OptionalChain . IdentifierName` + Op::ChainDot => (nd.flag || is_p(ts, i, "."@)) && kd(a, k0) == NodeKind::IdentifierName + && j == i + 2 && dc(a, ts, n, k0, cx, i + 1, j), + // `OptionalChain : ?. PrivateIdentifier | OptionalChain . PrivateIdentifier` + Op::ChainPrivateDot => (nd.flag || is_p(ts, i, "."@)) && kd(a, k0) + == NodeKind::PrivateIdentifier && j == i + 2 && dc(a, ts, n, k0, cx, i + 1, j), + // `OptionalChain : ?. [ Expression[+In] ] | OptionalChain [ Expression[+In] ]` + Op::ChainBracket => { + let q = ed(a, ts, k0, s + 1); + is_p(ts, s, "["@) && lvl(a, k0) >= -1 && dc(a, ts, n, k0, cx_in(cx, true), s + 1, q) + && is_p(ts, q, "]"@) && j == q + 1 + }, + // `OptionalChain : ?. Arguments | OptionalChain Arguments` + Op::ChainCall => kd(a, k0) == NodeKind::Arguments && dc(a, ts, n, k0, cx, s, j), + // `OptionalChain : ?. TemplateLiteral[+Tagged] | OptionalChain TemplateLiteral[+Tagged]`: + // in the grammar so that §12.10 does not apply; always a Syntax Error (13.3.1.1). + Op::ChainTemplate => kd(a, k0) == NodeKind::TemplateLiteral && dc(a, ts, n, k0, cx, s, j), + _ => false, + } +} + +/// §13.2, §13.3: the arms of [`d`] for primary, member, call and optional chain +/// expressions. +pub open spec fn d_expr_lhs(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 2int, 0int, 0int, +{ + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let cxt = cx_in(cx, true); + match nd.kind { + // 13.1 `IdentifierReference`, `BindingIdentifier`, `LabelIdentifier`. + NodeKind::IdentifierReference | NodeKind::BindingIdentifier | NodeKind::LabelIdentifier => ks.len() + == 0 && j == i + 1 && ident_tok(ts, i, cx), + // 12.7 `IdentifierName` as a `PropertyName`, a property access or an export/import name. + NodeKind::IdentifierName => ks.len() == 0 && j == i + 1 && is_name(ts, i), + // 12.7 `PrivateIdentifier`. + NodeKind::PrivateIdentifier => ks.len() == 0 && j == i + 1 && is_kind( + ts, + i, + TokKind::PrivateIdentifier, + ), + // 13.2.1 `PrimaryExpression : this` + NodeKind::ThisExpression => ks.len() == 0 && j == i + 1 && is_kw(ts, i, "this"@), + // 13.2.3 `Literal : NullLiteral | BooleanLiteral | NumericLiteral | StringLiteral` + NodeKind::NullLiteral => ks.len() == 0 && j == i + 1 && is_kw(ts, i, "null"@), + NodeKind::BooleanLiteral => ks.len() == 0 && j == i + 1 && (is_kw(ts, i, "true"@) || is_kw( + ts, + i, + "false"@, + )), + NodeKind::NumericLiteral => ks.len() == 0 && j == i + 1 && is_kind( + ts, + i, + TokKind::NumericLiteral, + ), + NodeKind::StringLiteral => ks.len() == 0 && j == i + 1 && is_kind( + ts, + i, + TokKind::StringLiteral, + ), + // 13.2.7 `RegularExpressionLiteral` + NodeKind::RegularExpressionLiteral => ks.len() == 0 && j == i + 1 && is_kind( + ts, + i, + TokKind::RegularExpressionLiteral, + ), + // 13.2.8 `TemplateLiteral[Yield, Await, Tagged]`; `Expression[+In]` in each substitution. + NodeKind::TemplateLiteral => dtemplate(a, ts, n, ks, 0, cxt, i, j), + // 13.2.8 a template component: a token of the kinds of 12.9.6. + NodeKind::TemplateElement => ks.len() == 0 && j == i + 1 && 0 <= i < tn(ts) && ( + ts.toks[i].kind is Template), + // 13.2.4 `ArrayLiteral : [ Elision? ] | [ ElementList ] | [ ElementList , Elision? ]` + NodeKind::ArrayLiteral => i + 1 < j && is_p(ts, i, "["@) && is_p(ts, j - 1, "]"@) + && (forall|k: int| + 0 <= k < ks.len() ==> is_assign_expr(a, #[trigger] ks[k]) || kd(a, ks[k]) + == NodeKind::SpreadElement || kd(a, ks[k]) == NodeKind::Elision) && darray( + a, + ts, + n, + ks, + 0, + cxt, + i + 1, + j - 1, + ), + // 13.2.5 `ObjectLiteral : { } | { PropertyDefinitionList } | { PropertyDefinitionList , }` + NodeKind::ObjectLiteral => i + 1 < j && is_p(ts, i, "{"@) && is_p(ts, j - 1, "}"@) + && (forall|k: int| + 0 <= k < ks.len() ==> kd(a, #[trigger] ks[k]) == NodeKind::PropertyDefinition || kd( + a, + ks[k], + ) == NodeKind::CoverInitializedName || kd(a, ks[k]) == NodeKind::MethodDefinition) + && dsep(a, ts, n, ks, 0, cxt, i + 1, j - 1, true), + // 13.2.5 `PropertyDefinition : IdentifierReference | PropertyName : AssignmentExpression + // | ... AssignmentExpression` (`MethodDefinition` is its own node). + NodeKind::PropertyDefinition => match nd.op { + Op::Shorthand => ks.len() == 1 && kd(a, k0) == NodeKind::IdentifierReference && dc( + a, + ts, + n, + k0, + cx, + i, + i + 1, + ) && j == i + 1, + Op::KeyValue => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && is_prop_name_kind(kd(a, k0)) && is_assign_expr(a, k1) && i < m + && dc(a, ts, n, k0, cx, i, m) && is_p(ts, m, ":"@) && dc(a, ts, n, k1, cxt, m + 1, j) + }, + Op::Spread => ks.len() == 1 && is_p(ts, i, "..."@) && is_assign_expr(a, k0) && dc( + a, + ts, + n, + k0, + cxt, + i + 1, + j, + ), + _ => false, + }, + // 13.2.5 `CoverInitializedName : IdentifierReference Initializer[+In]` + NodeKind::CoverInitializedName => ks.len() == 2 && kd(a, k0) == NodeKind::IdentifierReference + && dc(a, ts, n, k0, cx, i, i + 1) && d_init(a, ts, n, k1, cxt, i + 1, j, true), + // 13.2.5 `ComputedPropertyName : [ AssignmentExpression[+In] ]` + NodeKind::ComputedPropertyName => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 1 && is_p(ts, i, "["@) && is_assign_expr(a, k0) && dc(a, ts, n, k0, cxt, i + 1, m) + && is_p(ts, m, "]"@) && j == m + 1 + }, + // 13.2.9 `ParenthesizedExpression : ( Expression[+In] )` + NodeKind::ParenthesizedExpression => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 1 && is_p(ts, i, "("@) && lvl(a, k0) >= -1 && dc(a, ts, n, k0, cxt, i + 1, m) + && is_p(ts, m, ")"@) && j == m + 1 + }, + // 13.3.2 `MemberExpression : MemberExpression [ Expression[+In] ]`, `. IdentifierName`, + // `. PrivateIdentifier`, and the same for `CallExpression`. + NodeKind::MemberExpression => { + let m = ed(a, ts, k0, i); + let q = ed(a, ts, k1, m + 1); + ks.len() == 2 && is_member_or_call(a, k0) && i < m && dc(a, ts, n, k0, cx, i, m) && match nd.op { + Op::Dot => is_p(ts, m, "."@) && kd(a, k1) == NodeKind::IdentifierName && j == m + 2 + && dc(a, ts, n, k1, cx, m + 1, j), + Op::PrivateDot => is_p(ts, m, "."@) && kd(a, k1) == NodeKind::PrivateIdentifier && j + == m + 2 && dc(a, ts, n, k1, cx, m + 1, j), + Op::Bracket => is_p(ts, m, "["@) && lvl(a, k1) >= -1 && dc(a, ts, n, k1, cxt, m + 1, q) + && is_p(ts, q, "]"@) && j == q + 1, + _ => false, + } + }, + // 13.3.7 `SuperProperty : super [ Expression[+In] ] | super . IdentifierName` + NodeKind::SuperProperty => { + let q = ed(a, ts, k0, i + 2); + ks.len() == 1 && is_kw(ts, i, "super"@) && match nd.op { + Op::Dot => is_p(ts, i + 1, "."@) && kd(a, k0) == NodeKind::IdentifierName && j == i + 3 + && dc(a, ts, n, k0, cx, i + 2, j), + Op::Bracket => is_p(ts, i + 1, "["@) && lvl(a, k0) >= -1 && dc(a, ts, n, k0, cxt, i + 2, q) + && is_p(ts, q, "]"@) && j == q + 1, + _ => false, + } + }, + // 13.3.7 `SuperCall : super Arguments` + NodeKind::SuperCall => ks.len() == 1 && is_kw(ts, i, "super"@) && kd(a, k0) + == NodeKind::Arguments && dc(a, ts, n, k0, cx, i + 1, j), + // 13.3.10 `ImportCall : import ( AssignmentExpression[+In] ,? )` and + // `import ( AssignmentExpression[+In] , AssignmentExpression[+In] ,? )` + NodeKind::ImportCall => { + let m = ed(a, ts, k0, i + 2); + let q = ed(a, ts, k1, m + 1); + ks.len() == 2 && is_kw(ts, i, "import"@) && is_p(ts, i + 1, "("@) && is_assign_expr(a, k0) + && dc(a, ts, n, k0, cxt, i + 2, m) && (if kd(a, k1) == NodeKind::Absent { + dc(a, ts, n, k1, cxt, m, m) && ((is_p(ts, m, ")"@) && j == m + 1) || (is_p( + ts, + m, + ","@, + ) && is_p(ts, m + 1, ")"@) && j == m + 2)) + } else { + is_assign_expr(a, k1) && is_p(ts, m, ","@) && dc(a, ts, n, k1, cxt, m + 1, q) && ((is_p( + ts, + q, + ")"@, + ) && j == q + 1) || (is_p(ts, q, ","@) && is_p(ts, q + 1, ")"@) && j == q + 2)) + }) + }, + // 13.3.12 `ImportMeta : import . meta` + NodeKind::ImportMeta => ks.len() == 0 && j == i + 3 && is_kw(ts, i, "import"@) && is_p( + ts, + i + 1, + "."@, + ) && is_kw(ts, i + 2, "meta"@), + // 13.3.12 `NewTarget : new . target` + NodeKind::NewTarget => ks.len() == 0 && j == i + 3 && is_kw(ts, i, "new"@) && is_p( + ts, + i + 1, + "."@, + ) && is_kw(ts, i + 2, "target"@), + // 13.3.5 `NewExpression : new NewExpression`, `MemberExpression : new MemberExpression Arguments` + NodeKind::NewExpression => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 2 && is_kw(ts, i, "new"@) && i + 1 < m && dc(a, ts, n, k0, cx, i + 1, m) && ( + if kd(a, k1) == NodeKind::Absent { + (lvl(a, k0) == 16 || lvl(a, k0) == 17) && j == m && dc(a, ts, n, k1, cx, m, m) + } else { + lvl(a, k0) == 17 && kd(a, k1) == NodeKind::Arguments && dc(a, ts, n, k1, cx, m, j) + }) + }, + // 13.3.6 `CallExpression : MemberExpression Arguments | CallExpression Arguments` + NodeKind::CallExpression => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && is_member_or_call(a, k0) && kd(a, k1) == NodeKind::Arguments && i < m + && dc(a, ts, n, k0, cx, i, m) && dc(a, ts, n, k1, cx, m, j) + }, + // 13.3.8 `Arguments : ( ) | ( ArgumentList ) | ( ArgumentList , )` + NodeKind::Arguments => i + 1 < j && is_p(ts, i, "("@) && is_p(ts, j - 1, ")"@) && all_args( + a, + ks, + ) && dsep(a, ts, n, ks, 0, cxt, i + 1, j - 1, true), + // 13.3.11 `MemberExpression : MemberExpression TemplateLiteral[?Yield, ?Await, +Tagged]`, + // and `CallExpression TemplateLiteral` + NodeKind::TaggedTemplate => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && is_member_or_call(a, k0) && kd(a, k1) == NodeKind::TemplateLiteral && i + < m && dc(a, ts, n, k0, cx, i, m) && dc(a, ts, n, k1, cx, m, j) + }, + // 13.3.9 `OptionalExpression : MemberExpression OptionalChain | CallExpression + // OptionalChain | OptionalExpression OptionalChain` + NodeKind::OptionalExpression => { + let m = ed(a, ts, k0, i); + ks.len() >= 2 && is_member_or_call(a, k0) && all_kind2( + a, + ks, + 1, + NodeKind::OptionalChain, + NodeKind::OptionalChain, + ) && flag_of(a, k1) && i < m && dc(a, ts, n, k0, cx, i, m) && dl(a, ts, n, ks, 1, cx, m, j) + }, + NodeKind::OptionalChain => d_chain_link(a, ts, n, cx, i, j), + _ => false, + } +} + +/// §13.4 to §13.16, §15.5.1 and §15.8.1: the arms of [`d`] for the operators. +pub open spec fn d_expr(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 3int, 0int, 0int, +{ + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let cxt = cx_in(cx, true); + d_expr_lhs(a, ts, n, cx, i, j) || match nd.kind { + // An optional symbol that is not there. + NodeKind::Absent => ks.len() == 0 && i == j, + // 13.4 `UpdateExpression : LeftHandSideExpression [no LineTerminator here] ++ | -- ...`, + // `++ UnaryExpression | -- UnaryExpression` + NodeKind::UpdateExpression => { + let m = ed(a, ts, k0, i); + ks.len() == 1 && (if nd.op == Op::PostfixIncrement || nd.op == Op::PostfixDecrement { + is_lhs_expr(a, k0) && i < m && dc(a, ts, n, k0, cx, i, m) && is_op_tok(ts, m, nd.op) + && !nl(ts, m) && j == m + 1 + } else { + (nd.op == Op::PrefixIncrement || nd.op == Op::PrefixDecrement) && is_op_tok( + ts, + i, + nd.op, + ) && lvl(a, k0) >= 12 && dc(a, ts, n, k0, cx, i + 1, j) + }) + }, + // 13.5 `UnaryExpression : delete | void | typeof | + | - | ~ | ! UnaryExpression` + NodeKind::UnaryExpression => ks.len() == 1 && is_unary_op(nd.op) && is_op_tok(ts, i, nd.op) + && lvl(a, k0) >= 12 && dc(a, ts, n, k0, cx, i + 1, j), + // 15.8 `UnaryExpression : [+Await] AwaitExpression`, `AwaitExpression : await UnaryExpression` + NodeKind::AwaitExpression => ks.len() == 1 && cx.await_ && is_kw(ts, i, "await"@) && lvl( + a, + k0, + ) >= 12 && dc(a, ts, n, k0, cx, i + 1, j), + // 13.6 to 13.13: the binary operators; `in` only with `[+In]` (13.10). + NodeKind::ExponentiationExpression | NodeKind::MultiplicativeExpression + | NodeKind::AdditiveExpression | NodeKind::ShiftExpression | NodeKind::RelationalExpression + | NodeKind::EqualityExpression | NodeKind::BitwiseANDExpression + | NodeKind::BitwiseXORExpression | NodeKind::BitwiseORExpression + | NodeKind::LogicalANDExpression | NodeKind::LogicalORExpression + | NodeKind::CoalesceExpression => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && bin_op_ok(nd.kind, nd.op) && (nd.op == Op::In ==> cx.in_) + && binary_operands_ok(a, nd.kind, nd.op, k0, k1) && i < m && dc(a, ts, n, k0, cx, i, m) + && is_op_tok(ts, m, nd.op) && dc(a, ts, n, k1, cx, m + 1, j) + }, + // 13.14 `ConditionalExpression : ShortCircuitExpression ? AssignmentExpression[+In] : + // AssignmentExpression` + NodeKind::ConditionalExpression => { + let m = ed(a, ts, k0, i); + let q = ed(a, ts, k1, m + 1); + ks.len() == 3 && (lvl(a, k0) >= 1 || kd(a, k0) == NodeKind::CoalesceExpression) + && is_assign_expr(a, k1) && is_assign_expr(a, k2) && i < m && dc(a, ts, n, k0, cx, i, m) + && is_p(ts, m, "?"@) && dc(a, ts, n, k1, cxt, m + 1, q) && is_p(ts, q, ":"@) && dc( + a, + ts, + n, + k2, + cx, + q + 1, + j, + ) + }, + // 13.15 `AssignmentExpression : LeftHandSideExpression = AssignmentExpression | + // LeftHandSideExpression AssignmentOperator AssignmentExpression | ...`; for `=` the + // target may be an `AssignmentPattern` (13.15.5). + NodeKind::AssignmentExpression => { + let m = ed(a, ts, k0, i); + ks.len() == 2 && is_assign_op(nd.op) && (if nd.op == Op::Assign { + is_destructuring_target(a, k0) + } else { + is_lhs_expr(a, k0) + }) && is_assign_expr(a, k1) && i < m && dc(a, ts, n, k0, cx, i, m) && is_op_tok( + ts, + m, + nd.op, + ) && dc(a, ts, n, k1, cx, m + 1, j) + }, + // 15.5 `YieldExpression : yield | yield [no LineTerminator here] AssignmentExpression | + // yield [no LineTerminator here] * AssignmentExpression`, `[+Yield]` + NodeKind::YieldExpression => ks.len() == 1 && cx.yield_ && is_kw(ts, i, "yield"@) && ( + if nd.op == Op::YieldPlain { + if kd(a, k0) == NodeKind::Absent { + j == i + 1 && dc(a, ts, n, k0, cx, i + 1, i + 1) + } else { + !nl(ts, i + 1) && is_assign_expr(a, k0) && dc(a, ts, n, k0, cx, i + 1, j) + } + } else { + nd.op == Op::YieldDelegate && !nl(ts, i + 1) && is_p(ts, i + 1, "*"@) && is_assign_expr( + a, + k0, + ) && dc(a, ts, n, k0, cx, i + 2, j) + }), + // 13.16 `Expression : AssignmentExpression | Expression , AssignmentExpression` + NodeKind::Expression => ks.len() >= 2 && (forall|k: int| + 0 <= k < ks.len() ==> is_assign_expr(a, #[trigger] ks[k])) && dsep( + a, + ts, + n, + ks, + 0, + cx, + i, + j, + false, + ), + // 13.2.4, 13.3.8 `SpreadElement : ... AssignmentExpression[+In]` + NodeKind::SpreadElement => ks.len() == 1 && is_p(ts, i, "..."@) && is_assign_expr(a, k0) && dc( + a, + ts, + n, + k0, + cxt, + i + 1, + j, + ), + _ => false, + } +} + +} // verus! + +verus! { + +/// §16.2.3 `ModuleExportName : IdentifierName | StringLiteral`, and the +/// same alternatives of `AttributeKey` and `ImportSpecifier`. +pub open spec fn is_module_export_name_kind(k: NodeKind) -> bool { + k == NodeKind::IdentifierName || k == NodeKind::StringLiteral +} + +/// The optional `WithClause` child `c` starting at token `s` and ending at `r`, +/// then the `;` ending at `j`. +pub open spec fn d_with_semi(a: AstV, ts: Stream, bound: int, c: int, cx: Ctx, s: int, j: int) -> bool + decreases bound, 1int, 0int, 0int, +{ + let r = ed(a, ts, c, s); + (kd(a, c) == NodeKind::Absent || kd(a, c) == NodeKind::WithClause) && dc(a, ts, bound, c, cx, s, r) + && semi(ts, r, j, false) +} + +/// §16.2.2 and §16.2.3: the arms of [`d`] for imports and exports. +pub open spec fn d_module(a: AstV, ts: Stream, n: int, cx: Ctx, i: int, j: int) -> bool + decreases n, 3int, 0int, 0int, +{ + let nd = a.nodes[n]; + let ks = nd.children; + let k0 = kid(ks, 0); + let k1 = kid(ks, 1); + let k2 = kid(ks, 2); + let cxd = Ctx { yield_: cx.yield_, await_: cx.await_, in_: cx.in_, return_: cx.return_, deflt: true }; + match nd.kind { + // 16.2.2 `ImportDeclaration : import ImportClause FromClause WithClause? ; | + // import ModuleSpecifier WithClause? ;`, `FromClause : from ModuleSpecifier` + NodeKind::ImportDeclaration => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 3 && is_kw(ts, i, "import"@) && kd(a, k1) == NodeKind::StringLiteral && (kd( + a, + k0, + ) == NodeKind::Absent || kd(a, k0) == NodeKind::ImportClause) && dc( + a, + ts, + n, + k0, + cx, + i + 1, + m, + ) && (if kd(a, k0) == NodeKind::Absent { + dc(a, ts, n, k1, cx, m, m + 1) && d_with_semi(a, ts, n, k2, cx, m + 1, j) + } else { + is_kw(ts, m, "from"@) && dc(a, ts, n, k1, cx, m + 1, m + 2) && d_with_semi( + a, + ts, + n, + k2, + cx, + m + 2, + j, + ) + }) + }, + // 16.2.2 `ImportClause : ImportedDefaultBinding | NameSpaceImport | NamedImports | + // ImportedDefaultBinding , NameSpaceImport | ImportedDefaultBinding , NamedImports` + NodeKind::ImportClause => ks.len() == 2 && (kd(a, k0) == NodeKind::BindingIdentifier + || kd(a, k0) == NodeKind::Absent) && (kd(a, k1) == NodeKind::NameSpaceImport || kd( + a, + k1, + ) == NodeKind::NamedImports || kd(a, k1) == NodeKind::Absent) && (if kd(a, k1) + == NodeKind::Absent { + kd(a, k0) == NodeKind::BindingIdentifier && j == i + 1 && dc(a, ts, n, k0, cx, i, j) + && dc(a, ts, n, k1, cx, j, j) + } else if kd(a, k0) == NodeKind::Absent { + dc(a, ts, n, k0, cx, i, i) && dc(a, ts, n, k1, cx, i, j) + } else { + dc(a, ts, n, k0, cx, i, i + 1) && is_p(ts, i + 1, ","@) && dc(a, ts, n, k1, cx, i + 2, j) + }), + // 16.2.2 `NameSpaceImport : * as ImportedBinding` + NodeKind::NameSpaceImport => ks.len() == 1 && is_p(ts, i, "*"@) && is_kw(ts, i + 1, "as"@) + && kd(a, k0) == NodeKind::BindingIdentifier && j == i + 3 && dc(a, ts, n, k0, cx, i + 2, j), + // 16.2.2 `NamedImports : { } | { ImportsList } | { ImportsList , }` + NodeKind::NamedImports => i + 1 < j && is_p(ts, i, "{"@) && is_p(ts, j - 1, "}"@) + && all_kind2(a, ks, 0, NodeKind::ImportSpecifier, NodeKind::ImportSpecifier) + && dsep(a, ts, n, ks, 0, cx, i + 1, j - 1, true), + // 16.2.2 `ImportSpecifier : ImportedBinding | ModuleExportName as ImportedBinding` + NodeKind::ImportSpecifier => ks.len() == 2 && kd(a, k1) == NodeKind::BindingIdentifier && ( + if kd(a, k0) == NodeKind::Absent { + j == i + 1 && dc(a, ts, n, k0, cx, i, i) && dc(a, ts, n, k1, cx, i, j) + } else { + is_module_export_name_kind(kd(a, k0)) && is_kw(ts, i + 1, "as"@) && j == i + 3 && dc( + a, + ts, + n, + k0, + cx, + i, + i + 1, + ) && dc(a, ts, n, k1, cx, i + 2, j) + }), + // 16.2.2 `WithClause : with { } | with { WithEntries ,? }` + NodeKind::WithClause => i + 2 < j && is_kw(ts, i, "with"@) && is_p(ts, i + 1, "{"@) && is_p( + ts, + j - 1, + "}"@, + ) && all_kind2(a, ks, 0, NodeKind::ImportAttribute, NodeKind::ImportAttribute) && dsep( + a, + ts, + n, + ks, + 0, + cx, + i + 2, + j - 1, + true, + ), + // 16.2.2 `ImportAttribute : AttributeKey : StringLiteral` + NodeKind::ImportAttribute => ks.len() == 2 && is_module_export_name_kind(kd(a, k0)) && kd( + a, + k1, + ) == NodeKind::StringLiteral && is_p(ts, i + 1, ":"@) && j == i + 3 && dc( + a, + ts, + n, + k0, + cx, + i, + i + 1, + ) && dc(a, ts, n, k1, cx, i + 2, j), + // 16.2.3 `ExportDeclaration` + NodeKind::ExportDeclaration => is_kw(ts, i, "export"@) && match nd.op { + // `export * FromClause WithClause? ;` + Op::ExportAll => ks.len() == 2 && is_p(ts, i + 1, "*"@) && is_kw(ts, i + 2, "from"@) + && kd(a, k0) == NodeKind::StringLiteral && dc(a, ts, n, k0, cx, i + 3, i + 4) + && d_with_semi(a, ts, n, k1, cx, i + 4, j), + // `export * as ModuleExportName FromClause WithClause? ;` + Op::ExportAllAs => ks.len() == 3 && is_p(ts, i + 1, "*"@) && is_kw(ts, i + 2, "as"@) + && is_module_export_name_kind(kd(a, k0)) && dc(a, ts, n, k0, cx, i + 3, i + 4) + && is_kw(ts, i + 4, "from"@) && kd(a, k1) == NodeKind::StringLiteral && dc( + a, + ts, + n, + k1, + cx, + i + 5, + i + 6, + ) && d_with_semi(a, ts, n, k2, cx, i + 6, j), + // `export ExportFromClause FromClause WithClause? ;` with `NamedExports` + Op::ExportFrom => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 3 && kd(a, k0) == NodeKind::NamedExports && kd(a, k1) + == NodeKind::StringLiteral && dc(a, ts, n, k0, cx, i + 1, m) && is_kw( + ts, + m, + "from"@, + ) && dc(a, ts, n, k1, cx, m + 1, m + 2) && d_with_semi(a, ts, n, k2, cx, m + 2, j) + }, + // `export NamedExports ;` + Op::ExportNamed => { + let m = ed(a, ts, k0, i + 1); + ks.len() == 1 && kd(a, k0) == NodeKind::NamedExports && dc(a, ts, n, k0, cx, i + 1, m) + && semi(ts, m, j, false) + }, + // `export VariableStatement[~Yield, +Await]` + Op::ExportVar => ks.len() == 1 && kd(a, k0) == NodeKind::VariableStatement && dc( + a, + ts, + n, + k0, + cx, + i + 1, + j, + ), + // `export Declaration[~Yield, +Await]` + Op::ExportDecl => ks.len() == 1 && is_declaration_kind(kd(a, k0)) && dc( + a, + ts, + n, + k0, + cx, + i + 1, + j, + ), + // `export default HoistableDeclaration[~Yield, +Await, +Default]` + Op::ExportDefaultHoistable => ks.len() == 1 && is_kw(ts, i + 1, "default"@) + && is_hoistable_kind(kd(a, k0)) && dc(a, ts, n, k0, cxd, i + 2, j), + // `export default ClassDeclaration[~Yield, +Await, +Default]` + Op::ExportDefaultClass => ks.len() == 1 && is_kw(ts, i + 1, "default"@) && kd(a, k0) + == NodeKind::ClassDeclaration && dc(a, ts, n, k0, cxd, i + 2, j), + // `export default [lookahead ∉ { function, async [no LineTerminator here] function, + // class }] AssignmentExpression[+In, ~Yield, +Await] ;` + Op::ExportDefaultExpression => { + let m = ed(a, ts, k0, i + 2); + ks.len() == 1 && is_kw(ts, i + 1, "default"@) && !is_kw(ts, i + 2, "function"@) && !( + is_kw(ts, i + 2, "async"@) && is_kw(ts, i + 3, "function"@) && !nl(ts, i + 3)) + && !is_kw(ts, i + 2, "class"@) && is_assign_expr(a, k0) && dc( + a, + ts, + n, + k0, + cx_in(cx, true), + i + 2, + m, + ) && semi(ts, m, j, false) + }, + _ => false, + }, + // 16.2.3 `NamedExports : { } | { ExportsList } | { ExportsList , }` + NodeKind::NamedExports => i + 1 < j && is_p(ts, i, "{"@) && is_p(ts, j - 1, "}"@) + && all_kind2(a, ks, 0, NodeKind::ExportSpecifier, NodeKind::ExportSpecifier) + && dsep(a, ts, n, ks, 0, cx, i + 1, j - 1, true), + // 16.2.3 `ExportSpecifier : ModuleExportName | ModuleExportName as ModuleExportName` + NodeKind::ExportSpecifier => ks.len() == 2 && is_module_export_name_kind(kd(a, k0)) && dc( + a, + ts, + n, + k0, + cx, + i, + i + 1, + ) && (if kd(a, k1) == NodeKind::Absent { + j == i + 1 && dc(a, ts, n, k1, cx, i + 1, i + 1) + } else { + is_module_export_name_kind(kd(a, k1)) && is_kw(ts, i + 1, "as"@) && j == i + 3 && dc( + a, + ts, + n, + k1, + cx, + i + 2, + j, + ) + }), + _ => false, + } +} + +/// The context of the goal symbol: `Script : ScriptBody?` is `[~Yield, ~Await, +/// ~Return]`; `Module : ModuleBody?` is `[~Yield, +Await, ~Return]` (§16.1, §16.2). +pub open spec fn root_ctx(g: SourceGoal) -> Ctx { + Ctx { yield_: false, await_: g == SourceGoal::Module, in_: true, return_: false, deflt: false } +} + +/// `ast` is a derivation of all of the token stream `ts` from the goal symbol +/// `g`: a well-formed arena whose root derives every token. +pub open spec fn derives_root(a: AstV, ts: Stream, g: SourceGoal) -> bool { + ast_wf(a) && kd(a, a.root) == (if g == SourceGoal::Script { + NodeKind::Script + } else { + NodeKind::Module + }) && d(a, ts, a.root, root_ctx(g), 0, tn(ts)) +} + +} // verus! + +verus! { + +// --------------------------------------------------------------------- +// §12.10 Automatic Semicolon Insertion +// --------------------------------------------------------------------- + +/// `n` gaps with nothing inserted. +pub open spec fn no_gaps(n: int) -> Seq { + Seq::new(n as nat, |k: int| Ins::None) +} + +/// The stream `ts` (with its gap before the end) is, as it stands, a whole +/// `Script` or `Module` of the grammar: the test of §12.10.1 rule 2 ("the +/// parser is unable to parse the input token stream as a single instance of +/// the goal nonterminal"), applied to what comes before the end. +pub open spec fn complete_free(ts: Stream, g: SourceGoal) -> bool { + exists|a: AstV| derives_root(a, ts, g) +} + +/// The tokens `pt`, with the gaps `pg` before each of them (and none +/// at the end), are the beginning of some sentence of the grammar without further +/// semicolon insertion: some tokens can follow them to make a whole `Script` or +/// `Module`. "A token is not allowed by any production of the grammar" +/// (§12.10.1 rule 1) is that the tokens before it and the token itself are not +/// viable. +pub open spec fn viable_free(pt: Seq, pg: Seq, g: SourceGoal) -> bool { + exists|st: Seq, a: AstV| + pg.len() == pt.len() && #[trigger] derives_root( + a, + Stream { toks: pt + st, gaps: pg + no_gaps(st.len() as int + 1) }, + g, + ) +} + +/// The lexer step at `pos` with `goal` is a token that can follow the tokens `pt` +/// (with the gaps `pg` before them) in a sentence of the grammar. +pub open spec fn viable_after_lexing( + src: Src, + pos: int, + goal: LexGoal, + pt: Seq, + pg: Seq, + g: SourceGoal, +) -> bool { + match L::next_token(src, pos, goal) { + LexStep::Token(t2) => viable_free(pt.push(t2), pg.push(Ins::None), g), + _ => false, + } +} + +/// The gap before token `k` of `ts` (`k == tn(ts)`: before the end of the +/// input) holds the `;` that §12.10.1 allows, for the source text `src`: +/// +/// 1. rule 1: the `;` is `Ins::Semicolon`, the token after it (the "offending +/// token") is separated from the one before by a line terminator or is `}`, +/// and none of the tokens that the code points at that position can lex +/// to, under any goal symbol, can follow what comes before, in any +/// sentence of the grammar (the stream so far, inserted `;` included); +/// 2. rule 2: it is the gap before the end of the stream, and what comes +/// before is not a whole `Script` or `Module`; +/// 3. rule 1, third condition: it is `Ins::DoWhile`, after a `)`. The grammar of [`d_stmt`] +/// accepts this one only after a `do`-`while`. +/// +/// Rule 4 (a semicolon is never inserted when it would be an empty statement or +/// one of the two `;` of a `for` header) is in [`d_stmt`]: `EmptyStatement` and +/// the `for` header accept a `;` token only. No gap holds one at the start. +pub open spec fn gap_justified(src: Src, ts: Stream, k: int, g: SourceGoal) -> bool { + match ts.gaps[k] { + Ins::None => true, + Ins::DoWhile => 0 < k && is_p(ts, k - 1, ")"@), + Ins::Semicolon => 0 < k && if k == tn(ts) { + !complete_free( + Stream { toks: ts.toks, gaps: ts.gaps.subrange(0, k).push(Ins::None) }, + g, + ) + } else { + (ts.toks[k].nl_before || is_p(ts, k, "}"@)) && !exists|goal: LexGoal| + #[trigger] viable_after_lexing::( + src, + ts.toks[k - 1].end, + goal, + ts.toks.subrange(0, k), + ts.gaps.subrange(0, k), + g, + ) + }, + } +} + +/// `ts` is the token stream of `src` for the parser: a lexing of it +/// ([`lexes_as`]), with the semicolons that §12.10 inserts, each justified +/// ([`gap_justified`]). +pub open spec fn tokens_of(src: Src, ts: Stream, g: SourceGoal) -> bool { + lexes_as::(src, ts.toks) && ts.gaps.len() == ts.toks.len() + 1 && (forall|k: int| + 0 <= k < ts.gaps.len() ==> #[trigger] gap_justified::(src, ts, k, g)) +} + +/// `a` is a parse of the source text `src` as `g` by the grammar: some token +/// stream of `src` ([`tokens_of`], semicolon insertion included) is derived +/// from the goal symbol with `a` as the derivation ([`derives_root`]), and +/// the spans of `a` are inside `src`. This does not include the early errors +/// ([`early_errors_ok`]); [`is_valid_parse`] does. +pub open spec fn is_parse(src: Src, g: SourceGoal, a: AstV) -> bool { + exists|ts: Stream| + tokens_of::(src, ts, g) && derives_root(a, ts, g) && spans_wf(a, src.len() as int) +} + +} // verus! diff --git a/xtask/src/status.rs b/xtask/src/status.rs index 789eae3c3..f051ec36c 100644 --- a/xtask/src/status.rs +++ b/xtask/src/status.rs @@ -73,6 +73,7 @@ const PLACES: &[(&str, Place)] = &[ ("clazel-selectors", Place::Renderer), ("clazel-js-lexer", Place::Renderer), ("clazel-js-vm", Place::Renderer), + ("clazel-js-syntax", Place::Renderer), ("clazel-style", Place::Renderer), ("clazel-font", Place::Renderer), ("clazel-layout", Place::Renderer),