Skip to contentMethod: DefinitionTypeState()
      1: package parser.states.definitionstates;
2: 
3: import model.RouterSoftware;
4: import model.definition.HasNotDefault;
5: import model.type.AnonVariableType;
6: import model.type.NamedVariableType;
7: import parser.DefinitionFileParser;
8: import parser.ParserException;
9: import parser.states.GlobalFunctions;
10: import parser.tempparserobjects.TempVariableDefinition;
11: import symbols.AbstractSymbol;
12: import symbols.IdentifierSymbol;
13: import symbols.RegExpSymbol;
14: import symbols.StringSymbol;
15: import basic.Buffer;
16: 
17: /**
18:  * 
19:  * @author Muri
20:  * 
21:  */
22: public class DefinitionTypeState extends AbstractDefinitionState {
23: 
24:         /**
25:          * Generates Anonymous VariableType if a RegEx has been delivered. Otherwise, it gets the
26:          * VariableTypes List and searches for it.
27:          * 
28:          * @param tVD
29:          *            TempVariableDefinition
30:          * @param dFP
31:          *            DefinitionFileParser
32:          * @param buffer
33:          *            Buffer<Symbol>
34:          * @return
35:          * @throws InterruptedException
36:          *             InterruptedException
37:          * @throws ParserException
38:          *             ParserException
39:          */
40:         @Override
41:         public void parse(final RouterSoftware rs, final TempVariableDefinition tVD,
42:                         final Buffer<AbstractSymbol> buffer, final DefinitionFileParser dFP)
43:                         throws InterruptedException, ParserException {
44:                 final AbstractSymbol nextSymbol = buffer.peek();
45:                 if (nextSymbol instanceof RegExpSymbol) {
46: 
47:                         tVD.setVariableType(new AnonVariableType(GlobalFunctions
48:                                         .generateRegEx(((RegExpSymbol) nextSymbol).getExpression())));
49:                         buffer.remove();
50:                         if (buffer.peek() instanceof StringSymbol) {
51:                                 dFP.setState(new DefinitionDefaultState());
52:                         } else {
53:                                 tVD.setDefaultValue(new HasNotDefault());
54:                                 tVD.setReady(true);
55:                                 dFP.setState(new DefinitionInitialState());
56:                         }
57: 
58:                 } else if (nextSymbol instanceof IdentifierSymbol) {
59: 
60:                         final NamedVariableType type =
61:                                         rs.findTypeByName(((IdentifierSymbol) nextSymbol).getText());
62:                         if (type == null) {
63:                                 throw new ParserException("Type '" + ((IdentifierSymbol) nextSymbol).getText()
64:                                                 + "' not found!");
65:                         }
66:                         tVD.setVariableType(type);
67: 
68:                         buffer.remove();
69:                         if (buffer.peek() instanceof StringSymbol) {
70:                                 dFP.setState(new DefinitionDefaultState());
71:                         } else {
72:                                 tVD.setDefaultValue(new HasNotDefault());
73:                                 tVD.setReady(true);
74:                                 dFP.setState(new DefinitionInitialState());
75:                         }
76: 
77:                 } else {
78:                         throw new ParserException("no valid symbol: "
79:                                         + buffer.peek().getClass().getCanonicalName());
80:                 }
81:         }
82: }