Skip to content

Commit

Permalink
Feat/spark g4 (#168)
Browse files Browse the repository at this point in the history
* feat: spark g4 test

* fix: fixed build lint

---------

Co-authored-by: liuyi <[email protected]>
Co-authored-by: dilu <[email protected]>
  • Loading branch information
3 people committed Oct 8, 2023
1 parent 0a9a7d1 commit 05da14d
Show file tree
Hide file tree
Showing 18 changed files with 27,912 additions and 26,927 deletions.
52 changes: 7 additions & 45 deletions src/grammar/spark/SparkSqlLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -26,52 +26,14 @@ lexer grammar SparkSqlLexer;
*/
public has_unclosed_bracketed_comment = false;
/**
* Verify whether current token is a valid decimal token (which contains dot).
* Returns true if the character that follows the token is not a digit or letter or underscore.
*
* For example:
* For char stream "2.3", "2." is not a valid decimal token, because it is followed by digit '3'.
* For char stream "2.3_", "2.3" is not a valid decimal token, because it is followed by '_'.
* For char stream "2.3W", "2.3" is not a valid decimal token, because it is followed by 'W'.
* For char stream "12.0D 34.E2+0.12 " 12.0D is a valid decimal token because it is followed
* by a space. 34.E2 is a valid decimal token because it is followed by symbol '+'
* which is not a digit or letter or underscore.
*/
public isValidDecimal() {
const nextChar = _input.LA(1);
if (nextChar >= 'A' && nextChar <= 'Z' || nextChar >= '0' && nextChar <= '9' ||
nextChar == '_') {
return false;
} else {
return true;
}
}

/**
* This method will be called when we see '/*' and try to match it as a bracketed comment.
* If the next character is '+', it should be parsed as hint later, and we cannot match
* it as a bracketed comment.
*
* Returns true if the next character is '+'.
*/
public isHint() {
const nextChar = _input.LA(1);
if (nextChar == '+') {
return true;
} else {
return false;
}
}

/**
* This method will be called when the character stream ends and try to find out the
* unclosed bracketed comment.
* If the method be called, it means the end of the entire character stream match,
* and we set the flag and fail later.
*/
public markUnclosedComment() {
has_unclosed_bracketed_comment = true;
this.has_unclosed_bracketed_comment = true;
}
}

Expand Down Expand Up @@ -488,26 +450,26 @@ INTEGER_VALUE

EXPONENT_VALUE
: DIGIT+ EXPONENT
| DECIMAL_DIGITS EXPONENT {isValidDecimal()}?
| DECIMAL_DIGITS EXPONENT
;

DECIMAL_VALUE
: DECIMAL_DIGITS {isValidDecimal()}?
: DECIMAL_DIGITS
;

FLOAT_LITERAL
: DIGIT+ EXPONENT? 'F'
| DECIMAL_DIGITS EXPONENT? 'F' {isValidDecimal()}?
| DECIMAL_DIGITS EXPONENT? 'F'
;

DOUBLE_LITERAL
: DIGIT+ EXPONENT? 'D'
| DECIMAL_DIGITS EXPONENT? 'D' {isValidDecimal()}?
| DECIMAL_DIGITS EXPONENT? 'D'
;

BIGDECIMAL_LITERAL
: DIGIT+ EXPONENT? 'BD'
| DECIMAL_DIGITS EXPONENT? 'BD' {isValidDecimal()}?
| DECIMAL_DIGITS EXPONENT? 'BD'
;

IDENTIFIER
Expand Down Expand Up @@ -540,7 +502,7 @@ SIMPLE_COMMENT
;

BRACKETED_COMMENT
: '/*' {!isHint()}? ( BRACKETED_COMMENT | . )*? ('*/' | {markUnclosedComment();} EOF) -> channel(HIDDEN)
: '/*' ( BRACKETED_COMMENT | . )*? ('*/' | {this.markUnclosedComment();} EOF) -> channel(HIDDEN)
;

WS
Expand Down
12 changes: 8 additions & 4 deletions src/grammar/spark/SparkSqlParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -1166,11 +1166,15 @@ windowSpec
RIGHT_PAREN
;

/**
* replace start identifier with start_ in grammar.
* https://github.com/tunnelvisionlabs/antlr4ts/issues/417
*/
windowFrame
: frameType=KW_RANGE start=frameBound
| frameType=KW_ROWS start=frameBound
| frameType=KW_RANGE KW_BETWEEN start=frameBound KW_AND end=frameBound
| frameType=KW_ROWS KW_BETWEEN start=frameBound KW_AND end=frameBound
: frameType=KW_RANGE start_=frameBound
| frameType=KW_ROWS start_=frameBound
| frameType=KW_RANGE KW_BETWEEN start_=frameBound KW_AND end=frameBound
| frameType=KW_ROWS KW_BETWEEN start_=frameBound KW_AND end=frameBound
;

frameBound
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@ export * from './lib/hive/HiveSqlParserListener';
export * from './lib/hive/HiveSqlParserVisitor';
export * from './lib/plsql/PlSqlParserListener';
export * from './lib/plsql/PlSqlParserVisitor';
export * from './lib/spark/SparkSqlVisitor';
export * from './lib/spark/SparkSqlListener';
export * from './lib/spark/SparkSqlParserVisitor';
export * from './lib/spark/SparkSqlParserListener';
export * from './lib/pgsql/PostgreSQLParserListener';
export * from './lib/pgsql/PostgreSQLParserVisitor';
export * from './lib/trinosql/TrinoSqlListener';
export * from './lib/trinosql/TrinoSqlVisitor';
export { SyntaxContextType } from './parser/common/basic-parser-types'
export { SyntaxContextType } from './parser/common/basic-parser-types';


export type * from './parser/common/basic-parser-types';
Expand Down
Loading

0 comments on commit 05da14d

Please sign in to comment.