View Javadoc
1   package de.fhdw.wtf.parser;
2   
3   import java.util.ArrayList;
4   import java.util.Collection;
5   
6   import de.fhdw.wtf.common.ast.Operation;
7   import de.fhdw.wtf.common.ast.OperationModifier;
8   import de.fhdw.wtf.common.ast.type.ClassType;
9   import de.fhdw.wtf.common.ast.type.ProductType;
10  import de.fhdw.wtf.common.ast.type.Type;
11  import de.fhdw.wtf.common.exception.parser.AbstractParserException;
12  import de.fhdw.wtf.common.stream.TokenStream;
13  import de.fhdw.wtf.common.token.IdentifierToken;
14  import de.fhdw.wtf.common.token.Token;
15  
16  /**
17   * Parser to parse the given TokenStream to {@link Operation}s.
18   */
19  final class OperationParser {
20  	/**
21  	 * TokenStream.
22  	 */
23  	private final TokenStream stream;
24  	
25  	/**
26  	 * Collection of exceptions.
27  	 */
28  	private final Collection<AbstractParserException> exceptions;
29  	
30  	/**
31  	 * Constructor of {@link OperationParser}.
32  	 * 
33  	 * @param stream
34  	 *            tokenStream
35  	 * @param exceptions
36  	 *            collection of exceptions
37  	 */
38  	private OperationParser(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
39  		this.stream = stream;
40  		this.exceptions = exceptions;
41  	}
42  	
43  	/**
44  	 * FactoryMethod for {@link OperationParser}.
45  	 * 
46  	 * @param stream
47  	 *            tokenStream
48  	 * @param exceptions
49  	 *            collection of exceptions
50  	 * @return The {@link OperationParser}-Object.
51  	 */
52  	static OperationParser create(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
53  		return new OperationParser(stream, exceptions);
54  	}
55  	
56  	/**
57  	 * Parses a series of Tokens and creates an {@link Operation}. The name of the operation will be <code>name</code>.
58  	 * 
59  	 * @param name
60  	 *            name
61  	 * @param firstToken
62  	 *            firstToken
63  	 * @return Operation
64  	 * @throws AbstractParserException
65  	 *             AbstractParserException
66  	 */
67  	Operation parse(final String name, final ClassType containingType, final IdentifierToken firstToken)
68  			throws AbstractParserException {
69  		final Collection<OperationModifier> modifiers = new ArrayList<>();
70  		final Token firstTokenProduct = this.stream.peek();
71  		final ProductType params = ProductType.create(firstTokenProduct);
72  		ParserUtils.requireAndRemoveBracketOpenToken(this.stream);
73  		while (this.stream.hasNext() && this.stream.peek().isIdentifierToken()) {
74  			params.addElement(ParameterParser.create(this.stream, this.exceptions).parse());
75  			if (this.stream.peek().isCommaToken()) {
76  				this.stream.removeFirst();
77  			}
78  		}
79  		ParserUtils.requireAndRemoveBracketCloseToken(this.stream);
80  		params.setLastToken(this.stream.peek());
81  		ParserUtils.requireAndRemoveArrowToken(this.stream);
82  		final Type type = TypeParser.create(this.stream, this.exceptions).parse();
83  		// TODO anpassen
84  		final Operation result =
85  				Operation.create(name, modifiers, params, containingType, type, firstToken, type.getLastToken());
86  		ParserUtils.requireAndRemoveDoubleSquareBracketCloseToken(this.stream);
87  		modifiers.addAll(OperationModifierParser.create(this.stream, this.exceptions).parse(result));
88  		return result;
89  	}
90  }