PageRenderTime 24ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/external/antlr/antlr-3.4/runtime/Java/src/main/java/org/antlr/runtime/LegacyCommonTokenStream.java

https://gitlab.com/brian0218/rk3066_r-box_android4.2.2_sdk
Java | 394 lines | 257 code | 48 blank | 89 comment | 57 complexity | 6a037a5f1a45883db6cbca13bcfeb82c MD5 | raw file
  1. /*
  2. [The "BSD license"]
  3. Copyright (c) 2005-2009 Terence Parr
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions
  7. are met:
  8. 1. Redistributions of source code must retain the above copyright
  9. notice, this list of conditions and the following disclaimer.
  10. 2. Redistributions in binary form must reproduce the above copyright
  11. notice, this list of conditions and the following disclaimer in the
  12. documentation and/or other materials provided with the distribution.
  13. 3. The name of the author may not be used to endorse or promote products
  14. derived from this software without specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
  16. IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  17. OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  18. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
  19. INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  20. NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  21. DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  22. THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  24. THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. package org.antlr.runtime;
  27. import java.util.*;
  28. /** The most common stream of tokens is one where every token is buffered up
  29. * and tokens are prefiltered for a certain channel (the parser will only
  30. * see these tokens and cannot change the filter channel number during the
  31. * parse).
  32. *
  33. * TODO: how to access the full token stream? How to track all tokens matched per rule?
  34. */
  35. public class LegacyCommonTokenStream implements TokenStream {
  36. protected TokenSource tokenSource;
  37. /** Record every single token pulled from the source so we can reproduce
  38. * chunks of it later.
  39. */
  40. protected List tokens;
  41. /** Map<tokentype, channel> to override some Tokens' channel numbers */
  42. protected Map channelOverrideMap;
  43. /** Set<tokentype>; discard any tokens with this type */
  44. protected Set discardSet;
  45. /** Skip tokens on any channel but this one; this is how we skip whitespace... */
  46. protected int channel = Token.DEFAULT_CHANNEL;
  47. /** By default, track all incoming tokens */
  48. protected boolean discardOffChannelTokens = false;
  49. /** Track the last mark() call result value for use in rewind(). */
  50. protected int lastMarker;
  51. protected int range = -1; // how deep have we gone?
  52. /** The index into the tokens list of the current token (next token
  53. * to consume). p==-1 indicates that the tokens list is empty
  54. */
  55. protected int p = -1;
  56. public LegacyCommonTokenStream() {
  57. tokens = new ArrayList(500);
  58. }
  59. public LegacyCommonTokenStream(TokenSource tokenSource) {
  60. this();
  61. this.tokenSource = tokenSource;
  62. }
  63. public LegacyCommonTokenStream(TokenSource tokenSource, int channel) {
  64. this(tokenSource);
  65. this.channel = channel;
  66. }
  67. /** Reset this token stream by setting its token source. */
  68. public void setTokenSource(TokenSource tokenSource) {
  69. this.tokenSource = tokenSource;
  70. tokens.clear();
  71. p = -1;
  72. channel = Token.DEFAULT_CHANNEL;
  73. }
  74. /** Load all tokens from the token source and put in tokens.
  75. * This is done upon first LT request because you might want to
  76. * set some token type / channel overrides before filling buffer.
  77. */
  78. protected void fillBuffer() {
  79. int index = 0;
  80. Token t = tokenSource.nextToken();
  81. while ( t!=null && t.getType()!=CharStream.EOF ) {
  82. boolean discard = false;
  83. // is there a channel override for token type?
  84. if ( channelOverrideMap!=null ) {
  85. Integer channelI = (Integer)
  86. channelOverrideMap.get(new Integer(t.getType()));
  87. if ( channelI!=null ) {
  88. t.setChannel(channelI.intValue());
  89. }
  90. }
  91. if ( discardSet!=null &&
  92. discardSet.contains(new Integer(t.getType())) )
  93. {
  94. discard = true;
  95. }
  96. else if ( discardOffChannelTokens && t.getChannel()!=this.channel ) {
  97. discard = true;
  98. }
  99. if ( !discard ) {
  100. t.setTokenIndex(index);
  101. tokens.add(t);
  102. index++;
  103. }
  104. t = tokenSource.nextToken();
  105. }
  106. // leave p pointing at first token on channel
  107. p = 0;
  108. p = skipOffTokenChannels(p);
  109. }
  110. /** Move the input pointer to the next incoming token. The stream
  111. * must become active with LT(1) available. consume() simply
  112. * moves the input pointer so that LT(1) points at the next
  113. * input symbol. Consume at least one token.
  114. *
  115. * Walk past any token not on the channel the parser is listening to.
  116. */
  117. public void consume() {
  118. if ( p<tokens.size() ) {
  119. p++;
  120. p = skipOffTokenChannels(p); // leave p on valid token
  121. }
  122. }
  123. /** Given a starting index, return the index of the first on-channel
  124. * token.
  125. */
  126. protected int skipOffTokenChannels(int i) {
  127. int n = tokens.size();
  128. while ( i<n && ((Token)tokens.get(i)).getChannel()!=channel ) {
  129. i++;
  130. }
  131. return i;
  132. }
  133. protected int skipOffTokenChannelsReverse(int i) {
  134. while ( i>=0 && ((Token)tokens.get(i)).getChannel()!=channel ) {
  135. i--;
  136. }
  137. return i;
  138. }
  139. /** A simple filter mechanism whereby you can tell this token stream
  140. * to force all tokens of type ttype to be on channel. For example,
  141. * when interpreting, we cannot exec actions so we need to tell
  142. * the stream to force all WS and NEWLINE to be a different, ignored
  143. * channel.
  144. */
  145. public void setTokenTypeChannel(int ttype, int channel) {
  146. if ( channelOverrideMap==null ) {
  147. channelOverrideMap = new HashMap();
  148. }
  149. channelOverrideMap.put(new Integer(ttype), new Integer(channel));
  150. }
  151. public void discardTokenType(int ttype) {
  152. if ( discardSet==null ) {
  153. discardSet = new HashSet();
  154. }
  155. discardSet.add(new Integer(ttype));
  156. }
  157. public void discardOffChannelTokens(boolean discardOffChannelTokens) {
  158. this.discardOffChannelTokens = discardOffChannelTokens;
  159. }
  160. public List getTokens() {
  161. if ( p == -1 ) {
  162. fillBuffer();
  163. }
  164. return tokens;
  165. }
  166. public List getTokens(int start, int stop) {
  167. return getTokens(start, stop, (BitSet)null);
  168. }
  169. /** Given a start and stop index, return a List of all tokens in
  170. * the token type BitSet. Return null if no tokens were found. This
  171. * method looks at both on and off channel tokens.
  172. */
  173. public List getTokens(int start, int stop, BitSet types) {
  174. if ( p == -1 ) {
  175. fillBuffer();
  176. }
  177. if ( stop>=tokens.size() ) {
  178. stop=tokens.size()-1;
  179. }
  180. if ( start<0 ) {
  181. start=0;
  182. }
  183. if ( start>stop ) {
  184. return null;
  185. }
  186. // list = tokens[start:stop]:{Token t, t.getType() in types}
  187. List filteredTokens = new ArrayList();
  188. for (int i=start; i<=stop; i++) {
  189. Token t = (Token)tokens.get(i);
  190. if ( types==null || types.member(t.getType()) ) {
  191. filteredTokens.add(t);
  192. }
  193. }
  194. if ( filteredTokens.size()==0 ) {
  195. filteredTokens = null;
  196. }
  197. return filteredTokens;
  198. }
  199. public List getTokens(int start, int stop, List types) {
  200. return getTokens(start,stop,new BitSet(types));
  201. }
  202. public List getTokens(int start, int stop, int ttype) {
  203. return getTokens(start,stop,BitSet.of(ttype));
  204. }
  205. /** Get the ith token from the current position 1..n where k=1 is the
  206. * first symbol of lookahead.
  207. */
  208. public Token LT(int k) {
  209. if ( p == -1 ) {
  210. fillBuffer();
  211. }
  212. if ( k==0 ) {
  213. return null;
  214. }
  215. if ( k<0 ) {
  216. return LB(-k);
  217. }
  218. //System.out.print("LT(p="+p+","+k+")=");
  219. if ( (p+k-1) >= tokens.size() ) {
  220. return (Token)tokens.get(tokens.size()-1);
  221. }
  222. //System.out.println(tokens.get(p+k-1));
  223. int i = p;
  224. int n = 1;
  225. // find k good tokens
  226. while ( n<k ) {
  227. // skip off-channel tokens
  228. i = skipOffTokenChannels(i+1); // leave p on valid token
  229. n++;
  230. }
  231. if ( i>=tokens.size() ) {
  232. return (Token)tokens.get(tokens.size()-1); // must be EOF
  233. }
  234. if ( i>range ) range = i;
  235. return (Token)tokens.get(i);
  236. }
  237. /** Look backwards k tokens on-channel tokens */
  238. protected Token LB(int k) {
  239. //System.out.print("LB(p="+p+","+k+") ");
  240. if ( p == -1 ) {
  241. fillBuffer();
  242. }
  243. if ( k==0 ) {
  244. return null;
  245. }
  246. if ( (p-k)<0 ) {
  247. return null;
  248. }
  249. int i = p;
  250. int n = 1;
  251. // find k good tokens looking backwards
  252. while ( n<=k ) {
  253. // skip off-channel tokens
  254. i = skipOffTokenChannelsReverse(i-1); // leave p on valid token
  255. n++;
  256. }
  257. if ( i<0 ) {
  258. return null;
  259. }
  260. return (Token)tokens.get(i);
  261. }
  262. /** Return absolute token i; ignore which channel the tokens are on;
  263. * that is, count all tokens not just on-channel tokens.
  264. */
  265. public Token get(int i) {
  266. return (Token)tokens.get(i);
  267. }
  268. /** Get all tokens from start..stop inclusively */
  269. public List get(int start, int stop) {
  270. if ( p == -1 ) fillBuffer();
  271. if ( start<0 || stop<0 ) return null;
  272. return tokens.subList(start, stop);
  273. }
  274. public int LA(int i) {
  275. return LT(i).getType();
  276. }
  277. public int mark() {
  278. if ( p == -1 ) {
  279. fillBuffer();
  280. }
  281. lastMarker = index();
  282. return lastMarker;
  283. }
  284. public void release(int marker) {
  285. // no resources to release
  286. }
  287. public int size() {
  288. return tokens.size();
  289. }
  290. public int index() {
  291. return p;
  292. }
  293. public int range() {
  294. return range;
  295. }
  296. public void rewind(int marker) {
  297. seek(marker);
  298. }
  299. public void rewind() {
  300. seek(lastMarker);
  301. }
  302. public void reset() {
  303. p = 0;
  304. lastMarker = 0;
  305. }
  306. public void seek(int index) {
  307. p = index;
  308. }
  309. public TokenSource getTokenSource() {
  310. return tokenSource;
  311. }
  312. public String getSourceName() {
  313. return getTokenSource().getSourceName();
  314. }
  315. public String toString() {
  316. if ( p == -1 ) {
  317. fillBuffer();
  318. }
  319. return toString(0, tokens.size()-1);
  320. }
  321. public String toString(int start, int stop) {
  322. if ( start<0 || stop<0 ) {
  323. return null;
  324. }
  325. if ( p == -1 ) {
  326. fillBuffer();
  327. }
  328. if ( stop>=tokens.size() ) {
  329. stop = tokens.size()-1;
  330. }
  331. StringBuffer buf = new StringBuffer();
  332. for (int i = start; i <= stop; i++) {
  333. Token t = (Token)tokens.get(i);
  334. buf.append(t.getText());
  335. }
  336. return buf.toString();
  337. }
  338. public String toString(Token start, Token stop) {
  339. if ( start!=null && stop!=null ) {
  340. return toString(start.getTokenIndex(), stop.getTokenIndex());
  341. }
  342. return null;
  343. }
  344. }