Skip to content

Ast Lego Bricks

Lancine Doumbia edited this page Jul 23, 2022 · 4 revisions

1227am 7/23/2022

A list of common code pieces in ast form. Shoutout to btmills. More info

Programs

A complete program source tree.

{
    type: "Program";
    body: [ Statement ];
}

Functions

A function declaration or expression.

{
    type: "FunctionDeclaration" | "FunctionExpression"
    id: Identifier | null;
    params: [ Pattern ];
    defaults: [ Expression ];
    rest: Identifier | null;
    body: BlockStatement 
}
Syntax:
function <id>(<params>){
<body>
}

Statements

Any statement.

A block statement, i.e., a sequence of statements surrounded by braces.

{
    type: "BlockStatement";
    body: [ Statement ];
}
Syntax:
function e(){
<body>
}

An expression statement, i.e., a statement consisting of a single expression.

{
    type: "ExpressionStatement";
    expression: Expression;
}

An if statement. else statement is the alternate property key.

{
    type: "IfStatement";
    test: Expression;
    consequent: Statement;
    alternate: Statement | null;
}
Syntax:
if (<test>){
    <consequent>
} else if (<test>){
    <consequent>
} else {
    <alternate>
}
if(<test>){<consequent>}

A break statement.

{
    type: "BreakStatement";
}
Syntax:
break;

A continue statement.

{
    type: "ContinueStatement";
}
Syntax:
continue;

A switch statement.

The lexical flag is metadata indicating whether the switch statement contains any unnested let declarations (and therefore introduces a new lexical scope).

{
    type: "SwitchStatement";
    discriminant: Expression;
    cases: [ SwitchCase ];
}
Syntax:
switch(<discriminant>){
   <cases>
}

A return statement.

{
    type: "ReturnStatement";
    argument: Expression | null;
}
Syntax:
return <argument>

A throw statement.

{
    type: "ThrowStatement";
    argument: Expression;
}

A try statement.

{
    type: "TryStatement";
    block: BlockStatement;
    handler: CatchClause | null;
    finalizer: BlockStatement | null;
}

A while statement.

{
    type: "WhileStatement";
    test: Expression;
    body: Statement;
}
Syntax:
while(<test>){
   <body>
}

A do/while statement.

{
    type: "DoWhileStatement";
    body: Statement;
    test: Expression;
}

A for statement.

{
    type: "ForStatement";
    init: VariableDeclaration | Expression | null;
    test: Expression | null;
    update: Expression | null;
    body: Statement;
}
Syntax:
for(<init>;<test>;<update>){
   <body>
}

A for/in statement, or, if each is true, a for each/in statement.

{
    type: "ForInStatement";
    left: VariableDeclaration | Expression;
    right: Expression;
    body: Statement;
    each: boolean;
}

A for/of statement.

interface ForOfStatement <: Statement {
    type: "ForOfStatement";
    left: VariableDeclaration | Expression;
    right: Expression;
    body: Statement;
}

Declarations

Any declaration node.

A function declaration.

{
    type: "FunctionDeclaration";
    id: Identifier;
    params: [ Pattern ];
    defaults: [ Expression ];
    rest: Identifier | null;
    body: BlockStatement 
}

A variable declaration, via one of var, let, or const.

{
    type: "VariableDeclaration";
    declarations: [ VariableDeclarator ];
    kind: "var" | "let" | "const";
}

A variable declarator.

{
    type: "VariableDeclarator";
    id: Pattern;
    init: Expression | null;
}

Expressions

Any expression node.

A this expression.

{
    type: "ThisExpression";
}

An array expression.

{
    type: "ArrayExpression";
    elements: [ Expression | null ];
}

An object expression.

A literal property in an object expression can have either a string or number as its value.

Paired with the property key init

{
    type: "ObjectExpression";
    properties: [ Property ];
}
Syntax:
var obj = <properties>

A function expression.

{
    type: "FunctionExpression";
    id: Identifier | null;
    params: [ Pattern ];
    defaults: [ Expression ];
    rest: Identifier | null;
    body: BlockStatement
}

A fat arrow function expression, i.e., let foo = (bar) => { /* body */ }.

