/plugins/JavaSideKick/tags/javasidekick-2-4-0/src/sidekick/property/parser/property/PropertyParser.java

# · Java · 484 lines · 395 code · 43 blank · 46 comment · 116 complexity · 7b1a129f1ea618701f108d64a56e9bfc MD5 · raw file

  1. /* Generated By:JavaCC: Do not edit this line. PropertyParser.java */
  2. package sidekick.property.parser.property;
  3. import java.util.*;
  4. import sidekick.util.Location;
  5. public class PropertyParser implements PropertyParserConstants {
  6. // accumulates parse exceptions
  7. private List<ParseException> exceptions = new ArrayList<ParseException>();
  8. // for testing...
  9. public static void main(String args[]) throws ParseException {
  10. PropertyParser parser = new PropertyParser(System.in);
  11. parser.Properties();
  12. }
  13. /**
  14. * Utility to trim horizontal whitespace from the front of a string.
  15. * @param the string to trim
  16. * @return the trimmed string
  17. */
  18. public String trimFront(String s) {
  19. if (s == null || s.length() == 0) {
  20. return s;
  21. }
  22. int index = 0;
  23. for (int i = 0; i < s.length(); i++) {
  24. if (s.charAt(i) == ' ' || s.charAt(i) == '\t')
  25. ++index;
  26. else
  27. break;
  28. }
  29. return s.substring(index);
  30. }
  31. /**
  32. * Setting the tab size makes the token locations more accurate.
  33. * @param size the size of the tabs in the current jEdit buffer
  34. */
  35. public void setTabSize(int size) {
  36. jj_input_stream.setTabSize(size);
  37. }
  38. /**
  39. * @return the current tab size that the parser is using
  40. */
  41. public int getTabSize() {
  42. return jj_input_stream.getTabSize(0);
  43. }
  44. /**
  45. * Creates a start location from the given token.
  46. * @param t a token
  47. * @return the start location of the token.
  48. */
  49. public Location createStartLocation(Token t) {
  50. if (t == null)
  51. return new Location(0, 0);
  52. return new Location(t.beginLine, t.beginColumn);
  53. }
  54. /**
  55. * Creates an end location from the given token.
  56. * @param t a token
  57. * @return the end location of the token.
  58. */
  59. public Location createEndLocation(Token t) {
  60. if (t == null)
  61. return new Location(0, 0);
  62. return new Location(t.endLine, t.endColumn);
  63. }
  64. /**
  65. * Add an exception to the list of exceptions. Rather than failing on
  66. * any exception, this parser will continue parsing and will accumulate
  67. * any/all exceptions.
  68. * @param pe a ParseException to accumulate
  69. */
  70. public void addException(ParseException pe) {
  71. if (pe != null)
  72. exceptions.add(pe);
  73. }
  74. /**
  75. * @return the list of accumulated ParseExceptions
  76. */
  77. public List<ParseException> getExceptions() {
  78. return exceptions;
  79. }
  80. final public List<Property> Properties() throws ParseException {
  81. List<Property> list = new ArrayList<Property>();
  82. Property p = null;
  83. label_1:
  84. while (true) {
  85. switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
  86. case COMMENT:
  87. case BARE_KEY:
  88. case KEY:
  89. ;
  90. break;
  91. default:
  92. jj_la1[0] = jj_gen;
  93. break label_1;
  94. }
  95. if (jj_2_1(2)) {
  96. Comment();
  97. } else {
  98. switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
  99. case BARE_KEY:
  100. case KEY:
  101. p = Property();
  102. if (p != null)
  103. list.add(p);
  104. break;
  105. default:
  106. jj_la1[1] = jj_gen;
  107. jj_consume_token(-1);
  108. throw new ParseException();
  109. }
  110. }
  111. }
  112. jj_consume_token(0);
  113. /* always sort the list by property name -- could make the caller do
  114. this and just return the properties in the original order */
  115. Collections.sort(list);
  116. {if (true) return list;}
  117. throw new Error("Missing return statement in function");
  118. }
  119. /**
  120. * @return a single property from the property file.
  121. */
  122. final public Property Property() throws ParseException {
  123. Token key = null;
  124. Token value = null;
  125. try {
  126. if (jj_2_2(2)) {
  127. key = jj_consume_token(KEY);
  128. token_source.SwitchTo(ParseEquals);
  129. jj_consume_token(EQUALS);
  130. token_source.SwitchTo(ParseValue);
  131. value = jj_consume_token(VALUE);
  132. } else {
  133. switch ((jj_ntk==-1)?jj_ntk():jj_ntk) {
  134. case BARE_KEY:
  135. key = jj_consume_token(BARE_KEY);
  136. token_source.SwitchTo(DEFAULT);
  137. break;
  138. default:
  139. jj_la1[2] = jj_gen;
  140. jj_consume_token(-1);
  141. throw new ParseException();
  142. }
  143. }
  144. Property prop = new Property();
  145. prop.setStartLocation(createStartLocation(key));
  146. prop.setEndLocation(createEndLocation(value == null ? key : value));
  147. /* key -- need to trim as the regex production can capture whitespace,
  148. and by definition, the key cannot contain any whitespace characters */
  149. String out = key.image.trim();
  150. /* key can have escaped 'equals' characters, unescape them */
  151. out = out.replaceAll("\\\\=", "=");
  152. out = out.replaceAll("\\\\:", ":");
  153. out = out.replaceAll("\\\\ ", " ");
  154. out = out.replaceAll("\\\\\\t", "\t");
  155. prop.setKey(out);
  156. /* value -- need to combine multi-line values into a single line. Leading
  157. whitespace on continuation lines is discarded, as is leading whitespace
  158. at the start of the value. */
  159. out = value == null ? "" : value.image.replaceAll("\\\\(\\s)+", "");
  160. out = trimFront(out);
  161. prop.setValue(out);
  162. {if (true) return prop;}
  163. } catch (ParseException e) {
  164. addException(generateParseException());
  165. }
  166. throw new Error("Missing return statement in function");
  167. }
  168. /* For completeness only. This parser does nothing special with comments. */
  169. final public void Comment() throws ParseException {
  170. jj_consume_token(COMMENT);
  171. }
  172. final private boolean jj_2_1(int xla) {
  173. jj_la = xla; jj_lastpos = jj_scanpos = token;
  174. try { return !jj_3_1(); }
  175. catch(LookaheadSuccess ls) { return true; }
  176. finally { jj_save(0, xla); }
  177. }
  178. final private boolean jj_2_2(int xla) {
  179. jj_la = xla; jj_lastpos = jj_scanpos = token;
  180. try { return !jj_3_2(); }
  181. catch(LookaheadSuccess ls) { return true; }
  182. finally { jj_save(1, xla); }
  183. }
  184. final private boolean jj_3_2() {
  185. if (jj_scan_token(KEY)) return true;
  186. if (jj_scan_token(EQUALS)) return true;
  187. return false;
  188. }
  189. final private boolean jj_3_1() {
  190. if (jj_scan_token(4)) return true;
  191. return false;
  192. }
  193. public PropertyParserTokenManager token_source;
  194. JavaCharStream jj_input_stream;
  195. public Token token, jj_nt;
  196. private int jj_ntk;
  197. private Token jj_scanpos, jj_lastpos;
  198. private int jj_la;
  199. public boolean lookingAhead = false;
  200. private boolean jj_semLA;
  201. private int jj_gen;
  202. final private int[] jj_la1 = new int[3];
  203. static private int[] jj_la1_0;
  204. static {
  205. jj_la1_0();
  206. }
  207. private static void jj_la1_0() {
  208. jj_la1_0 = new int[] {0x70,0x60,0x20,};
  209. }
  210. final private JJCalls[] jj_2_rtns = new JJCalls[2];
  211. private boolean jj_rescan = false;
  212. private int jj_gc = 0;
  213. public PropertyParser(java.io.InputStream stream) {
  214. this(stream, null);
  215. }
  216. public PropertyParser(java.io.InputStream stream, String encoding) {
  217. try { jj_input_stream = new JavaCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
  218. token_source = new PropertyParserTokenManager(jj_input_stream);
  219. token = new Token();
  220. jj_ntk = -1;
  221. jj_gen = 0;
  222. for (int i = 0; i < 3; i++) jj_la1[i] = -1;
  223. for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  224. }
  225. public void ReInit(java.io.InputStream stream) {
  226. ReInit(stream, null);
  227. }
  228. public void ReInit(java.io.InputStream stream, String encoding) {
  229. try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); }
  230. token_source.ReInit(jj_input_stream);
  231. token = new Token();
  232. jj_ntk = -1;
  233. jj_gen = 0;
  234. for (int i = 0; i < 3; i++) jj_la1[i] = -1;
  235. for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  236. }
  237. public PropertyParser(java.io.Reader stream) {
  238. jj_input_stream = new JavaCharStream(stream, 1, 1);
  239. token_source = new PropertyParserTokenManager(jj_input_stream);
  240. token = new Token();
  241. jj_ntk = -1;
  242. jj_gen = 0;
  243. for (int i = 0; i < 3; i++) jj_la1[i] = -1;
  244. for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  245. }
  246. public void ReInit(java.io.Reader stream) {
  247. jj_input_stream.ReInit(stream, 1, 1);
  248. token_source.ReInit(jj_input_stream);
  249. token = new Token();
  250. jj_ntk = -1;
  251. jj_gen = 0;
  252. for (int i = 0; i < 3; i++) jj_la1[i] = -1;
  253. for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  254. }
  255. public PropertyParser(PropertyParserTokenManager tm) {
  256. token_source = tm;
  257. token = new Token();
  258. jj_ntk = -1;
  259. jj_gen = 0;
  260. for (int i = 0; i < 3; i++) jj_la1[i] = -1;
  261. for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  262. }
  263. public void ReInit(PropertyParserTokenManager tm) {
  264. token_source = tm;
  265. token = new Token();
  266. jj_ntk = -1;
  267. jj_gen = 0;
  268. for (int i = 0; i < 3; i++) jj_la1[i] = -1;
  269. for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls();
  270. }
  271. final private Token jj_consume_token(int kind) throws ParseException {
  272. Token oldToken;
  273. if ((oldToken = token).next != null) token = token.next;
  274. else token = token.next = token_source.getNextToken();
  275. jj_ntk = -1;
  276. if (token.kind == kind) {
  277. jj_gen++;
  278. if (++jj_gc > 100) {
  279. jj_gc = 0;
  280. for (int i = 0; i < jj_2_rtns.length; i++) {
  281. JJCalls c = jj_2_rtns[i];
  282. while (c != null) {
  283. if (c.gen < jj_gen) c.first = null;
  284. c = c.next;
  285. }
  286. }
  287. }
  288. return token;
  289. }
  290. token = oldToken;
  291. jj_kind = kind;
  292. throw generateParseException();
  293. }
  294. static private final class LookaheadSuccess extends java.lang.Error { }
  295. final private LookaheadSuccess jj_ls = new LookaheadSuccess();
  296. final private boolean jj_scan_token(int kind) {
  297. if (jj_scanpos == jj_lastpos) {
  298. jj_la--;
  299. if (jj_scanpos.next == null) {
  300. jj_lastpos = jj_scanpos = jj_scanpos.next = token_source.getNextToken();
  301. } else {
  302. jj_lastpos = jj_scanpos = jj_scanpos.next;
  303. }
  304. } else {
  305. jj_scanpos = jj_scanpos.next;
  306. }
  307. if (jj_rescan) {
  308. int i = 0; Token tok = token;
  309. while (tok != null && tok != jj_scanpos) { i++; tok = tok.next; }
  310. if (tok != null) jj_add_error_token(kind, i);
  311. }
  312. if (jj_scanpos.kind != kind) return true;
  313. if (jj_la == 0 && jj_scanpos == jj_lastpos) throw jj_ls;
  314. return false;
  315. }
  316. final public Token getNextToken() {
  317. if (token.next != null) token = token.next;
  318. else token = token.next = token_source.getNextToken();
  319. jj_ntk = -1;
  320. jj_gen++;
  321. return token;
  322. }
  323. final public Token getToken(int index) {
  324. Token t = lookingAhead ? jj_scanpos : token;
  325. for (int i = 0; i < index; i++) {
  326. if (t.next != null) t = t.next;
  327. else t = t.next = token_source.getNextToken();
  328. }
  329. return t;
  330. }
  331. final private int jj_ntk() {
  332. if ((jj_nt=token.next) == null)
  333. return (jj_ntk = (token.next=token_source.getNextToken()).kind);
  334. else
  335. return (jj_ntk = jj_nt.kind);
  336. }
  337. private java.util.Vector jj_expentries = new java.util.Vector();
  338. private int[] jj_expentry;
  339. private int jj_kind = -1;
  340. private int[] jj_lasttokens = new int[100];
  341. private int jj_endpos;
  342. private void jj_add_error_token(int kind, int pos) {
  343. if (pos >= 100) return;
  344. if (pos == jj_endpos + 1) {
  345. jj_lasttokens[jj_endpos++] = kind;
  346. } else if (jj_endpos != 0) {
  347. jj_expentry = new int[jj_endpos];
  348. for (int i = 0; i < jj_endpos; i++) {
  349. jj_expentry[i] = jj_lasttokens[i];
  350. }
  351. boolean exists = false;
  352. for (java.util.Enumeration e = jj_expentries.elements(); e.hasMoreElements();) {
  353. int[] oldentry = (int[])(e.nextElement());
  354. if (oldentry.length == jj_expentry.length) {
  355. exists = true;
  356. for (int i = 0; i < jj_expentry.length; i++) {
  357. if (oldentry[i] != jj_expentry[i]) {
  358. exists = false;
  359. break;
  360. }
  361. }
  362. if (exists) break;
  363. }
  364. }
  365. if (!exists) jj_expentries.addElement(jj_expentry);
  366. if (pos != 0) jj_lasttokens[(jj_endpos = pos) - 1] = kind;
  367. }
  368. }
  369. public ParseException generateParseException() {
  370. jj_expentries.removeAllElements();
  371. boolean[] la1tokens = new boolean[10];
  372. for (int i = 0; i < 10; i++) {
  373. la1tokens[i] = false;
  374. }
  375. if (jj_kind >= 0) {
  376. la1tokens[jj_kind] = true;
  377. jj_kind = -1;
  378. }
  379. for (int i = 0; i < 3; i++) {
  380. if (jj_la1[i] == jj_gen) {
  381. for (int j = 0; j < 32; j++) {
  382. if ((jj_la1_0[i] & (1<<j)) != 0) {
  383. la1tokens[j] = true;
  384. }
  385. }
  386. }
  387. }
  388. for (int i = 0; i < 10; i++) {
  389. if (la1tokens[i]) {
  390. jj_expentry = new int[1];
  391. jj_expentry[0] = i;
  392. jj_expentries.addElement(jj_expentry);
  393. }
  394. }
  395. jj_endpos = 0;
  396. jj_rescan_token();
  397. jj_add_error_token(0, 0);
  398. int[][] exptokseq = new int[jj_expentries.size()][];
  399. for (int i = 0; i < jj_expentries.size(); i++) {
  400. exptokseq[i] = (int[])jj_expentries.elementAt(i);
  401. }
  402. return new ParseException(token, exptokseq, tokenImage);
  403. }
  404. final public void enable_tracing() {
  405. }
  406. final public void disable_tracing() {
  407. }
  408. final private void jj_rescan_token() {
  409. jj_rescan = true;
  410. for (int i = 0; i < 2; i++) {
  411. try {
  412. JJCalls p = jj_2_rtns[i];
  413. do {
  414. if (p.gen > jj_gen) {
  415. jj_la = p.arg; jj_lastpos = jj_scanpos = p.first;
  416. switch (i) {
  417. case 0: jj_3_1(); break;
  418. case 1: jj_3_2(); break;
  419. }
  420. }
  421. p = p.next;
  422. } while (p != null);
  423. } catch(LookaheadSuccess ls) { }
  424. }
  425. jj_rescan = false;
  426. }
  427. final private void jj_save(int index, int xla) {
  428. JJCalls p = jj_2_rtns[index];
  429. while (p.gen > jj_gen) {
  430. if (p.next == null) { p = p.next = new JJCalls(); break; }
  431. p = p.next;
  432. }
  433. p.gen = jj_gen + xla - jj_la; p.first = token; p.arg = xla;
  434. }
  435. static final class JJCalls {
  436. int gen;
  437. Token first;
  438. int arg;
  439. JJCalls next;
  440. }
  441. }