Skip to content

Method: parse(String, Token)

1: package de.fhdw.wtf.parser;
2:
3: import java.util.ArrayList;
4: import java.util.Collection;
5: import java.util.List;
6:
7: import de.fhdw.wtf.common.ast.Attribute;
8: import de.fhdw.wtf.common.ast.AttributeModifier;
9: import de.fhdw.wtf.common.ast.type.Type;
10: import de.fhdw.wtf.common.exception.parser.AbstractParserException;
11: import de.fhdw.wtf.common.stream.TokenStream;
12: import de.fhdw.wtf.common.token.Token;
13:
14: /**
15: * Parser to parse the given TokenStream to {@link Attribute}s.
16: */
17: final class AttributeParser {
18:         
19:         /**
20:          * Tokenstream.
21:          */
22:         private final TokenStream stream;
23:         
24:         /**
25:          * Collection for exceptions.
26:          */
27:         private final Collection<AbstractParserException> exceptions;
28:         
29:         /**
30:          * Constrcutor of {@link AttributeParser}.
31:          *
32:          * @param stream
33:          * tokenstream
34:          * @param exceptions
35:          * collection for exceptions
36:          */
37:         private AttributeParser(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
38:                 this.stream = stream;
39:                 this.exceptions = exceptions;
40:         }
41:         
42:         /**
43:          * Creates a {@link AttributeParser}-Object.
44:          *
45:          * @param stream
46:          * tokenstream
47:          * @param exceptions
48:          * collection for exceptions
49:          * @return the {@link AttributeParser}-Object.
50:          */
51:         static AttributeParser create(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
52:                 return new AttributeParser(stream, exceptions);
53:         }
54:         
55:         /**
56:          * Parses a series of Tokens and creates an Attribute. The name of the attribute will be <code>attributeName</code>.
57:          *
58:          * @param attributeName
59:          * attributeName
60:          * @param firstToken
61:          * firstToken
62:          * @return Attribute
63:          * @throws AbstractParserException
64:          * AbstractParserException
65:          */
66:         Attribute parse(final String attributeName, final Token firstToken) throws AbstractParserException {
67:                 final Type attrType = TypeParser.create(this.stream, this.exceptions).parse();
68:                 final List<AttributeModifier> modifiers = new ArrayList<>();
69:                 
70:•                if (!this.stream.peek().isSemicolonToken()) {
71:                         modifiers.addAll(AttributeModifierParser.create(this.stream, this.exceptions).parse());
72:                 }
73:                 
74:                 ParserUtils.requireSemicolonToken(this.stream);
75:                 this.stream.removeFirst();
76:                 final Token lastToken = this.stream.peek();
77:                 
78:                 return Attribute.create(attributeName, attrType, modifiers, firstToken, lastToken);
79:         }
80: }