{
    type: "ArrowExpression";
    params: [ Pattern ];
    defaults: [ Expression ];
    rest: Identifier | null;
    body: BlockStatement | Expression;
    generator: boolean;
    expression: boolean;
}

A sequence expression, i.e., a comma-separated sequence of expressions.

{
    type: "SequenceExpression";
    expressions: [ Expression ];
}

A unary operator expression.

{
    type: "UnaryExpression";
    operator: UnaryOperator;
    prefix: boolean;
    argument: Expression;
}

UnaryOperators: "-" | "+" | "!" | "~" | "typeof" | "void" | "delete"

A binary operator expression.

{
    type: "BinaryExpression";
    operator: BinaryOperator;
    left: Expression;
    right: Expression;
}
Syntax:
(<left> <operator> <right>)

BinaryOperators: "==" | "!=" | "===" | "!==" | "<" | "<=" | ">" | ">=" | "<<" | ">>" | ">>>" | "+" | "-" | "*" | "/" | "%" | "&" | "|" | "^" | "in" | "instanceof"

An assignment operator expression.

{
    type: "AssignmentExpression";
    operator: AssignmentOperator;
    left: Expression;
    right: Expression;
}
Syntax:
(<left> <operator> <right>)

AssignmentOperators: "=" | "+=" | "-=" | "*=" | "/=" | "%=" | "<<=" | ">>=" | ">>>=" | "|=" | "^=" | "&="

An update (increment or decrement) operator expression. prefix === true if operator comes before the expression; prefix === false if operator comes after

{
    type: "UpdateExpression";
    operator: UpdateOperator;
    argument: Expression;
    prefix: boolean;
}
Syntax:
<operator><argument>  |  <argument><operator>

UpdateOperators: "++" | "--"

A logical operator expression.

{
    type: "LogicalExpression";
    operator: LogicalOperator;
    left: Expression;
    right: Expression;
}

LogicalOperator: "||" | "&&"

Syntax:
(<left> <operator> <right>)

A conditional expression, i.e., a ternary ?/: expression.

{
    type: "ConditionalExpression";
    test: Expression;
    alternate: Expression;
    consequent: Expression;
}
Syntax:
(<test>) ?  <consequent> : <alternate>

A new expression.

{
    type: "NewExpression";
    callee: Expression;
    arguments: [ Expression | null ];
}

A function or method call expression.

{
    type: "CallExpression";
    callee: Expression;
    arguments: [ Expression | null ];
}
Syntax:
<callee>(<arguments>)

A member expression.

{
    type: "MemberExpression";
    object: Expression;
    property: Identifier | Expression;
    computed: boolean;
}

Patterns

An object-destructuring pattern.

A literal property in an object pattern can have either a string or number as its value.

{
    type: "ObjectPattern";
    properties: [ PropertyPattern ];
}

An object property pattern.

{
    type: "PropertyPattern";
    key: Literal | Identifier;
    value: Pattern;
}

An array-destructuring pattern.

{
    type: "ArrayPattern";
    elements: [ Pattern | null ];
}

Clauses

A case (if test is an Expression) or default (if test === null) clause in the body of a switch statement.

{
    type: "SwitchCase";
    test: Expression | null;
    consequent: [ Statement ];
}
Syntax:
case <test>:
    <consequent>
    <type BreakStmt/ReturnStmt optional>
default:  
    <consequent>

Miscellaneous

An identifier.

{
    type: "Identifier";
    name: string;
}

A literal token. For number literal tokens, decimal numbers and scientific notation inputs are also accepted

{
    type: "Literal";
    value: string | boolean | null | number | RegExp;
}

An object property initializer. kind is init.

{
    type: "Property";
    key: Literal | Identifier;
    value: Expression;
    kind: "init" | "get" | "set";
}
Syntax:
var obj2 = {
    <key>:<value>
}

1015pm 7/13/2022

Building a separate scanner and tokenizer to see what I've learned from Vaidehi Joshi's blog.

616pm 7/16/2022

Separate scanner and tokenizer completed since 4pm. Syntactic Analyzer operation now in progress.

Clone this wiki locally