Registry / serialization / lop
library0.0jsnpmunverified

lop is a JavaScript parsing library using parser combinators that emphasizes helpful error messages. Current stable version is 0.4.2, with infrequent releases. Unlike many parser libraries (e.g., PEG.js, nearley), lop integrates directly with tokenisers and provides cut operators to control backtracking and produce precise error locations. Its unique selling point is the generation of detailed, file-and-line-anchored error messages when parsing fails. It supports both automatic regex-based tokenisation and custom tokenisers. The library is untyped (no TypeScript definitions). It is lightweight with no dependencies.

npm install lop
INSTALL
IMPORT
SIG · LOP
L
lop
serializationjavascriptv0.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

lop.Parser
import { Parser } from 'lop';
const lop = require('lop'); const Parser = lop.Parser;
lop does not have a default export; use named import or destructure from require.
RegexTokeniser
import { RegexTokeniser } from 'lop';
const RegexTokeniser = require('lop').RegexTokeniser;
Named export; same pattern as other symbols.
rules
import { rules } from 'lop';
const { rules } = require('lop');
rules object contains combinators like tokenOfType, sequence, firstOf.
StringSource
import { StringSource } from 'lop';
const StringSource = require('lop').StringSource;
Used for custom tokeniser input.
default (none)
import * as lop from 'lop';
import lop from 'lop';
No default export; namespace import works.

Demonstrates a simple parser using lop with a regex tokeniser and parser combinators, including handling of cut and custom token types.

import { Parser, rules, tokenOfType, token, sequence, firstOf, rule } from 'lop'; import { RegexTokeniser } from 'lop'; // Define tokeniser const tokeniser = new RegexTokeniser([ { name: 'integer', regex: /(\d+)/ }, { name: 'keyword', regex: /(if|then|else)/ }, { name: 'whitespace', regex: /(\s+)/ }, ]); class IntegerNode { constructor(value) { this.value = value; } } class IfNode { constructor(condition, trueBranch, falseBranch) { this.condition = condition; this.trueBranch = trueBranch; this.falseBranch = falseBranch; } } const integerRule = rules.then( rules.tokenOfType('integer'), (value) => new IntegerNode(parseInt(value, 10)) ); const expressionRule = rules.rule(() => rules.firstOf('expression', integerRule, ifRule) ); const ifRule = rules.sequence( rules.token('keyword', 'if'), rules.sequence.cut(), rules.sequence.capture(expressionRule), rules.token('keyword', 'then'), rules.sequence.capture(expressionRule), rules.token('keyword', 'else'), rules.sequence.capture(expressionRule) ).map((condition, trueBranch, falseBranch) => new IfNode(condition, trueBranch, falseBranch) ); const parser = new Parser(); const tokens = tokeniser.tokenise('if 1 then 2 else 3'); try { const result = parser.parseTokens(expressionRule, tokens); console.log(result); } catch (e) { console.error(e.message); }
Debug
Known issues
gotchaRules must be wrapped in lop.rule() to defer evaluation when referencing rules that are defined later.
fix
Use rules.rule(() => ...) or lop.rule(...) for mutually recursive rules.
affects: >=0.0.0
gotchaToken values are the first regex capture group; if your regex has no groups, value is undefined.
fix
Add a capture group to your regex: /(\d+)/ instead of /\d+/.
affects: >=0.0.0
gotchaThe lexer's rule order matters: the first matching rule produces the token. Unrecognized characters become 'unrecognisedCharacter' tokens.
fix
List specific patterns before generic ones (e.g., keywords before identifiers).
affects: >=0.0.0
deprecatedNo known deprecations in current version; the library is stable but low-maintenance.
affects: 0.4.2
gotchaError messages reference 'File', 'Line number', and 'Character number' only if tokens have source info from StringSource. Using raw strings without source produces less informative errors.
fix
Wrap input in new StringSource(string, 'description') before tokenising.
affects: >=0.0.0
Errors
Common errors & fixes
Error: Expected keyword "then" but got integer "42"
Parsing failed inside a rule after a cut, preventing backtracking.
fix
Check that the token sequence matches the rule exactly. The error indicates a specific mismatch after the cut point.
TypeError: lop.Parser is not a constructor
Using import or require incorrectly; likely used default import instead of named.
fix
Use const { Parser } = require('lop'); or import { Parser } from 'lop';
Error: First argument to rules.firstOf must be a string (name), followed by rules
Missing rule name as first argument to firstOf.
fix
Call rules.firstOf('expressionName', rule1, rule2) with a string name first.
Error: The lop module does not have a default export
Attempted import lop from 'lop' which is incorrect.
fix
Use import * as lop from 'lop' or import { specific } from 'lop'.
Upgrade
Version history
0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
2 hits · last 30 days
node
2
Resources
lop — npm install lop · libregistry