01: /***** BEGIN LICENSE BLOCK *****
02: * Version: CPL 1.0/GPL 2.0/LGPL 2.1
03: *
04: * The contents of this file are subject to the Common Public
05: * License Version 1.0 (the "License"); you may not use this file
06: * except in compliance with the License. You may obtain a copy of
07: * the License at http://www.eclipse.org/legal/cpl-v10.html
08: *
09: * Software distributed under the License is distributed on an "AS
10: * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11: * implied. See the License for the specific language governing
12: * rights and limitations under the License.
13: *
14: * Copyright (C) 2001-2002 Jan Arne Petersen <jpetersen@uni-bonn.de>
15: *
16: * Alternatively, the contents of this file may be used under the terms of
17: * either of the GNU General Public License Version 2 or later (the "GPL"),
18: * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
19: * in which case the provisions of the GPL or the LGPL are applicable instead
20: * of those above. If you wish to allow use of your version of this file only
21: * under the terms of either the GPL or the LGPL, and not to allow others to
22: * use your version of this file under the terms of the CPL, indicate your
23: * decision by deleting the provisions above and replace them with the notice
24: * and other provisions required by the GPL or the LGPL. If you do not delete
25: * the provisions above, a recipient may use your version of this file under
26: * the terms of any one of the CPL, the GPL or the LGPL.
27: ***** END LICENSE BLOCK *****/package org.jruby.util;
28:
29: public final class IdUtil {
30: public static final int CONSTANT = 0;
31: public static final int INSTANCE_VAR = 1;
32: public static final int CLASS_VAR = 2;
33: public static final int GLOBAL_VAR = 4;
34: public static final int LOCAL_VAR = 8;
35:
36: /**
37: * Get type of variable based on Ruby naming conventions. This is useful when you know
38: * you are going to want to know what type it is. It should in theory be cheaper than
39: * calling all the isFoo methods seperately. It also should be faster than isLocal.
40: *
41: * @param id the name to determine its type from
42: * @return value representing the type.
43: */
44: public static int getVarType(String id) {
45: char c = id.charAt(0);
46:
47: switch (c) {
48: case '@':
49: return id.charAt(1) == '@' ? CLASS_VAR : INSTANCE_VAR;
50: case '$':
51: return GLOBAL_VAR;
52: }
53:
54: return Character.isUpperCase(c) ? CONSTANT : LOCAL_VAR;
55: }
56:
57: /**
58: * rb_is_const_id and is_const_id
59: */
60: public static boolean isConstant(String id) {
61: return Character.isUpperCase(id.charAt(0));
62: }
63:
64: /**
65: * rb_is_class_id and is_class_id
66: */
67: public static boolean isClassVariable(String id) {
68: return id.length() > 1 && id.charAt(0) == '@'
69: && id.charAt(1) == '@';
70: }
71:
72: /**
73: * rb_is_instance_id and is_instance_id
74: */
75: public static boolean isInstanceVariable(String id) {
76: return id.length() > 0 && id.charAt(0) == '@'
77: && (id.length() < 2 || id.charAt(1) != '@');
78: }
79:
80: /**
81: * rb_is_global_id and is_global_id
82: */
83: public static boolean isGlobal(String id) {
84: return id.length() > 0 && id.charAt(0) == '$';
85: }
86:
87: /**
88: * rb_is_local_id and is_local_id
89: */
90: public static boolean isLocal(String id) {
91: return !isGlobal(id) && !isClassVariable(id)
92: && !isInstanceVariable(id) && !isConstant(id);
93: }
94:
95: public static boolean isAttrSet(String id) {
96: return id.endsWith("=");
97: }
98: }
|