1 19 20 package org.netbeans.modules.lexer.demo; 21 22 import org.netbeans.api.lexer.Token; 23 import org.netbeans.api.lexer.LexerInput; 24 import org.netbeans.api.lexer.TokenId; 25 import org.netbeans.spi.lexer.util.AbstractCharSequence; 26 import org.netbeans.spi.lexer.util.CharSubSequence; 27 import org.netbeans.spi.lexer.util.Compatibility; 28 29 35 36 public class StringLexerInput implements LexerInput { 37 38 private String text; 39 40 41 private int inputIndex; 42 43 44 private int tokenIndex; 45 46 47 private int lookaheadIndex; 48 49 50 private int eof; 51 52 56 private CharSubSequence subReadText; 57 58 public StringLexerInput(String text) { 59 this.text = text; 60 } 61 62 public int read() { 63 if (inputIndex >= text.length()) { 64 eof = 1; 65 return LexerInput.EOF; 66 67 } else { 68 return text.charAt(inputIndex++); 69 } 70 } 71 72 public int getReadLookahead() { 73 return Math.max(lookaheadIndex, inputIndex + eof) - tokenIndex; 74 } 75 76 public int getReadLength() { 77 return inputIndex - tokenIndex; 78 } 79 80 public boolean isEOFLookahead() { 81 return (eof != 0); 82 } 83 84 public void backup(int count) { 85 lookaheadIndex = Math.max(lookaheadIndex, inputIndex + eof); 86 inputIndex -= count; 87 if (inputIndex < tokenIndex) { 88 inputIndex += count; 89 throw new IllegalArgumentException ("count=" + count 90 + " > " + (inputIndex - tokenIndex)); 91 92 } else if (inputIndex > lookaheadIndex - eof) { 93 inputIndex += count; 94 throw new IllegalArgumentException ("count=" + count 95 + " < " + (inputIndex + eof - lookaheadIndex)); 96 } 97 } 98 99 public Token createToken(TokenId id, int tokenLength) { 100 if (tokenLength <= 0) { 101 throw new IllegalArgumentException ("tokenLength=" 102 + tokenLength + " <= 0"); 103 } 104 105 if (tokenIndex + tokenLength > inputIndex) { 106 throw new IllegalArgumentException ("tokenLength=" 107 + tokenLength + " > number-of-read-characters=" 108 + (inputIndex - tokenIndex) 109 ); 110 } 111 112 Token ret = new StringToken(id, text.substring(tokenIndex, 113 tokenIndex + tokenLength)); 114 tokenIndex += tokenLength; 115 return ret; 116 } 117 118 public Token createToken(TokenId id) { 119 return createToken(id, inputIndex - tokenIndex); 120 } 121 122 public CharSequence getReadText(int start, int end) { 123 if (subReadText == null) { 124 subReadText = new CharSubSequence(new ReadText()); 125 } 126 127 subReadText.setBounds(start, end); 128 129 return subReadText; 130 } 131 132 char readTextCharAt(int index) { 133 if (index < 0) { 134 throw new IndexOutOfBoundsException ("index=" + index + " < 0"); 135 } 136 137 if (index >= getReadLength()) { 138 throw new IndexOutOfBoundsException ("index=" + index 139 + " >= getReadLength()=" + getReadLength()); 140 } 141 142 return text.charAt(tokenIndex + index); 143 } 144 145 private class ReadText extends AbstractCharSequence { 146 147 public int length() { 148 return getReadLength(); 149 } 150 151 public char charAt(int index) { 152 return readTextCharAt(index); 153 } 154 155 } 156 157 } 158 | Popular Tags |