@ainc/syntax

A tool for analysis lexical and syntax!

Usage no npm install needed!

<script type="module">
  import aincSyntax from 'https://cdn.skypack.dev/@ainc/syntax';
</script>

README

@ainc/syntax

A tool for analysis lexical and syntax!

Install

$ yarn add @ainc/syntax

Usage

import * as syntax from '@ainc/syntax';

// tokenize source
const tokens = syntax.tokenize('const answer = 42;');

// token list
expect(tokens).toEqual([
    {
        type: 'Keyword',
        value: 'const',
        start: 0,
        end: 5,
        loc: { start: [Object], end: [Object] }
    },
    {
        type: 'Identifier',
        value: 'answer',
        start: 6,
        end: 12,
        loc: { start: [Object], end: [Object] }
    },
    {
        type: 'Punctuator',
        value: '=',
        start: 13,
        end: 14,
        loc: { start: [Object], end: [Object] }
    },
    {
        type: 'Numeric',
        value: '42',
        start: 15,
        end: 17,
        loc: { start: [Object], end: [Object] }
    },
    {
        type: 'Punctuator',
        value: ';',
        start: 17,
        end: 18,
        loc: { start: [Object], end: [Object] }
    }
]);

// create source parser
const parser = new syntax.Parser({
    include: ['VariableDeclaration'],
    rules: {
        VariableDeclaration: [
            { type: 'Keyword', key: 'kind' },
            { type: 'Identifier', key: 'id' },
            { value: '=' },
            { type: 'ObjectDeclaration', key: 'init' },
            { value: ';', optional: true },
        ],
        ObjectDeclaration: [
            { value: '{' },
            { value: '}', include: ['PropertyDeclaration'], key: 'properties' },
        ],
        PropertyDeclaration: [
            { type: 'Identifier', key: 'key' },
            { value: ':' },
            { oneof: ['StringLiteral', 'NumericLiteral', 'BooleanLiteral', 'NullLiteral' ], key: 'value' },
            { value: ',', optional: true },
        ],
    },
});

// parse source
const program = parser.parse('const data = { name: "syntax" }');

// program ast
expect(program).toEqual({
    type: 'Program',
    start: 0,
    end: 31,
    loc: {
        start: { line: 1, column: 0 },
        end: { line: 1, column: 31 },
    },
    leadingComments: [],
    trailingComments: [],
    body: [
        {
            type: 'VariableDeclaration',
            start: 0,
            end: 31,
            loc: {
                start: { line: 1, column: 0 },
                end: { line: 1, column: 31 },
            },
            leadingComments: [],
            trailingComments: [],
            kind: {
                type: 'Keyword',
                value: 'const',
                start: 0,
                end: 5,
                loc: {
                    start: { line: 1, column: 0 },
                    end: { line: 1, column: 5 }
                },
                leadingComments: [],
                trailingComments: []
            },
            id: {
                type: 'Identifier',
                value: 'data',
                start: 6,
                end: 10,
                loc: {
                    start: { line: 1, column: 6 },
                    end: { line: 1, column: 10 }
                },
                leadingComments: [],
                trailingComments: []
            },
            init: {
                type: 'ObjectDeclaration',
                start: 13,
                end: 31,
                loc: {
                    start: { line: 1, column: 13 },
                    end: { line: 1, column: 31 }
                },
                leadingComments: [],
                trailingComments: [],
                properties: [
                    {
                        type: 'PropertyDeclaration',
                        start: 15,
                        end: 29,
                        loc: {
                            start: { line: 1, column: 15 },
                            end: { line: 1, column: 29 }
                        },
                        leadingComments: [],
                        trailingComments: [],
                        key: {
                            type: 'Identifier',
                            value: 'name',
                            start: 15,
                            end: 19,
                            loc: {
                                start: { line: 1, column: 15 },
                                end: { line: 1, column: 19 }
                            },
                            leadingComments: [],
                            trailingComments: []
                        },
                        value: {
                            type: 'String',
                            value: '\'syntax\'',
                            start: 21,
                            end: 29,
                            loc: {
                                start: { line: 1, column: 21 },
                                end: { line: 1, column: 29 }
                            },
                            leadingComments: [],
                            trailingComments: []
                        }
                    }
                ]
            }
        }
    ]
});