melody-parser is an extensible JavaScript parser specifically designed for the Twig template language, generating a detailed Abstract Syntax Tree (AST) from source code. Currently at version 1.7.5, the library maintains a steady release cadence with focus on robustness and extensibility. A key differentiator is its ability to be extended with custom parsing rules via "extensions," allowing it to handle custom Twig tags or specialized template syntaxes beyond standard Twig. It also provides granular control over error handling for unknown tags, differentiating between strict parsing for code generation and lenient parsing for mere AST inspection. It is an essential component for projects requiring deep analysis, transformation, or tooling around Twig templates.
npm install melody-parserVerified import paths — ran on the pinned version, not inferred.
Demonstrates basic parsing, parsing with configuration options, and extending the parser with custom logic using `melody-extension-core` and a hypothetical custom extension.
To allow parsing of unknown tags into `GenericTwigTag` AST nodes, set the `allowUnknownTags` option to `true` in the parse function's options object: `parse(code, { allowUnknownTags: true })`. Alternatively, provide a custom extension to explicitly handle the unknown tag.Always install `melody-types` alongside `melody-parser`, adhering to the peer dependency version range specified (e.g., `npm install melody-types@^1.1.0`). Regularly update both packages to their latest compatible versions.
Enable the `allowUnknownTags` option: `parse(code, { allowUnknownTags: true })`. If 'mytag' is a custom tag you intend to support fully, create and pass a `melody-parser` extension that defines how to parse 'mytag'.