Skip to content

Package: ProductParser

ProductParser

nameinstructionbranchcomplexitylinemethod
ProductParser(TokenStream, Collection)
M: 0 C: 9
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
create(TokenStream, Collection)
M: 0 C: 6
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
M: 0 C: 1
100%
parse()
M: 0 C: 78
100%
M: 3 C: 11
79%
M: 3 C: 5
63%
M: 0 C: 19
100%
M: 0 C: 1
100%

Coverage

1: package de.fhdw.wtf.parser;
2:
3: import java.util.Collection;
4:
5: import de.fhdw.wtf.common.ast.type.ProductType;
6: import de.fhdw.wtf.common.exception.parser.AbstractParserException;
7: import de.fhdw.wtf.common.exception.parser.NoBracketCloseException;
8: import de.fhdw.wtf.common.stream.TokenStream;
9: import de.fhdw.wtf.common.token.Token;
10:
11: /**
12: * Parser to parse the given TokenStream to {@link ProductType}s.
13: */
14: final class ProductParser {
15:         
16:         /**
17:          * TokenStream.
18:          */
19:         private final TokenStream stream;
20:         
21:         /**
22:          * Collection of exceptions.
23:          */
24:         private final Collection<AbstractParserException> exceptions;
25:         
26:         /**
27:          * Constructor of {@link ProductParser}.
28:          *
29:          * @param stream
30:          * tokenStream
31:          * @param exceptions
32:          * collection of exceptions
33:          */
34:         private ProductParser(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
35:                 this.stream = stream;
36:                 this.exceptions = exceptions;
37:         }
38:         
39:         /**
40:          * FactoryMethod for {@link ProductParser}.
41:          *
42:          * @param stream
43:          * tokenStream
44:          * @param exceptions
45:          * collection of exceptions
46:          * @return The {@link ProductParser}-Object.
47:          */
48:         static ProductParser create(final TokenStream stream, final Collection<AbstractParserException> exceptions) {
49:                 return new ProductParser(stream, exceptions);
50:         }
51:         
52:         /**
53:          * Parses a series of tokens and creates a {@link ProductType} from it. If the series of tokens does not match the
54:          * grammar an Exception will be thrown.
55:          *
56:          * @return ProductType
57:          * @throws AbstractParserException
58:          * AbstractParserException
59:          */
60:         ProductType parse() throws AbstractParserException {
61:                 final Token firstToken = this.stream.next();
62:                 final ProductType prod = ProductType.create(firstToken);
63:                 boolean nextTokenType = true;
64:•                if (this.stream.peek().isBracketCloseToken()) {
65:                         nextTokenType = false;
66:                 }
67:•                while (this.stream.hasNext() && this.stream.peek().isIdentifierToken() && nextTokenType) {
68:                         final ParameterParser parameterParser = ParameterParser.create(this.stream, this.exceptions);
69:                         prod.addElement(parameterParser.parse());
70:•                        if (this.stream.peek().isCommaToken()) {
71:                                 this.stream.removeFirst();
72:                                 nextTokenType = true;
73:                         } else {
74:                                 nextTokenType = false;
75:                         }
76:                 }
77:•                if (this.stream.hasNext() && this.stream.peek().isBracketCloseToken()) {
78:                         this.stream.removeFirst();
79:                 } else {
80:                         throw NoBracketCloseException.create(this.stream.peek());
81:                 }
82:                 prod.setLastToken(this.stream.peek());
83:                 return prod;
84:         }
85: }