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
3 changes: 3 additions & 0 deletions Cargo.lock

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

12 changes: 9 additions & 3 deletions crates/clazel-js-lexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@ license.workspace = true
publish.workspace = true
description = "The JavaScript lexer for Clazel's Phase 7 browser (phase-7.md item 7.2): ECMA-262 section 12 source text to tokens, the goal symbol an input."

[dependencies]
vstd = "=0.0.0-2026-10-04-0306"

# Verification tier (docs/VERIFICATION.md). `tier` is what is proven about the
# shipped code today; `target-tier` is what the policy requires, since this
# lexes scripts a page chose (phase-7.md 7.0(a)). Tier 0: this is only the
# skeleton that holds the test data of item 7.1. The contracts come first
# (rule 3), in item 7.2.
# lexes scripts a page chose (phase-7.md 7.0(a)). Tier 0: the spec and the
# contracts are written (item 7.2, first half, CLAUDE.md rule 3) and check
# with Verus, but no implementation exists, so nothing shipped is proven.
[package.metadata.clazel]
tier = 0
target-tier = 2

[package.metadata.verus]
verify = true

[lints]
workspace = true
56 changes: 56 additions & 0 deletions crates/clazel-js-lexer/mutations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
{
"mutations": [
{
"id": "line-terminator-drops-ls",
"file": "src/spec.rs",
"find": "c == 0x000A || c == 0x000D || c == 0x2028 || c == 0x2029",
"replace": "c == 0x000A || c == 0x000D || c == 0x2029",
"expect": "killed",
"reason": "Drops <LS> from LineTerminator (§12.3): U+2028 would no longer end a line or trigger ASI, so example_line_terminators is false.",
"certificate": {
"file": "src/examples.rs",
"lemma": "example_line_terminators()",
"witness": "!is_line_terminator(0x2028u32)"
}
},
{
"id": "line-terminator-adds-nel",
"file": "src/spec.rs",
"find": "c == 0x000A || c == 0x000D || c == 0x2028 || c == 0x2029",
"replace": "c == 0x000A || c == 0x000D || c == 0x0085 || c == 0x2028 || c == 0x2029",
"expect": "killed",
"reason": "Counts U+0085 NEL as a LineTerminator, which §12.3 does not: example_line_terminators is false.",
"certificate": {
"file": "src/examples.rs",
"lemma": "example_line_terminators()",
"witness": "is_line_terminator(0x0085u32)"
}
},
{
"id": "hex-digit-value-lowercase-offset",
"file": "src/spec.rs",
"find": "c as int - 0x61 + 10",
"replace": "c as int - 0x61",
"expect": "killed",
"reason": "Gives `b` the MV 1 instead of 11 (§12.9.3.1), so `\\x62`-style escapes and `0xb` would get the wrong value: example_hex_digit_values is false.",
"certificate": {
"file": "src/examples.rs",
"lemma": "example_hex_digit_values()",
"witness": "hex_digit_value(0x62u32) == 1"
}
},
{
"id": "legacy-octal-two-digits-only",
"file": "src/spec.rs",
"find": " || (i + 2 < j && is_octal_digit(s[j - 1]) && legacy_octal_integer_literal(s, i, j - 1)))",
"replace": " )",
"expect": "killed",
"reason": "Drops the recursive alternative `LegacyOctalIntegerLiteral OctalDigit` (§12.9.3, Annex B), so `010` is no longer a LegacyOctalIntegerLiteral: example_legacy_numeric_literals is false.",
"certificate": {
"file": "src/examples.rs",
"lemma": "example_legacy_numeric_literals()",
"witness": "!legacy_octal_integer_literal(seq![0x30u32, 0x31, 0x30], 0, 3)"
}
}
]
}
153 changes: 153 additions & 0 deletions crates/clazel-js-lexer/src/api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
//! The lexer's public types and its contract-only API.
//!
//! The types are the output of item 7.2: tokens with spans, the values the
//! standard defines for them (identifier code points, string values as
//! UTF-16, template values), and the line-terminator flag automatic
//! semicolon insertion (§12.10) needs. The functions are in [`JsLexer`], a
//! trait with no implementation: each 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
//! implementation (item 7.2, second half) implements it, or replaces it
//! with free functions whose `requires` and `ensures` are the same
//! predicates.

#[allow(unused_imports)]
use super::spec::*;
#[allow(unused_imports)]
use vstd::prelude::*;

