ReviewOS

zig-utils/zig-js

Parser: diagnose strict literals and RegExp flags

#963
Closed chris opened this 9 days ago ·
9 days ago

Parent: #899

Problem

Primary-expression validation still drops grammar context for escaped reserved words, strict-mode legacy literals, and invalid regular-expression flags. Five direct ParseError.UnexpectedToken exits reach JavaScript as the internal tag.

Executed eval probes against raw JavaScriptCore 625.1.22 and current main (4841712f) show the missing distinctions:

sourceJavaScriptCorezig-js
void v\\u0061rUnexpected escaped characters in keyword token: 'v\\u0061r'eval: UnexpectedToken at 1:6
'use strict'; 010Decimal integer literals with a leading zero are forbidden in strict modeeval: UnexpectedToken at 1:18
'use strict'; '\\1'The only valid numeric escape in strict mode is '\\0'eval: UnexpectedToken at 1:19
/a/ggInvalid regular expression: invalid flagseval: UnexpectedToken at 1:6
/a/uvInvalid regular expression: invalid flagseval: UnexpectedToken at 1:6

Implementation

  • Reuse the existing escaped-keyword diagnostic at primary-expression sites and retain the raw escaped spelling.
  • Attach distinct strict legacy-decimal and numeric-escape reasons at the literal token offset.
  • Classify the existing flag-validation failure when the regex compiler has no pattern diagnostic; keep the retained validation arena and accepted-regex path unchanged.
  • Do not add a second flag scan or allocation to successful regex parsing.

Verification

  • Byte-for-byte JavaScriptCore differential for escaped reserved spellings, strict legacy decimal/non-octal decimal literals, strict numeric string escapes, duplicate/unknown regex flags, and mutually exclusive u/v flags.
  • Focused frontend message checks and the complete standalone parser module.
  • Exact-parent test262 comparison over escaped-keyword, strict-mode literal, and RegExp literal/flag subtrees with nonzero denominators and path-level flip accounting.
9 days ago

Implemented and pushed to main in 8e8884c.

  • 9/9 eval probes match raw JavaScriptCore 625.1.22 byte for byte for escaped reserved words, strict legacy decimal/non-octal decimal literals, strict numeric string escapes, duplicate/unknown RegExp flags, and mutually exclusive u/v flags.
  • RegExp flag classification reuses the existing single validation pass and retained validation arena; accepted literals gain no second flag scan or allocation.
  • Exact-parent test262 comparison across five identifier/literal subtrees is unchanged at 700 pass / 50 known failures, with identical failure-path inventories and 0 flips.
  • The standalone parser module passes 239/239 tests; the focused frontend case passes.
  • Direct bare ParseError.UnexpectedToken exits fell from 29 to 24.

Sign in to comment on this issue.