1 package persistence.antlr; 2 3 8 9 import persistence.antlr.collections.impl.BitSet; 10 11 18 public class TokenStreamHiddenTokenFilter extends TokenStreamBasicFilter implements TokenStream { 19 protected BitSet hideMask; 21 22 protected CommonHiddenStreamToken nextMonitoredToken; 23 24 27 protected CommonHiddenStreamToken lastHiddenToken; 28 29 protected CommonHiddenStreamToken firstHidden = null; 30 31 public TokenStreamHiddenTokenFilter(TokenStream input) { 32 super(input); 33 hideMask = new BitSet(); 34 } 35 36 protected void consume() throws TokenStreamException { 37 nextMonitoredToken = (CommonHiddenStreamToken)input.nextToken(); 38 } 39 40 private void consumeFirst() throws TokenStreamException { 41 consume(); 43 CommonHiddenStreamToken p = null; 46 while (hideMask.member(LA(1).getType()) || discardMask.member(LA(1).getType())) { 48 if (hideMask.member(LA(1).getType())) { 49 if (p == null) { 50 p = LA(1); 51 } 52 else { 53 p.setHiddenAfter(LA(1)); 54 LA(1).setHiddenBefore(p); p = LA(1); 56 } 57 lastHiddenToken = p; 58 if (firstHidden == null) { 59 firstHidden = p; } 61 } 62 consume(); 63 } 64 } 65 66 public BitSet getDiscardMask() { 67 return discardMask; 68 } 69 70 73 public CommonHiddenStreamToken getHiddenAfter(CommonHiddenStreamToken t) { 74 return t.getHiddenAfter(); 75 } 76 77 80 public CommonHiddenStreamToken getHiddenBefore(CommonHiddenStreamToken t) { 81 return t.getHiddenBefore(); 82 } 83 84 public BitSet getHideMask() { 85 return hideMask; 86 } 87 88 91 public CommonHiddenStreamToken getInitialHiddenToken() { 92 return firstHidden; 93 } 94 95 public void hide(int m) { 96 hideMask.add(m); 97 } 98 99 public void hide(BitSet mask) { 100 hideMask = mask; 101 } 102 103 protected CommonHiddenStreamToken LA(int i) { 104 return nextMonitoredToken; 105 } 106 107 117 public Token nextToken() throws TokenStreamException { 118 if (LA(1) == null) { 121 consumeFirst(); 122 } 123 124 CommonHiddenStreamToken monitored = LA(1); 127 monitored.setHiddenBefore(lastHiddenToken); 129 lastHiddenToken = null; 130 131 consume(); 134 CommonHiddenStreamToken p = monitored; 135 while (hideMask.member(LA(1).getType()) || discardMask.member(LA(1).getType())) { 137 if (hideMask.member(LA(1).getType())) { 138 p.setHiddenAfter(LA(1)); 141 if (p != monitored) { LA(1).setHiddenBefore(p); 144 } 145 p = lastHiddenToken = LA(1); 146 } 147 consume(); 148 } 149 return monitored; 150 } 151 } 152 | Popular Tags |