Parsers

Underlying subset of components for offering typical functionalities like:

Parsers

The parsers currently shipped in Compose are SQL focused. They power advanced SQL autocompletes and other functionalities.

Refer the SQL Parser Documentation for how they works and are built.

Live Demo

Import

Import the parser you need with something like below and run it on an SQL statement:

import dbsqlAutocompleteParser as sqlAutocompleteParser from 'gethue/parse/sql/dbsql/dbsqlAutocompleteParser';

const beforeCursor = 'SELECT col1, col2, tbl2.col3 FROM tbl; '; // Note extra space at end
const afterCursor = '';
const debug = false;

console.log(
  JSON.stringify(
    sqlAutocompleteParser.parseSql(beforeCursor, afterCursor, debug),
    null,
    2
  )
);

Which then will output keywords suggestions and all the known locations:

{
  "locations": [
    {
      "type": "statement",
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 1,
        "last_column": 38
      }
    },
    {
      "type": "statementType",
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 1,
        "last_column": 7
      },
      "identifier": "SELECT"
    },
    {
      "type": "selectList",
      "missing": false,
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 8,
        "last_column": 29
      }
    },
    {
      "type": "column",
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 8,
        "last_column": 12
      },
      "identifierChain": [
        {
          "name": "col1"
        }
      ],
      "qualified": false,
      "tables": [
        {
          "identifierChain": [
            {
              "name": "tbl"
            }
          ]
        }
      ]
    },
    {
      "type": "column",
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 14,
        "last_column": 18
      },
      "identifierChain": [
        {
          "name": "col2"
        }
      ],
      "qualified": false,
      "tables": [
        {
          "identifierChain": [
            {
              "name": "tbl"
            }
          ]
        }
      ]
    },
    {
      "type": "column",
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 20,
        "last_column": 24
      },
      "identifierChain": [
        {
          "name": "tbl2"
        }
      ],
      "qualified": false,
      "tables": [
        {
          "identifierChain": [
            {
              "name": "tbl"
            }
          ]
        }
      ]
    },
    {
      "type": "complex",
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 25,
        "last_column": 29
      },
      "identifierChain": [
        {
          "name": "tbl2"
        },
        {
          "name": "col3"
        }
      ],
      "qualified": true,
      "tables": [
        {
          "identifierChain": [
            {
              "name": "tbl"
            }
          ]
        }
      ]
    },
    {
      "type": "table",
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 35,
        "last_column": 38
      },
      "identifierChain": [
        {
          "name": "tbl"
        }
      ]
    },
    {
      "type": "whereClause",
      "missing": true,
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 38,
        "last_column": 38
      }
    },
    {
      "type": "limitClause",
      "missing": true,
      "location": {
        "first_line": 1,
        "last_line": 1,
        "first_column": 38,
        "last_column": 38
      }
    }
  ],
  "lowerCase": false,
  "suggestKeywords": [
    {
      "value": "ADD",
      "weight": -1
    },
    {
      "value": "ALTER",
      "weight": -1
    },
    {
      "value": "ANALYZE TABLE",
      "weight": -1
    },
    {
      "value": "CACHE",
      "weight": -1
    },
    {
      "value": "CLEAR",
      "weight": -1
    },
    {
      "value": "CLONE",
      "weight": -1
    },
    {
      "value": "CONVERT",
      "weight": -1
    },
    {
      "value": "COPY",
      "weight": -1
    },
    {
      "value": "CREATE",
      "weight": -1
    },
    {
      "value": "DELETE",
      "weight": -1
    },
    {
      "value": "DENY",
      "weight": -1
    },
    {
      "value": "DESCRIBE",
      "weight": -1
    },
    {
      "value": "DROP",
      "weight": -1
    },
    {
      "value": "EXPLAIN",
      "weight": -1
    },
    {
      "value": "EXPORT",
      "weight": -1
    },
    {
      "value": "FROM",
      "weight": -1
    },
    {
      "value": "FSCK",
      "weight": -1
    },
    {
      "value": "GENERATE",
      "weight": -1
    },
    {
      "value": "GRANT",
      "weight": -1
    },
    {
      "value": "IMPORT",
      "weight": -1
    },
    {
      "value": "INSERT",
      "weight": -1
    },
    {
      "value": "LIST",
      "weight": -1
    },
    {
      "value": "LOAD",
      "weight": -1
    },
    {
      "value": "MERGE",
      "weight": -1
    },
    {
      "value": "MSCK",
      "weight": -1
    },
    {
      "value": "OPTIMIZE",
      "weight": -1
    },
    {
      "value": "REFRESH",
      "weight": -1
    },
    {
      "value": "RELOAD FUNCTION",
      "weight": -1
    },
    {
      "value": "RESET",
      "weight": -1
    },
    {
      "value": "RESTORE",
      "weight": -1
    },
    {
      "value": "REVOKE",
      "weight": -1
    },
    {
      "value": "SELECT",
      "weight": -1
    },
    {
      "value": "SET",
      "weight": -1
    },
    {
      "value": "SHOW",
      "weight": -1
    },
    {
      "value": "TRUNCATE",
      "weight": -1
    },
    {
      "value": "UNCACHE",
      "weight": -1
    },
    {
      "value": "UPDATE",
      "weight": -1
    },
    {
      "value": "USE",
      "weight": -1
    },
    {
      "value": "VACUUM",
      "weight": -1
    },
    {
      "value": "WITH",
      "weight": -1
    }
  ],
  "definitions": []
}

Example

A full example can be found in the demo app README.

Note: parsers can be imported via the repository but the lib is not yet published in the public component.

import sqlParserRepository from 'gethue/parse/sql/sqlParserRepository';

const sqlAutocompleteParser = sqlParserRepository.getAutocompleteParser('dbsql);
...

Formatter

import { format } from 'sql-formatter';

console.log(format('SELECT * FROM tbl'));

This will output:

SELECT
  *
FROM
  tbl

Pretty Print

<sql-text :value="sql_statement" :dialect="dbsql" />

Predict next keywords

WIP