Commit | Line | Data |
---|---|---|
b1ea73b5 MK |
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 | |
b1ea73b5 | 28 | */ |
4055c3a1 | 29 | public final class VariantDeclarationParser extends AbstractScopedCommonTreeParser { |
b1ea73b5 MK |
30 | |
31 | /** | |
32 | * Parameter Object | |
33 | * | |
34 | * @author Matthew Khouzam | |
35 | * | |
36 | */ | |
37 | @NonNullByDefault | |
38 | public static final class Param implements ICommonTreeParserParameter { | |
39 | private final VariantDeclaration fVariant; | |
40 | private final DeclarationScope fDeclarationScope; | |
41 | private final CTFTrace fTrace; | |
42 | ||
43 | /** | |
44 | * Parameter Object Contructor | |
45 | * | |
46 | * @param variant | |
47 | * variant declaration to populate | |
48 | * @param trace | |
49 | * trace | |
50 | * @param scope | |
51 | * current scope | |
52 | */ | |
53 | public Param(VariantDeclaration variant, CTFTrace trace, DeclarationScope scope) { | |
54 | fVariant = variant; | |
55 | fTrace = trace; | |
56 | fDeclarationScope = scope; | |
57 | } | |
58 | } | |
59 | ||
60 | /** | |
61 | * Instance | |
62 | */ | |
63 | public final static VariantDeclarationParser INSTANCE = new VariantDeclarationParser(); | |
64 | ||
65 | private VariantDeclarationParser() { | |
66 | } | |
67 | ||
68 | /** | |
69 | * Parses the variant declaration and gets a {@link VariantDeclaration} | |
70 | * back. | |
71 | * | |
72 | * @param declaration | |
73 | * the variant declaration AST node | |
74 | * @param param | |
75 | * the {@link Param} parameter object | |
76 | * @return the {@link VariantDeclaration} | |
77 | * @throws ParseException | |
78 | * if the AST is malformed | |
79 | */ | |
80 | @Override | |
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$ | |
84 | } | |
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$ | |
91 | } | |
92 | ||
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$ | |
97 | } | |
98 | /* Get the type declarator list */ | |
99 | List<CommonTree> typeDeclaratorList = typeDeclaratorListNode.getChildren(); | |
100 | ||
101 | /* | |
102 | * For each type declarator, parse the declaration and add a field to | |
103 | * the variant | |
104 | */ | |
105 | for (CommonTree typeDeclaratorNode : typeDeclaratorList) { | |
106 | ||
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)); | |
111 | ||
112 | String name = identifierSB.toString(); | |
113 | ||
114 | if (variant.hasField(name)) { | |
115 | throw new ParseException("variant: duplicate field " //$NON-NLS-1$ | |
116 | + name); | |
117 | } | |
118 | ||
119 | scope.registerIdentifier(name, decl); | |
120 | ||
121 | variant.addField(name, decl); | |
122 | } | |
123 | return variant; | |
124 | } | |
125 | ||
126 | } |