complete ts type, add test

This commit is contained in:
HSunboy
2019-05-14 16:57:09 +08:00
parent c20549970f
commit 41aed6b4ce
10 changed files with 314 additions and 1117 deletions

View File

@ -1,4 +1,32 @@
interface Location {
first_column: number,
first_line: number,
last_column: number,
last_line: number
}
declare enum sqlType {
Hive = 'hive',
None = 'sql',
Impala = 'impala'
}
interface Locations {
// 语句类型
type: string;
// 语法是否缺失
missing?: Boolean;
// 语句位置
location: Location;
}
export const parser:{
parseSql:Function
}
interface SuggestKeyword {
value: string;
weight: number;
}
interface CompleteParser {
parseSql: (preSql: string, sufSql: string, type: sqlType, d: Boolean) => CompleteResult
}
export interface CompleteResult {
locations: Locations[];
suggestKeywords: SuggestKeyword[];
}
export const parser:CompleteParser;

View File

@ -1,3 +1,29 @@
export const parser:{
parseSyntax:Function
}
import { Locations, SuggestKeyword } from "./sqlAutoCompleteParser";
declare enum sqlType {
Hive = 'hive',
None = 'sql',
Impala = 'impala'
}
interface SyntaxParser {
parseSyntax: (preSql: string, sufSql: string, type: sqlType, d: Boolean) => SyntaxResult | false
}
interface ExpectedWord {
text: string;
distance: number;
}
interface ErrorLoc {
first_column: number;
first_line: number;
last_column: number;
last_line: number;
}
export interface SyntaxResult {
text: string;
expected: ExpectedWord[];
incompleteStatement: Boolean;
loc: ErrorLoc,
token: string
}
export const parser: SyntaxParser;

View File

@ -1,41 +1,35 @@
// import * as sqlSyntaxParser from '../core/sqlSyntaxParser';
import * as sqlSyntaxParser from '../core/sqlSyntaxParser';
import * as sqlAutoCompleteParser from '../core/sqlAutoCompleteParser';
type sql = string | string [];
enum sqlType {
Hive = 'hive',
None = 'sql',
Impala = 'impala'
}
function sqlToParserArgs (sql: sql) {
let preSql = '', sufSql = '';
if(Object.prototype.toString.call(sql) == '[object Array]'){
preSql=sql[0];
sufSql=sql[1];
}
return [preSql, sufSql];
}
/**
* 自动补全提示
* @param {(string | Array<string>)} sql
* @param {string} [type="hive"]
* 校验语法
*/
function parseSyntax(sql, type) {
if (typeof type == "undefined") {
type = "hive"
}
let sql1=sql;
let sql2='';
if(sql instanceof Array){
sql1=sql[0];
sql2=sql[1];
}
// @ts-nocheck
return sqlSyntaxParser.parser.parseSyntax(sql1, sql2, type, false)
function parseSyntax(sql: sql, type:sqlType = sqlType.Hive): sqlSyntaxParser.SyntaxResult | false {
const parserArgs = sqlToParserArgs(sql);
return sqlSyntaxParser.parser.parseSyntax(parserArgs[0], parserArgs[1], type, false)
}
/**
* 自动补全提示
* @param {(string | Array<string>)} sql
* @param {string} [type="hive"]
*/
function parserSql(sql, type) {
if (typeof type == "undefined") {
type = "hive"
}
let sql1=sql;
let sql2='';
if(sql instanceof Array){
sql1=sql[0];
sql2=sql[1];
}
return sqlAutoCompleteParser.parser.parseSql(sql1, sql2, type, false)
function parserSql(sql: sql, type:sqlType = sqlType.Hive): sqlAutoCompleteParser.CompleteResult {
const parserArgs = sqlToParserArgs(sql);
return sqlAutoCompleteParser.parser.parseSql(parserArgs[0], parserArgs[1], type, false)
}
export {

View File

@ -13,10 +13,10 @@ start
union_stmt
=stmt:
=single_stmt:
(
words:(!kw_start word:. {return word})*
stmt:(comment:comment {return ''}/quote:quote {return quote}/";" {isSplit=true;return ";"})
words:text*
stmt:(comment:comment {return comment}/quote:quote {return quote}/";" {isSplit=true;return ";"})
{
const text=words.join("")+stmt;
let index=Math.max(lines.length-1,0);
@ -29,9 +29,9 @@ union_stmt
}
)* other:.*
{
const text=stmt.join("")+other.join("")
const text=single_stmt.join("")+other.join("")
let index=Math.max(lines.length-1,0);
lines[index]=lines[index]+other.join("");
lines[index]=(lines[index]||'')+other.join("");
return text;
}
@ -75,7 +75,7 @@ quote
text = !kw_start word:. {return word}
kw_start=KW_SINGLE_LINE_START/KW_MULTI_LINE_START/"\""/"'"/";"

File diff suppressed because it is too large Load Diff