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-2006 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.modules.languages;
043:
044: import java.beans.PropertyChangeListener;
045: import java.util.ArrayList;
046: import java.util.Collections;
047: import java.util.HashMap;
048: import java.util.HashSet;
049: import java.util.Iterator;
050: import java.util.List;
051: import java.util.Map;
052: import java.util.Set;
053:
054: import org.netbeans.api.languages.ParseException;
055: import org.netbeans.modules.languages.parser.LLSyntaxAnalyser;
056: import org.netbeans.modules.languages.parser.Parser;
057: import org.netbeans.modules.languages.parser.Pattern;
058:
059: /**
060: *
061: * @author Jan Jancura
062: */
063: public class TestLanguage extends Language {
064:
065: private String mimeType = "test/test";
066: private Parser parser;
067: private LLSyntaxAnalyser analyser;
068: private FeatureList featureList = new FeatureList();
069: private List<TokenType> tokenTypes;
070: private List<Rule> rules;
071:
072: /** Creates a new instance of Language */
073: public TestLanguage() {
074: }
075:
076: public void addToken(int typeID, String typeName) {
077: tokenTypeToID.put(typeName, typeID);
078: idToTokenType.put(typeID, typeName);
079: tokenTypeCount = idToTokenType.keySet().size();
080: }
081:
082: public void addToken(int typeID, String typeName, Pattern pattern,
083: String startState, String endState, int priority,
084: Feature properties) {
085: tokenTypeToID.put(typeName, typeID);
086: idToTokenType.put(typeID, typeName);
087: tokenTypeCount = idToTokenType.keySet().size();
088: if (tokenTypes == null)
089: tokenTypes = new ArrayList<TokenType>();
090: tokenTypes.add(new TokenType(startState, pattern, typeName,
091: typeID, endState, priority, properties));
092: }
093:
094: public void addRule(String nt, List rightSide) {
095: if (rules == null)
096: rules = new ArrayList<Rule>();
097: rules.add(Rule.create(nt, rightSide));
098: }
099:
100: public void addFeature(Feature feature) {
101: featureList.add(feature);
102: }
103:
104: public String getMimeType() {
105: return mimeType;
106: }
107:
108: public Parser getParser() {
109: if (tokenTypes != null && parser == null)
110: parser = Parser.create(tokenTypes);
111: return parser;
112: }
113:
114: public LLSyntaxAnalyser getAnalyser() {
115: if (rules != null && analyser == null) {
116: Set<Integer> skipTokenIDs = new HashSet<Integer>();
117: Iterator<Feature> it = featureList.getFeatures("SKIP")
118: .iterator();
119: while (it.hasNext()) {
120: Feature feature = it.next();
121: if (feature.getFeatureName().equals("SKIP")) {
122: skipTokenIDs.add(tokenTypeToID.get(feature
123: .getSelector().toString()));
124: }
125: }
126: try {
127: analyser = LLSyntaxAnalyser.create(this , rules,
128: skipTokenIDs);
129: } catch (ParseException ex) {
130: ex.printStackTrace();
131: }
132: }
133: return analyser;
134: }
135:
136: public FeatureList getFeatureList() {
137: return featureList;
138: }
139:
140: public void addPropertyChangeListener(PropertyChangeListener l) {
141: }
142:
143: public void removePropertyChangeListener(PropertyChangeListener l) {
144: }
145:
146: // ids ...
147:
148: private Map<String, Integer> tokenTypeToID = new HashMap<String, Integer>();
149: private Map<Integer, String> idToTokenType = new HashMap<Integer, String>();
150: private int tokenTypeCount = 0;
151:
152: public int getTokenID(String tokenType) {
153: if (!tokenTypeToID.containsKey(tokenType))
154: System.err.println("unknown token type: " + tokenType);
155: return tokenTypeToID.get(tokenType);
156: }
157:
158: public int getTokenTypeCount() {
159: return tokenTypeCount;
160: }
161:
162: public String getTokenType(int tokenTypeID) {
163: return idToTokenType.get(tokenTypeID);
164: }
165:
166: private Map<String, Integer> ntToNTID;
167: private Map<Integer, String> ntidToNt;
168:
169: public int getNTID(String nt) {
170: if (ntidToNt == null)
171: ntidToNt = new HashMap<Integer, String>();
172: if (ntToNTID == null)
173: ntToNTID = new HashMap<String, Integer>();
174: if (!ntToNTID.containsKey(nt)) {
175: int id = ntToNTID.size();
176: ntToNTID.put(nt, id);
177: ntidToNt.put(id, nt);
178: }
179: return ntToNTID.get(nt);
180: }
181:
182: public int getNTCount() {
183: if (ntToNTID == null)
184: return 0;
185: return ntToNTID.size();
186: }
187:
188: public String getNT(int ntid) {
189: return ntidToNt.get(ntid);
190: }
191:
192: // imports ...
193:
194: public Feature getPreprocessorImport() {
195: return null;
196: }
197:
198: public Map<String, Feature> getTokenImports() {
199: return Collections.<String, Feature> emptyMap();
200: }
201:
202: public List<Language> getImportedLanguages() {
203: return Collections.<Language> emptyList();
204: }
205: }
|