KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jfun > parsec > Operators


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.ArrayList JavaDoc;
16 import java.util.Arrays JavaDoc;
17 import java.util.Comparator JavaDoc;
18 import java.util.HashMap JavaDoc;
19
20 import jfun.parsec.tokens.Tokens;
21
22 /**
23  * @author Ben Yu
24  *
25  * Dec 19, 2004
26  */

27 final class Operators implements java.io.Serializable JavaDoc{
28   static WordsData instance(final String JavaDoc[] names){
29     final HashMap JavaDoc<String JavaDoc,Object JavaDoc> operators = new HashMap JavaDoc<String JavaDoc,Object JavaDoc>();
30     final String JavaDoc[] ops = sort(names);
31     final Parser[] lxs = new Parser[ops.length];
32     for(int i=0; i<ops.length; i++){
33       final String JavaDoc s = ops[i];
34       final Parser<?> scanner = s.length()==1?
35           Scanners.isChar(s.charAt(0)): Scanners.isString(s);
36       final Object JavaDoc tok = Tokens.reserved(s);
37       operators.put(s, tok);
38       final Parser<Tok> lx = Lexers.lexer(scanner, ConstTokenizer.instance(tok));
39       lxs[i] = lx;
40     }
41     return new WordsData(Maps.jmap(operators), lxs);
42   }
43   private static final Comparator JavaDoc<Object JavaDoc> com = new Comparator JavaDoc<Object JavaDoc>(){
44     public int compare(final Object JavaDoc a, final Object JavaDoc b){
45       if(a==b) return 0;
46       if(a==null) return -1;
47       if(b==null) return 1;
48       return a.toString().length() - b.toString().length();
49     }
50   };
51   private static final class Suite{
52     private final ArrayList JavaDoc<String JavaDoc> list = new ArrayList JavaDoc<String JavaDoc>();
53     //contained are behined containers.
54
Suite(String JavaDoc s){
55       if(s.length()>0)
56         list.add(s);
57     }
58     boolean add(final String JavaDoc v){
59       if(v.length()==0) return true;
60       for(int i=list.size()-1; i>=0; i--){
61         final String JavaDoc s = (String JavaDoc)list.get(i);
62         if(s.startsWith(v)){
63           if(s.length()==v.length()) return true; // ignore duplicates
64
list.add(i+1, v);
65           return true;
66         }
67       }
68       return false;
69     }
70     int size(){return list.size();}
71     String JavaDoc get(int i){return (String JavaDoc)list.get(i);}
72   }
73   private static final class Suites{
74     private final ArrayList JavaDoc<Suite> list = new ArrayList JavaDoc<Suite>();
75     //bigger suite first
76
void add(final String JavaDoc v){
77       for(int i=0; i<list.size(); i++){
78         final Suite suite = (Suite)list.get(i);
79         if(suite.add(v)) return;
80       }
81       list.add(new Suite(v));
82     }
83     String JavaDoc[] toArray(){
84       final ArrayList JavaDoc<String JavaDoc> result = new ArrayList JavaDoc<String JavaDoc>();
85       for(int i=list.size()-1;i>=0;i--){
86         final Suite suite = (Suite)list.get(i);
87         for(int j=0; j<suite.size(); j++){
88           result.add(suite.get(j));
89         }
90       }
91       final String JavaDoc[] ret = new String JavaDoc[result.size()];
92       result.toArray(ret);
93       return ret;
94     }
95   }
96   public static String JavaDoc[] sort(final String JavaDoc[] names){
97     //short name first, unless it is fully contained in a longer name
98
final String JavaDoc[] _names = (String JavaDoc[])names.clone();
99     Arrays.sort(_names, com);
100     final Suites suites = new Suites();
101     for(int i=_names.length-1; i>=0; i--){
102       suites.add(_names[i]);
103     }
104     return suites.toArray();
105   }
106 }
107
Popular Tags