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 java
.util
.List
;
13 import org
.antlr
.runtime
.tree
.CommonTree
;
14 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
15 import org
.eclipse
.tracecompass
.ctf
.core
.event
.metadata
.DeclarationScope
;
16 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.IDeclaration
;
17 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.VariantDeclaration
;
18 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
19 import org
.eclipse
.tracecompass
.ctf
.parser
.CTFParser
;
20 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.AbstractScopedCommonTreeParser
;
21 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.ParseException
;
22 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TypeDeclaratorParser
;
25 * This parses the (sub)declarations located IN a variant declaration.
27 * @author Matthew Khouzam
29 public final class VariantDeclarationParser
extends AbstractScopedCommonTreeParser
{
34 * @author Matthew Khouzam
38 public static final class Param
implements ICommonTreeParserParameter
{
39 private final VariantDeclaration fVariant
;
40 private final DeclarationScope fDeclarationScope
;
41 private final CTFTrace fTrace
;
44 * Parameter Object Contructor
47 * variant declaration to populate
53 public Param(VariantDeclaration variant
, CTFTrace trace
, DeclarationScope scope
) {
56 fDeclarationScope
= scope
;
63 public final static VariantDeclarationParser INSTANCE
= new VariantDeclarationParser();
65 private VariantDeclarationParser() {
69 * Parses the variant declaration and gets a {@link VariantDeclaration}
73 * the variant declaration AST node
75 * the {@link Param} parameter object
76 * @return the {@link VariantDeclaration}
77 * @throws ParseException
78 * if the AST is malformed
81 public VariantDeclaration
parse(CommonTree declaration
, ICommonTreeParserParameter param
) throws ParseException
{
82 if (!(param
instanceof Param
)) {
83 throw new IllegalArgumentException("Param must be a " + Param
.class.getCanonicalName()); //$NON-NLS-1$
85 VariantDeclaration variant
= ((Param
) param
).fVariant
;
86 final DeclarationScope scope
= ((Param
) param
).fDeclarationScope
;
87 /* Get the type specifier list node */
88 CommonTree typeSpecifierListNode
= (CommonTree
) declaration
.getFirstChildWithType(CTFParser
.TYPE_SPECIFIER_LIST
);
89 if (typeSpecifierListNode
== null) {
90 throw new ParseException("Variant need type specifiers"); //$NON-NLS-1$
93 /* Get the type declarator list node */
94 CommonTree typeDeclaratorListNode
= (CommonTree
) declaration
.getFirstChildWithType(CTFParser
.TYPE_DECLARATOR_LIST
);
95 if (typeDeclaratorListNode
== null) {
96 throw new ParseException("Cannot have empty variant"); //$NON-NLS-1$
98 /* Get the type declarator list */
99 List
<CommonTree
> typeDeclaratorList
= typeDeclaratorListNode
.getChildren();
102 * For each type declarator, parse the declaration and add a field to
105 for (CommonTree typeDeclaratorNode
: typeDeclaratorList
) {
107 StringBuilder identifierSB
= new StringBuilder();
108 CTFTrace trace
= ((Param
) param
).fTrace
;
109 IDeclaration decl
= TypeDeclaratorParser
.INSTANCE
.parse(typeDeclaratorNode
,
110 new TypeDeclaratorParser
.Param(trace
, typeSpecifierListNode
, scope
, identifierSB
));
112 String name
= identifierSB
.toString();
114 if (variant
.hasField(name
)) {
115 throw new ParseException("variant: duplicate field " //$NON-NLS-1$
119 scope
.registerIdentifier(name
, decl
);
121 variant
.addField(name
, decl
);