dfa
raw JSON → 1.2.0 verified Fri May 01 auth: no javascript maintenance
A JavaScript library that compiles a regular-expression-like syntax into fast deterministic finite automata (DFA). Version 1.2.0 is the latest as of 2025, with no active development in recent years. It is designed for pattern matching against non-string sequences (e.g., arrays of symbols) and provides a portable state machine representation. Supports common regex operators like alternation, concatenation, repetition, and tagging. Files are written in a custom CoffeeScript-like DSL and compiled to efficient match tables.
Common errors
error SyntaxError: Unexpected token 'export' ↓
cause Using require() on an ESM-only module without a transpiler.
fix
Use import syntax or set type: 'module' in package.json, or use dynamic import().
error TypeError: compile is not a function ↓
cause Using the default export incorrectly: e.g., import { compile } from 'dfa' instead of import compile from 'dfa/compile'.
fix
Use the correct import: import compile from 'dfa/compile'.
error Error: Failed to load module: dfa/compile ↓
cause The package is not recognized; possibly missing a build step or using an older bundler.
fix
Ensure your bundler (webpack, rollup, etc.) supports ESM and that the package is installed correctly.
Warnings
gotcha The package uses ESM-only imports. CommonJS require() will not work without additional configuration (e.g., using .default). ↓
fix Use import syntax or set up your environment to handle ESM packages. If you must use require(), use require('dfa/compile').default.
deprecated The package has not been updated since 2017 and the author has not responded to issues. No security fixes or new features are expected. ↓
fix Consider using an alternative library or forking the repository if updates are needed.
gotcha The compile function expects a string containing the DSL, not a file path. Passing a file path will result in a parse error. ↓
fix Read the file contents using fs.readFileSync or similar, then pass the string to compile.
gotcha The match() method returns an iterator of [start, end] arrays, but end is exclusive. Users often expect inclusive end indices. ↓
fix Adjust your logic: the match covers indices from start to end-1 inclusive.
gotcha Symbols must be integers; non-integer values (e.g., strings) are not supported and will cause unexpected behavior. ↓
fix Map your input values to integers before using match().
breaking In version 1.2.0, the package changed from CommonJS to ESM. Older versions used CJS and had different import behavior. ↓
fix If upgrading from <1.2.0, update import statements from require('dfa/compile') to import compile from 'dfa/compile'.
Install
npm install dfa yarn add dfa pnpm add dfa Imports
- compile wrong
import { compile } from 'dfa'correctimport compile from 'dfa/compile' - default wrong
const dfa = require('dfa')correctimport dfa from 'dfa' - compile (CJS) wrong
const compile = require('dfa/compile')correctconst compile = require('dfa/compile').default - dfa wrong
import { dfa } from 'dfa'correctimport dfa from 'dfa'; dfa.compile(...)
Quickstart
import compile from 'dfa/compile';
import fs from 'fs';
const machineCode = `
X = 0;
L = 1;
V = 2;
T = 3;
LV = 4;
LVT = 5;
M = 6;
decomposed = L V T?;
partial = LV T?;
composed = LVT;
main = (decomposed | partial | composed) M?;
`;
const stateMachine = compile(machineCode);
// Input as array of symbols (as per your symbol mapping)
const input = [0, 1, 2, 3, 0, 4, 6];
for (const [start, end] of stateMachine.match(input)) {
console.log(`Match from ${start} to ${end}`);
}