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
28 * @author Efficios - Javadoc
31 public class VariantDeclarationParser
extends AbstractScopedCommonTreeParser
{
36 * @author Matthew Khouzam
40 public static final class Param
implements ICommonTreeParserParameter
{
41 private final VariantDeclaration fVariant
;
42 private final DeclarationScope fDeclarationScope
;
43 private final CTFTrace fTrace
;
46 * Parameter Object Contructor
49 * variant declaration to populate
55 public Param(VariantDeclaration variant
, CTFTrace trace
, DeclarationScope scope
) {
58 fDeclarationScope
= scope
;
65 public final static VariantDeclarationParser INSTANCE
= new VariantDeclarationParser();
67 private VariantDeclarationParser() {
71 * Parses the variant declaration and gets a {@link VariantDeclaration}
75 * the variant declaration AST node
77 * the {@link Param} parameter object
78 * @return the {@link VariantDeclaration}
79 * @throws ParseException
80 * if the AST is malformed
83 public VariantDeclaration
parse(CommonTree declaration
, ICommonTreeParserParameter param
) throws ParseException
{
84 if (!(param
instanceof Param
)) {
85 throw new IllegalArgumentException("Param must be a " + Param
.class.getCanonicalName()); //$NON-NLS-1$
87 VariantDeclaration variant
= ((Param
) param
).fVariant
;
88 final DeclarationScope scope
= ((Param
) param
).fDeclarationScope
;
89 /* Get the type specifier list node */
90 CommonTree typeSpecifierListNode
= (CommonTree
) declaration
.getFirstChildWithType(CTFParser
.TYPE_SPECIFIER_LIST
);
91 if (typeSpecifierListNode
== null) {
92 throw new ParseException("Variant need type specifiers"); //$NON-NLS-1$
95 /* Get the type declarator list node */
96 CommonTree typeDeclaratorListNode
= (CommonTree
) declaration
.getFirstChildWithType(CTFParser
.TYPE_DECLARATOR_LIST
);
97 if (typeDeclaratorListNode
== null) {
98 throw new ParseException("Cannot have empty variant"); //$NON-NLS-1$
100 /* Get the type declarator list */
101 List
<CommonTree
> typeDeclaratorList
= typeDeclaratorListNode
.getChildren();
104 * For each type declarator, parse the declaration and add a field to
107 for (CommonTree typeDeclaratorNode
: typeDeclaratorList
) {
109 StringBuilder identifierSB
= new StringBuilder();
110 CTFTrace trace
= ((Param
) param
).fTrace
;
111 IDeclaration decl
= TypeDeclaratorParser
.INSTANCE
.parse(typeDeclaratorNode
,
112 new TypeDeclaratorParser
.Param(trace
, typeSpecifierListNode
, scope
, identifierSB
));
114 String name
= identifierSB
.toString();
116 if (variant
.hasField(name
)) {
117 throw new ParseException("variant: duplicate field " //$NON-NLS-1$
121 scope
.registerIdentifier(name
, decl
);
123 variant
.addField(name
, decl
);