| 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2015 Ericsson |
| 3 | * |
| 4 | * All rights reserved. This program and the accompanying materials are made |
| 5 | * available under the terms of the Eclipse Public License v1.0 which |
| 6 | * 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; |
| 11 | |
| 12 | import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.isUnaryInteger; |
| 13 | |
| 14 | import org.antlr.runtime.tree.CommonTree; |
| 15 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser; |
| 16 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; |
| 17 | |
| 18 | /** |
| 19 | * Type size, in bits, for integers and floats is that returned by sizeof() in C |
| 20 | * multiplied by CHAR_BIT. We require the size of char and unsigned char types |
| 21 | * (CHAR_BIT) to be fixed to 8 bits for cross-endianness compatibility. |
| 22 | * |
| 23 | * TSDL metadata representation: |
| 24 | * |
| 25 | * <pre> |
| 26 | * size = /* value is in bits * / |
| 27 | * </pre> |
| 28 | * |
| 29 | * @author Matthew Khouzam |
| 30 | * @author Efficios - javadoc preamble. |
| 31 | */ |
| 32 | public final class SizeParser implements ICommonTreeParser { |
| 33 | private static final String INVALID_VALUE_FOR_SIZE = "Invalid value for size"; //$NON-NLS-1$ |
| 34 | |
| 35 | /** |
| 36 | * Instance |
| 37 | */ |
| 38 | public static final SizeParser INSTANCE = new SizeParser(); |
| 39 | |
| 40 | private SizeParser() { |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Gets the value of a "size" integer attribute. |
| 45 | * |
| 46 | * @param rightNode |
| 47 | * A CTF_RIGHT node. |
| 48 | * @param param |
| 49 | * unused |
| 50 | * @return The "size" value. Can be 4 bytes. |
| 51 | * @throws ParseException |
| 52 | * if the size is not an int or a negative |
| 53 | */ |
| 54 | @Override |
| 55 | public Long parse(CommonTree rightNode, ICommonTreeParserParameter param) throws ParseException { |
| 56 | CommonTree firstChild = (CommonTree) rightNode.getChild(0); |
| 57 | if (isUnaryInteger(firstChild)) { |
| 58 | if (rightNode.getChildCount() > 1) { |
| 59 | throw new ParseException(INVALID_VALUE_FOR_SIZE); |
| 60 | } |
| 61 | long size = UnaryIntegerParser.INSTANCE.parse(firstChild, null); |
| 62 | if (size < 1) { |
| 63 | throw new ParseException(INVALID_VALUE_FOR_SIZE); |
| 64 | } |
| 65 | return size; |
| 66 | } |
| 67 | throw new ParseException(INVALID_VALUE_FOR_SIZE); |
| 68 | } |
| 69 | |
| 70 | } |