1 package org.objectweb.celtix.tools.common.toolspec.parser; 2 3 import java.util.logging.Level ; 4 import java.util.logging.Logger ; 5 6 import org.objectweb.celtix.common.logging.LogUtils; 7 8 public class TokenInputStream { 9 10 private static final Logger LOG = LogUtils.getL7dLogger(TokenInputStream.class); 11 private final String [] tokens; 12 private int pos; 13 14 public TokenInputStream(String [] t) { 15 this.tokens = t; 16 } 17 18 public String read() { 19 if (LOG.isLoggable(Level.INFO)) { 20 LOG.info("Reading token " + tokens[pos]); 21 } 22 return tokens[pos++]; 23 } 24 25 public String readNext() { 26 return read(pos++); 27 } 28 29 public String read(int position) { 30 if (position < 0) { 31 pos = 0; 32 } 33 if (position > tokens.length) { 34 pos = tokens.length - 1; 35 } 36 return tokens[pos]; 37 } 38 39 public String readPre() { 40 if (pos != 0) { 41 pos--; 42 } 43 return tokens[pos]; 44 } 45 46 public String peek() { 47 if (LOG.isLoggable(Level.INFO)) { 48 LOG.info("Peeking token " + tokens[pos]); 49 } 50 return tokens[pos]; 51 } 52 53 public String peekPre() { 54 if (pos == 0) { 55 return tokens[pos]; 56 } 57 return tokens[pos - 1]; 58 } 59 60 public String peek(int position) { 61 if (position < 0) { 62 return tokens[0]; 63 } 64 if (position > tokens.length) { 65 return tokens[tokens.length - 1]; 66 } 67 return tokens[position]; 68 } 69 70 public int getPosition() { 71 return pos; 72 } 73 74 public void setPosition(int p) { 75 this.pos = p; 76 } 77 78 public int available() { 79 return tokens.length - pos; 80 } 81 82 public boolean hasNext() { 83 return available() > 1; 84 } 85 86 public boolean isOutOfBound() { 87 return pos >= tokens.length; 88 } 89 90 public String toString() { 91 StringBuffer sb = new StringBuffer ("[ "); 92 93 for (int i = pos; i < tokens.length; i++) { 94 sb.append(tokens[i]); 95 if (i < tokens.length - 1) { 96 sb.append(" "); 97 } 98 } 99 sb.append(" ]"); 100 return sb.toString(); 101 } 102 103 } 104 | Popular Tags |