{"library":"lop","title":"lop","description":"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.","language":"javascript","status":"active","last_verified":"Sat Apr 25","install":{"commands":["npm install lop"],"cli":null},"imports":["import { Parser } from 'lop';","import { RegexTokeniser } from 'lop';","import { rules } from 'lop';","import { StringSource } from 'lop';","import * as lop from 'lop';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Parser, rules, tokenOfType, token, sequence, firstOf, rule } from 'lop';\nimport { RegexTokeniser } from 'lop';\n\n// Define tokeniser\nconst tokeniser = new RegexTokeniser([\n  { name: 'integer', regex: /(\\d+)/ },\n  { name: 'keyword', regex: /(if|then|else)/ },\n  { name: 'whitespace', regex: /(\\s+)/ },\n]);\n\nclass IntegerNode { constructor(value) { this.value = value; } }\nclass IfNode { constructor(condition, trueBranch, falseBranch) { this.condition = condition; this.trueBranch = trueBranch; this.falseBranch = falseBranch; } }\n\nconst integerRule = rules.then(\n  rules.tokenOfType('integer'),\n  (value) => new IntegerNode(parseInt(value, 10))\n);\n\nconst expressionRule = rules.rule(() =>\n  rules.firstOf('expression', integerRule, ifRule)\n);\n\nconst ifRule = rules.sequence(\n  rules.token('keyword', 'if'),\n  rules.sequence.cut(),\n  rules.sequence.capture(expressionRule),\n  rules.token('keyword', 'then'),\n  rules.sequence.capture(expressionRule),\n  rules.token('keyword', 'else'),\n  rules.sequence.capture(expressionRule)\n).map((condition, trueBranch, falseBranch) =>\n  new IfNode(condition, trueBranch, falseBranch)\n);\n\nconst parser = new Parser();\nconst tokens = tokeniser.tokenise('if 1 then 2 else 3');\ntry {\n  const result = parser.parseTokens(expressionRule, tokens);\n  console.log(result);\n} catch (e) {\n  console.error(e.message);\n}","lang":"javascript","description":"Demonstrates a simple parser using lop with a regex tokeniser and parser combinators, including handling of cut and custom token types.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}