Public: anyone can read this; the crew can change it.

Merge scanTomlValue and scanYamlValue into one value scanner #17091

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

mason/forge:mason/16803-merge-scantomlvalue-and-scanyamlvalue-in → main 0 commits fork

  • Merged. mason/16803-merge-scantomlvalue-and-scanyamlvalue-in was merged into main as dcc6797.

Review

Sign in to review this change.
4 files changed +137 −136
src/web/highlight/toml.ts +7 −59
@@ -1,17 +1,18 @@
11 /** TOML and INI scanner. */
2 import { isAlphaCh, isDigitCh, isIdentCh, isSpaceCh } from "./chars.ts";
3 import { consumeStringBody, scanNumber, spaceRun } from "./lexing.ts";
2 import { consumeStringBody } from "./lexing.ts";
43 import { type ScanState, type Scanned } from "./state.ts";
54 import { Out } from "./tokens.ts";
5 import { scanConfigValue } from "./value.ts";
66
77 export function scanToml(line: string, state: ScanState, toml: boolean): Scanned {
88 const out = new Out();
9 const dialect = toml ? "toml" : "ini";
910
1011 if (state.str) {
1112 const consumed = consumeStringBody(line, 0, state.str);
1213 out.push("string", line.slice(0, consumed.end));
1314 if (!consumed.closed) return { tokens: out.done(), state };
14 scanTomlValue(line, consumed.end, out, toml);
15 scanConfigValue(line, consumed.end, out, dialect);
1516 return { tokens: out.done(), state: { ...state, str: null } };
1617 }
1718
@@ -45,6 +46,7 @@ export function scanTomlRest(
4546 state: ScanState,
4647 toml: boolean,
4748 ): Scanned {
49 const dialect = toml ? "toml" : "ini";
4850 if (toml && line.startsWith('"""', from)) {
4951 const consumed = consumeStringBody(line, from + 3, {
5052 close: '"""',
@@ -57,65 +59,11 @@ export function scanTomlRest(
5759 state: { ...state, str: { close: '"""', escape: true } },
5860 };
5961 }
60 scanTomlValue(line, consumed.end, out, toml);
62 scanConfigValue(line, consumed.end, out, dialect);
6163 return { tokens: out.done(), state };
6264 }
63 scanTomlValue(line, from, out, toml);
65 scanConfigValue(line, from, out, dialect);
6466 return { tokens: out.done(), state };
6567 }
6668
67 export function scanTomlValue(
68 line: string,
69 from: number,
70 out: Out,
71 toml: boolean,
72 ): void {
73 const n = line.length;
74 let i = from;
75 while (i < n) {
76 const c = line.charCodeAt(i);
77 if (isSpaceCh(c)) {
78 const j = spaceRun(line, i);
79 out.push("plain", line.slice(i, j));
80 i = j;
81 continue;
82 }
83 if (c === 35 || (!toml && c === 59)) {
84 out.push("comment", line.slice(i));
85 return;
86 }
87 if (c === 34 || c === 39) {
88 const consumed = consumeStringBody(line, i + 1, {
89 close: line[i]!,
90 escape: c === 34,
91 });
92 out.push("string", line.slice(i, consumed.end));
93 i = consumed.end;
94 continue;
95 }
96 if (isDigitCh(c) || (c === 45 && isDigitCh(line.charCodeAt(i + 1)))) {
97 let j = scanNumber(line, c === 45 ? i + 1 : i);
98 while (j < n && /[-:.TZ0-9+]/.test(line[j]!)) j++;
99 out.push("number", line.slice(i, j));
100 i = j;
101 continue;
102 }
103 if (isAlphaCh(c) || c === 95) {
104 let j = i;
105 while (j < n && (isIdentCh(line.charCodeAt(j)) || line[j] === "-")) j++;
106 const word = line.slice(i, j);
107 out.push(
108 word === "true" || word === "false" ? "constant" : "plain",
109 word,
110 );
111 i = j;
112 continue;
113 }
114 if (c === 91 || c === 93 || c === 123 || c === 125 || c === 44)
115 out.push("punctuation", line[i]!);
116 else out.push("operator", line[i]!);
117 i++;
118 }
119 }
120
12169 // ---- HTML ----
src/web/highlight/value.ts added +99
@@ -0,0 +1,99 @@
1 /** The value scanner TOML, INI and YAML share: strings, numbers, words, punctuation. */
2 import { isAlphaCh, isDigitCh, isIdentCh, isSpaceCh } from "./chars.ts";
3 import { consumeStringBody, scanNumber, spaceRun } from "./lexing.ts";
4 import { Out } from "./tokens.ts";
5
6 export type ValueDialect = "toml" | "ini" | "yaml";
7
8 const YAML_CONSTANTS = new Set([
9 "true",
10 "false",
11 "null",
12 "yes",
13 "no",
14 "on",
15 "off",
16 "~",
17 ]);
18
19 /** Does a comment open at `i`? YAML needs `#` at a word start; INI also takes `;`. */
20 function opensComment(line: string, i: number, dialect: ValueDialect): boolean {
21 const c = line.charCodeAt(i);
22 if (dialect === "yaml")
23 return c === 35 && (i === 0 || isSpaceCh(line.charCodeAt(i - 1)));
24 return c === 35 || (dialect === "ini" && c === 59);
25 }
26
27 function isConstant(word: string, dialect: ValueDialect): boolean {
28 if (dialect === "yaml") return YAML_CONSTANTS.has(word.toLowerCase());
29 return word === "true" || word === "false";
30 }
31
32 export function scanConfigValue(
33 line: string,
34 from: number,
35 out: Out,
36 dialect: ValueDialect,
37 ): void {
38 const n = line.length;
39 let i = from;
40 while (i < n) {
41 const c = line.charCodeAt(i);
42 if (isSpaceCh(c)) {
43 const j = spaceRun(line, i);
44 out.push("plain", line.slice(i, j));
45 i = j;
46 continue;
47 }
48 if (opensComment(line, i, dialect)) {
49 out.push("comment", line.slice(i));
50 return;
51 }
52 if (c === 34 || c === 39) {
53 const consumed = consumeStringBody(line, i + 1, {
54 close: line[i]!,
55 escape: c === 34,
56 });
57 out.push("string", line.slice(i, consumed.end));
58 i = consumed.end;
59 continue;
60 }
61 if (dialect === "yaml" && (c === 38 || c === 42)) {
62 // Anchor (&name) or alias (*name).
63 let j = i + 1;
64 while (j < n && isIdentCh(line.charCodeAt(j))) j++;
65 if (j > i + 1) {
66 out.push("meta", line.slice(i, j), "variable");
67 i = j;
68 continue;
69 }
70 }
71 if (dialect === "yaml" && c === 33) {
72 let j = i;
73 while (j < n && !isSpaceCh(line.charCodeAt(j))) j++;
74 out.push("type", line.slice(i, j));
75 i = j;
76 continue;
77 }
78 if (isDigitCh(c) || (c === 45 && isDigitCh(line.charCodeAt(i + 1)))) {
79 let j = scanNumber(line, c === 45 ? i + 1 : i);
80 // A date reads as a number too: 2026-09-01.
81 while (j < n && /[-:.TZ0-9+]/.test(line[j]!)) j++;
82 out.push("number", line.slice(i, j));
83 i = j;
84 continue;
85 }
86 if (isAlphaCh(c) || c === 95) {
87 let j = i;
88 while (j < n && (isIdentCh(line.charCodeAt(j)) || line[j] === "-")) j++;
89 const word = line.slice(i, j);
90 out.push(isConstant(word, dialect) ? "constant" : "plain", word);
91 i = j;
92 continue;
93 }
94 if (c === 123 || c === 125 || c === 91 || c === 93 || c === 44)
95 out.push("punctuation", line[i]!);
96 else out.push("operator", line[i]!);
97 i++;
98 }
99 }
src/web/highlight/yaml.ts +3 −77
@@ -1,8 +1,8 @@
11 /** YAML scanner. */
2 import { isAlphaCh, isDigitCh, isIdentCh, isSpaceCh } from "./chars.ts";
3 import { consumeStringBody, indentOf, scanNumber, spaceRun } from "./lexing.ts";
2 import { indentOf } from "./lexing.ts";
43 import { type ScanState, type Scanned } from "./state.ts";
54 import { Out } from "./tokens.ts";
5 import { scanConfigValue } from "./value.ts";
66
77 export const YAML_KEY =
88 /^(\s*(?:-\s+)*)((?:"[^"]*"|'[^']*'|[^\s:#][^:#]*?))(\s*:)(\s|$)/;
@@ -60,82 +60,8 @@ export function scanYaml(line: string, state: ScanState): Scanned {
6060 };
6161 }
6262
63 scanYamlValue(line, i, out);
63 scanConfigValue(line, i, out, "yaml");
6464 return { tokens: out.done(), state };
6565 }
6666
67 export function scanYamlValue(line: string, from: number, out: Out): void {
68 const n = line.length;
69 let i = from;
70 while (i < n) {
71 const c = line.charCodeAt(i);
72 if (isSpaceCh(c)) {
73 const j = spaceRun(line, i);
74 out.push("plain", line.slice(i, j));
75 i = j;
76 continue;
77 }
78 if (c === 35 /* # */ && (i === 0 || isSpaceCh(line.charCodeAt(i - 1)))) {
79 out.push("comment", line.slice(i));
80 return;
81 }
82 if (c === 34 || c === 39) {
83 const consumed = consumeStringBody(line, i + 1, {
84 close: line[i]!,
85 escape: c === 34,
86 });
87 out.push("string", line.slice(i, consumed.end));
88 i = consumed.end;
89 continue;
90 }
91 if (c === 38 /* & */ || c === 42 /* * */) {
92 let j = i + 1;
93 while (j < n && isIdentCh(line.charCodeAt(j))) j++;
94 if (j > i + 1) {
95 out.push("meta", line.slice(i, j), "variable");
96 i = j;
97 continue;
98 }
99 }
100 if (c === 33 /* ! */) {
101 let j = i;
102 while (j < n && !isSpaceCh(line.charCodeAt(j))) j++;
103 out.push("type", line.slice(i, j));
104 i = j;
105 continue;
106 }
107 if (isDigitCh(c) || (c === 45 && isDigitCh(line.charCodeAt(i + 1)))) {
108 let j = c === 45 ? i + 1 : i;
109 j = scanNumber(line, j);
110 // A date reads as a number too: 2026-09-01.
111 while (j < n && /[-:.TZ0-9+]/.test(line[j]!)) j++;
112 out.push("number", line.slice(i, j));
113 i = j;
114 continue;
115 }
116 if (isAlphaCh(c) || c === 95) {
117 let j = i;
118 while (j < n && (isIdentCh(line.charCodeAt(j)) || line[j] === "-")) j++;
119 const word = line.slice(i, j);
120 const lower = word.toLowerCase();
121 const isConst =
122 lower === "true" ||
123 lower === "false" ||
124 lower === "null" ||
125 lower === "yes" ||
126 lower === "no" ||
127 lower === "on" ||
128 lower === "off" ||
129 lower === "~";
130 out.push(isConst ? "constant" : "plain", word);
131 i = j;
132 continue;
133 }
134 if (c === 123 || c === 125 || c === 91 || c === 93 || c === 44)
135 out.push("punctuation", line[i]!);
136 else out.push("operator", line[i]!);
137 i++;
138 }
139 }
140
14167 // ---- TOML and INI ----
test/highlight-value.test.ts added +28
@@ -0,0 +1,28 @@
1 import { expect, test } from "bun:test";
2 import { scanConfigValue } from "../src/web/highlight/value.ts";
3 import { Out } from "../src/web/highlight/tokens.ts";
4
5 function kinds(text: string, dialect: "toml" | "ini" | "yaml") {
6 const out = new Out();
7 scanConfigValue(text, 0, out, dialect);
8 return out.done().map((t) => [t.cls, t.text]);
9 }
10
11 test("only INI treats ; as a comment", () => {
12 expect(kinds("1 ; x", "ini").at(-1)).toEqual(["comment", "; x"]);
13 expect(kinds("1 ; x", "toml").some(([k]) => k === "comment")).toBe(false);
14 });
15
16 test("only YAML reads anchors, tags and yes/no", () => {
17 expect(kinds("&a yes", "yaml")).toEqual([
18 ["meta", "&a"],
19 ["plain", " "],
20 ["constant", "yes"],
21 ]);
22 expect(kinds("yes", "toml")).toEqual([["plain", "yes"]]);
23 });
24
25 test("YAML # opens a comment only after a space", () => {
26 expect(kinds("a#b", "yaml").some(([k]) => k === "comment")).toBe(false);
27 expect(kinds("a #b", "yaml").at(-1)).toEqual(["comment", "#b"]);
28 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs