KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > languages > Language


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.modules.languages;
21
22 import org.netbeans.api.languages.ASTItem;
23 import org.netbeans.api.languages.ASTPath;
24 import org.netbeans.api.languages.LanguagesManager;
25 import org.netbeans.api.languages.ParseException;
26 import org.netbeans.api.languages.ASTNode;
27 import org.netbeans.api.languages.ASTToken;
28 import org.netbeans.modules.languages.LanguagesManagerImpl;
29 import org.netbeans.modules.languages.parser.*;
30 import java.awt.*;
31 import java.util.*;
32 import java.util.List JavaDoc;
33 import org.netbeans.modules.languages.parser.LLSyntaxAnalyser;
34 import org.netbeans.modules.languages.parser.LLSyntaxAnalyser.Rule;
35 import org.openide.ErrorManager;
36
37
38 /**
39  *
40  * @author Jan Jancura
41  */

42 public class Language {
43
44     
45     public static final String JavaDoc ACTION = "ACTION";
46     public static final String JavaDoc AST = "AST";
47     public static final String JavaDoc BRACE = "BRACE";
48     public static final String JavaDoc COLOR = "COLOR";
49     public static final String JavaDoc COMPLETE = "COMPLETE";
50     public static final String JavaDoc COMPLETION = "COMPLETION";
51     public static final String JavaDoc FOLD = "FOLD";
52     public static final String JavaDoc HYPERLINK = "HYPERLINK";
53     public static final String JavaDoc IMPORT = "IMPORT";
54     public static final String JavaDoc INDENT = "INDENT";
55     public static final String JavaDoc MARK = "MARK";
56     public static final String JavaDoc NAVIGATOR = "NAVIGATOR";
57     public static final String JavaDoc PARSE = "PARSE";
58     public static final String JavaDoc PROPERTIES = "PROPERTIES";
59     public static final String JavaDoc REFORMAT = "REFORMAT";
60     public static final String JavaDoc SKIP = "SKIP";
61     public static final String JavaDoc STORE = "STORE";
62     public static final String JavaDoc TOKEN = "TOKEN";
63     public static final String JavaDoc TOOLTIP = "TOOLTIP";
64     
65     private Parser parser;
66     private List JavaDoc<TokenType> tokenTypes = new ArrayList<TokenType> ();
67     private Set<String JavaDoc> skipTokenTypes;
68     private String JavaDoc mimeType;
69     private LLSyntaxAnalyser analyser = null;
70     private List JavaDoc<ASTNode> grammarASTNodes = new ArrayList<ASTNode> ();
71     private List JavaDoc<Rule> grammarRules;
72     private List JavaDoc<Language> importedLangauges = new ArrayList<Language> ();
73
74     
75     /** Creates a new instance of Language */
76     public Language (String JavaDoc mimeType) {
77         this.mimeType = mimeType;
78     }
79     
80     
81     // public methods ..........................................................
82

83     public String JavaDoc getMimeType () {
84         return mimeType;
85     }
86
87     public Parser getParser () {
88         if (parser == null)
89             parser = Parser.create (tokenTypes);
90         return parser;
91     }
92     
93     public List JavaDoc<TokenType> getTokenTypes () {
94         return Collections.unmodifiableList (tokenTypes);
95     }
96     
97     public Set<String JavaDoc> getSkipTokenTypes () {
98         if (skipTokenTypes == null) {
99             skipTokenTypes = new HashSet<String JavaDoc> ();
100             List JavaDoc<Feature> ss = getFeatures ("SKIP");
101             Iterator<Feature> it = ss.iterator ();
102             while (it.hasNext ()) {
103                 Feature s = it.next ();
104                 skipTokenTypes.add (s.getSelector ().getAsString ());
105             }
106         }
107         return skipTokenTypes;
108     }
109     
110     public boolean hasAnalyser () {
111         return !grammarASTNodes.isEmpty ();
112     }
113     
114     public LLSyntaxAnalyser getAnalyser () {
115         if (analyser != null) return analyser;
116         synchronized (this) {
117             if (analyser != null) return analyser;
118             analyser = LLSyntaxAnalyser.create (this);
119             return analyser;
120         }
121     }
122     
123     public List JavaDoc<Language> getImportedLanguages () {
124         return importedLangauges;
125     }
126     
127     public static TokenType createTokenType (
128         String JavaDoc startState,
129         Pattern<TokenType> pattern,
130         String JavaDoc type,
131         String JavaDoc endState,
132         int priority,
133         Feature properties
134     ) {
135         return new TokenType (
136             startState,
137             pattern,
138             type,
139             endState,
140             priority,
141             properties
142         );
143     }
144
145     
146     // package private interface ...............................................
147

148     void addToken (
149         String JavaDoc startState,
150         String JavaDoc type,
151         Pattern pattern,
152         String JavaDoc endState,
153         Feature properties
154     ) {
155         if (parser != null)
156             throw new InternalError JavaDoc ();
157         tokenTypes.add (createTokenType (
158             startState,
159             pattern,
160             type,
161             endState,
162             tokenTypes.size (),
163             properties
164         ));
165     }
166     
167     void addRule (ASTNode rule) {
168         if (analyser != null)
169             throw new InternalError JavaDoc ();
170         grammarASTNodes.add (rule);
171     }
172     
173     public void addRule (Rule rule) {
174         if (grammarRules == null) grammarRules = new ArrayList<Rule> ();
175         grammarRules.add (rule);
176     }
177     
178     public List JavaDoc<Rule> getRules () {
179         if (grammarRules == null)
180             grammarRules = Petra.convert (grammarASTNodes, getMimeType ());
181         return grammarRules;
182     }
183     
184     private Feature preprocessorImport;
185     
186     public Feature getPreprocessorImport () {
187         return preprocessorImport;
188     }
189     
190     private Map<String JavaDoc,Feature> tokenImports = new HashMap<String JavaDoc,Feature> ();
191     
192     public Map<String JavaDoc,Feature> getTokenImports () {
193         return tokenImports;
194     }
195     
196     void importLanguage (
197         Feature feature
198     ) {
199         try {
200             String JavaDoc mimeType = (String JavaDoc) feature.getValue ("mimeType");
201             Language language = ((LanguagesManagerImpl) LanguagesManager.getDefault ()).getLanguage (mimeType);
202             if (feature.getPattern ("start") != null) {
203                 //feature.put ("token", "PE");
204
assert (preprocessorImport == null);
205                 preprocessorImport = feature;
206                 importedLangauges.add (language);
207                 return;
208             }
209             if (feature.getValue ("state") == null) {
210                 String JavaDoc tokenName = feature.getSelector ().getAsString ();
211                 assert (!tokenImports.containsKey (tokenName));
212                 tokenImports.put (tokenName, feature);
213                 importedLangauges.add (language);
214                 return;
215             }
216
217             String JavaDoc state = (String JavaDoc) feature.getValue ("state");
218             String JavaDoc tokenName = feature.getSelector ().getAsString ();
219
220             // import tokenTypes
221
Iterator<TokenType> it = language.getTokenTypes ().iterator ();
222             while (it.hasNext ()) {
223                 TokenType tt = it.next ();
224                 String JavaDoc startState = tt.getStartState ();
225                 Pattern pattern = tt.getPattern ().clonePattern ();
226                 String JavaDoc endState = tt.getEndState ();
227                 if (startState == null || Parser.DEFAULT_STATE.equals (startState))
228                     startState = state;
229                 else
230                     startState = tokenName + '-' + startState;
231                 if (endState == null || Parser.DEFAULT_STATE.equals (endState))
232                     endState = state;
233                 else
234                     endState = tokenName + '-' + endState;
235                 addToken (startState, tt.getType (), pattern, endState, tt.getProperties ());
236             }
237
238             // import grammar rues
239
grammarASTNodes.addAll (language.grammarASTNodes);
240             // import features
241
importAllFeatures (language);
242             importedLangauges.addAll (language.importedLangauges);
243             tokenImports.putAll (language.tokenImports);
244         } catch (ParseException ex) {
245             ErrorManager.getDefault ().notify (ex);
246         }
247     }
248
249     
250     // private helper methods ..................................................
251

252     private void importAllFeatures (Language l) {
253         Iterator<String JavaDoc> it = l.featureLists.keySet ().iterator ();
254         while (it.hasNext ()) {
255             String JavaDoc featureName = it.next ();
256             List JavaDoc<Feature> features = l.getFeatures (featureName);
257             Iterator<Feature> it2 = features.iterator ();
258             while (it2.hasNext ()) {
259                 Feature f = it2.next ();
260                 addFeature (f);
261             }
262         }
263     }
264
265     private Map<String JavaDoc,List JavaDoc<Feature>> featureLists = new HashMap<String JavaDoc,List JavaDoc<Feature>> ();
266     private Map<String JavaDoc,Object JavaDoc> featuresMap = new HashMap<String JavaDoc,Object JavaDoc> ();
267     private static final Object JavaDoc BLA = new Object JavaDoc ();
268     
269     public void addFeature (Feature feature) {
270         String JavaDoc featureName = feature.getFeatureName ();
271         if (featureName.equals ("IMPORT")) {
272             importLanguage (feature);
273             return;
274         }
275         
276         List JavaDoc<Feature> list = featureLists.get (featureName);
277         if (list == null) {
278             list = new ArrayList<Feature> ();
279             featureLists.put (featureName, list);
280         }
281         list.add (feature);
282
283         if (feature.getSelector () == null) {
284             Object JavaDoc o = featuresMap.get (featureName);
285             if (o == null)
286                 featuresMap.put (featureName, feature);
287             else
288             if (o instanceof List JavaDoc)
289                 ((List JavaDoc) o).add (feature);
290             else {
291                 List JavaDoc<Feature> l = new ArrayList<Feature> ();
292                 l.add ((Feature) o);
293                 l.add (feature);
294                 featuresMap.put (featureName, l);
295             }
296             return;
297         }
298         Map m = (Map) featuresMap.get (featureName);
299         if (m == null) {
300             m = new HashMap ();
301             featuresMap.put (featureName, m);
302         }
303         List JavaDoc<String JavaDoc> path = feature.getSelector ().getPath ();
304         for (int i = path.size () - 1; i > 0; i--) {
305             String JavaDoc name = path.get (i);
306             Object JavaDoc o = m.get (name);
307             if (o instanceof Map)
308                 m = (Map) o;
309             else {
310                 Map mm = new HashMap ();
311                 if (o != null)
312                     mm.put (BLA, o);
313                 m.put (name, mm);
314                 m = mm;
315             }
316         }
317         String JavaDoc name = path.get (0);
318         Object JavaDoc o = m.get (name);
319         if (o instanceof List JavaDoc)
320             ((List JavaDoc<Feature>) o).add (feature);
321         else
322         if (o instanceof Map) {
323             m = (Map) o;
324             o = m.get (BLA);
325             if (o instanceof List JavaDoc)
326                 ((List JavaDoc) o).add (feature);
327             else
328             if (o == null)
329                 m.put (BLA, feature);
330             else {
331                 List JavaDoc l = new ArrayList ();
332                 l.add (o);
333                 l.add (feature);
334                 m.put (BLA, l);
335             }
336         } else
337         if (o == null)
338             m.put (name, feature);
339         else {
340             List JavaDoc l = new ArrayList ();
341             l.add (o);
342             l.add (feature);
343             m.put (name, l);
344         }
345     }
346
347     public List JavaDoc<Feature> getFeatures (String JavaDoc featureName) {
348         List JavaDoc<Feature> r = featureLists.get (featureName);
349         if (r != null) return r;
350         return Collections.<Feature>emptyList ();
351     }
352
353     public Feature getFeature (String JavaDoc featureName) {
354         List JavaDoc<Feature> r = featureLists.get (featureName);
355         if (r == null) return null;
356         if (r.size () == 1) return r.get (0);
357         throw new IllegalArgumentException JavaDoc ();
358     }
359     
360     public Feature getFeature (String JavaDoc featureName, ASTPath path) {
361         List JavaDoc<Feature> r = getFeatures (featureName, path);
362         if (r.isEmpty ()) return null;
363         if (r.size () == 1) return r.get (0);
364         throw new IllegalArgumentException JavaDoc ();
365     }
366     
367     public Feature getFeature (String JavaDoc featureName, String JavaDoc id) {
368         Map m = (Map) featuresMap.get (featureName);
369         if (m == null) return null;
370         Object JavaDoc o = m.get (id);
371         if (o instanceof Map)
372             o = ((Map) o).get (BLA);
373         if (o == null) return null;
374         if (o instanceof Feature)
375             return (Feature) o;
376         List JavaDoc<Feature> r = (List JavaDoc<Feature>) o;
377         if (r.isEmpty ()) return null;
378         if (r.size () == 1) return r.get (0);
379         throw new IllegalArgumentException JavaDoc ();
380     }
381
382     public List JavaDoc<Feature> getFeatures (String JavaDoc featureName, ASTPath path) {
383         Map m = (Map) featuresMap.get (featureName);
384         if (m == null) return Collections.<Feature>emptyList ();
385         Object JavaDoc last = null;
386         int i = path.size () - 1;
387         for (; i > 0; i--) {
388             ASTItem item = path.get (i);
389             String JavaDoc name = item instanceof ASTToken ?
390                 ((ASTToken) item).getType () :
391                 ((ASTNode) item).getNT ();
392             Object JavaDoc o = m.get (name);
393             if (m.containsKey (BLA))
394                 last = m.get (BLA);
395             if (o instanceof Map) {
396                 m = (Map) o;
397                 continue;
398             }
399             if (o instanceof List JavaDoc)
400                 return (List JavaDoc<Feature>) o;
401             if (o != null)
402                 return Collections.<Feature>singletonList ((Feature) o);
403             if (last != null) {
404                 if (last instanceof List JavaDoc)
405                     return (List JavaDoc<Feature>) last;
406                  return Collections.<Feature>singletonList ((Feature) o);
407             }
408             break;
409         }
410         return Collections.<Feature>emptyList ();
411     }
412     
413 // public Object getFeature (String featureName, ASTPath path) {
414
// Map m = (Map) features.get (featureName);
415
// if (m == null) return null;
416
// ListIterator<ASTItem> it = path.listIterator (path.size ());
417
// while (it.hasPrevious ()) {
418
// ASTItem item = it.previous ();
419
// Object value = (item instanceof ASTToken) ?
420
// m.get (((ASTToken) item).getType ()) :
421
// m.get (((ASTNode) item).getNT ());
422
// if (value == null) return m.get ("");
423
// if (value instanceof MMap)
424
// m = (Map) value;
425
// else
426
// return value;
427
// }
428
// return null;
429
// }
430
//
431
// public Object getFeature (String featureName, String id) {
432
// Map m = (Map) features.get (featureName);
433
// if (m == null) return null;
434
// Object value = m.get (id);
435
// if (value == null) return m.get ("");
436
// if (value instanceof MMap) {
437
// m = (Map) value;
438
// return m.get ("");
439
// }
440
// return value;
441
// }
442
//
443
// public Collection getFeatures (String featureName) {
444
// Map m = (Map) features.get (featureName);
445
// if (m == null) return null;
446
// return m.values ();
447
// }
448
//// public Map getFeature (String featureName) {
449
//// return (Map) features.get (featureName);
450
//// }
451
//
452
//// private Object getFeature (String featureName, ASTItem item) {
453
//// if (item instanceof ASTNode)
454
//// return getFeature (featureName, (ASTNode) item);
455
//// return getFeature (featureName, (ASTToken) item);
456
//// }
457
////
458
//// private Object getFeature (String featureName, ASTToken token) {
459
//// Map m = (Map) features.get (featureName);
460
//// if (m == null) return null;
461
//// Object result = m.get (token.getType ());
462
//// if (result instanceof MMap) return null;
463
//// return result;
464
//// }
465

466 // public boolean supportsFeature (String featureName) {
467
// return features.get (featureName) != null;
468
// }
469
//
470
// void addFeature (String featureName, Identifier id, Object feature) {
471
// Map m = getFolder (featureName);
472
// for (int i = id.name.size () - 1; i > 0; i--) {
473
// Object o = m.get (id.name.get (i));
474
// if (o instanceof MMap)
475
// m = (Map) o;
476
// else {
477
// Map mm = new MMap ();
478
// if (o != null)
479
// mm.put ("", o);
480
// m.put (id.name.get (i), mm);
481
// m = mm;
482
// }
483
// }
484
// Object o = m.get (id.name.get (0));
485
// if (o != null && o instanceof Map)
486
// ((Map) o).put ("", feature);
487
// else
488
// m.put (
489
// id.name.get (0),
490
// feature
491
// );
492
// }
493

494 // private Map getFolder (String featureName) {
495
// Map m = (Map) features.get (featureName);
496
// if (m == null) {
497
// m = new HashMap ();
498
// features.put (featureName, m);
499
// }
500
// return m;
501
// }
502

503     void print () {
504         System.out.println("\nPrint " + mimeType);
505         System.out.println("Tokens:");
506         Iterator<TokenType> it = getTokenTypes ().iterator ();
507         while (it.hasNext ()) {
508             TokenType r = it.next ();
509             System.out.println(" " + r);
510         }
511         System.out.println("Grammar Rules:");
512         Iterator<Rule> it2 = getAnalyser ().getRules ().iterator ();
513         while (it.hasNext ()) {
514             Rule r = it2.next ();
515             System.out.println(" " + r);
516         }
517     }
518     
519     public String JavaDoc toString () {
520         return "Language " + mimeType;
521     }
522     
523     
524     // innerclasses ............................................................
525

526     public static final class TokenType {
527         
528         private String JavaDoc startState;
529         private Pattern<TokenType> pattern;
530         private String JavaDoc type;
531         private String JavaDoc endState;
532         private int priority;
533         private Feature properties;
534         
535         private TokenType (
536             String JavaDoc startState,
537             Pattern<TokenType> pattern,
538             String JavaDoc type,
539             String JavaDoc endState,
540             int priority,
541             Feature properties
542         ) {
543             this.startState = startState == null ? Parser.DEFAULT_STATE : startState;
544             this.pattern = pattern;
545             this.type = type;
546             this.endState = endState == null ? Parser.DEFAULT_STATE : endState;
547             this.priority = priority;
548             this.properties = properties;
549         }
550         
551         public String JavaDoc getType () {
552             return type;
553         }
554         
555         public String JavaDoc getStartState () {
556             return startState;
557         }
558         
559         public String JavaDoc getEndState () {
560             return endState;
561         }
562         
563         public Pattern<TokenType> getPattern () {
564             return pattern;
565         }
566         
567         public int getPriority () {
568             return priority;
569         }
570         
571         public Feature getProperties () {
572             return properties;
573         }
574         
575         public String JavaDoc toString () {
576             return "Rule " + startState + " : type " + type + " : " + endState;
577         }
578     }
579 }
580
581
582
Popular Tags