ctf: make all parser implementations final classes
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / metadata / tsdl / stream / StreamParser.java
CommitLineData
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
10package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.stream;
11
12import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError;
13
14import java.util.List;
15
16import org.antlr.runtime.tree.CommonTree;
17import org.eclipse.jdt.annotation.NonNullByDefault;
18import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
19import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
20import org.eclipse.tracecompass.ctf.parser.CTFParser;
21import org.eclipse.tracecompass.internal.ctf.core.event.metadata.AbstractScopedCommonTreeParser;
22import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings;
23import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
24import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypeAliasParser;
25import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TypedefParser;
26import org.eclipse.tracecompass.internal.ctf.core.trace.CTFStream;
27
28/**
29 * A stream is a collection of packets with events in them. It will contain data
30 * types.
31 *
32 * @author Matthew Khouzam
33 *
34 */
4055c3a1 35public final class StreamParser extends AbstractScopedCommonTreeParser {
b1ea73b5
MK
36
37 /**
38 * Parameter Object with a trace and scope
39 *
40 * @author Matthew Khouzam
41 *
42 */
43 @NonNullByDefault
44 public static final class Param implements ICommonTreeParserParameter {
45
46 private final DeclarationScope fCurrentScope;
47 private final CTFTrace fTrace;
48
49 /**
50 * Constructor
51 *
52 * @param trace
53 * the trace
54 * @param currentScope
55 * the scope
56 */
57 public Param(CTFTrace trace, DeclarationScope currentScope) {
58 fTrace = trace;
59 fCurrentScope = currentScope;
60 }
61
62 }
63
64 /**
65 * The instance
66 */
67 public static final StreamParser INSTANCE = new StreamParser();
68
69 private StreamParser() {
70 }
71
72 /**
73 * Parses an stream declaration and returns a stream type
74 *
75 * @param streamNode
76 * the steam node
77 * @param param
78 * the parameter object
79 *
80 * @return The corresponding enum declaration.
81 * @throws ParseException
82 * badly defined stream
83 */
84 @Override
85 public CTFStream parse(CommonTree streamNode, ICommonTreeParserParameter param) throws ParseException {
86 if (!(param instanceof Param)) {
87 throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
88 }
89 Param parameter = (Param) param;
90 CTFTrace trace = ((Param) param).fTrace;
91 CTFStream stream = new CTFStream(trace);
92
93 List<CommonTree> children = streamNode.getChildren();
94 if (children == null) {
95 throw new ParseException("Empty stream block"); //$NON-NLS-1$
96 }
97
98 DeclarationScope scope = new DeclarationScope(parameter.fCurrentScope, MetadataStrings.STREAM);
99
100 for (CommonTree child : children) {
101 switch (child.getType()) {
102 case CTFParser.TYPEALIAS:
103 TypeAliasParser.INSTANCE.parse(child, new TypeAliasParser.Param(trace, scope));
104 break;
105 case CTFParser.TYPEDEF:
106 TypedefParser.INSTANCE.parse(child, new TypedefParser.Param(trace, scope));
107 break;
108 case CTFParser.CTF_EXPRESSION_TYPE:
109 case CTFParser.CTF_EXPRESSION_VAL:
110 StreamDeclarationParser.INSTANCE.parse(child, new StreamDeclarationParser.Param(trace, stream, scope));
111 break;
112 default:
113 throw childTypeError(child);
114 }
115 }
116
117 if (stream.isIdSet() &&
118 (!trace.packetHeaderIsSet() || !trace.getPacketHeader().hasField(MetadataStrings.STREAM_ID))) {
119 throw new ParseException("Stream has an ID, but there is no stream_id field in packet header."); //$NON-NLS-1$
120 }
121
122 trace.addStream(stream);
123
124 return stream;
125 }
126
127}
This page took 0.050595 seconds and 5 git commands to generate.