999f3c47a60572622b331aaf7cae648b4f21b236
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / metadata / tsdl / event / EventParser.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
10 package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.event;
11
12 import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError;
13
14 import java.util.List;
15
16 import org.antlr.runtime.tree.CommonTree;
17 import org.eclipse.jdt.annotation.NonNullByDefault;
18 import org.eclipse.tracecompass.ctf.core.event.metadata.DeclarationScope;
19 import org.eclipse.tracecompass.ctf.core.trace.CTFTrace;
20 import org.eclipse.tracecompass.ctf.core.trace.ICTFStream;
21 import org.eclipse.tracecompass.ctf.parser.CTFParser;
22 import org.eclipse.tracecompass.internal.ctf.core.event.EventDeclaration;
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;
28 import org.eclipse.tracecompass.internal.ctf.core.trace.CTFStream;
29
30 /**
31 *
32 *
33 * An event stream can be divided into contiguous event packets of variable
34 * size. An event packet can contain a certain amount of padding at the end. The
35 * stream header is repeated at the beginning of each event packet. The
36 * rationale for the event stream design choices is explained in Stream header
37 * rationale.
38 * <p>
39 * The event stream header will therefore be referred to as the event packet
40 * header throughout the rest of this document.
41 *
42 * @author Matthew Khouzam - Initial API and implementation
43 * @author Efficios - Documentation
44 *
45 */
46 public class EventParser extends AbstractScopedCommonTreeParser {
47
48 /**
49 * Parameter object with trace and scope
50 *
51 * @author Matthew Khouzam
52 *
53 */
54 @NonNullByDefault
55 public static final class Param implements ICommonTreeParserParameter {
56
57 private final DeclarationScope fCurrentScope;
58 private final CTFTrace fTrace;
59
60 /**
61 * Constructor
62 *
63 * @param trace
64 * the trace
65 * @param currentScope
66 * the scope
67 */
68 public Param(CTFTrace trace, DeclarationScope currentScope) {
69 fTrace = trace;
70 fCurrentScope = currentScope;
71 }
72
73 }
74
75 /**
76 * The instance
77 */
78 public static final EventParser INSTANCE = new EventParser();
79
80 private EventParser() {
81 }
82
83 /**
84 * Parses an enum declaration and returns the corresponding declaration.
85 *
86 * @param eventNode
87 * An event node.
88 * @param param
89 * the parameter object
90 *
91 * @return The corresponding enum declaration.
92 * @throws ParseException
93 * event stream was badly defined
94 */
95 @Override
96 public EventDeclaration parse(CommonTree eventNode, ICommonTreeParserParameter param) throws ParseException {
97 if (!(param instanceof Param)) {
98 throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
99 }
100 Param parameter = (Param) param;
101 CTFTrace trace = ((Param) param).fTrace;
102 List<CommonTree> children = eventNode.getChildren();
103 if (children == null) {
104 throw new ParseException("Empty event block"); //$NON-NLS-1$
105 }
106
107 EventDeclaration event = new EventDeclaration();
108
109 DeclarationScope scope = new DeclarationScope(parameter.fCurrentScope, MetadataStrings.EVENT);
110
111 for (CommonTree child : children) {
112 switch (child.getType()) {
113 case CTFParser.TYPEALIAS:
114 TypeAliasParser.INSTANCE.parse(child, new TypeAliasParser.Param(trace, scope));
115 break;
116 case CTFParser.TYPEDEF:
117 TypedefParser.INSTANCE.parse(child, new TypedefParser.Param(trace, scope));
118 break;
119 case CTFParser.CTF_EXPRESSION_TYPE:
120 case CTFParser.CTF_EXPRESSION_VAL:
121 EventDeclarationParser.INSTANCE.parse(child, new EventDeclarationParser.Param(trace, event, scope));
122 break;
123 default:
124 throw childTypeError(child);
125 }
126 }
127
128 if (!event.nameIsSet()) {
129 throw new ParseException("Event name not set"); //$NON-NLS-1$
130 }
131
132 /*
133 * If the event did not specify a stream, then the trace must be single
134 * stream
135 */
136 if (!event.streamIsSet()) {
137 if (trace.nbStreams() > 1) {
138 throw new ParseException("Event without stream_id with more than one stream"); //$NON-NLS-1$
139 }
140
141 /*
142 * If the event did not specify a stream, the only existing stream
143 * must not have an id. Note: That behavior could be changed, it
144 * could be possible to just get the only existing stream, whatever
145 * is its id.
146 */
147 ICTFStream iStream = trace.getStream(null);
148 if (iStream instanceof CTFStream) {
149 CTFStream ctfStream = (CTFStream) iStream;
150 event.setStream(ctfStream);
151 } else {
152 throw new ParseException("Event without stream_id, but there is no stream without id"); //$NON-NLS-1$
153 }
154 }
155
156 /*
157 * Add the event to the stream.
158 */
159 event.getStream().addEvent(event);
160
161 return event;
162 }
163
164 }
This page took 0.041677 seconds and 4 git commands to generate.