CKTIS / JavaScript
Finite-state machines
A finite-state machine describes behavior through states and input-dependent transitions. The CSV parsing state machine example shows how explicit state distinguishes a field separator from a comma inside quotes and helps detect invalid input.
Version: A finite-state machine is a design pattern, not a versioned JavaScript feature. The CSV parser uses ECMAScript 2015 features and requires no browser APIs. The example's separate logging call uses host-provided console.log.
Syntax
state = transition(state, input);A deterministic finite-state machine (FSM) has a finite set of states, an initial state, and transition rules. The current state and the next input symbol uniquely determine the next transition. A rule can also append output or signal an error. This is a design pattern, not a separate JavaScript construct; state = transition(state, input) expresses its general idea.
The CSV parsing state machine example processes one record. START means the beginning of a field, BARE an unquoted field, QUOTED the inside of a quoted field, and AFTER_QUOTE the position after a possible closing quote. A comma separates fields outside QUOTED. In AFTER_QUOTE, a second quote appends one " character and restores QUOTED.
End of input also needs a rule: QUOTED means an unclosed field, while the other states allow the final field to be appended. This preserves an empty field after a trailing comma. Empty input means one empty field here. The parser rejects a quote in an unquoted field and characters other than a comma after a closing quote; it does not trim whitespace. It deliberately rejects every CR and LF, even inside quotes, so it is not a complete CSV file parser.
The set of control states is finite, but the buffers field and fields are not; the entire program with growing buffers is therefore not a machine with only finite memory. The example uses ECMAScript 2015 syntax or earlier, without Web APIs. The ECMAScript specification defines the language constructs used, not this parser's CSV rules; console.log is supplied by the runtime environment. Test valid transitions, errors, and end of input in every state.
Examples
function parseCsvRecord(text) {
const fields = [];
let field = "";
let state = "START";
for (const ch of text) {
if (ch === "\r" || ch === "\n") {
throw new SyntaxError("Line breaks are not supported");
}
switch (state) {
case "START":
if (ch === '"') state = "QUOTED";
else if (ch === ",") fields.push("");
else { field += ch; state = "BARE"; }
break;
case "BARE":
if (ch === ",") {
fields.push(field); field = ""; state = "START";
} else if (ch === '"') {
throw new SyntaxError("Quote in unquoted field");
} else field += ch;
break;
case "QUOTED":
if (ch === '"') state = "AFTER_QUOTE";
else field += ch;
break;
case "AFTER_QUOTE":
if (ch === '"') { field += ch; state = "QUOTED"; }
else if (ch === ",") {
fields.push(field); field = ""; state = "START";
} else throw new SyntaxError("Unexpected character after quote");
break;
default:
throw new Error("Invalid state");
}
}
if (state === "QUOTED") throw new SyntaxError("Unclosed quote");
fields.push(field);
return fields;
}
console.log(JSON.stringify(parseCsvRecord('Ada,"x,y","say ""hi""",')));Result
["Ada","x,y","say \"hi\"",""]
The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.