985699db26d5843734f5622396ce5c852af9c87a
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / metadata / tsdl / variant / VariantDeclarationParser.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 java.util.List;
12
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;
23
24 /**
25 * This parses the (sub)declarations located IN a variant declaration.
26 *
27 * @author Matthew Khouzam
28 * @author Efficios - Javadoc
29 *
30 */
31 public class VariantDeclarationParser extends AbstractScopedCommonTreeParser {
32
33 /**
34 * Parameter Object
35 *
36 * @author Matthew Khouzam
37 *
38 */
39 @NonNullByDefault
40 public static final class Param implements ICommonTreeParserParameter {
41 private final VariantDeclaration fVariant;
42 private final DeclarationScope fDeclarationScope;
43 private final CTFTrace fTrace;
44
45 /**
46 * Parameter Object Contructor
47 *
48 * @param variant
49 * variant declaration to populate
50 * @param trace
51 * trace
52 * @param scope
53 * current scope
54 */
55 public Param(VariantDeclaration variant, CTFTrace trace, DeclarationScope scope) {
56 fVariant = variant;
57 fTrace = trace;
58 fDeclarationScope = scope;
59 }
60 }
61
62 /**
63 * Instance
64 */
65 public final static VariantDeclarationParser INSTANCE = new VariantDeclarationParser();
66
67 private VariantDeclarationParser() {
68 }
69
70 /**
71 * Parses the variant declaration and gets a {@link VariantDeclaration}
72 * back.
73 *
74 * @param declaration
75 * the variant declaration AST node
76 * @param param
77 * the {@link Param} parameter object
78 * @return the {@link VariantDeclaration}
79 * @throws ParseException
80 * if the AST is malformed
81 */
82 @Override
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$
86 }
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$
93 }
94
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$
99 }
100 /* Get the type declarator list */
101 List<CommonTree> typeDeclaratorList = typeDeclaratorListNode.getChildren();
102
103 /*
104 * For each type declarator, parse the declaration and add a field to
105 * the variant
106 */
107 for (CommonTree typeDeclaratorNode : typeDeclaratorList) {
108
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));
113
114 String name = identifierSB.toString();
115
116 if (variant.hasField(name)) {
117 throw new ParseException("variant: duplicate field " //$NON-NLS-1$
118 + name);
119 }
120
121 scope.registerIdentifier(name, decl);
122
123 variant.addField(name, decl);
124 }
125 return variant;
126 }
127
128 }
This page took 0.041688 seconds and 5 git commands to generate.