001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.lib.lexer.lang;
043:
044: import org.netbeans.api.lexer.PartType;
045: import org.netbeans.lib.lexer.lang.TestJoinSectionsTextTokenId;
046: import org.netbeans.api.lexer.Token;
047: import org.netbeans.spi.lexer.Lexer;
048: import org.netbeans.spi.lexer.LexerInput;
049: import org.netbeans.spi.lexer.LexerRestartInfo;
050: import org.netbeans.spi.lexer.TokenFactory;
051:
052: /**
053: *
054: * @author mmetelka
055: */
056: final class TestJoinSectionsTextLexer implements
057: Lexer<TestJoinSectionsTextTokenId> {
058:
059: // Copy of LexerInput.EOF
060: private static final int EOF = LexerInput.EOF;
061:
062: private final LexerInput input;
063:
064: private final TokenFactory<TestJoinSectionsTextTokenId> tokenFactory;
065:
066: private Token<TestJoinSectionsTextTokenId> previousSectionLastToken;
067:
068: private int state;
069:
070: private static final int IN_BRACES = 1;
071:
072: TestJoinSectionsTextLexer(
073: LexerRestartInfo<TestJoinSectionsTextTokenId> info) {
074: this .input = info.input();
075: this .tokenFactory = info.tokenFactory();
076: this .state = (info.state() != null) ? (Integer) info.state()
077: : 0;
078: }
079:
080: public Object state() {
081: return state;
082: }
083:
084: public Token<TestJoinSectionsTextTokenId> nextToken() {
085: // Check for unfinished incomplete token
086: if (state == IN_BRACES) {
087: return finishIncompleteBraces();
088: }
089:
090: int c = input.read();
091: switch (c) {
092: case '{':
093: if (input.readLength() > 1) {
094: input.backup(1);
095: return token(TestJoinSectionsTextTokenId.TEXT);
096: }
097: while (true) {
098: switch ((c = input.read())) {
099: case '}':
100: return token(TestJoinSectionsTextTokenId.BRACES);
101: case EOF:
102: state = IN_BRACES;
103: return tokenFactory.createToken(
104: TestJoinSectionsTextTokenId.BRACES, input
105: .readLength(), PartType.START);
106: }
107: }
108: // break;
109:
110: case EOF: // no more chars on the input
111: return null; // the only legal situation when null can be returned
112:
113: default: // In regular text
114: while (true) {
115: switch ((c = input.read())) {
116: case '{':
117: case EOF:
118: input.backup(1);
119: return token(TestJoinSectionsTextTokenId.TEXT);
120: }
121: }
122: // break;
123: }
124: }
125:
126: private Token<TestJoinSectionsTextTokenId> finishIncompleteBraces() {
127: while (true) {
128: switch (input.read()) {
129: case '}':
130: state = 0;
131: return tokenFactory.createToken(
132: TestJoinSectionsTextTokenId.BRACES, input
133: .readLength(), PartType.END);
134:
135: case EOF:
136: input.backup(1);
137: if (input.readLength() == 0)
138: return null;
139: return tokenFactory.createToken(
140: TestJoinSectionsTextTokenId.BRACES, input
141: .readLength(), PartType.MIDDLE);
142: }
143: }
144: }
145:
146: private Token<TestJoinSectionsTextTokenId> token(
147: TestJoinSectionsTextTokenId id) {
148: return tokenFactory.createToken(id);
149: }
150:
151: public void release() {
152: }
153:
154: }
|