Skip to content

Commit

Permalink
Fix #3129: detect whether a string is an identifier or not in the parser
Browse files Browse the repository at this point in the history
  • Loading branch information
Rixium committed Apr 12, 2021
1 parent 60178da commit 7c89b6c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 23 deletions.
Expand Up @@ -63,8 +63,11 @@ protected String readKeyword(PeekingReader reader, Delimiter delimiter, ParserCo

@Override
protected Boolean detectCanExecuteInTransaction(String simplifiedStatement, List<Token> keywords) {
String current = keywords.get(keywords.size() - 1).getText();
if ("BACKUP".equals(current) || "RESTORE".equals(current) || "RECONFIGURE".equals(current)) {
Token currentToken = keywords.get(keywords.size() - 1);
String current = currentToken.getText();

if (currentToken.getType() != TokenType.IDENTIFIER &&
("BACKUP".equals(current) || "RESTORE".equals(current) || "RECONFIGURE".equals(current))) {
return false;
}

Expand Down Expand Up @@ -131,4 +134,14 @@ private boolean isDistributedTransaction(List<Token> tokens, Token keyword, Stri
protected int getTransactionalDetectionCutoff() {
return Integer.MAX_VALUE;
}

@Override
protected char getOpeningIdentifierSymbol() {
return '[';
}

@Override
protected char getClosingIdentifierSymbol() {
return ']';
}
}
Expand Up @@ -83,6 +83,14 @@ protected char getAlternativeStringLiteralQuote() {
return 0;
}

protected char getOpeningIdentifierSymbol() {
return 0;
}

protected char getClosingIdentifierSymbol() {
return 0;
}

protected Set<String> getValidKeywords() {
return null;
}
Expand Down Expand Up @@ -358,9 +366,6 @@ protected int getLastKeywordIndex(List<Token> tokens, int endIndex) {
return -1;
}

/**
* Returns the last token at the given parensDepth. Skips comments and blank lines. Will return null if no token found.
*/
protected static Token getPreviousToken(List<Token> tokens, int parensDepth) {
for (int i = tokens.size()-1; i >= 0; i--) {
Token previousToken = tokens.get(i);
Expand All @@ -380,9 +385,6 @@ protected static Token getPreviousToken(List<Token> tokens, int parensDepth) {
return null;
}

/**
* Returns true if the previous token matches the tokenText
*/
protected static boolean lastTokenIs(List<Token> tokens, int parensDepth, String tokenText) {
Token previousToken = getPreviousToken(tokens, parensDepth);
if (previousToken == null) {
Expand All @@ -392,9 +394,6 @@ protected static boolean lastTokenIs(List<Token> tokens, int parensDepth, String
return tokenText.equals(previousToken.getText());
}

/**
* Returns true if the previous token is on the given line
*/
protected static boolean lastTokenIsOnLine(List<Token> tokens, int parensDepth, int line) {
Token previousToken = getPreviousToken(tokens, parensDepth);
if (previousToken == null) {
Expand All @@ -408,9 +407,6 @@ protected static boolean tokenAtIndexIs(List<Token> tokens, int index, String to
return tokens.get(index).getText().equals(tokenText);
}

/**
* Check if the previous tokens in the statement at the same depth as the current token match the provided regex
*/
protected static boolean doTokensMatchPattern(List<Token> previousTokens, Token current, Pattern regex) {
ArrayList<String> tokenStrings = new ArrayList<>();
tokenStrings.add(current.getText());
Expand Down Expand Up @@ -468,9 +464,6 @@ protected Boolean detectCanExecuteInTransaction(String simplifiedStatement, List






private Token readToken(PeekingReader reader, PositionTracker tracker, ParserContext context) throws IOException {
int pos = tracker.getPos();
int line = tracker.getLine();
Expand Down Expand Up @@ -546,6 +539,10 @@ private Token readToken(PeekingReader reader, PositionTracker tracker, ParserCon
if (isDelimiter(peek, context, col)) {
return handleDelimiter(reader, context, pos, line, col);
}
if (isOpeningIdentifier(c)) {
String text = readIdentifier(reader);
return new Token(TokenType.IDENTIFIER, pos, line, col, text, text, context.getParensDepth());
}
if (isLetter(c, context)) {
String text = readKeyword(reader, context.getDelimiter(), context);
if (reader.peek('.')) {
Expand Down Expand Up @@ -576,6 +573,10 @@ protected String readKeyword(PeekingReader reader, Delimiter delimiter, ParserCo
return "" + (char) reader.read() + reader.readKeywordPart(delimiter, context);
}

protected String readIdentifier(PeekingReader reader) throws IOException {
return "" + (char) reader.read() + reader.readUntilIncluding(getClosingIdentifierSymbol());
}

protected Token handleDelimiter(PeekingReader reader, ParserContext context, int pos, int line, int col) throws IOException {
String text = context.getDelimiter().getDelimiter();
reader.swallow(text.length());
Expand All @@ -594,16 +595,14 @@ protected boolean isLetter(char c, ParserContext context) {
return (c == '_' || context.isLetter(c));
}

private boolean isOpeningIdentifier(char c) {
return c == getOpeningIdentifierSymbol();
}

protected boolean isSingleLineComment(String peek, ParserContext context, int col) {
return peek.startsWith("--");
}

/**
* Checks whether this is a keyword ({@code true}) or not ({@code false} = identifier, ...).
*
* @param text The token to check.
* @return {@code true} if it is, {@code false} if not.
*/
protected boolean isKeyword(String text) {
for (int i = 0; i < text.length(); i++) {
char c = text.charAt(i);
Expand Down

0 comments on commit 7c89b6c

Please sign in to comment.