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: * If you wish your version of this file to be governed by only the CDDL
025: * or only the GPL Version 2, indicate your decision by adding
026: * "[Contributor] elects to include this software in this distribution
027: * under the [CDDL or GPL Version 2] license." If you do not indicate a
028: * single choice of license, a recipient has the option to distribute
029: * your version of this file under either the CDDL, the GPL Version 2 or
030: * to extend the choice of license to its licensees as provided above.
031: * However, if you add GPL Version 2 code and therefore, elected the GPL
032: * Version 2 license, then the option applies only if the new code is
033: * made subject to such option by the copyright holder.
034: *
035: * Contributor(s):
036: *
037: * The Original Software is NetBeans. The Initial Developer of the Original
038: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
039: * Microsystems, Inc. All Rights Reserved.
040: *
041: * If you wish your version of this file to be governed by only the CDDL
042: * or only the GPL Version 2, indicate your decision by adding
043: * "[Contributor] elects to include this software in this distribution
044: * under the [CDDL or GPL Version 2] license." If you do not indicate a
045: * single choice of license, a recipient has the option to distribute
046: * your version of this file under either the CDDL, the GPL Version 2 or
047: * to extend the choice of license to its licensees as provided above.
048: * However, if you add GPL Version 2 code and therefore, elected the GPL
049: * Version 2 license, then the option applies only if the new code is
050: * made subject to such option by the copyright holder.
051: */
052:
053: package org.netbeans.modules.cnd.lexer;
054:
055: import org.netbeans.api.lexer.Token;
056: import org.netbeans.cnd.api.lexer.CppTokenId;
057: import org.netbeans.cnd.api.lexer.Filter;
058: import org.netbeans.spi.lexer.LexerRestartInfo;
059:
060: /**
061: *
062: * @author Vladimir Voskresensky
063: */
064: public class CppLexer extends CndLexer {
065:
066: private final Filter<CppTokenId> lexerFilter;
067:
068: @SuppressWarnings("unchecked")
069: public CppLexer(Filter<CppTokenId> defaultFilter,
070: LexerRestartInfo<CppTokenId> info) {
071: super (info);
072: Filter<CppTokenId> filter = (Filter<CppTokenId>) info
073: .getAttributeValue("lexer-filter"); // NOI18N
074: this .lexerFilter = filter != null ? filter : defaultFilter;
075: }
076:
077: @SuppressWarnings("fallthrough")
078: @Override
079: protected Token<CppTokenId> finishSharp() {
080: // one prerpocessor directive block
081: // we should eat block comments to skip it's new lines
082: // also eat string and char literals to prevent incorrect recognition
083: // of started block comment like #define A "/*"
084: while (true) {
085: switch (read(true)) {
086: case '\"':
087: skipLiteral(true);
088: break;
089: case '\'':
090: skipLiteral(false);
091: break;
092: case '/':
093: if (read(true) == '*') { // block or doxygen comment
094: skipComment();
095: } else {
096: backup(1);
097: }
098: break;
099: case '\r':
100: consumeNewline();
101: // nobreak
102: case '\n':
103: case EOF:
104: return token(CppTokenId.PREPROCESSOR_DIRECTIVE);
105: }
106: }
107: }
108:
109: private void skipComment() {
110: super .finishComment(false);
111: }
112:
113: @SuppressWarnings("fallthrough")
114: private void skipLiteral(boolean endDblQuote) {
115: while (true) { // string literal
116: switch (read(true)) {
117: case '"': // NOI18N
118: if (endDblQuote) {
119: return;
120: }
121: break;
122: case '\'': // NOI18N
123: if (!endDblQuote) {
124: return;
125: }
126: break;
127: case '\\': // escaped char
128: read(false); // read escaped char
129: break;
130: case '\r':
131: case '\n':
132: backup(1);
133: case EOF:
134: return;
135: }
136: }
137: }
138:
139: @Override
140: protected CppTokenId getKeywordOrIdentifierID(CharSequence text) {
141: CppTokenId id = lexerFilter.check(text);
142: return id != null ? id : CppTokenId.IDENTIFIER;
143: }
144: }
|