Commit | Line | Data |
---|---|---|
b1ea73b5 MK |
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.integer; | |
11 | ||
12 | import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.concatenateUnaryStrings; | |
13 | import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.isUnaryInteger; | |
14 | import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.isUnaryString; | |
15 | ||
16 | import org.antlr.runtime.tree.CommonTree; | |
17 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser; | |
18 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.MetadataStrings; | |
19 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; | |
20 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.UnaryIntegerParser; | |
21 | ||
22 | /** | |
23 | * Singed status, whether an integer is capable of accepting negative values or | |
24 | * not. | |
25 | * | |
26 | * @author Matthew Khouzam | |
27 | */ | |
28 | public class SignedParser implements ICommonTreeParser { | |
29 | /** | |
30 | * Instance | |
31 | */ | |
32 | public static final SignedParser INSTANCE = new SignedParser(); | |
33 | ||
34 | private static final String INVALID_BOOLEAN_VALUE = "Invalid boolean value "; //$NON-NLS-1$ | |
35 | ||
36 | private SignedParser() { | |
37 | } | |
38 | ||
39 | /** | |
40 | * Parses whether the parent is signed or not. Typical syntax would be | |
41 | * "signed = true;" or "signed = false;" | |
42 | * | |
43 | * @param tree | |
44 | * the AST node containing "signed = boolean;" | |
45 | * @param unused | |
46 | * unused | |
47 | * @return @link {@link Boolean#TRUE} if signed, {@link Boolean#FALSE} if | |
48 | * unsigned | |
49 | * @throws ParseException | |
50 | * on a malformed tree | |
51 | */ | |
52 | @Override | |
53 | public Boolean parse(CommonTree tree, ICommonTreeParserParameter unused) throws ParseException { | |
54 | boolean ret = false; | |
55 | CommonTree firstChild = (CommonTree) tree.getChild(0); | |
56 | ||
57 | if (isUnaryString(firstChild)) { | |
58 | String strval = concatenateUnaryStrings(tree.getChildren()); | |
59 | ||
60 | if (strval.equals(MetadataStrings.TRUE) | |
61 | || strval.equals(MetadataStrings.TRUE2)) { | |
62 | ret = true; | |
63 | } else if (strval.equals(MetadataStrings.FALSE) | |
64 | || strval.equals(MetadataStrings.FALSE2)) { | |
65 | ret = false; | |
66 | } else { | |
67 | throw new ParseException(INVALID_BOOLEAN_VALUE | |
68 | + firstChild.getChild(0).getText()); | |
69 | } | |
70 | } else if (isUnaryInteger(firstChild)) { | |
71 | /* Happens if the value is something like "1234.hello" */ | |
72 | if (tree.getChildCount() > 1) { | |
73 | throw new ParseException(INVALID_BOOLEAN_VALUE); | |
74 | } | |
75 | ||
76 | long intval = UnaryIntegerParser.INSTANCE.parse(firstChild, null); | |
77 | ||
78 | if (intval == 1) { | |
79 | ret = true; | |
80 | } else if (intval == 0) { | |
81 | ret = false; | |
82 | } else { | |
83 | throw new ParseException(INVALID_BOOLEAN_VALUE | |
84 | + firstChild.getChild(0).getText()); | |
85 | } | |
86 | } else { | |
87 | throw new ParseException(INVALID_BOOLEAN_VALUE); | |
88 | } | |
89 | return ret; | |
90 | } | |
91 | ||
92 | } |