aboutsummaryrefslogtreecommitdiff
path: root/tool/src/main/java/org/antlr/tool/Attribute.java
blob: 9834ec8ccbc7b8f280ee42c363297ef3e6e3fc04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/*
 * [The "BSD license"]
 *  Copyright (c) 2010 Terence Parr
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions
 *  are met:
 *  1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *  2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *  3. The name of the author may not be used to endorse or promote products
 *      derived from this software without specific prior written permission.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 *  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 *  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 *  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.antlr.tool;

/** Track the names of attributes define in arg lists, return values,
 *  scope blocks etc...
 */
public class Attribute {
	/** The entire declaration such as "String foo;" */
	public String decl;

	/** The type; might be empty such as for Python which has no static typing */
	public String type;

	/** The name of the attribute "foo" */
	public String name;

	/** The optional attribute intialization expression */
	public String initValue;

	public Attribute(String decl) {
		extractAttribute(decl);
	}

	public Attribute(String name, String decl) {
		this.name = name;
		this.decl = decl;
	}

	/** For decls like "String foo" or "char *foo32[3]" compute the ID
	 *  and type declarations.  Also handle "int x=3" and 'T t = new T("foo")'
	 *  but if the separator is ',' you cannot use ',' in the initvalue.
	 *  AttributeScope.addAttributes takes care of the separation so we are
	 *  free here to use from '=' to end of string as the expression.
	 *
	 *  Set name, type, initvalue, and full decl instance vars.
	 */
	protected void extractAttribute(String decl) {
		if ( decl==null ) {
			return;
		}
		boolean inID = false;
		int start = -1;
		int rightEdgeOfDeclarator = decl.length()-1;
		int equalsIndex = decl.indexOf('=');
		if ( equalsIndex>0 ) {
			// everything after the '=' is the init value
			this.initValue = decl.substring(equalsIndex+1,decl.length());
			rightEdgeOfDeclarator = equalsIndex-1;
		}
		// walk backwards looking for start of an ID
		for (int i=rightEdgeOfDeclarator; i>=0; i--) {
			// if we haven't found the end yet, keep going
			if ( !inID && Character.isLetterOrDigit(decl.charAt(i)) ) {
			    inID = true;
			}
			else if ( inID &&
				      !(Character.isLetterOrDigit(decl.charAt(i))||
				       decl.charAt(i)=='_') ) {
				start = i+1;
				break;
			}
		}
		if ( start<0 && inID ) {
			start = 0;
		}
		if ( start<0 ) {
			ErrorManager.error(ErrorManager.MSG_CANNOT_FIND_ATTRIBUTE_NAME_IN_DECL,decl);
		}
		// walk forwards looking for end of an ID
		int stop=-1;
		for (int i=start; i<=rightEdgeOfDeclarator; i++) {
			// if we haven't found the end yet, keep going
			if ( !(Character.isLetterOrDigit(decl.charAt(i))||
				decl.charAt(i)=='_') )
			{
				stop = i;
				break;
			}
			if ( i==rightEdgeOfDeclarator ) {
				stop = i+1;
			}
		}

		// the name is the last ID
		this.name = decl.substring(start,stop);

		// the type is the decl minus the ID (could be empty)
		this.type = decl.substring(0,start);
		if ( stop<=rightEdgeOfDeclarator ) {
			this.type += decl.substring(stop,rightEdgeOfDeclarator+1);
		}
		this.type = type.trim();
		if ( this.type.length()==0 ) {
			this.type = null;
		}

		this.decl = decl;
	}

	public String toString() {
		if ( initValue!=null ) {
			return type+" "+name+"="+initValue;
		}
		return type+" "+name;
	}
}