Skip to content

Commit 5d20d06

Browse files
Define Reserved Keywords explicitly
Derive All Keywords from Grammar directly Generate production for Object Names (semi-) automatically Add parametrized Keyword Tests
1 parent 1b69d25 commit 5d20d06

11 files changed

Lines changed: 414 additions & 639 deletions

File tree

build.gradle

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,13 @@ task renderRR() {
176176
}
177177
}
178178
}
179+
180+
task updateKeywords(type: JavaExec) {
181+
group = "Execution"
182+
description = "Run the main class with JavaExecTask"
183+
classpath = sourceSets.main.runtimeClasspath
184+
main = "net.sf.jsqlparser.parser.ParserKeywordsUtils"
185+
}
179186

180187

181188
publishing {

src/main/java/net/sf/jsqlparser/parser/AbstractJSqlParser.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,4 @@ public void setErrorRecovery(boolean errorRecovery) {
4848
public List<ParseException> getParseErrors() {
4949
return parseErrors;
5050
}
51-
5251
}

src/main/java/net/sf/jsqlparser/parser/CCJSqlParserUtil.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,5 +257,4 @@ public static int getNestingDepth(String sql) {
257257
}
258258
return maxlevel;
259259
}
260-
261260
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package net.sf.jsqlparser.parser;
2+
3+
import java.util.*;
4+
5+
public class ParserKeywordsUtils {
6+
public final static int RESTRICTED_FUNCTION = 1;
7+
public final static int RESTRICTED_SCHEMA = 2;
8+
public final static int RESTRICTED_TABLE = 4;
9+
public final static int RESTRICTED_COLUMN = 8;
10+
public final static int RESTRICTED_EXPRESSION = 16;
11+
public final static int RESTRICTED_ALIAS = 32;
12+
public final static int RESTRICTED_SQL2016 = 64;
13+
14+
public final static int RESTRICTED_JSQLPARSER = 128
15+
| RESTRICTED_FUNCTION
16+
| RESTRICTED_SCHEMA
17+
| RESTRICTED_TABLE
18+
| RESTRICTED_COLUMN
19+
| RESTRICTED_EXPRESSION
20+
| RESTRICTED_ALIAS
21+
| RESTRICTED_SQL2016;
22+
23+
public static List<String> getDefinedKeywords() throws Exception {
24+
return CCJSqlParser.getDefinedKeywords();
25+
}
26+
27+
public static List<String> getReservedKeywords(int restriction) {
28+
return CCJSqlParser.getReservedKeywords(restriction);
29+
}
30+
31+
public final static void main(String[] args) {
32+
try {
33+
System.out.println( buildGrammarForRelObjectNameWithoutValue() );
34+
System.out.println( buildGrammarForRelObjectName() );
35+
} catch (Exception e) {
36+
e.printStackTrace();
37+
}
38+
}
39+
40+
public static String buildGrammarForRelObjectNameWithoutValue() throws Exception {
41+
TreeSet<String> allKeywords = new TreeSet<>();
42+
allKeywords.addAll(CCJSqlParser.getDefinedKeywords());
43+
for (String reserved: CCJSqlParser.getReservedKeywords(RESTRICTED_JSQLPARSER)) {
44+
allKeywords.remove(reserved);
45+
}
46+
47+
StringBuilder builder = new StringBuilder();
48+
builder.append("String RelObjectNameWithoutValue() :\n"
49+
+ "{ Token tk = null; }\n"
50+
+ "{\n"
51+
+ " ( tk=<S_IDENTIFIER> | tk=<S_QUOTED_IDENTIFIER> | tk=<K_DATE_LITERAL> | tk=<K_DATETIMELITERAL> | tk=<K_STRING_FUNCTION_NAME>\n"
52+
+ " ");
53+
54+
for (String keyword: allKeywords) {
55+
builder.append(" | tk=\"").append(keyword).append("\"");
56+
}
57+
58+
builder.append(" )\n"
59+
+ " { return tk.image; }\n"
60+
+ "}");
61+
62+
return builder.toString();
63+
}
64+
65+
public static String buildGrammarForRelObjectName() throws Exception {
66+
TreeSet<String> allKeywords = new TreeSet<>();
67+
for (String reserved: CCJSqlParser.getReservedKeywords(RESTRICTED_ALIAS)) {
68+
allKeywords.add(reserved);
69+
}
70+
71+
for (String reserved: CCJSqlParser.getReservedKeywords(RESTRICTED_JSQLPARSER & ~RESTRICTED_ALIAS)) {
72+
allKeywords.remove(reserved);
73+
}
74+
75+
StringBuilder builder = new StringBuilder();
76+
builder.append("String RelObjectName() :\n"
77+
+ "{ Token tk = null; String result = null; }\n"
78+
+ "{\n"
79+
+ " (result = RelObjectNameWithoutValue()\n"
80+
+ " ");
81+
82+
for (String keyword: allKeywords) {
83+
builder.append(" | tk=\"").append(keyword).append("\"");
84+
}
85+
86+
builder.append(" )\n"
87+
+ " { return tk!=null ? tk.image : result; }\n"
88+
+ "}");
89+
90+
return builder.toString();
91+
}
92+
}

0 commit comments

Comments
 (0)