1 package org.antlr.works.ate.syntax.generic; 2 3 import org.antlr.works.ate.syntax.misc.ATEToken; 4 5 import java.util.List ; 6 import java.util.Stack ; 7 37 38 public abstract class ATESyntaxParser { 39 40 private List <ATEToken> tokens; 41 private Stack <Integer > marks = new Stack <Integer >(); 42 private int position; 43 44 private ATEToken t0; 45 private ATEToken t1; 46 47 public ATESyntaxParser() { 48 } 49 50 public void parse(List <ATEToken> tokens) { 51 this.tokens = tokens; 52 marks.clear(); 53 position = -1; 54 clearTokenCache(); 55 parseTokens(); 56 } 57 58 public abstract void parseTokens(); 59 60 public List <ATEToken> getTokens() { 61 return tokens; 62 } 63 64 public int getPosition() { 65 return position; 66 } 67 68 public void mark() { 69 marks.push(position); 70 } 71 72 public void rewind() { 73 position = marks.pop(); 74 clearTokenCache(); 75 } 76 77 public boolean previousToken() { 78 position--; 79 clearTokenCache(); 80 return position >= 0; 81 } 82 83 public boolean nextToken() { 84 position++; 85 clearTokenCache(); 86 return position<tokens.size(); 87 } 88 89 public boolean moreTokens() { 90 return position<tokens.size(); 91 } 92 93 public boolean skip(int count) { 94 if(count == 1) { 95 return nextToken(); 96 } else { 97 for(int i=0; i<count; i++) { 98 if(!nextToken()) return false; 99 } 100 } 101 return true; 102 } 103 104 public ATEToken T(int index) { 105 if(index == 0) { 106 if(t0 == null) 107 t0 = getToken(0); 108 return t0; 109 } else if(index == 1) { 110 if(t1 == null) 111 t1 = getToken(1); 112 return t1; 113 } else 114 return getToken(index); 115 } 116 117 public ATEToken getToken(int index) { 118 if(position+index >= 0 && position+index < tokens.size()) 119 return tokens.get(position+index); 120 else 121 return null; 122 } 123 124 private void clearTokenCache() { 125 t0 = null; 126 t1 = null; 127 } 128 129 public boolean matchSingleComment(int index) { 130 if(isSingleComment(index)) { 131 nextToken(); 132 return true; 133 } else { 134 return false; 135 } 136 } 137 138 public boolean matchComplexComment(int index) { 139 if(isComplexComment(index)) { 140 nextToken(); 141 return true; 142 } else { 143 return false; 144 } 145 } 146 147 public boolean isChar(int index, String c) { 148 return isTokenType(index, ATESyntaxLexer.TOKEN_CHAR) && T(index).getAttribute().equals(c); 149 } 150 151 public boolean isSingleComment(int index) { 152 return isTokenType(index, ATESyntaxLexer.TOKEN_SINGLE_COMMENT); 153 } 154 155 public boolean isComplexComment(int index) { 156 return isTokenType(index, ATESyntaxLexer.TOKEN_COMPLEX_COMMENT); 157 } 158 159 public boolean isID(int index) { 160 return isTokenType(index, ATESyntaxLexer.TOKEN_ID); 161 } 162 163 public boolean isID(int index, String attribute) { 164 return isTokenType(index, ATESyntaxLexer.TOKEN_ID) && T(index).getAttribute().equals(attribute); 165 } 166 167 public boolean isTokenType(int index, int type) { 168 return T(index) != null && T(index).type == type; 169 } 170 171 } 172 | Popular Tags |