{"library":"melody-parser","title":"Melody Twig Template Parser","description":"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.","language":"javascript","status":"active","last_verified":"Wed Apr 22","install":{"commands":["npm install melody-parser"],"cli":null},"imports":["import { parse } from 'melody-parser';","import { extension as coreExtensions } from 'melody-extension-core';","import customExtension from 'melody-extension-custom';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { parse } from 'melody-parser';\nimport { extension as coreExtensions } from 'melody-extension-core';\n\n// Example 1: Basic parsing\nconst twigCodeBasic = '{% spaceless %} Hello, {{ name }}! {% endspaceless %}';\nconst astBasic = parse(twigCodeBasic);\nconsole.log('Basic AST:', JSON.stringify(astBasic, null, 2));\n\n// Example 2: Parsing with options\nconst twigCodeWithOptions = '{# This is a comment #} {{ var }}';\nconst astWithOptions = parse(twigCodeWithOptions, {\n  ignoreComments: false, // Ensure comments are included in the AST\n  decodeEntities: true,\n});\nconsole.log('AST with options:', JSON.stringify(astWithOptions, null, 2));\n\n// Example 3: Parsing with extensions (core and a hypothetical custom one)\n// For a real custom extension, you'd implement its parser logic.\nconst customExtension = {\n  tags: {\n    exit: {\n      parse(parser, token) {\n        // Simplified parsing for an {% exit 404 %} tag\n        parser.advance(); // consume 'exit'\n        const parts = [parser.expression()];\n        parser.expect(token.type, 'end of tag');\n        return {\n          type: 'GenericTwigTag',\n          tagName: 'exit',\n          parts: parts,\n          sections: [],\n        };\n      }\n    }\n  }\n};\n\nconst twigCodeWithExtensions = '{% exit 404 %}';\nconst astWithExtensions = parse(\n  twigCodeWithExtensions,\n  { allowUnknownTags: true }, // Important for custom or unknown tags\n  coreExtensions,\n  customExtension\n);\nconsole.log('AST with extensions:', JSON.stringify(astWithExtensions, null, 2));","lang":"typescript","description":"Demonstrates basic parsing, parsing with configuration options, and extending the parser with custom logic using `melody-extension-core` and a hypothetical custom extension.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}