Skip to content

Latest commit

 

History

History
216 lines (167 loc) · 5.51 KB

AST.md

File metadata and controls

216 lines (167 loc) · 5.51 KB

AST for JSONC

See details: ../src/parser/ast.ts

Node

interface BaseJSONNode {
    type: string;
    loc: SourceLocation;
    range: [number, number];
}

All nodes have type, range, loc and parent properties according to ESLint - The AST specification.

Identifiers

JSONIdentifier

export interface JSONIdentifier extends BaseJSONNode {
    type: "JSONIdentifier"
    name: string
    parent:
        | JSONArrayExpression
        | JSONProperty
        | JSONExpressionStatement
        | JSONUnaryExpression
}

This node is Identifier for JSON.

Literals

JSONLiteral

export interface JSONLiteralBase extends BaseJSONNode {
    type: "JSONLiteral"
    value: string | number | boolean | null
    regex: 
        | {
            pattern: string
            flags: string
        }
        | null
    bigint: string | null
    raw: string
    parent:
        | JSONArrayExpression
        | JSONProperty
        | JSONExpressionStatement
        | JSONUnaryExpression
        | JSONBinaryExpression
}

This node is Literal for JSON.

JSONTemplateLiteral

export interface JSONTemplateLiteral extends BaseJSONNode {
    type: "JSONTemplateLiteral"
    quasis: [JSONTemplateElement]
    expressions: []
    parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
}

This node is TemplateLiteral for JSON. This parser can only parse static values. You cannot use template literals in actual JSON, JSONC and JSON5.

JSONTemplateElement

export interface JSONTemplateElement extends BaseJSONNode {
    type: "JSONTemplateElement"
    tail: boolean
    value: {
        cooked: string
        raw: string
    }
    parent: JSONTemplateLiteral
}

This node is TemplateElement for JSON.

Expressions

JSONExpression

export type JSONExpression =
    | JSONArrayExpression
    | JSONObjectExpression
    | JSONLiteral
    | JSONUnaryExpression
    | ( JSONIdentifier & { name: "Infinity" | "NaN" | "undefined" } )
    | JSONTemplateLiteral
    | JSONBinaryExpression

This parser can parse "Infinity", "NaN" and "undefined" as values. But you can't use these values in actual JSON, JSONC and JSON5.

JSONObjectExpression

export interface JSONObjectExpression extends BaseJSONNode {
    type: "JSONObjectExpression"
    properties: JSONProperty[]
    parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
}

This node is ObjectExpression for JSON.

JSONProperty

export interface JSONProperty extends BaseJSONNode {
    type: "JSONProperty"
    key: JSONIdentifier | ( JSONLiteral & { value: string | number } )
    value: JSONExpression
    kind: "init"
    method: false
    shorthand: false
    computed: false
    parent: JSONObjectExpression
}

This node is Property for JSON.

JSONArrayExpression

export interface JSONArrayExpression extends BaseJSONNode {
    type: "JSONArrayExpression"
    elements: (JSONExpression | null)[]
    parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
}

This node is ArrayExpression for JSON.

JSONUnaryExpression

export interface JSONUnaryExpression extends BaseJSONNode {
    type: "JSONUnaryExpression"
    operator: "-" | "+"
    prefix: true
    argument: ( JSONLiteral & { value: number } ) | ( JSONIdentifier & { name: "Infinity" | "NaN" } )
    parent: JSONArrayExpression | JSONProperty | JSONExpressionStatement
}

This node is UnaryExpression for JSON.

Only "-" can be used by operator in JSON and JSONC.

JSONBinaryExpression

export interface JSONUnaryExpression extends BaseJSONNode {
    type: "JSONBinaryExpression"
    operator: "-" | "+" | "*" | "/" | "%" | "**"
    left: JSONNumberLiteral | JSONUnaryExpression | JSONBinaryExpression
    right: JSONNumberLiteral | JSONUnaryExpression | JSONBinaryExpression
    parent:
        | JSONArrayExpression
        | JSONProperty
        | JSONExpressionStatement
        | JSONUnaryExpression
        | JSONBinaryExpression
}

This node is BinaryExpression for JSON.

This parser can only parse binary expressions of static numbers. You cannot use binary expressions in actual JSON, JSONC and JSON5.

Statements

JSONExpressionStatement

export interface JSONExpressionStatement extends BaseJSONNode {
    type: "JSONExpressionStatement"
    expression: JSONExpression
    parent: JSONProgram
}

This node is ExpressionStatement for JSON. There is always one JSONExpressionStatement in one JSON.

Root

Program

extend interface JSONProgram extends BaseJSONNode {
    type: "Program"
    body: [JSONExpressionStatement]
}

The body of the Program node generated by this parser is an array of one JSONExpressionStatement.