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
.struct
;
11 import static org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TsdlUtils
.childTypeError
;
13 import java
.util
.Collections
;
14 import java
.util
.List
;
16 import org
.antlr
.runtime
.tree
.CommonTree
;
17 import org
.eclipse
.jdt
.annotation
.NonNullByDefault
;
18 import org
.eclipse
.jdt
.annotation
.Nullable
;
19 import org
.eclipse
.tracecompass
.ctf
.core
.event
.metadata
.DeclarationScope
;
20 import org
.eclipse
.tracecompass
.ctf
.core
.event
.types
.StructDeclaration
;
21 import org
.eclipse
.tracecompass
.ctf
.core
.trace
.CTFTrace
;
22 import org
.eclipse
.tracecompass
.ctf
.parser
.CTFParser
;
23 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.AbstractScopedCommonTreeParser
;
24 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.MetadataStrings
;
25 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.ParseException
;
26 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TypeAliasParser
;
27 import org
.eclipse
.tracecompass
.internal
.ctf
.core
.event
.metadata
.tsdl
.TypedefParser
;
30 * A struct body can have any of the following elements, even though some of
31 * them do not make sense:
42 * <li>floating_point</li>
63 * @author Matthew Khouzam
66 public class StructBodyParser
extends AbstractScopedCommonTreeParser
{
69 * The parameter object
71 * @author Matthew Khouzam
75 public static final class Param
implements ICommonTreeParserParameter
{
76 private final DeclarationScope fDeclarationScope
;
77 private final @Nullable String fName
;
78 private final StructDeclaration fStructDeclaration
;
79 private final CTFTrace fTrace
;
84 * @param structDeclaration
85 * struct declaration to populate
93 public Param(StructDeclaration structDeclaration
, CTFTrace trace
, @Nullable String name
, DeclarationScope scope
) {
94 fStructDeclaration
= structDeclaration
;
96 fDeclarationScope
= scope
;
104 public final static StructBodyParser INSTANCE
= new StructBodyParser();
106 private StructBodyParser() {
110 * Parse the body of a struct, so anything between the '{' '}'
113 * the struct body AST node
115 * the struct body parameters
116 * @return {@link StructDeclaration} that is now populated
117 * @throws ParseException
118 * The AST is malformed
121 public StructDeclaration
parse(CommonTree structBody
, ICommonTreeParserParameter param
) throws ParseException
{
122 if (!(param
instanceof Param
)) {
123 throw new IllegalArgumentException("Param must be a " + Param
.class.getCanonicalName()); //$NON-NLS-1$
125 String structName
= ((Param
) param
).fName
;
126 final DeclarationScope scope
= new DeclarationScope(((Param
) param
).fDeclarationScope
, structName
== null ? MetadataStrings
.STRUCT
: structName
);
127 StructDeclaration structDeclaration
= ((Param
) param
).fStructDeclaration
;
128 List
<CommonTree
> structDeclarations
= structBody
.getChildren();
129 if (structDeclarations
== null) {
130 structDeclarations
= Collections
.emptyList();
134 * If structDeclaration is null, structBody has no children and the
135 * struct body is empty.
138 CTFTrace trace
= ((Param
) param
).fTrace
;
140 for (CommonTree declarationNode
: structDeclarations
) {
141 switch (declarationNode
.getType()) {
142 case CTFParser
.TYPEALIAS
:
143 TypeAliasParser
.INSTANCE
.parse(declarationNode
, new TypeAliasParser
.Param(trace
, scope
));
145 case CTFParser
.TYPEDEF
:
146 TypedefParser
.INSTANCE
.parse(declarationNode
, new TypedefParser
.Param(trace
, scope
));
147 StructDeclarationParser
.INSTANCE
.parse(declarationNode
, new StructDeclarationParser
.Param(structDeclaration
, trace
, scope
));
149 case CTFParser
.SV_DECLARATION
:
150 StructDeclarationParser
.INSTANCE
.parse(declarationNode
, new StructDeclarationParser
.Param(structDeclaration
, trace
, scope
));
153 throw childTypeError(declarationNode
);
156 return structDeclaration
;