1 19 package org.netbeans.modules.xml.text.syntax.javacc.lib; 20 21 import java.io.*; 22 23 import org.netbeans.editor.*; 24 25 import org.netbeans.modules.xml.text.syntax.javacc.*; 26 27 33 public class JJEditorSyntax extends Syntax implements JJConstants { 34 35 36 private static final boolean DEBUG = false; 37 private static PrintStream debug = null; 38 private static boolean debugColoring = Boolean.getBoolean("netbeans.debug.editor.draw"); private static int dbgOffset; 40 41 42 private final JJSyntaxInterface lexan; 43 private final JJMapperInterface mapper; 44 private final StringParserInput pi; 45 46 47 public JJEditorSyntax(JJSyntaxInterface lexan,JJMapperInterface mapper, TokenContextPath context) { 48 if (DEBUG) { 49 try { 50 debug = new PrintStream(new FileOutputStream("/home/pkuzel/tmp/jj1.log")); } catch (IOException ex) { 52 debug = System.err; 53 } 54 } 55 56 tokenContextPath = context; 57 this.lexan = lexan; 58 this.mapper = mapper; 59 pi = new UCode_CharStream(); 60 } 61 62 63 protected TokenID parseToken() { 64 65 try { 66 JJTokenID token; 67 68 if ((stopOffset-tokenOffset < 10) && !lastBuffer) { 71 offset = stopOffset; 72 return null; 73 } else { 74 offset = tokenOffset; 75 } 76 77 pi.setBuffer(buffer, tokenOffset, stopOffset-tokenOffset); 78 79 if (DEBUG) { 80 if ( tokenOffset != dbgOffset ) { 81 dbgOffset = tokenOffset; 82 debug.println("Tokenize at [" + offset + "," + tokenOffset + "] state:" + state + ":\n" + new String (buffer, tokenOffset, stopOffset-tokenOffset) 84 ); 85 } 86 } 87 88 89 if (state == INIT) { 90 lexan.init(pi); 91 } else { 92 lexan.init(pi, state); } 94 95 lexan.next(); 97 state = lexan.getState(); 98 int id = lexan.getID(); 99 100 if (id != JJ_EOF) { 101 token = mapper.createToken(id); 102 103 if (id == JJ_ERR && lexan.getLength() == 0) { 106 offset++; 107 } 108 } else { 109 token = mapper.guessToken(lexan.getImage(), state, lastBuffer); 110 } 111 112 if (token != null) { 114 offset += lexan.getLength(); 115 116 if (token.isError()) { 118 supposedTokenID = mapper.supposedToken(lexan.getImage(), state, id); 119 } 120 121 } else { 122 offset = stopOffset; 124 } 125 126 127 if (DEBUG) 128 debug.println("Tokenized: " + lexan.getImage() + " as: "+ token + " offset:" + offset); 130 return token; 131 132 } catch (Error err) { 133 if (DEBUG) { 134 err.printStackTrace(debug); 135 return mapper.guessToken("", -999, true); } else { 137 throw err; 138 } 139 } catch (RuntimeException ex) { 140 if (DEBUG) { 141 ex.printStackTrace(debug); 142 return mapper.guessToken("", -999, true); } else { 144 throw ex; 145 } 146 } 147 } 148 149 155 public void loadState(StateInfo stateInfo) { 156 157 if (DEBUG) debug.println("Loading state ["+ offset + "," + tokenOffset + "]: " + stateInfo + "@" + stopOffset); 159 super.loadState(stateInfo); 160 161 JJStateInfo info = (JJStateInfo) stateInfo; 162 lexan.setStateInfo(info.getSubStates()); 163 } 164 165 166 public void storeState(StateInfo stateInfo) { 167 168 super.storeState(stateInfo); 170 171 JJStateInfo info = (JJStateInfo) stateInfo; 172 info.setSubStates(lexan.getStateInfo()); 173 174 if (DEBUG) debug.println("Storing state ["+ offset + "," + tokenOffset + "]: " + info + "@" + stopOffset); 176 } 177 178 179 public int compareState(StateInfo state) { 180 if (super.compareState(state) == Syntax.DIFFERENT_STATE) 181 return Syntax.DIFFERENT_STATE; 182 183 return ((JJStateInfo)state).compareSubStates(lexan.getStateInfo()); 184 } 185 186 187 public StateInfo createStateInfo() { 188 return new JJStateInfo(); 189 } 190 191 } 192 | Popular Tags |