Skip to main content
summaryrefslogtreecommitdiffstats
blob: db32e50a903d4f7806fced43596b071a1aeba261 (plain) (blame)
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
/**
 * <copyright>
 *
 * Copyright (c) 2005, 2006 Springsite BV (The Netherlands) and others
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *   Martin Taal
 * </copyright>
 *
 * $Id: AnnotationParser.java,v 1.7 2006/09/07 22:27:42 mtaal Exp $
 */

package org.eclipse.emf.teneo.annotations.parser;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.ENamedElement;

/**
 * Parses an annotation and creates a tree of parserNodes.
 * 
 * See AnnotationTokenizer for a short description on the type of parsed
 * tokens.
 * 
 * @author <a href="mailto:mtaal at elver.org">Martin Taal</a>
 */
public class AnnotationParser {

	/** The StringTokenizer being used */
	private AnnotationTokenizer annotationTokenizer;

	/** The list of root nodes */
	private List parserNodes = new ArrayList();

	/** Parses the content and returns a list of parsernodes */
	public List parse(ENamedElement eNamedElement, String content) {
		annotationTokenizer = new AnnotationTokenizer(eNamedElement, content);
		parserNodes.clear();
		int token;
		while (annotationTokenizer.getCurrentToken() != AnnotationTokenizer.T_EOF && 
			(token = annotationTokenizer.nextToken()) != AnnotationTokenizer.T_EOF) {
			if (token != AnnotationTokenizer.T_TYPENAME) {
				throw new AnnotationParserException("Only typenames are allowed at the root of the " +
						"annotation, see _ for the error " + annotationTokenizer.getErrorText());
			}
			parseTypeName(null);
		}
		return parserNodes;
	}

	/** Adds a child to the parent */
	private void addToParent(NamedParserNode parent, NamedParserNode child) {
		if (parent == null) return;
		if (parent instanceof ComplexNode) {
			((ComplexNode)parent).getChildren().add(child);
		} else if (parent instanceof ArrayValueNode) {
			((ArrayValueNode)parent).getChildren().add(child);
		} else if (parent instanceof ReferenceValueNode) {
			((ReferenceValueNode)parent).setValue(child);
		}
	}
	
	/** Parse a type name (a complex type) */
	private void parseTypeName(NamedParserNode pn) {
		final ComplexNode cn = new ComplexNode();
		cn.setName(annotationTokenizer.getLexeme());
		addToParent(pn, cn);
		if (pn == null) {
			parserNodes.add(cn);
		}
		
		// now parse the next token
		final int token = annotationTokenizer.nextToken();
		switch (token) {
			case AnnotationTokenizer.T_EOF:
				return;
			case AnnotationTokenizer.T_CONTENTSTART:
				parseContent(cn);
				break;
			case AnnotationTokenizer.T_TYPENAME:
				parseTypeName(null); // the next one
				break;
			case AnnotationTokenizer.T_COMMA: // in case of array
				if (!(pn instanceof ArrayValueNode)) {
					throw new AnnotationParserException("Encountered comma but not within an array definition, see _ for error location " +
							annotationTokenizer.getErrorText());
				}
				return;
			default:
				throw new AnnotationParserException("Unknown token, see _ for error position: " +
						annotationTokenizer.getErrorText());
		}
	}
	
	/** Parse the content of a typeName */
	private void parseContent(NamedParserNode pn) {
		// content can either be an array or a set of values
		boolean finished = false;
		while (!finished) {
			final int token = annotationTokenizer.nextToken();
			switch (token) {
				case AnnotationTokenizer.T_EOF:
					throw new AnnotationParserException("Unexcepted end to annotation string, " + 
							annotationTokenizer.getErrorText());
				case AnnotationTokenizer.T_CONTENTEND:
					return;
				case AnnotationTokenizer.T_IDENTIFIER:
					final String identifier = annotationTokenizer.getLexeme();
					// next token must be an is
					int nextToken = annotationTokenizer.nextToken();
					if (nextToken == AnnotationTokenizer.T_CONTENTEND) { 
						final PrimitiveValueNode vn = new PrimitiveValueNode();
						vn.setName("value");
						vn.setValue(identifier);
						addToParent(pn, vn);
						return;
					}
					if (nextToken != AnnotationTokenizer.T_IS) {
						throw new AnnotationParserException("No = character after identifier, see _ for error position " +
								annotationTokenizer.getErrorText());
					}
					nextToken = annotationTokenizer.nextToken();
					if (nextToken == AnnotationTokenizer.T_VALUE) { 
						final String value = annotationTokenizer.getLexeme();
						final PrimitiveValueNode vn = new PrimitiveValueNode();
						vn.setName(identifier);
						vn.setValue(value);
						addToParent(pn, vn);
					}if (nextToken == AnnotationTokenizer.T_VALUE) { 
						final String value = annotationTokenizer.getLexeme();
						final PrimitiveValueNode vn = new PrimitiveValueNode();
						vn.setName(identifier);
						vn.setValue(value);
						addToParent(pn, vn);
					} else if (nextToken == AnnotationTokenizer.T_IDENTIFIER) { 
						final String value = annotationTokenizer.getLexeme();
						final PrimitiveValueNode vn = new PrimitiveValueNode();
						vn.setName(identifier);
						vn.setValue(value);
						addToParent(pn, vn);
					} else if (nextToken == AnnotationTokenizer.T_TYPENAME) {
						final ReferenceValueNode rvn = new ReferenceValueNode();
						rvn.setName(identifier);
						parseTypeName(rvn);
						addToParent(pn, rvn);
					} else if (nextToken == AnnotationTokenizer.T_ARRAYSTART) {
						final ArrayValueNode avn = new ArrayValueNode();
						avn.setName(identifier);
						parseArray(avn);
						addToParent(pn, avn);
					} else if (annotationTokenizer.nextToken() != AnnotationTokenizer.T_VALUE) {
						throw new AnnotationParserException("No value token after =, see _ for error position " +
								annotationTokenizer.getErrorText());
					}
					break;
				case AnnotationTokenizer.T_ARRAYSTART:
					// special case in which the main type is just a list of other types
					// for example SecondaryTables which is just a list of SecondaryTable
					parseArray(pn);
					((ComplexNode)pn).setList(true);
					break;
			}
		}
	}
	
	/** Parse an array */
	private void parseArray(NamedParserNode pn) {
		// content can either be an array or a set of values
		final ArrayValueNode an = new ArrayValueNode();
		addToParent(pn, an);
		boolean finished = false;
		while (!finished) {
			final int token = annotationTokenizer.nextToken();
			switch (token) {
				case AnnotationTokenizer.T_EOF:
					throw new AnnotationParserException("Unexcepted end to annotation string, " + 
							annotationTokenizer.getErrorText());
				case AnnotationTokenizer.T_TYPENAME:
					parseTypeName(an);
					break;
				case AnnotationTokenizer.T_IDENTIFIER:
					an.getChildren().add(annotationTokenizer.getLexeme());
					break;
				case AnnotationTokenizer.T_COMMA:
					break;
				case AnnotationTokenizer.T_ARRAYEND:
					return;
			}
		}
	}
}

Back to the top