| 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.stream; |
| 10 | |
| 11 | import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.isUnaryInteger; |
| 12 | |
| 13 | import org.antlr.runtime.tree.CommonTree; |
| 14 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser; |
| 15 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; |
| 16 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.UnaryIntegerParser; |
| 17 | |
| 18 | /** |
| 19 | * <strong>Stream ID</strong>, used as reference to stream description in |
| 20 | * metadata. This field is optional if there is only one stream description in |
| 21 | * the metadata, but becomes required if there are more than one stream in the |
| 22 | * TSDL metadata description. |
| 23 | * |
| 24 | * @author Matthew Khouzam |
| 25 | * @author Efficios - Javadoc |
| 26 | * |
| 27 | */ |
| 28 | public final class StreamIdParser implements ICommonTreeParser { |
| 29 | |
| 30 | /** Instance */ |
| 31 | public static final StreamIdParser INSTANCE = new StreamIdParser(); |
| 32 | |
| 33 | private StreamIdParser() { |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Parses a stream id |
| 38 | * |
| 39 | * @param tree |
| 40 | * the AST node with "id = N;" |
| 41 | * @return the value of the stream as a {@link Long} |
| 42 | */ |
| 43 | @Override |
| 44 | public Long parse(CommonTree tree, ICommonTreeParserParameter param) throws ParseException { |
| 45 | CommonTree firstChild = (CommonTree) tree.getChild(0); |
| 46 | if (isUnaryInteger(firstChild)) { |
| 47 | if (tree.getChildCount() > 1) { |
| 48 | throw new ParseException("invalid value for stream id"); //$NON-NLS-1$ |
| 49 | } |
| 50 | long intval = UnaryIntegerParser.INSTANCE.parse(firstChild, null); |
| 51 | return intval; |
| 52 | } |
| 53 | throw new ParseException("invalid value for stream id"); //$NON-NLS-1$ |
| 54 | } |
| 55 | |
| 56 | } |