verus! {

/// The value of an input element, where the standard defines one.
pub enum Value {
/// The element has no value but its span (white space, comments,
/// punctuators, and so on).
None,
/// An `IdentifierName` or `PrivateIdentifier` (the name after the `#`):
/// its IdentifierCodePoints (§12.7.1.2), with escapes resolved, and
/// whether it was written with a `UnicodeEscapeSequence`, in which case
/// it is never a `ReservedWord` (§12.7.2).
Name {
/// The code points of the name.
code_points: Vec<u32>,
/// Whether any `\` `UnicodeEscapeSequence` was used.
escaped: bool,
},
/// A `StringLiteral`: its SV (§12.9.4.2) as UTF-16 code units, lone
/// surrogates allowed.
String {
/// The code units of the string value.
units: Vec<u16>,
},
/// A template component: its TV (§12.9.6.1), `None` for *undefined*
/// (an invalid escape sequence, an error unless the template is tagged),
/// and its TRV (§12.9.6.2).
Template {
/// The template value.
cooked: Option<Vec<u16>>,
/// The template raw value.
raw: Vec<u16>,
},
/// A `NumericLiteral`. Its value is computed from its span (7.11).
Numeric {
/// A BigInt literal (it ends in `BigIntLiteralSuffix`).
bigint: bool,
/// A `LegacyOctalIntegerLiteral` or a `NonOctalDecimalIntegerLiteral`
/// (§12.9.3.1: a Syntax Error in strict mode code).
legacy: bool,
},
/// A `RegularExpressionLiteral`: the index of its closing `/`. BodyText
/// is `start + 1..body_end` (§12.9.5.1) and FlagText is `body_end +
/// 1..end` (§12.9.5.2).
RegExp {
/// The index of the closing `/`.
body_end: usize,
},
}

/// One input element: the longest element of the goal symbol at `start`.
pub struct Element {
/// What the input element is.
pub kind: ElementKind,
/// Its first code point's index in the source text.
pub start: usize,
/// One past its last code point's index.
pub end: usize,
/// Its value.
pub value: Value,
}

/// The result of [`JsLexer::next_input_element`] when no error occurs.
pub enum Step {
/// The end of the source text: there is no input element.
Eof,
/// An input element, trivia included.
Element(Element),
}

/// A token: an input element that is not trivia, with the flag that
/// automatic semicolon insertion (§12.10) needs.
pub struct Token {
/// The token.
pub element: Element,
/// Whether a line terminator precedes the token since the previous one:
/// a `LineTerminator`, a multi-line comment that contains one, or a
/// `SingleLineHTMLCloseComment` (§12.3, §12.4, Annex B.1.1).
pub newline_before: bool,
}

/// The result of [`JsLexer::next_token`] when no error occurs.
pub enum Next {
/// Only trivia remain before the end of the source text.
Eof {
/// Whether the trivia hold a line terminator.
newline_before: bool,
},
/// The next token.
Token(Token),
}

/// The lexer's contract (CLAUDE.md rule 3): both methods are total, which
/// is to say that for every source text, position and goal symbol the result
/// is determined by [`super::spec`], and there is no precondition but
/// `pos <= src.len()`.
pub trait JsLexer {
/// The next input element of `goal` at `pos`, trivia included: what the
/// grammar of `goal` derives at `pos`, taking the longest (§12).
///
/// - **Totality:** every `src`, `pos <= src.len()`, `goal` and `html`
/// has a result: `Err` exactly when [`lex_error`] says so, `Eof`
/// exactly at `src.len()`, and otherwise an element.
/// - **Span coverage:** the element starts at `pos`, ends after it and
/// within `src`, so repeated calls from each element's `end` tile the
/// source text with no gap and no overlap.
/// - **Agreement:** the element's kind derives the range, no longer
/// input element derives a longer one, and the value is the one the
/// standard defines.
fn next_input_element(src: &[u32], pos: usize, goal: Goal, html: bool) -> (r: Result<
Step,
LexError,
>)
requires
crate::contract::next_input_element_requires(src@, pos, goal, html),
ensures
crate::contract::next_input_element_ensures(src@, pos, goal, html, r),
;

/// The next token of `goal` at `pos`: skips the trivia (white space,
/// line terminators, comments) in front of it, with the line-terminator
/// flag for ASI.
///
/// - **Span coverage:** the skipped trivia are consecutive input elements
/// ([`skip_trivia`]), the token follows them, and the next call starts
/// at the token's `end`: trivia, then token, then trivia, then token,
/// concatenated, are the input.
fn next_token(src: &[u32], pos: usize, goal: Goal, html: bool) -> (r: Result<Next, LexError>)
requires
crate::contract::next_token_requires(src@, pos, goal, html),
ensures
crate::contract::next_token_ensures(src@, pos, goal, html, r),
;
}

} // verus!
Loading
Loading