KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > Expression


1 /*
2  * Expression.java
3  *
4  * Copyright (C) 2002 Peter Graves
5  * $Id: Expression.java,v 1.1.1.1 2002/09/24 16:09:00 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20  */

21
22 package org.armedbear.j;
23
24 public class Expression
25 {
26     protected final String JavaDoc name;
27     protected final int arity;
28
29     public Expression(String JavaDoc name)
30     {
31         this(name, -1);
32     }
33
34     public Expression(String JavaDoc name, int arity)
35     {
36         this.name = name;
37         this.arity = arity;
38     }
39
40     public final String JavaDoc getName()
41     {
42         return name;
43     }
44
45     public final int getArity()
46     {
47         return arity;
48     }
49
50     public boolean matches(LocalTag tag)
51     {
52         if (!name.equals(tag.getMethodName()))
53             return false;
54         if (arity >= 0) {
55             int n = getArity(tag.getCanonicalSignature());
56             if (n < 0 || n == arity)
57                 return true;
58             else
59                 return false;
60         }
61         return true;
62     }
63
64     // BUG! This function is not aware of comments!
65
public static int getArity(String JavaDoc s)
66     {
67         if (s == null)
68             return -1;
69         int start = -1;
70         int parenCount = 0;
71         int arity = 0;
72         char quoteChar = '\0';
73         boolean inQuote = false;
74         for (int i = 0; i < s.length(); i++) {
75             char c = s.charAt(i);
76             if (start < 0) {
77                 if (c == '(')
78                     start = i+1;
79                 continue;
80             }
81             // Reaching here, we've seen the opening paren of the argument list.
82
if (inQuote) {
83                 if (c == quoteChar)
84                     inQuote = false;
85                 continue;
86             }
87             // Not in a quoted string.
88
if (c == '"' || c == '\'') {
89                 inQuote = true;
90                 quoteChar = c;
91                 continue;
92             }
93             if (c == ',') {
94                 if (parenCount == 0) // Top level.
95
++arity;
96                 continue;
97             }
98             if (c == '(') {
99                 ++parenCount;
100                 continue;
101             }
102             if (c == ')') {
103                 --parenCount;
104                 if (parenCount < 0) {
105                     // Closing paren, done.
106
if (arity == 0) {
107                         // We haven't seen a comma.
108
String JavaDoc enclosed = s.substring(start, i);
109                         boolean isBlank = true;
110                         for (int j = 0; j < enclosed.length(); j++) {
111                             if (!Character.isWhitespace(enclosed.charAt(j))) {
112                                 isBlank = false;
113                                 break;
114                             }
115                         }
116                         if (!isBlank)
117                             arity = 1;
118                     } else
119                         ++arity;
120                     return arity;
121                 }
122                 continue;
123             }
124         }
125         return -1;
126     }
127 }
128
Popular Tags