ctf: fix Modifier order to always be static final
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / metadata / tsdl / variant / VariantBodyParser.java
1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
3 *
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
8 *******************************************************************************/
9 package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.variant;
10
11 import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError;
12
13 import java.util.List;
14 import java.util.Map;
15 import java.util.Map.Entry;
16
17 import org.antlr.runtime.tree.CommonTree;
18 import org.eclipse.jdt.annotation.NonNullByDefault;
19 import org.eclipse.jdt.annotation.Nullable;
20 import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
21 import org.eclipse.tracecompass.ctf.core.event.types.IDeclaration;
22 import org.eclipse.tracecompass.ctf.core.event.types.VariantDeclaration;
23 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
24 import org.eclipse.tracecompass.ctf.parser.CTFParser;
25 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
26 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings;
27 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
28 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeAliasParser;
29 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypedefParser;
30
31 /**
32 * Variant body parser. This handles all the inside of a variant, so it handles
33 * everything between the '{' and '}' of a TSDL variant declaration.
34 *
35 * @author Matthew Khouzam
36 *
37 */
38 public final class VariantBodyParser extends AbstractScopedCommonTreeParser {
39
40 /**
41 * Parameter object
42 *
43 * @author Matthew Khouzam
44 *
45 */
46 @NonNullByDefault
47 public static final class Param implements ICommonTreeParserParameter {
48 private final DeclarationScope fDeclarationScope;
49 private final @Nullable String fName;
50 private final VariantDeclaration fVariantDeclaration;
51 private final CTFTrace fTrace;
52
53 /**
54 * Constructor
55 *
56 * @param variantDeclaration
57 * the declaration to populate
58 * @param trace
59 * the trace
60 * @param name
61 * the variant name
62 * @param scope
63 * the current scope
64 */
65 public Param(VariantDeclaration variantDeclaration, CTFTrace trace, @Nullable String name, DeclarationScope scope) {
66 fVariantDeclaration = variantDeclaration;
67 fTrace = trace;
68 fDeclarationScope = scope;
69 fName = name;
70 }
71 }
72
73 /**
74 * The instance
75 */
76 public static final VariantBodyParser INSTANCE = new VariantBodyParser();
77
78 private VariantBodyParser() {
79 }
80
81 /**
82 * Parse the variant body, fills the variant with the results.
83 *
84 * @param variantBody
85 * the variant body AST node
86 * @param param
87 * the {@link Param} parameter object
88 * @return a populated {@link VariantDeclaration}
89 * @throws ParseException
90 * if the AST is malformed
91 */
92 @Override
93 public VariantDeclaration parse(CommonTree variantBody, ICommonTreeParserParameter param) throws ParseException {
94 if (!(param instanceof Param)) {
95 throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
96 }
97
98 String variantName = ((Param) param).fName;
99 VariantDeclaration variantDeclaration = ((Param) param).fVariantDeclaration;
100 List<CommonTree> variantDeclarations = variantBody.getChildren();
101
102 final DeclarationScope scope = new DeclarationScope(((Param) param).fDeclarationScope, variantName == null ? MetadataStrings.VARIANT : variantName);
103 CTFTrace trace = ((Param) param).fTrace;
104 for (CommonTree declarationNode : variantDeclarations) {
105 switch (declarationNode.getType()) {
106 case CTFParser.TYPEALIAS:
107 TypeAliasParser.INSTANCE.parse(declarationNode, new TypeAliasParser.Param(trace, scope));
108 break;
109 case CTFParser.TYPEDEF:
110 Map<String, IDeclaration> decs = TypedefParser.INSTANCE.parse(declarationNode, new TypedefParser.Param(trace, scope));
111 for (Entry<String, IDeclaration> declarationEntry : decs.entrySet()) {
112 variantDeclaration.addField(declarationEntry.getKey(), declarationEntry.getValue());
113 }
114 break;
115 case CTFParser.SV_DECLARATION:
116 VariantDeclarationParser.INSTANCE.parse(declarationNode, new VariantDeclarationParser.Param(variantDeclaration, trace, scope));
117 break;
118 default:
119 throw childTypeError(declarationNode);
120 }
121 }
122
123 return variantDeclaration;
124 }
125
126 }
This page took 0.04169 seconds and 5 git commands to generate.