File D:\maven-qcr-plugin-testsunitaires\qcr4java\h2-1.3.170-sources\target\site\qc\lecode\bnf\RuleFixed.java.html

1 /*
2 * Copyright 2004-2011 H2 Group. Multiple-Licensed under the H2 License,
3 * Version 1.0, and under the Eclipse Public License, Version 1.0
4 * (http://h2database.com/html/license.html).
5 * Initial Developer: H2 Group
6 */
7 package org.h2.bnf;
8
9 import java.util.HashMap;
10
11 /**
12 * Represents a hard coded terminal rule in a BNF object.
13 */
14 public class RuleFixed implements Rule {
15
16 public static final int YMD = 0, HMS = 1, NANOS = 2;
17 public static final int ANY_EXCEPT_SINGLE_QUOTE = 3;
18 public static final int ANY_EXCEPT_DOUBLE_QUOTE = 4;
19 public static final int ANY_UNTIL_EOL = 5;
20 public static final int ANY_UNTIL_END = 6;
21 public static final int ANY_WORD = 7;
22 public static final int ANY_EXCEPT_2_DOLLAR = 8;
23 public static final int HEX_START = 10, CONCAT = 11, AZ_UNDERSCORE = 12, AF = 13, DIGIT = 14;
24 public static final int OPEN_BRACKET = 15, CLOSE_BRACKET = 16;
25
26 private final int type;
27
28 RuleFixed(int type) {
29 this.type = type;
30 }
31
32 public void accept(BnfVisitor visitor) {
33 visitor.visitRuleFixed(type);
34 }
35
36 public void setLinks(HashMap<String, RuleHead> ruleMap) {
37 // nothing to do
38 }
39
40 public boolean autoComplete(Sentence sentence) {
41 sentence.stopIfRequired();
42 String query = sentence.getQuery();
43 String s = query;
44 switch(type) {
45 case YMD:
46 while (s.length() > 0 && "0123456789-".indexOf(s.charAt(0)) >= 0) {
47 s = s.substring(1);
48 }
49 if (s.length() == 0) {
50 sentence.add("2006-01-01", "1", Sentence.KEYWORD);
51 }
52 break;
53 case HMS:
54 while (s.length() > 0 && "0123456789:".indexOf(s.charAt(0)) >= 0) {
55 s = s.substring(1);
56 }
57 if (s.length() == 0) {
58 sentence.add("12:00:00", "1", Sentence.KEYWORD);
59 }
60 break;
61 case NANOS:
62 while (s.length() > 0 && Character.isDigit(s.charAt(0))) {
63 s = s.substring(1);
64 }
65 if (s.length() == 0) {
66 sentence.add("nanoseconds", "0", Sentence.KEYWORD);
67 }
68 break;
69 case ANY_EXCEPT_SINGLE_QUOTE:
70 while (true) {
71 while (s.length() > 0 && s.charAt(0) != '\'') {
72 s = s.substring(1);
73 }
74 if (s.startsWith("''")) {
75 s = s.substring(2);
76 } else {
77 break;
78 }
79 }
80 if (s.length() == 0) {
81 sentence.add("anything", "Hello World", Sentence.KEYWORD);
82 sentence.add("'", "'", Sentence.KEYWORD);
83 }
84 break;
85 case ANY_EXCEPT_2_DOLLAR:
86 while (s.length() > 0 && !s.startsWith("$$")) {
87 s = s.substring(1);
88 }
89 if (s.length() == 0) {
90 sentence.add("anything", "Hello World", Sentence.KEYWORD);
91 sentence.add("$$", "$$", Sentence.KEYWORD);
92 }
93 break;
94 case ANY_EXCEPT_DOUBLE_QUOTE:
95 while (true) {
96 while (s.length() > 0 && s.charAt(0) != '\"') {
97 s = s.substring(1);
98 }
99 if (s.startsWith("\"\"")) {
100 s = s.substring(2);
101 } else {
102 break;
103 }
104 }
105 if (s.length() == 0) {
106 sentence.add("anything", "identifier", Sentence.KEYWORD);
107 sentence.add("\"", "\"", Sentence.KEYWORD);
108 }
109 break;
110 case ANY_WORD:
111 while (s.length() > 0 && !Character.isSpaceChar(s.charAt(0))) {
112 s = s.substring(1);
113 }
114 if (s.length() == 0) {
115 sentence.add("anything", "anything", Sentence.KEYWORD);
116 }
117 break;
118 case HEX_START:
119 if (s.startsWith("0X") || s.startsWith("0x")) {
120 s = s.substring(2);
121 } else if ("0".equals(s)) {
122 sentence.add("0x", "x", Sentence.KEYWORD);
123 } else if (s.length() == 0) {
124 sentence.add("0x", "0x", Sentence.KEYWORD);
125 }
126 break;
127 case CONCAT:
128 if (s.equals("|")) {
129 sentence.add("||", "|", Sentence.KEYWORD);
130 } else if (s.startsWith("||")) {
131 s = s.substring(2);
132 } else if (s.length() == 0) {
133 sentence.add("||", "||", Sentence.KEYWORD);
134 }
135 break;
136 case AZ_UNDERSCORE:
137 if (s.length() > 0 && (Character.isLetter(s.charAt(0)) || s.charAt(0) == '_')) {
138 s = s.substring(1);
139 }
140 if (s.length() == 0) {
141 sentence.add("character", "A", Sentence.KEYWORD);
142 }
143 break;
144 case AF:
145 if (s.length() > 0) {
146 char ch = Character.toUpperCase(s.charAt(0));
147 if (ch >= 'A' && ch <= 'F') {
148 s = s.substring(1);
149 }
150 }
151 if (s.length() == 0) {
152 sentence.add("hex character", "0A", Sentence.KEYWORD);
153 }
154 break;
155 case DIGIT:
156 if (s.length() > 0 && Character.isDigit(s.charAt(0))) {
157 s = s.substring(1);
158 }
159 if (s.length() == 0) {
160 sentence.add("digit", "1", Sentence.KEYWORD);
161 }
162 break;
163 case OPEN_BRACKET:
164 if (s.length() == 0) {
165 sentence.add("[", "[", Sentence.KEYWORD);
166 } else if (s.charAt(0) == '[') {
167 s = s.substring(1);
168 }
169 break;
170 case CLOSE_BRACKET:
171 if (s.length() == 0) {
172 sentence.add("]", "]", Sentence.KEYWORD);
173 } else if (s.charAt(0) == ']') {
174 s = s.substring(1);
175 }
176 break;
177 // no autocomplete support for comments
178 // (comments are not reachable in the bnf tree)
179 case ANY_UNTIL_EOL:
180 case ANY_UNTIL_END:
181 default:
182 throw new AssertionError("type="+type);
183 }
184 if (!s.equals(query)) {
185 while (s.length() > 0 && Character.isSpaceChar(s.charAt(0))) {
186 s = s.substring(1);
187 }
188 sentence.setQuery(s);
189 return true;
190 }
191 return false;
192 }
193
194 }
195