npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

ampscript-parser

v0.1.2

Published

AMPscript lexer and parser producing an AST for SFMC tooling

Readme

ampscript-parser

AMPscript lexer and parser that produces an AST for Salesforce Marketing Cloud (SFMC) tooling.

Handles all AMPscript embedding syntaxes:

  • Block syntax: %%[ ... ]%%
  • Script-tag syntax: <script runat="server" language="ampscript"> ... </script>
  • Inline expressions: %%=...=%%
  • Plain HTML/text content between AMPscript segments

This package is used internally by:

Installation

npm install ampscript-parser

Usage

import { parse, tokenizeBlock, parseStatements, TokenType } from 'ampscript-parser';

parse(text)

Parses a full document string (HTML with embedded AMPscript) into a Document AST. This is the main entry point for most use cases.

import { parse } from 'ampscript-parser';

const doc = parse(`
<p>Hello %%=AttributeValue('firstname')=%%</p>
%%[
  SET @greeting = "Welcome"
  IF @greeting == "Welcome" THEN
    Output(@greeting)
  ENDIF
]%%
`);

// doc.type === 'Document'
// doc.children — array of Content, Block, and InlineExpression nodes
for (const node of doc.children) {
    console.log(node.type); // 'Content' | 'Block' | 'InlineExpression'
}

AST node types

| Node type | Description | |---|---| | Document | Root node; has a children array | | Content | Plain HTML/text segment between AMPscript regions | | Block | A %%[ ]%% or script-tag block; has a statements array | | InlineExpression | A %%=...=%% expression; has an expression property |

tokenizeBlock(code, offset?)

Tokenizes a raw AMPscript code string (the content inside a block or inline expression, without the surrounding delimiters). Returns an array of token objects.

import { tokenizeBlock } from 'ampscript-parser';

const tokens = tokenizeBlock("SET @name = 'World'");

for (const token of tokens) {
    console.log(token.type);  // e.g. 'SET', 'VARIABLE', 'EQUALS', 'STRING'
    console.log(token.value); // raw text of the token
    console.log(token.start); // character offset in the source
    console.log(token.end);   // end offset
}

The optional offset parameter shifts all token positions by a base character offset, useful when the code snippet originates from a larger document.

parseStatements(tokens)

Parses an array of tokens (as returned by tokenizeBlock) into an array of statement AST nodes. Useful when you already have tokens and want to build the AST incrementally.

import { tokenizeBlock, parseStatements } from 'ampscript-parser';

const tokens = tokenizeBlock("VAR @x\nSET @x = Add(1, 2)");
const statements = parseStatements(tokens);

for (const stmt of statements) {
    console.log(stmt.type); // e.g. 'VarStatement', 'SetStatement'
}

TokenType

An object of token type constants used to identify tokens returned by tokenizeBlock.

import { TokenType } from 'ampscript-parser';

console.log(TokenType.SET);        // 'SET'
console.log(TokenType.IF);         // 'IF'
console.log(TokenType.VARIABLE);   // 'VARIABLE'
console.log(TokenType.STRING);     // 'STRING'
console.log(TokenType.NUMBER);     // 'NUMBER'
console.log(TokenType.BOOLEAN);    // 'BOOLEAN'
console.log(TokenType.IDENTIFIER); // 'IDENTIFIER'
console.log(TokenType.COMMENT);    // 'COMMENT'

Full list of token types: BLOCK_OPEN, BLOCK_CLOSE, INLINE_OPEN, INLINE_CLOSE, VAR, SET, IF, THEN, ELSEIF, ELSE, ENDIF, FOR, TO, DOWNTO, DO, NEXT, AND, OR, NOT, COMMA, LPAREN, RPAREN, EQUALS, EQ, NEQ, GT, LT, GTE, LTE, STRING, NUMBER, BOOLEAN, IDENTIFIER, VARIABLE, COMMENT, NEWLINE, WHITESPACE.

License

MIT