1 package fri.patterns.interpreter.parsergenerator.lexer; 2 3 import java.util.*; 4 import java.io.IOException ; 5 6 12 13 class InputText extends Input 14 { 15 private int [] line; private int [] prevLine; private int prevLength; 18 private boolean wasCr = false; private int column; private List lineLengths = new ArrayList(); private int [] scanPoint; 22 23 24 InputText(Object input) 25 throws IOException 26 { 27 super(input); 28 } 29 30 31 protected int convertInput(int i) { 32 if (i == '\r') { 33 wasCr = true; 34 newLine(); 35 } 36 else { 37 if (i == '\n') { 38 if (wasCr == false) newLine(); 40 else lineLengths.set( 42 lineLengths.size() - 1, 43 new Integer ( ((Integer ) lineLengths.get(lineLengths.size() - 1)).intValue() + 1 ) 44 ); 45 } 46 else { 47 storeRead(i); 48 } 49 wasCr = false; 50 } 51 52 scanPoint = null; return i; 54 } 55 56 private void newLine() { 57 lineLengths.add(new Integer (column + 1)); 59 prevLength = column; 60 column = 0; 62 if (line == null) 63 line = new int[64]; 64 65 if (prevLine == null || prevLine.length < line.length) 66 prevLine = new int [Math.max(line.length, 64)]; 67 68 System.arraycopy(line, 0, prevLine, 0, prevLength); 69 } 70 71 private void storeRead(int i) { 72 if (line == null) { line = new int [64]; 74 } 75 else 76 if (column == line.length) { int [] old = line; 78 line = new int[old.length * 2]; 79 System.arraycopy(old, 0, line, 0, old.length); 80 } 81 82 line[column] = i; 83 column++; 84 } 85 86 87 88 public int getReadLine() { 89 return lineLengths.size() + 1; 90 } 91 92 93 public int getReadColumn() { 94 return column; 95 } 96 97 98 public int getScanLine() { 99 return calculateScanPoint()[0]; 100 } 101 102 103 public int getScanColumn() { 104 return calculateScanPoint()[1]; 105 } 106 107 private int [] calculateScanPoint() { if (scanPoint != null) return scanPoint; 110 111 int diff = getReadColumn() - getUnreadLength(); 112 if (diff >= 0) 113 return scanPoint = new int[] { getReadLine(), diff }; 114 115 for (int i = lineLengths.size() - 1; i >= 0; i--) { int len = ((Integer ) lineLengths.get(i)).intValue(); 117 diff += len; 118 if (diff >= 0) 119 return scanPoint = new int[] { i + 1, diff }; 120 } 121 throw new IllegalStateException ("Something went wrong when calculating scan point: "+diff); 122 } 123 124 125 public int read() throws IOException { 126 scanPoint = null; 127 return super.read(); 128 } 129 130 131 public void setMark(int mark) { 132 scanPoint = null; 133 super.setMark(mark); 134 } 135 136 137 138 public String getLine() { 139 return createLineString(line, column); 140 } 141 142 143 public String getPreviousLine() { 144 return createLineString(prevLine, prevLength); 145 } 146 147 148 public String getUnreadText() { 149 int [] buf = getUnreadBuffer(); 150 return createLineString(buf, buf.length); 151 } 152 153 private String createLineString(int [] line, int end) { 154 StringBuffer sb = new StringBuffer (); 155 for (int i = 0; i < end; i++) 156 sb.append((char) line[i]); 157 return sb.toString(); 158 } 159 160 } | Popular Tags |