{"library":"pug-parser","title":"Pug Parser","description":"pug-parser is a core component of the Pug templating engine, responsible for transforming an array of tokens (generated by `pug-lexer`) into an Abstract Syntax Tree (AST). This package is currently at version 6.0.0, which was published six years ago. While the broader Pug ecosystem (the `pug` package) is actively maintained with regular updates and is considered a key ecosystem project, `pug-parser` as a standalone npm package has not seen direct updates in several years. Development for `pug-parser` continues as part of the `pugjs/pug` monorepo, meaning new features or fixes for the parsing logic are integrated into the main `pug` package. Its primary differentiator is its foundational role as the parsing layer within the Pug templating process, providing the structured AST that `pug-code-gen` then converts into renderable JavaScript functions.","language":"javascript","status":"maintenance","last_verified":"Sun Apr 19","install":{"commands":["npm install pug-parser"],"cli":null},"imports":["import parse from 'pug-parser';","import parse from 'pug-parser';\nconst parserInstance = new parse.Parser(tokens, options);","const parse = require('pug-parser');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"const lex = require('pug-lexer');\nconst parse = require('pug-parser');\n\nconst filename = 'my-file.pug';\nconst src = 'div(data-foo=\"bar\")\\n  span.text-muted Hello world';\n\ntry {\n  // First, lex the source to get an array of tokens\n  const tokens = lex(src, { filename });\n\n  // Then, parse the tokens to convert them into an AST\n  const ast = parse(tokens, { filename, src });\n\n  console.log('Successfully parsed Pug source to AST:');\n  console.log(JSON.stringify(ast, null, '  '));\n\n  // Example of accessing parts of the AST\n  if (ast.nodes && ast.nodes.length > 0) {\n    console.log(`\\nRoot node type: ${ast.nodes[0].type}`);\n    console.log(`Root node name: ${ast.nodes[0].name}`);\n  }\n} catch (error) {\n  console.error('An error occurred during parsing:', error.message);\n  if (error.loc) {\n    console.error(`Error at line ${error.loc.line}, column ${error.loc.column} in ${error.loc.filename}`);\n  }\n}","lang":"javascript","description":"Demonstrates how to use `pug-lexer` to tokenize Pug source and then `pug-parser` to convert those tokens into a hierarchical Abstract Syntax Tree (AST).","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}