KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > text > syntax > javacc > lib > JJEditorSyntax


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 /**
28  * General purpose framework for javacc generated grammars.
29  *
30  * @author Petr Kuzel
31  * @version 1.0
32  */

33 public class JJEditorSyntax extends Syntax implements JJConstants {
34
35     /** debugging support. */
36     private static final boolean DEBUG = false;
37     private static PrintStream debug = null;
38     private static boolean debugColoring = Boolean.getBoolean("netbeans.debug.editor.draw"); // NOI18N
39
private static int dbgOffset;
40
41     /** Instance of analyzer. */
42     private final JJSyntaxInterface lexan;
43     private final JJMapperInterface mapper;
44     private final StringParserInput pi;
45     
46     /** Creates new XMLEditorSyntax */
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")); // NOI18N
51
} 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     /** General implementation frame. */
63     protected TokenID parseToken() {
64
65         try {
66             JJTokenID token;
67
68             //!!! jj HACK never stop in the middle of regexp
69
//that change jj state
70
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" + // NOI18N
83
new String JavaDoc(buffer, tokenOffset, stopOffset-tokenOffset)
84                     );
85                 }
86             }
87
88             /** Map syntax init state to javacc init state. */
89             if (state == INIT) {
90                 lexan.init(pi);
91             } else {
92                 lexan.init(pi, state); //restore state
93
}
94
95             lexan.next(); // call jj analyzer bridge
96

97             state = lexan.getState();
98             int id = lexan.getID();
99
100             if (id != JJ_EOF) {
101                 token = mapper.createToken(id);
102                 
103                 //!!! hack: if grammar does not recognize a char at input
104
//let try recognize next one, otherwise StackOverFlowError
105
if (id == JJ_ERR && lexan.getLength() == 0) {
106                     offset++;
107                 }
108             } else {
109                 token = mapper.guessToken(lexan.getImage(), state, lastBuffer);
110             }
111
112             // move offset ahead
113
if (token != null) {
114                 offset += lexan.getLength();
115
116                 // if it is kind of error token set supposed one
117
if (token.isError()) {
118                     supposedTokenID = mapper.supposedToken(lexan.getImage(), state, id);
119                 }
120                 
121             } else {
122                 //lexan.getLength() may return invalid value at buffer boundary (EOF)
123
offset = stopOffset;
124             }
125
126             
127             if (DEBUG)
128                 debug.println("Tokenized: " + lexan.getImage() + " as: "+ token + " offset:" + offset); // NOI18N
129

130             return token;
131             
132         } catch (Error JavaDoc err) {
133             if (DEBUG) {
134                 err.printStackTrace(debug);
135                 return mapper.guessToken("", -999, true); // NOI18N
136
} else {
137                 throw err;
138             }
139         } catch (RuntimeException JavaDoc ex) {
140             if (DEBUG) {
141                 ex.printStackTrace(debug);
142                 return mapper.guessToken("", -999, true); // NOI18N
143
} else {
144                 throw ex;
145             }
146         }
147     }
148
149     /** Load valid mark state into the analyzer. Offsets
150     * are already initialized when this method is called. This method
151     * must get the state from the mark and set it to the analyzer. Then
152     * it must decrease tokenOffset by the preScan stored in the mark state.
153     * @param markState mark state to be loaded into syntax. It must be non-null value.
154     */

155     public void loadState(StateInfo stateInfo) {
156
157         if (DEBUG) debug.println("Loading state ["+ offset + "," + tokenOffset + "]: " + stateInfo + "@" + stopOffset); // NOI18N
158

159         super.loadState(stateInfo);
160         
161         JJStateInfo info = (JJStateInfo) stateInfo;
162         lexan.setStateInfo(info.getSubStates());
163     }
164
165     /** Store state of this analyzer into given mark state. */
166     public void storeState(StateInfo stateInfo) {
167         
168 // Thread.dumpStack();
169
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); // NOI18N
175

176     }
177
178     /** compare state of this analyzed to given state info. */
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     /** Create state info appropriate for particular analyzer */
187     public StateInfo createStateInfo() {
188         return new JJStateInfo();
189     }
190
191 }
192
Popular Tags