1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
;
11 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.childTypeError
;
13 import java
.util
.List
;
15 import java
.util
.Map
.Entry
;
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
;
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.
35 * @author Matthew Khouzam
38 public final class VariantBodyParser
extends AbstractScopedCommonTreeParser
{
43 * @author Matthew Khouzam
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
;
56 * @param variantDeclaration
57 * the declaration to populate
65 public Param(VariantDeclaration variantDeclaration
, CTFTrace trace
, @Nullable String name
, DeclarationScope scope
) {
66 fVariantDeclaration
= variantDeclaration
;
68 fDeclarationScope
= scope
;
76 public final static VariantBodyParser INSTANCE
= new VariantBodyParser();
78 private VariantBodyParser() {
82 * Parse the variant body, fills the variant with the results.
85 * the variant body AST node
87 * the {@link Param} parameter object
88 * @return a populated {@link VariantDeclaration}
89 * @throws ParseException
90 * if the AST is malformed
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$
98 String variantName
= ((Param
) param
).fName
;
99 VariantDeclaration variantDeclaration
= ((Param
) param
).fVariantDeclaration
;
100 List
<CommonTree
> variantDeclarations
= variantBody
.getChildren();
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
));
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());
115 case CTFParser
.SV_DECLARATION
:
116 VariantDeclarationParser
.INSTANCE
.parse(declarationNode
, new VariantDeclarationParser
.Param(variantDeclaration
, trace
, scope
));
119 throw childTypeError(declarationNode
);
123 return variantDeclaration
;