const TOKEN_KEYWORD = 'keyword' ; const TOKEN_KEYSPACE = 'entity.name.keyspace' ; const TOKEN_TABLE = 'entity.name.table' ; interface IToken { type : string ; value : string ; index ? : number ; start ? : number ; } export default class QueryTokenizer { private actionOptionsMap = { } ; constructor ( readonly editor ) { this . actionOptionsMap = { select : [ 'WHERE' , 'ORDER BY' , 'LIMIT' , 'ALLOW FILTERING' ] , insert : [ 'VALUES' , 'USING TTL' , 'TIMESTAMP' ] , update : [ 'SET' , 'USING TTL' , 'USING TIMESTAMP' , 'IF' , 'IF EXISTS' , 'IF NOT EXISTS' , ] , } ; } public getKeyspace ( ) { return this . getTokenValue ( TOKEN_KEYSPACE ) ; } public getTable ( ) { return this . getTokenValue ( TOKEN_TABLE ) ; } public isSelectStatement ( ) { return ! ! this . findTokenWithValue ( TOKEN_KEYWORD , 'select' ) ; } public isInsertStatement ( ) { return ! ! this . findTokenWithValue ( TOKEN_KEYWORD , 'insert' ) ; } public isUpdateStatement ( ) { return ! ! this . findTokenWithValue ( TOKEN_KEYWORD , 'update' ) ; } parma returns public findToken ( test ) { const rowCount = this . editor . session . getLength ( ) ; for ( let i = 0 ; i <= rowCount ; i ++ ) { const lineTokens = this . editor . session . getTokens ( i ) as IToken [ ] ; const token = typeof test === 'string' ? lineTokens . find ( ( tok ) => tok . type === test ) : lineTokens . find ( test ) ; if ( token ) { return token ; } } return null ; } param tokenType param tokenValue returns public findTokenWithValue ( tokenType , tokenValue ) { return this . findToken ( ( tok ) => tok . type === tokenType && tok . value . toLowerCase ( ) === tokenValue . toLowerCase ( ) ) ; } parma returns public filterTokens ( test ) { let allTokens = new Array < IToken > ( ) ; const rowCount = this . editor . session . getLength ( ) ; for ( let i = 0 ; i <= rowCount ; i ++ ) { const lineTokens = this . editor . session . getTokens ( i ) as IToken [ ] ; allTokens = allTokens . concat ( lineTokens ) ; } return typeof test === 'string' ? allTokens . filter ( ( tok ) => tok . type === test ) : allTokens . filter ( test ) ; } public getOptionsForKeyword ( keywordToken ) { return this . actionOptionsMap [ keywordToken . toLowerCase ( ) ] ; } returns public getActionToken ( ) { const actionTokenSet = new Set ( Object . keys ( this . actionOptionsMap ) ) ; return this . findToken ( ( tok ) => tok . type === 'keyword' && actionTokenSet . has ( tok . value . toLowerCase ( ) ) ) ; } /**
     * Checks the list of tokens in the editor to see if it contains an action (e.g. SELECT, INSERT, etc.).
     */ public containsActionToken ( ) { return ! ! this . getActionToken ( ) ; } param row returns public getTokensUpToRow ( row ) { let allPriorTokens = new Array < IToken > ( ) ; for ( let i = 0 ; i <= row ; i ++ ) { const lineTokens = this . editor . session . getTokens ( i ) ; allPriorTokens = allPriorTokens . concat ( lineTokens ) ; } return allPriorTokens ; } public getPreviousToken ( previousTokens , currentToken ) { if ( ! previousTokens || previousTokens . length === 0 ) { return null ; } const currentIndex = previousTokens . indexOf ( currentToken ) ; if ( currentIndex > 0 ) { return previousTokens [ currentIndex - 1 ] ; } return null ; } public getPreviousNonTextToken ( previousTokens , currentToken ) { let prev = currentToken ; while ( ( prev = this . getPreviousToken ( previousTokens , prev ) ) !== null ) { if ( prev . type !== 'text' && prev . type !== 'empty' ) { return prev ; } } return null ; } param tokenName returns private getTokenValue ( tokenName ) { const token = this . findToken ( tokenName ) ; return token ? token . value : null ; } }