KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > editor > SyntaxDebug


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
20 package org.netbeans.editor;
21
22 import java.io.IOException JavaDoc;
23
24 /**
25 * Debugging stuff for the syntax scanners
26 *
27 * @author Miloslav Metelka
28 * @version 1.00
29 */

30
31 public class SyntaxDebug {
32
33     public static final String JavaDoc NO_STATE_ASSIGNED = "NO STATE ASSIGNED"; // NOI18N
34
public static final String JavaDoc NULL_STATE = "NULL STATE"; // NOI18N
35
public static final String JavaDoc NULL_SYNTAX_MARK = "NULL SYNTAX MARK"; // NOI18N
36

37     public Syntax syntax;
38
39     public SyntaxDebug(Syntax syntax) {
40         this.syntax = syntax;
41     }
42
43     /** Scans the whole file by some syntax scanner.
44     * @return number of tokens found
45     */

46     public int parseFile(String JavaDoc fileName)
47     throws IOException JavaDoc {
48         char chars[] = Analyzer.loadFile(fileName); // line separator only '\n'
49
syntax.load(null, chars, 0, chars.length, true, 0);
50         int tokenCnt = debugScan();
51         return tokenCnt;
52     }
53
54     /** Debug scanning on the given string. Write output to console.
55     * It returns number of tokens found (excluding EOL and EOT).
56     */

57     public int debugScan() {
58         int tokenCnt = 0;
59         while (true) {
60             TokenID tokenID = syntax.nextToken();
61             if (tokenID == null) { // end of buffer
62
System.out.println("EOT at offset=" + syntax.getTokenOffset()); // NOI18N
63
return tokenCnt;
64             } else { // regular token
65
tokenCnt++;
66                 System.out.println(tokenID.getName() // NOI18N
67
+ " in " + syntax.getTokenContextPath() // NOI18N
68
+ ": TEXT='" + EditorDebug.debugChars(syntax.getBuffer(), // NOI18N
69
syntax.getTokenOffset(), syntax.getTokenLength())
70                         + "', offset=" + syntax.getTokenOffset() // NOI18N
71
+ ", len=" + syntax.getTokenLength() // NOI18N
72
);
73             }
74         }
75     }
76
77     /** Tests if the scanning returns the same number of EOLs as there's actually
78     * '\n' characters in the whole buffer. The test is performed on the whole buffer.
79     */

80     /* public boolean eolTest(char chars[]) {
81         int lfCount = Analyzer.getLFCount(chars);
82         syntax.load(null, chars, 0, chars.length, true, 0);
83         int eolCnt = 0;
84         TokenID tokenID;
85         do {
86           tokenID = syntax.nextToken();
87           if (tokenID == Syntax.EOL) {
88             eolCnt++;
89           }
90         } while (tokenID != Syntax.EOT);
91         if (lfCount == eolCnt) { // test succeeded
92           System.out.println("Test SUCCEEDED. " + lfCount + " new-lines found."); // NOI18N
93         } else {
94           System.out.println("Test FAILED! Number of '\\n' chars: " + lfCount // NOI18N
95               + ", number of EOLs: " + eolCnt); // NOI18N
96         }
97         return lfCount == eolCnt;
98       }
99
100       /** Create array of arrays of chars containing wrong characters */

101     /* protected abstract char[][] createWrongCharsArray();
102
103       /* Some arrays of typical wrong characters that can appear
104       * in the tokens.
105       */

106     /* public static final char[] WRONG_NL = new char[] { '\n' };
107       public static final char[] WRONG_NL_TAB = new char[] { '\n', '\t' };
108       public static final char[] WRONG_NL_TAB_SPC = new char[] { '\n', '\t', ' ' };
109       
110       /** Wrong character arrays for the tokens */

111     /* protected char[][] wrongCharsArray;
112
113       public boolean checkTokenText(int tokenID) {
114         boolean ok = true;
115         if (wrongCharsArray == null) {
116           wrongCharsArray = createWrongCharsArray();
117         }
118         if (wrongCharsArray != null) {
119           if (tokenID >= wrongCharsArray.length) {
120             return false;
121           }
122           char[] wrongChars = wrongCharsArray[tokenID];
123           for (int i = curInd - tokenLength; i < curInd; i++) {
124             for (int j = 0; j < wrongChars.length; j++) {
125               if (buffer[i] == wrongChars[j]) {
126                 System.err.println("Token '" + getTokenName(tokenID) + "' having text " // NOI18N
127                     + debugTokenArea() + " contains wrong character '" // NOI18N
128                     + debugChars(wrongChars, j, 1) + "'. State: " + this); // NOI18N
129                 ok = false;
130               }
131             }
132           }
133         }
134         return ok;
135       }
136
137       public String toStringArea() {
138         return toString() + ", scan area=" + debugBufferArea(); // NOI18N
139       }
140
141       public String debugChars(char chars[], int offset, int len) {
142         if (len < 0) {
143           return "debugChars() ERROR: len=" + len + " < 0"; // NOI18N
144         }
145         StringBuffer sb = new StringBuffer(len);
146         int endOffset = offset + len;
147         for (; offset < endOffset; offset++) {
148           switch (chars[offset]) {
149             case '\n':
150               sb.append("\\n"); // NOI18N
151               break;
152             case '\t':
153               sb.append("\\t"); // NOI18N
154               break;
155             case '\r':
156               sb.append("\\r"); // NOI18N
157               break;
158             default:
159               sb.append(chars[offset]);
160           }
161         }
162         return sb.toString();
163       }
164
165
166       /** Return string describing the area between begInd and curInd */

167     /* public String debugTokenArea() {
168         return debugBufferArea(0, 0);
169       }
170         
171       public String debugBufferArea() {
172         return debugBufferArea(5, 5);
173       }
174       
175       public String debugBufferArea(int preCnt, int postCnt) {
176         StringBuffer sb = new StringBuffer();
177         int preStart = Math.max(begInd - preCnt, 0);
178         preCnt = begInd - preStart;
179         if (preCnt > 0) {
180           sb.append(" prefix='"); // NOI18N
181           sb.append(debugChars(buffer, preStart, preCnt));
182           sb.append("' "); // NOI18N
183         }
184         sb.append("'"); // NOI18N
185         sb.append(debugChars(buffer, begInd, curInd - begInd));
186         sb.append("'"); // NOI18N
187         postCnt = stopInd - curInd;
188         if (postCnt > 0) {
189           sb.append(" suffix='"); // NOI18N
190           sb.append(debugChars(buffer, preStart, preCnt));
191           sb.append("' "); // NOI18N
192         }
193         return sb.toString();
194       }
195
196     */

197
198 }
199
Popular Tags