Accept reserved words as field names, labels, variant tags and JSX names
Why
A record field, argument label or JSX prop named like a keyword (type, open, in, for) has to be renamed and mapped back with @as, or escaped as \"type". Keys such as type are common in JavaScript objects; the standard library itself declares @as("type") type_ in JsxDOM.
Where the grammar expects a name that no variable is bound to, a reserved word is now that name: a record field (in a type, an expression, a pattern and after the . of a field access), an argument label after ~, a JSX prop, a polymorphic variant tag after # and a lowercase JSX tag name. Each of these positions is entered on a token that is not a keyword, so the reading is unambiguous; the one choice, { starting a record or a block, is made by a reserved word followed by :, which no block item starts with. The parse tree is the one the escaped spelling gives, so type checking and the generated JavaScript do not change.
A shorthand ({type}, ~type, <input type />) stands for a variable of the same name, and a reserved word still can't be one. After the . of a field access or the ~ of a label a reserved word is taken only on the same line, so user. or f(~ at the end of a line being written does not swallow the next let, and editor completion still sees the unfinished argument. let?, one token, is split into the name let and ? where a name is expected ({let?: int}, x == #let? a : b).
modifiedFormat escaped names
type t = {\"type": string, mutable \"open": bool}let get = r => r.\"type"let call = f(~\"for"="a")let tag = #"true"let el = <\"switch" />
rescript
removedDiagnostic keyword as a field name
let r = {type: 1}diagnostic
addedDiagnostic shorthand with a reserved word
let {type} = rdiagnostic
`type` is a reserved keyword, so it can't be a variable name and this shorthand can't be used.Write it out instead: `{type: pattern}`
Review
- Can a program that parses today change meaning? Every accepted form was a syntax error before. All 2,948 .res/.resi files in the repository that the previous parser accepts give the same parse tree (-print ml), and so do the 33,367 of 199,692 corpus mutants (keywords inserted, removed, escaped and unescaped, line breaks added) that it accepts. None crashes or hangs, with or without -recover, beyond the debug -print ml printer and the JSX transform's errors, which the previous parser reaches the same way. Formatting every accepted mutant gives the same tree again; the few that don't fail the same way with an ordinary name in place of the keyword.
- Why do runtime and test sources change? They used escaped labels and tags (
\"type": ...,#"true"), which the formatter now prints bare. The runtime and tests/tests compile to the same JavaScript. - Why do analysis snapshots change? Hover and completion print the types with bare tags (
[#and | #or],#switch). - Does parsing allocate more? The benchmark sources (tests/compiler_benchmarks/data) allocate within 0.03% of the previous parser: the new checks use one-token lookahead, no backtracking, and no closures on the field, label and JSX prop paths.
- What checks the two spellings agree? tests/syntax_tests generates 2,000 seeded programs in both spellings and checks that they parse, give the same tree, format identically and idempotently, and that formatting adds no escape; every shorthand with each of the 28 words, and every record row with one of them after a missing comma (
{a type: x}), must be rejected.