Skip to content

Commit

Permalink
Merge pull request #59 from ni-ze/newWorld
Browse files Browse the repository at this point in the history
[ISSUE #60]Add parser unit tests
  • Loading branch information
ni-ze authored Jan 16, 2023
2 parents 31d92ba + d99e310 commit 8f8488e
Show file tree
Hide file tree
Showing 42 changed files with 1,960 additions and 249 deletions.
8 changes: 4 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ target/
*.iml
*.ipr
*.gz
*/rsqldb.cs


### NetBeans ###
nbproject/private/
Expand All @@ -29,7 +29,7 @@ nbdist/

node_modules/
_book/

rsqldb-parser/src/main/antlr4/com.alibaba.rsqldb.parser/gen/
SqlLexer.tokens
*/dependency-reduced-pom.xml
/rsqldb-disk/log/
/rsqldb-disk/server/rsqldb.cs

Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ LESS_SYMBOL: '<';
NOT_EQUAL_SYMBOL: '<>' | '!=';
GREATER_EQUAL_SYMBOL: '>=';
LESS_EQUAL_SYMBOL: '<=';
//support like

// Operators. Bit

Expand Down Expand Up @@ -100,6 +101,7 @@ AND: 'AND' | 'and';
IN: 'IN' | 'in';
INTO: 'INTO' | 'into';
LEFT: 'LEFT' | 'left';
INNER: 'INNER' | 'inner';
ON: 'ON' | 'on';
YEAR: 'YEAR' | 'year';
MONTH: 'MONTH' | 'month';
Expand Down Expand Up @@ -129,6 +131,7 @@ INTEGER
: NUM+
;

// '123', "1234"
QUOTED_NUMBER
: '\'' NUMBER '\''
| '"' NUMBER '"'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ tableProperties
;

tableProperty
: identifier EQUAL_SYMBOL literal
: identifier EQUAL_SYMBOL value
;

query
Expand All @@ -59,7 +59,7 @@ havingPhrase
: HAVING booleanExpression
;
joinPhrase
: (LEFT)? JOIN tableName (AS identifier)? ON joinCondition
: (LEFT | INNER)? JOIN tableName (AS identifier)? ON joinCondition
;

selectField
Expand All @@ -83,21 +83,22 @@ oneJoinCondition

booleanExpression
: booleanExpression (AND | OR) booleanExpression #jointExpression
| fieldName operator literal #operatorExpression
| fieldName operator value #operatorExpression
| fieldName IS NULL #isNullExpression
| fieldName BETWEEN NUMBER AND NUMBER #betweenExpression
| fieldName IN values #inExpression
| function operator literal #functionExpression
| function operator value #functionExpression
;

literal
: NULL #nullLiteral
| (TRUE | FALSE) #booleanLiteral
| STRING #stringLiteral
| VARIABLE #variableLiteral
| NUMBER #numberLiteral
| QUOTED_NUMBER #quotedNumberLiteral
| QUOTED_STRING #quotedStringLiteral
value
: NULL #nullValue
| (TRUE | FALSE) #booleanValue
| STRING #stringValue
| VARIABLE #variableValue
| NUMBER #numberValue
| QUOTED_NUMBER #quotedNumberValue
| QUOTED_STRING #quotedStringValue
| BACKQUOTED_STRING #backQuotedStringValue
;

function
Expand Down Expand Up @@ -142,7 +143,7 @@ timeunit


values
: LR_BRACKET literal (COMMA literal)* RR_BRACKET
: LR_BRACKET value (COMMA value)* RR_BRACKET
;

columnConstraint
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,10 @@ public List<Statement> parseStatement(String sql) throws SyntaxErrorException {
CommonTokenStream tokens = new CommonTokenStream(sqlLexer);
com.alibaba.rsqldb.parser.SqlParser parser = new com.alibaba.rsqldb.parser.SqlParser(tokens);

sqlLexer.removeErrorListeners();
sqlLexer.addErrorListener(new DefaultErrorListener());

parser.removeErrorListeners();
parser.addErrorListener(new DefaultErrorListener());

com.alibaba.rsqldb.parser.SqlParser.SqlStatementsContext statements = parser.sqlStatements();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,46 @@
*/
package com.alibaba.rsqldb.parser.impl;

import com.alibaba.rsqldb.common.exception.SyntaxErrorException;
import com.alibaba.rsqldb.parser.SqlParser;
import com.alibaba.rsqldb.parser.util.ParserUtil;
import org.antlr.v4.runtime.BaseErrorListener;
import org.antlr.v4.runtime.CharStream;
import org.antlr.v4.runtime.CommonToken;
import org.antlr.v4.runtime.RecognitionException;
import org.antlr.v4.runtime.Recognizer;
import org.antlr.v4.runtime.RuleContext;
import org.antlr.v4.runtime.misc.Interval;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DefaultErrorListener extends BaseErrorListener {
private static final Logger logger = LoggerFactory.getLogger(DefaultErrorListener.class);
private final KeyWordPredictor predictor = new KeyWordPredictor();

@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol, int line, int charPositionInLine, String msg, RecognitionException e) {
System.err.println("line " + line + ":" + charPositionInLine + " " + msg);
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg, RecognitionException e) {
if (predictor.apply(msg, e)) {
return;
}

CharStream inputStream = ((CommonToken) offendingSymbol).getInputStream();
String sql = inputStream.getText(Interval.of(0, 1000));

logger.error("error when parse sql. position=(line:{}, char index:{}), error msg:{}, \n {}", line, charPositionInLine, msg, sql);
if (e != null) {
throw new RuntimeException(e);
if (e.getOffendingToken() != null) {
String targetIdentifier = e.getOffendingToken().getText();
boolean keyWord = ParserUtil.isKeyWord(targetIdentifier);
if (keyWord) {
logger.error("{} is a keyWord, change it to `{}`", targetIdentifier, targetIdentifier);
}
}
throw new SyntaxErrorException(msg, e);
} else {
throw new SyntaxErrorException(msg);
}
}

}
Loading

0 comments on commit 8f8488e

Please sign in to comment.