|
| 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