1 8 13 package jfun.parsec; 14 15 import jfun.parsec.pattern.Patterns; 16 17 18 19 26 public final class Words implements java.io.Serializable { 27 private final Map<String , Object > words; 28 private final Parser<Tok> lexer; 29 private Words(final Map<String , Object > map, final Parser<Tok> lexer){ 30 this.words = map; 31 this.lexer = lexer; 32 } 33 34 41 public Object getToken(final String name){ 42 final Object p = words.map(name); 43 if(p==null) throw new IllegalArgumentException ("token " + name + " unavailable"); 44 return p; 45 } 46 50 public Parser<Tok> getLexer(){ 51 return lexer; 52 } 53 private static boolean same(final String a, final String b, final boolean cs){ 54 if(cs){ 55 return a.equals(b); 56 } 57 else 58 return a.equalsIgnoreCase(b); 59 } 60 private static void checkDup(final String [] a, final String [] b, boolean cs){ 61 for(int i=0; i<a.length; i++){ 62 final String s1 = a[i]; 63 for(int j=0; j<b.length; j++){ 64 final String s2 = b[j]; 65 if(same(s1, s2, cs)) throw new IllegalArgumentException (s1 + " duplicated"); 66 } 67 } 68 } 69 75 static Words getOperators(final String [] ops){ 76 final WordsData data = Operators.instance(ops); 77 return new Words(data.getTokens(), Parsers.plus(data.getLexers())); 79 } 80 private static final Parser<?> default_word = Scanners.isPattern(Patterns.isWord(), "word"); 81 92 static Words getCaseInsensitive( 93 final String [] ops, final String [] keywords){ 94 return instance(default_word, ops, keywords, false, String2TokenWord.instance()); 95 } 96 107 static Words getCaseSensitive( 108 final String [] ops, final String [] keywords){ 109 return instance(default_word, ops, keywords, true, String2TokenWord.instance()); 110 } 111 121 static Words getCaseInsensitive(final Parser<?> wscanner, 122 final String [] ops, final String [] keywords){ 123 return instance(wscanner, ops, keywords, false, String2TokenWord.instance()); 124 } 125 135 static Words getCaseSensitive(final Parser<?> wscanner, 136 final String [] ops, final String [] keywords){ 137 return instance(wscanner, ops, keywords, true, String2TokenWord.instance()); 138 } 139 140 static Words getCaseSensitive(final Parser<?> wscanner, String [] ops, final String [] keywords, 141 FromString<?> toWord){ 142 return instance(wscanner, ops, keywords, true, toWord); 143 } 144 static Words getCaseInsensitive(final Parser<?> wscanner, String [] ops, String [] keywords, 145 FromString<?> toWord){ 146 return instance(wscanner, ops, keywords, false, toWord); 147 } 148 private static Words instance(final Parser<?> wscanner, 149 final String [] ops, final String [] keywords, final boolean cs, 150 FromString<?> toWord){ 151 checkDup(ops, keywords, true); 152 final WordsData data = combine(Operators.instance(ops), 153 Keywords.getInstance("word", wscanner, keywords, cs, toWord)); 154 return toWords(data); 155 } 156 private static Words toWords(WordsData data){ 157 return new Words(data.getTokens(), Parsers.plus(data.getLexers())); 158 } 159 private static WordsData combine(final WordsData w1, final WordsData w2){ 160 final Map<String , Object > map1 = w1.getTokens(); 161 final Map<String , Object > map2 = w2.getTokens(); 162 final Parser<Tok>[] l1 = w1.getLexers(); 163 final Parser<Tok>[] l2 = w2.getLexers(); 164 final Map<String , Object > map = new Map<String , Object >(){ 165 public Object map(final String k){ 166 final Object r = map1.map(k); 167 if(r!=null) return r; 168 return map2.map(k); 169 } 170 }; 171 final Parser<Tok>[] l = new Parser[l1.length+l2.length]; 172 178 System.arraycopy(l1, 0, l, 0, l1.length); 179 System.arraycopy(l2, 0, l, l1.length, l2.length); 180 return new WordsData(map, l); 181 } 182 } 183 | Popular Tags |