This is the main entrypoint to this library. First, you instantiate an instance of this class with the SQL dialect you require. Then, use some of the getter methods described below to retrieve the ANTLR4ts classes. All grammars support multiple queries in a single string.
Requires the SQL dialect you will be parsing. See SQLDialect.
import { antlr4tsSQL, SQLDialect } from 'antlr4ts-sql';
const antlr4tssql = new antlr4tsSQL(SQLDialect.MYSQL);
Retrieve a CommonTokenStream for your string.
Essentially, the CommonTokenStream converts your SQL string to an array of “tokens” where a token can be individual SQL keywords, table or column names, a semicolon, etc. Depending on the SQL dialect, whitespace characters will either not be included in the CommonTokenStream (such as for T-SQL), or they will be included on a separate channel than the other tokens. This is due to how the ANTLR4 grammars have been written.
errorListeners
is an optional parameter that will set the errorListeners for the token stream.
To remove all error listeners, provide an empty array.
If this value is not provided, the default ConsoleErrorListener will be attached.
import { antlr4tsSQL, SQLDialect } from 'antlr4ts-sql';
const antlr4tssql = new antlr4tsSQL(SQLDialect.MYSQL);
const query = 'SELECT * FROM table1';
const tokens = anltr4tssql.getTokens(query);
console.log(tokens);
// CommonTokenStream {
// tokens: [],
// p: -1,
// fetchedEOF: false,
// _tokenSource: MySQLLexer {
// _listeners: [ ConsoleErrorListener {} ],
// _stateNumber: -1,
// _factory: CommonTokenFactory { copyText: false },
// _tokenStartCharIndex: -1,
// _tokenStartLine: 0,
// _tokenStartCharPositionInLine: 0,
// _hitEOF: false,
// _channel: 0,
// _type: 0,
// _modeStack: IntegerStack { _data: Int32Array(0) [], _size: 0 },
// _mode: 0,
// _input: ANTLRInputStream { p: 0, data: 'SELECT * FROM table1', n: 20 },
// _tokenFactorySourcePair: { source: [Circular], stream: [ANTLRInputStream] },
// inVersionComment: false,
// serverVersion: 50707,
// pendingTokens: [],
// charsets: [],
// _interp: LexerATNSimulator {
// atn: [ATN],
// optimize_tail_calls: true,
// startIndex: -1,
// _line: 1,
// _charPositionInLine: 0,
// mode: 0,
// prevAccept: [SimState],
// recog: [Circular]
// }
// },
// channel: 0
// }
Retrieves a Parser.
The Parser takes the tokens in the CommonTokenStream and attempts to match them, based on their sequence to the rules in the ANTL4 grammar. If it is able to match successfully, the tokens will be assigned a context based on the grammar rule that they matched.
errorListeners
is an optional parameter that will set the errorListeners for the parser.
To remove all error listeners, provide an empty array.
If this value is not provided, the default ConsoleErrorListener will be attached.
import { antlr4tsSQL, SQLDialect } from 'antlr4ts-sql';
const antlr4tssql = new antlr4tsSQL(SQLDialect.MYSQL);
const query = 'SELECT * FROM table1';
const tokens = anltr4tssql.getTokens(query);
const parser = anltr4tssql.getParser(tokens);
console.log(parser);
// MultiQueryMySQLParser {
// _listeners: [ ConsoleErrorListener {} ],
// _stateNumber: -1,
// _errHandler: DefaultErrorStrategy {
// errorRecoveryMode: false,
// lastErrorIndex: -1,
// nextTokensState: -1,
// lastErrorStates: undefined
// },
// _precedenceStack: IntegerStack { _data: Int32Array(4) [ 0, 0, 0, 0 ], _size: 1 },
// _buildParseTrees: true,
// _parseListeners: [],
// _syntaxErrors: 0,
// matchedEOF: false,
// _ctx: undefined,
// _input: CommonTokenStream {
// tokens: [],
// p: -1,
// fetchedEOF: false,
// _tokenSource: MySQLLexer {
// _listeners: [Array],
// _stateNumber: -1,
// _factory: [CommonTokenFactory],
// _tokenStartCharIndex: -1,
// _tokenStartLine: 0,
// _tokenStartCharPositionInLine: 0,
// _hitEOF: false,
// _channel: 0,
// _type: 0,
// _modeStack: [IntegerStack],
// _mode: 0,
// _input: [ANTLRInputStream],
// _tokenFactorySourcePair: [Object],
// inVersionComment: false,
// serverVersion: 50707,
// pendingTokens: [],
// charsets: [],
// _interp: [LexerATNSimulator]
// },
// channel: 0
// },
// serverVersion: 80021,
// _interp: ParserATNSimulator {
// atn: ATN {
// states: [Array],
// decisionToState: [Array],
// modeNameToStartState: Map {},
// modeToStartState: [],
// contextCache: [Array2DHashMap],
// decisionToDFA: [Array],
// modeToDFA: [],
// LL1Table: Map {},
// grammarType: 1,
// maxTokenType: 783,
// ruleToStartState: [Array],
// ruleToStopState: [Array]
// },
// predictionMode: 1,
// force_global_context: false,
// always_try_local_context: true,
// enable_global_context_dfa: false,
// optimize_unique_closure: true,
// optimize_ll1: true,
// optimize_tail_calls: true,
// tail_call_preserves_sll: true,
// treat_sllk1_conflict_as_ambiguity: false,
// reportAmbiguities: false,
// userWantsCtxSensitive: true,
// _parser: [Circular]
// }
// }
Initiates parsing by calling the initial rule in the grammar. Returns a ParseTree.
The parse tree will list the contexts that the parser has matched to the individual tokens in the SQL string.
import { antlr4tsSQL, SQLDialect } from 'antlr4ts-sql';
const antlr4tssql = new antlr4tsSQL(SQLDialect.MYSQL);
const query = 'SELECT * FROM table1';
const tokens = antlr4tssql.getTokens(query);
const parser = antlr4tssql.getParser(tokens);
const parseTree = antlr4tssql.getParseTree(parser);
console.log(parseTree);
// Sql_scriptContext {
// _parent: undefined,
// invokingState: -1,
// _start: CommonToken { _line: 1, _charPositionInLine: 0, _channel: 0,
// index: 0, _text: undefined, _type: 614,
// source: { source: [MySQLLexer], stream: [ANTLRInputStream] },
// start: 0, stop: 5
// },
// children: [
// QueryContext {
// _parent: [Circular], invokingState: 1212,
// _start: [CommonToken], children: [Array], _stop: [CommonToken]
// },
// TerminalNode { _symbol: [CommonToken], _parent: [Circular] }
// ],
// _stop: CommonToken { _line: 1, _charPositionInLine: 20, _channel: 0,
// index: 7, _text: undefined, _type: -1,
// source: { source: [MySQLLexer], stream: [ANTLRInputStream] },
// start: 20, stop: 19
// }
// }
Convenience method. Equivalent to calling getTokens and getParser.
import { antlr4tsSQL, SQLDialect } from 'antlr4ts-sql';
const antlr4tssql = new antlr4tsSQL(SQLDialect.MYSQL);
const query = 'SELECT * FROM table1';
const parser = anltr4tssql.getParserFromSQL(query);
console.log(parser);
// MultiQueryMySQLParser {
// _listeners: [ ConsoleErrorListener {} ],
// _stateNumber: -1,
// _errHandler: DefaultErrorStrategy {
// errorRecoveryMode: false,
// lastErrorIndex: -1,
// nextTokensState: -1,
// lastErrorStates: undefined
// },
// _precedenceStack: IntegerStack { _data: Int32Array(4) [ 0, 0, 0, 0 ], _size: 1 },
// _buildParseTrees: true,
// _parseListeners: [],
// _syntaxErrors: 0,
// matchedEOF: false,
// _ctx: undefined,
// _input: CommonTokenStream {
// tokens: [],
// p: -1,
// fetchedEOF: false,
// _tokenSource: MySQLLexer {
// _listeners: [Array],
// _stateNumber: -1,
// _factory: [CommonTokenFactory],
// _tokenStartCharIndex: -1,
// _tokenStartLine: 0,
// _tokenStartCharPositionInLine: 0,
// _hitEOF: false,
// _channel: 0,
// _type: 0,
// _modeStack: [IntegerStack],
// _mode: 0,
// _input: [ANTLRInputStream],
// _tokenFactorySourcePair: [Object],
// inVersionComment: false,
// serverVersion: 50707,
// pendingTokens: [],
// charsets: [],
// _interp: [LexerATNSimulator]
// },
// channel: 0
// },
// serverVersion: 80021,
// _interp: ParserATNSimulator {
// atn: ATN {
// states: [Array],
// decisionToState: [Array],
// modeNameToStartState: Map {},
// modeToStartState: [],
// contextCache: [Array2DHashMap],
// decisionToDFA: [Array],
// modeToDFA: [],
// LL1Table: Map {},
// grammarType: 1,
// maxTokenType: 783,
// ruleToStartState: [Array],
// ruleToStopState: [Array]
// },
// predictionMode: 1,
// force_global_context: false,
// always_try_local_context: true,
// enable_global_context_dfa: false,
// optimize_unique_closure: true,
// optimize_ll1: true,
// optimize_tail_calls: true,
// tail_call_preserves_sll: true,
// treat_sllk1_conflict_as_ambiguity: false,
// reportAmbiguities: false,
// userWantsCtxSensitive: true,
// _parser: [Circular]
// }
// }
Convenience method. Equivalent to calling getTokens , getParser and getParseTree.
import { antlr4tsSQL, SQLDialect } from 'antlr4ts-sql';
const antlr4tssql = new antlr4tsSQL(SQLDialect.MYSQL);
const query = 'SELECT * FROM table1';
const parseTree = antlr4tssql.getParseTreeFromSQL(query);
console.log(parseTree);
// Sql_scriptContext {
// _parent: undefined,
// invokingState: -1,
// _start: CommonToken { _line: 1, _charPositionInLine: 0, _channel: 0,
// index: 0, _text: undefined, _type: 614,
// source: { source: [MySQLLexer], stream: [ANTLRInputStream] },
// start: 0, stop: 5
// },
// children: [
// QueryContext {
// _parent: [Circular], invokingState: 1212,
// _start: [CommonToken], children: [Array], _stop: [CommonToken]
// },
// TerminalNode { _symbol: [CommonToken], _parent: [Circular] }
// ],
// _stop: CommonToken { _line: 1, _charPositionInLine: 20, _channel: 0,
// index: 7, _text: undefined, _type: -1,
// source: { source: [MySQLLexer], stream: [ANTLRInputStream] },
// start: 20, stop: 19
// }
// }
This class enumerates the SQL dialects that are supported. Possible values include:
MYSQL
TSQL // For SQL Server
PLSQL // For Oracle
PLpgSQL // For PostgreSQL
import { antlr4tsSQL, SQLDialect } from 'antlr4ts-sql';
const antlr4tssql = new antlr4tsSQL(SQLDialect.MYSQL);