comparison lucene/src/luan/modules/lucene/queryparser/Parser.java @ 730:01e68da6983b

add sane-lucene-queryparser source to luan
author Franklin Schmidt <fschmidt@gmail.com>
date Fri, 10 Jun 2016 15:41:15 -0600
parents
children
comparison
equal deleted inserted replaced
729:4ce68aad92b7 730:01e68da6983b
1 package luan.modules.lucene.queryparser;
2
3 /**
4 * A general parser utility class.
5 * This class is not needed to use SaneQueryParser.
6 */
7 public class Parser {
8 public final String text;
9 private final int len;
10 private int[] stack = new int[256];
11 private int frame = 0;
12 private int iHigh;
13
14 Parser(String text) {
15 this.text = text;
16 this.len = text.length();
17 }
18
19 private int i() {
20 return stack[frame];
21 }
22
23 private void i(int i) {
24 stack[frame] += i;
25 if( iHigh < stack[frame] )
26 iHigh = stack[frame];
27 }
28
29 public int begin() {
30 frame++;
31 if( frame == stack.length ) {
32 int[] a = new int[2*frame];
33 System.arraycopy(stack,0,a,0,frame);
34 stack = a;
35 }
36 stack[frame] = stack[frame-1];
37 return i();
38 }
39
40 public void rollback() {
41 stack[frame] = stack[frame-1];
42 }
43
44 public <T> T success(T t) {
45 success();
46 return t;
47 }
48
49 public boolean success() {
50 frame--;
51 stack[frame] = stack[frame+1];
52 return true;
53 }
54
55 public <T> T failure(T t) {
56 failure();
57 return t;
58 }
59
60 public boolean failure() {
61 frame--;
62 return false;
63 }
64
65 public int currentIndex() {
66 return i();
67 }
68
69 public int errorIndex() {
70 return frame > 0 ? stack[frame-1] : 0;
71 }
72
73 public int highIndex() {
74 return iHigh;
75 }
76
77 public char lastChar() {
78 return text.charAt(i()-1);
79 }
80
81 public char currentChar() {
82 return text.charAt(i());
83 }
84
85 public boolean endOfInput() {
86 return i() >= len;
87 }
88
89 public boolean match(char c) {
90 if( endOfInput() || text.charAt(i()) != c )
91 return false;
92 i(1);
93 return true;
94 }
95
96 public boolean match(String s) {
97 int n = s.length();
98 if( !text.regionMatches(i(),s,0,n) )
99 return false;
100 i(n);
101 return true;
102 }
103
104 public boolean matchIgnoreCase(String s) {
105 int n = s.length();
106 if( !text.regionMatches(true,i(),s,0,n) )
107 return false;
108 i(n);
109 return true;
110 }
111
112 public boolean anyOf(String s) {
113 if( endOfInput() || s.indexOf(text.charAt(i())) == -1 )
114 return false;
115 i(1);
116 return true;
117 }
118
119 public boolean noneOf(String s) {
120 if( endOfInput() || s.indexOf(text.charAt(i())) != -1 )
121 return false;
122 i(1);
123 return true;
124 }
125
126 public boolean inCharRange(char cLow, char cHigh) {
127 if( endOfInput() )
128 return false;
129 char c = text.charAt(i());
130 if( !(cLow <= c && c <= cHigh) )
131 return false;
132 i(1);
133 return true;
134 }
135
136 public boolean anyChar() {
137 if( endOfInput() )
138 return false;
139 i(1);
140 return true;
141 }
142
143 public boolean test(char c) {
144 return !endOfInput() && text.charAt(i()) == c;
145 }
146
147 public boolean test(String s) {
148 return text.regionMatches(i(),s,0,s.length());
149 }
150
151 public String textFrom(int start) {
152 return text.substring(start,i());
153 }
154
155 }