KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > Keywords


1 /*****************************************************************************
2  * Copyright (C) Zephyr Business Solutions Corp. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  *****************************************************************************/

8 /*
9  * Created on Dec 19, 2004
10  *
11  * Author Ben Yu
12  */

13 package jfun.parsec;
14
15 import java.util.Arrays JavaDoc;
16 import java.util.Comparator JavaDoc;
17 import java.util.HashMap JavaDoc;
18 import java.util.TreeSet JavaDoc;
19
20 import jfun.parsec.tokens.Tokens;
21
22
23 /**
24  * @author Ben Yu
25  *
26  * Dec 19, 2004
27  */

28 final class Keywords implements java.io.Serializable JavaDoc{
29   private interface StringCase {
30     Comparator JavaDoc<String JavaDoc> getComparator();
31     String JavaDoc getKey(String JavaDoc k);
32     jfun.parsec.Map<String JavaDoc,Object JavaDoc> getMap(java.util.Map JavaDoc<String JavaDoc,Object JavaDoc> m);
33   }
34   private static final StringCase case_sensitive = new CaseSensitive();
35   private static final StringCase case_insensitive = new CaseInsensitive();
36   private static final class CaseSensitive implements StringCase{
37     private static final Comparator JavaDoc<String JavaDoc> comparator = new Comparator JavaDoc<String JavaDoc>(){
38       public int compare(String JavaDoc a, String JavaDoc b){
39         if(a==b) return 0;
40         else if(a==null) return -1;
41         else if(b==null) return 1;
42         else return a.compareTo(b);
43       }
44     };
45     public Comparator JavaDoc<String JavaDoc> getComparator(){return comparator;}
46     public String JavaDoc getKey(final String JavaDoc k){return k;}
47     public jfun.parsec.Map<String JavaDoc,Object JavaDoc> getMap(final java.util.Map JavaDoc<String JavaDoc,Object JavaDoc> m){
48       return Maps.jmap(m);
49     }
50   };
51   private static final class CaseInsensitive implements StringCase{
52     private static final Comparator JavaDoc<String JavaDoc> comparator = new Comparator JavaDoc<String JavaDoc>(){
53       public int compare(String JavaDoc a, String JavaDoc b){
54         if(a==b) return 0;
55         else if(a==null) return -1;
56         else if(b==null) return 1;
57         else return a.compareToIgnoreCase(b);
58       }
59     };
60     public Comparator JavaDoc getComparator(){return comparator;}
61     public String JavaDoc getKey(final String JavaDoc k){return k.toLowerCase();}
62     public jfun.parsec.Map<String JavaDoc,Object JavaDoc> getMap(final java.util.Map JavaDoc<String JavaDoc,Object JavaDoc> m){
63       return new jfun.parsec.Map<String JavaDoc,Object JavaDoc>(){
64         public Object JavaDoc map(final String JavaDoc key){
65           return m.get(key.toLowerCase());
66         }
67       };
68     }
69   };
70   private static StringCase getStringCase(boolean cs){
71     return cs?case_sensitive:case_insensitive;
72   }
73   private static String JavaDoc[] nub(final String JavaDoc[] names, Comparator JavaDoc<String JavaDoc> c){
74     final TreeSet JavaDoc<String JavaDoc> set = new TreeSet JavaDoc<String JavaDoc>(c);
75     set.addAll(Arrays.asList(names));
76     final String JavaDoc[] _names = new String JavaDoc[set.size()];
77     set.toArray(_names);
78     return _names;
79   }
80   /*
81   //this method tries each keyword sequentially. while getInstance() scans a word and then lookup in a hash table.
82   static WordsData instance(final String[] names, boolean cs){
83     final StringCase scase = getStringCase(cs);
84     final String[] _names = nub(names, scase.getComparator());
85     final HashMap map = new HashMap();
86     final Lexer[] lxs = new Lexer[_names.length];
87     for(int i=0; i<_names.length; i++){
88       final String n = _names[i];
89       final Token tok = new TokenReserved(n);
90       map.put(scase.getKey(n), tok);
91       final Parser kw = cs?Scanners.isString(n):Scanners.isStringCI(n);
92       final Lexer lx = Lexers.toLexer(
93           Scanners.delimited(kw), ConstTokenizer.instance(tok)
94       );
95       lxs[i] = lx;
96     }
97     return new WordsData(scase.getMap(map), lxs);
98   }*/

99   static WordsData getWordsInstance(final Parser<?> wscanner, final String JavaDoc[] keywords, boolean cs){
100     return getWordsInstance("words", wscanner, keywords, cs);
101   }
102   static WordsData getKeywordsInstance(final Parser<?> wscanner, final String JavaDoc[] keywords, boolean cs){
103     return getKeywordsInstance("keywords", wscanner, keywords, cs);
104   }
105   private static final FromString _unknown_keyword = new String2Value(null);
106   static WordsData getWordsInstance(final String JavaDoc name, final Parser<?> wscanner, final String JavaDoc[] keywords, boolean cs){
107     return getInstance(name, wscanner, keywords, cs, String2TokenWord.instance());
108   }
109   static WordsData getKeywordsInstance(final String JavaDoc name, final Parser<?> wscanner, final String JavaDoc[] keywords, boolean cs){
110     return getInstance(name, wscanner, keywords, cs, _unknown_keyword);
111   }
112   static <T> WordsData getInstance(final String JavaDoc name, final Parser<?> wscanner, final String JavaDoc[] names, boolean cs,
113       final FromString<T> stok){
114     final StringCase scase = getStringCase(cs);
115     final String JavaDoc[] _names = nub(names, scase.getComparator());
116     final HashMap JavaDoc<String JavaDoc,Object JavaDoc> map = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
117     for(int i=0; i<_names.length; i++){
118       final String JavaDoc n = _names[i];
119       final Object JavaDoc tok = Tokens.reserved(n);
120       map.put(scase.getKey(n), tok);
121     }
122     final jfun.parsec.Map<String JavaDoc,Object JavaDoc> fmap = scase.getMap(map);
123     final Tokenizer tn = new Tokenizer(){
124       public Object JavaDoc toToken(final CharSequence JavaDoc cs,
125           final int from, final int len){
126         final String JavaDoc txt = cs.subSequence(from, from+len).toString();
127         final Object JavaDoc t = fmap.map(txt);
128         if(t!=null) return t;
129         else return stok.fromString(from, len, txt);
130       }
131     };
132     final Parser<Tok> lx = Lexers.lexer(name, wscanner, tn);
133     return new WordsData(fmap, new Parser[]{lx});
134   }
135 }
136
Popular Tags