-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(YQL): add tokenize method for YQL (#205)
* feat(YQL): add tokenize method for yql * feat(YQL): code review * ci: add eslint rule
- Loading branch information
1 parent
82d75a2
commit 8c2f69a
Showing
5 changed files
with
68 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import {LexerConstructor, ParserSyntaxError} from './autocomplete-types'; | ||
import {Lexer as LexerType} from 'antlr4ng'; | ||
import {createLexer} from './query'; | ||
import {SqlErrorListener} from './sql-error-listener'; | ||
import {tokenDictionary} from '../databases/yql/helpers'; | ||
|
||
export type Token = { | ||
ruleName: string; | ||
startIndex: number; | ||
}; | ||
|
||
export type TokenizeResult = { | ||
tokens: Token[]; | ||
errors: ParserSyntaxError[]; | ||
}; | ||
|
||
const EOF = -1; | ||
|
||
export function tokenize<L extends LexerType>( | ||
Lexer: LexerConstructor<L>, | ||
symbolicNames: (string | null)[], | ||
query: string, | ||
): TokenizeResult { | ||
const lexer = createLexer(Lexer, query); | ||
const errorListener = new SqlErrorListener(tokenDictionary.SPACE); | ||
|
||
lexer.removeErrorListeners(); | ||
lexer.addErrorListener(errorListener); | ||
|
||
const tokens: Token[] = []; | ||
|
||
let done = false; | ||
do { | ||
const token = lexer.nextToken(); | ||
if (token === null || token.type === EOF) { | ||
done = true; | ||
} else { | ||
const tokenName = symbolicNames[token.type]; | ||
if (tokenName) { | ||
tokens.push({ | ||
ruleName: tokenName, | ||
startIndex: token.column, | ||
}); | ||
} | ||
} | ||
} while (!done); | ||
|
||
return { | ||
tokens, | ||
errors: errorListener.errors, | ||
}; | ||
} |