comparison src/luan/interp/Parser.java @ 165:94bbc4cbc106

merge luan/parser into interp git-svn-id: https://luan-java.googlecode.com/svn/trunk@166 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Sun, 22 Jun 2014 04:10:59 +0000
parents src/luan/parser/Parser.java@cc3a0578edac
children
comparison
equal deleted inserted replaced
164:78ba371ea1e9 165:94bbc4cbc106
1 package luan.interp;
2
3 import luan.LuanSource;
4
5
6 final class Parser {
7 private final LuanSource src;
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 public Parser(LuanSource src) {
15 this.src = src;
16 this.text = src.text;
17 this.len = text.length();
18 }
19
20 private int i() {
21 return stack[frame];
22 }
23
24 private void i(int i) {
25 stack[frame] += i;
26 if( iHigh < stack[frame] )
27 iHigh = stack[frame];
28 }
29
30 public int begin() {
31 frame++;
32 if( frame == stack.length ) {
33 int[] a = new int[2*frame];
34 System.arraycopy(stack,0,a,0,frame);
35 stack = a;
36 }
37 stack[frame] = stack[frame-1];
38 return i();
39 }
40
41 public void rollback() {
42 stack[frame] = stack[frame-1];
43 }
44
45 public <T> T success(T t) {
46 success();
47 return t;
48 }
49
50 public boolean success() {
51 frame--;
52 stack[frame] = stack[frame+1];
53 return true;
54 }
55
56 public <T> T failure(T t) {
57 failure();
58 return t;
59 }
60
61 public boolean failure() {
62 frame--;
63 return false;
64 }
65
66 public ParseException exception(String msg) {
67 return new ParseException(msg,src,i(),iHigh);
68 }
69
70 public ParseException exception() {
71 return exception("Invalid input");
72 }
73
74 public int currentIndex() {
75 return i();
76 }
77
78 public char lastChar() {
79 return text.charAt(i()-1);
80 }
81
82 public char currentChar() {
83 return text.charAt(i());
84 }
85
86 public boolean endOfInput() {
87 return i() >= len;
88 }
89
90 public boolean match(char c) {
91 if( endOfInput() || text.charAt(i()) != c )
92 return false;
93 i(1);
94 return true;
95 }
96
97 public boolean match(String s) {
98 int n = s.length();
99 if( !text.regionMatches(i(),s,0,n) )
100 return false;
101 i(n);
102 return true;
103 }
104
105 public boolean matchIgnoreCase(String s) {
106 int n = s.length();
107 if( !text.regionMatches(true,i(),s,0,n) )
108 return false;
109 i(n);
110 return true;
111 }
112
113 public boolean anyOf(String s) {
114 if( endOfInput() || s.indexOf(text.charAt(i())) == -1 )
115 return false;
116 i(1);
117 return true;
118 }
119
120 public boolean noneOf(String s) {
121 if( endOfInput() || s.indexOf(text.charAt(i())) != -1 )
122 return false;
123 i(1);
124 return true;
125 }
126
127 public boolean inCharRange(char cLow, char cHigh) {
128 if( endOfInput() )
129 return false;
130 char c = text.charAt(i());
131 if( !(cLow <= c && c <= cHigh) )
132 return false;
133 i(1);
134 return true;
135 }
136
137 public boolean anyChar() {
138 if( endOfInput() )
139 return false;
140 i(1);
141 return true;
142 }
143
144 public boolean test(String s) {
145 return text.regionMatches(i(),s,0,s.length());
146 }
147
148 public String textFrom(int start) {
149 return text.substring(start,i());
150 }
151
152 }