Skip to content

Instantly share code, notes, and snippets.

@mmun
Created November 5, 2014 21:30
Show Gist options
  • Save mmun/4e498b46b70c38978181 to your computer and use it in GitHub Desktop.
Save mmun/4e498b46b70c38978181 to your computer and use it in GitHub Desktop.

Handlebars AST

This is a work in progress attempt to formalize the Handlebars AST in the style of the SpiderMonkey Parser API.

Basic

interface Node {
    type: string;
    loc: SourceLocation | null;
}

interface SourceLocation {
    source: string | null;
    start: Position;
    end: Position;
}

interface Position {
    line: uint >= 1;
    column: uint >= 0;
}

Programs

interface Program <: Node {
    type: "Program";
    body: [ Statement ];
}

Statements

interface Statement <: Node { }

interface MustacheStatement <: Statement {
    type: "MustacheStatement";
    sexpr: Subexpression;
    escaped: boolean;
}

interface BlockStatement <: Statement {
    type: "BlockStatement";
    sexpr: Subexpression;
    program: Program;
    inverse: Program | null;
}

interface PartialStatement <: Statement {
    type: "PartialStatement";
    sexpr: Subexpression;
}

interface ContentStatement <: Statement {
    type: "ContentStatement";
    value: string;
    original: string;
}

interface CommentStatement <: Statement {
    type: "CommentStatement";
    value: string;
}

Expressions

interface Expression <: Node { }

Subexpressions

interface Subexpression <: Expression {
    type: "Subexpression";
    path: PathExpression;
    params: [ Expression ];
    hash: Hash;
}

Paths

interface PathExpression <: Expression {
    type: "PathExpression";
    data: boolean;
    depth: uint >= 0;
    parts: [ string ];
    original: string;
}

Literals

interface Literal <: Expression { }

interface StringLiteral <: Literal {
    type: "StringLiteral";
    value: string;
}

interface BooleanLiteral <: Literal {
    type: "BooleanLiteral";
    value: boolean;
}

interface NumberLiteral <: Literal {
    type: "NumberLiteral";
    value: number;
}

Miscellaneous

interface Hash <: Node {
    type: "Hash";
    pairs: [ HashPair ];
}

interface HashPair <: Node {
    type: "HashPair";
    key: string;
    value: Expression;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment