1 21 22 package org.armedbear.j; 23 24 public class DefaultSyntaxIterator implements SyntaxIterator 25 { 26 private Line line; 27 private int offset; 28 private char[] cachedChars; 29 private Line cachedLine; 30 31 protected DefaultSyntaxIterator(Position pos) 32 { 33 if (pos != null) { 34 line = pos.getLine(); 35 offset = pos.getOffset(); 36 } 37 } 38 39 public final Position getPosition() 40 { 41 return new Position(line, offset); 42 } 43 44 public final Line getLine() 45 { 46 return line; 47 } 48 49 public final char nextChar() 50 { 51 int limit = line.length(); 52 if (offset < limit - 1) { 53 if (cachedLine != line) { 54 cachedChars = hideSyntacticWhitespace(line); 55 cachedLine = line; 56 } 57 while (++offset < limit) { 58 if (cachedChars[offset] > ' ') 59 return cachedChars[offset]; 60 } 61 } 62 while (true) { 63 if (line.next() == null) 64 return DONE; 65 line = line.next(); 66 offset = 0; 67 cachedChars = hideSyntacticWhitespace(line); 68 cachedLine = line; 69 limit = line.length(); 70 while (offset < limit) { 71 if (cachedChars[offset] > ' ') 72 return cachedChars[offset]; 73 ++offset; 74 } 75 } 76 } 77 78 public final char prevChar() 79 { 80 if (offset > 0) { 81 if (cachedLine != line) { 82 cachedChars = hideSyntacticWhitespace(line); 83 cachedLine = line; 84 } 85 while (--offset >= 0) { 86 if (cachedChars[offset] > ' ') 87 return cachedChars[offset]; 88 } 89 } 90 while (true) { 91 if (line.previous() == null) { 92 offset = 0; 93 return DONE; 94 } 95 line = line.previous(); 96 cachedChars = hideSyntacticWhitespace(line); 97 cachedLine = line; 98 offset = line.length(); 99 while (--offset >= 0) { 100 if (cachedChars[offset] > ' ') 101 return cachedChars[offset]; 102 } 103 } 104 } 105 106 public char[] hideSyntacticWhitespace(Line line) 109 { 110 return hideSyntacticWhitespace(line.getText()); 111 } 112 113 public char[] hideSyntacticWhitespace(String s) 114 { 115 return s.toCharArray(); 116 } 117 } 118 | Popular Tags |