{"id":12954,"library":"chevrotain-allstar","title":"Chevrotain Allstar Lookahead Strategy","description":"Chevrotain Allstar is a specialized plugin for the Chevrotain parser library, designed to enhance its lookahead capabilities. It implements the ALL(*) lookahead algorithm, a technique initially introduced for ANTLR4. Unlike Chevrotain's default LL(*k*) behavior, which has a bounded lookahead, ALL(*) offers unbounded lookahead, allowing the parser to resolve ambiguities in more complex grammars by examining an arbitrary number of upcoming tokens. The current stable version is 0.4.1. This library is specifically tailored for users who require more powerful lookahead than Chevrotain's built-in strategies provide, typically when dealing with highly ambiguous or context-sensitive grammar rules. Its release cadence is generally tied to the development of the Langium project and Chevrotain itself, making updates less frequent than a core library.","status":"active","version":"0.4.1","language":"javascript","source_language":"en","source_url":"https://github.com/langium/chevrotain-allstar","tags":["javascript","parser","lookahead","chevrotain","langium","typescript"],"install":[{"cmd":"npm install chevrotain-allstar","lang":"bash","label":"npm"},{"cmd":"yarn add chevrotain-allstar","lang":"bash","label":"yarn"},{"cmd":"pnpm add chevrotain-allstar","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Core parser library that this package extends; required as a peer dependency.","package":"chevrotain","optional":false}],"imports":[{"note":"The library primarily uses ES module syntax. While CommonJS might work with transpilation, direct require is less common and might lead to issues in pure ESM environments. It's designed for use with TypeScript.","wrong":"const LLStarLookaheadStrategy = require('chevrotain-allstar').LLStarLookaheadStrategy","symbol":"LLStarLookaheadStrategy","correct":"import { LLStarLookaheadStrategy } from 'chevrotain-allstar'"},{"note":"While LLStarLookaheadStrategy is a class, its instance conforms to Chevrotain's ILookaheadStrategy interface. Directly importing types from chevrotain-allstar might not be necessary if only the class is instantiated.","symbol":"LLStarLookaheadStrategy (type)","correct":"import type { ILookaheadStrategy } from 'chevrotain'"},{"note":"Used in conjunction with `chevrotain-allstar` as demonstrated in the quickstart, this is a core Chevrotain class, not part of `chevrotain-allstar`.","symbol":"EmbeddedActionsParser","correct":"import { EmbeddedActionsParser } from 'chevrotain'"}],"quickstart":{"code":"import { Lexer, createToken, EmbeddedActionsParser } from 'chevrotain';\nimport { LLStarLookaheadStrategy } from 'chevrotain-allstar';\n\nconst A = createToken({ name: \"A\", pattern: /A/ });\nconst B = createToken({ name: \"B\", pattern: /B/ });\nconst C = createToken({ name: \"C\", pattern: /C/ });\nconst WhiteSpace = createToken({ name: \"WhiteSpace\", pattern: /\\s+/, group: Lexer.SKIPPED });\n\nconst allTokens = [WhiteSpace, A, B, C];\n\nclass MyParser extends EmbeddedActionsParser {\n    constructor() {\n        super(allTokens, {\n            lookaheadStrategy: new LLStarLookaheadStrategy()\n        });\n        this.performSelfAnalysis();\n    }\n\n    // Example production demonstrating a choice that might benefit from unbounded lookahead\n    public statement = this.RULE('statement', () => {\n        this.OR([\n            { ALT: () => { this.CONSUME(A); this.CONSUME(B); } },\n            { ALT: () => { this.CONSUME(A); this.CONSUME(C); } }\n        ]);\n    });\n}\n\nconst lexer = new Lexer(allTokens);\nconst parser = new MyParser();\n\n// Example usage with a simple input\nconst inputText = \"AC\";\nconst lexingResult = lexer.tokenize(inputText);\nparser.input = lexingResult.tokens;\nconst cst = parser.statement();\n\nif (parser.errors.length > 0) {\n    console.error(\"Parsing errors:\\n\" + parser.errors.map(err => err.message).join('\\n'));\n} else {\n    console.log(\"Parsing successful. CST:\\n\", JSON.stringify(cst, null, 2));\n}\n","lang":"typescript","description":"This quickstart demonstrates how to integrate `LLStarLookaheadStrategy` into a Chevrotain parser, showing a simple grammar with a choice that can utilize the unbounded lookahead. It defines tokens, a parser class, and then parses a sample input."},"warnings":[{"fix":"Profile your parser's performance with and without ALL(*) and consider if your grammar can be refactored to reduce ambiguity or if a bounded lookahead is sufficient. Only use ALL(*) for specific rules that genuinely require it.","message":"The ALL(*) lookahead strategy can introduce significant performance overhead compared to Chevrotain's default LL(*k*) bounded lookahead. Unbounded lookahead means the parser might examine a large portion of the input stream to resolve ambiguities, potentially slowing down parsing dramatically for complex grammars or large inputs. It should be used only when strictly necessary.","severity":"gotcha","affected_versions":">=0.1.0"},{"fix":"Always check the `chevrotain-allstar` release notes and `peerDependencies` when upgrading your core Chevrotain library. Ensure both packages are compatible versions.","message":"As a plugin for Chevrotain, `chevrotain-allstar`'s compatibility is tightly coupled to Chevrotain's major versions. A new major version of Chevrotain (e.g., v13) might introduce breaking changes in its parser API that could render older versions of `chevrotain-allstar` incompatible.","severity":"breaking","affected_versions":"All versions"},{"fix":"Thoroughly test your grammar and inputs after integrating ALL(*). Be aware of the theoretical underpinnings of ALL(*) and how it interacts with your grammar's specific ambiguities.","message":"The ALL(*) algorithm fundamentally changes how Chevrotain performs lookahead. This might lead to different parsing decisions than expected if you are migrating from an existing Chevrotain parser using LL(*k*) without fully understanding the implications of unbounded lookahead. Subtle ambiguities might be resolved differently.","severity":"gotcha","affected_versions":">=0.1.0"}],"env_vars":null,"last_verified":"2026-04-19T00:00:00.000Z","next_check":"2026-07-18T00:00:00.000Z","problems":[{"fix":"Install chevrotain: `npm install chevrotain` or `yarn add chevrotain`.","cause":"The `chevrotain` package is a peer dependency and must be installed explicitly.","error":"Cannot find module 'chevrotain' or its corresponding type declarations."},{"fix":"Ensure that `chevrotain-allstar` and `chevrotain` are compatible versions as specified in `chevrotain-allstar`'s peer dependencies. Update both packages if necessary.","cause":"Type mismatch, often due to incompatible versions of `chevrotain-allstar` and `chevrotain` or incorrect import.","error":"The 'lookaheadStrategy' property is not assignable to type 'ILookaheadStrategy'."},{"fix":"Ensure your parser class extends `EmbeddedActionsParser` (or `CstParser`) from `chevrotain` and that `LLStarLookaheadStrategy` is correctly instantiated and passed in the constructor options, as shown in the quickstart.","cause":"This is a hypothetical error indicating an internal issue or misuse where the parser instance is not correctly recognized by the strategy.","error":"Error: This grammar does not belong to the Chevrotain Allstar parser class hierarchy."}],"ecosystem":"npm","meta_description":null,"install_score":null,"install_tag":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":""}