1 21 22 package org.armedbear.j; 23 24 public class Expression 25 { 26 protected final String name; 27 protected final int arity; 28 29 public Expression(String name) 30 { 31 this(name, -1); 32 } 33 34 public Expression(String name, int arity) 35 { 36 this.name = name; 37 this.arity = arity; 38 } 39 40 public final String 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 public static int getArity(String 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 if (inQuote) { 83 if (c == quoteChar) 84 inQuote = false; 85 continue; 86 } 87 if (c == '"' || c == '\'') { 89 inQuote = true; 90 quoteChar = c; 91 continue; 92 } 93 if (c == ',') { 94 if (parenCount == 0) ++arity; 96 continue; 97 } 98 if (c == '(') { 99 ++parenCount; 100 continue; 101 } 102 if (c == ')') { 103 --parenCount; 104 if (parenCount < 0) { 105 if (arity == 0) { 107 String 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 |