1 8 13 package jfun.parsec; 14 15 import java.util.Arrays ; 16 import java.util.Comparator ; 17 import java.util.HashMap ; 18 import java.util.TreeSet ; 19 20 import jfun.parsec.tokens.Tokens; 21 22 23 28 final class Keywords implements java.io.Serializable { 29 private interface StringCase { 30 Comparator <String > getComparator(); 31 String getKey(String k); 32 jfun.parsec.Map<String ,Object > getMap(java.util.Map <String ,Object > 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 <String > comparator = new Comparator <String >(){ 38 public int compare(String a, String 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 <String > getComparator(){return comparator;} 46 public String getKey(final String k){return k;} 47 public jfun.parsec.Map<String ,Object > getMap(final java.util.Map <String ,Object > m){ 48 return Maps.jmap(m); 49 } 50 }; 51 private static final class CaseInsensitive implements StringCase{ 52 private static final Comparator <String > comparator = new Comparator <String >(){ 53 public int compare(String a, String 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 getComparator(){return comparator;} 61 public String getKey(final String k){return k.toLowerCase();} 62 public jfun.parsec.Map<String ,Object > getMap(final java.util.Map <String ,Object > m){ 63 return new jfun.parsec.Map<String ,Object >(){ 64 public Object map(final String 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 [] nub(final String [] names, Comparator <String > c){ 74 final TreeSet <String > set = new TreeSet <String >(c); 75 set.addAll(Arrays.asList(names)); 76 final String [] _names = new String [set.size()]; 77 set.toArray(_names); 78 return _names; 79 } 80 99 static WordsData getWordsInstance(final Parser<?> wscanner, final String [] keywords, boolean cs){ 100 return getWordsInstance("words", wscanner, keywords, cs); 101 } 102 static WordsData getKeywordsInstance(final Parser<?> wscanner, final String [] 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 name, final Parser<?> wscanner, final String [] keywords, boolean cs){ 107 return getInstance(name, wscanner, keywords, cs, String2TokenWord.instance()); 108 } 109 static WordsData getKeywordsInstance(final String name, final Parser<?> wscanner, final String [] keywords, boolean cs){ 110 return getInstance(name, wscanner, keywords, cs, _unknown_keyword); 111 } 112 static <T> WordsData getInstance(final String name, final Parser<?> wscanner, final String [] names, boolean cs, 113 final FromString<T> stok){ 114 final StringCase scase = getStringCase(cs); 115 final String [] _names = nub(names, scase.getComparator()); 116 final HashMap <String ,Object > map = new HashMap <String ,Object >(); 117 for(int i=0; i<_names.length; i++){ 118 final String n = _names[i]; 119 final Object tok = Tokens.reserved(n); 120 map.put(scase.getKey(n), tok); 121 } 122 final jfun.parsec.Map<String ,Object > fmap = scase.getMap(map); 123 final Tokenizer tn = new Tokenizer(){ 124 public Object toToken(final CharSequence cs, 125 final int from, final int len){ 126 final String txt = cs.subSequence(from, from+len).toString(); 127 final Object 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 |