Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions crates/clazel-js-syntax/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions crates/clazel-js-syntax/mutations.json
Original file line number Diff line number Diff line change
@@ -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)"
}
}
]
}
62 changes: 62 additions & 0 deletions crates/clazel-js-syntax/src/api.rs
Original file line number Diff line number Diff line change
@@ -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<L: LexerSpec> {
/// 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<crate::ast::Ast, ParseError>)
requires
crate::contract::parse_requires(src@, goal),
ensures
crate::contract::parse_ensures::<L>(src@, goal, r),
;
}

} // verus!
Loading
Loading