76a576616bebe5ea7ce7880fa96255677884d476
[deliverable/tracecompass.git] / ctf / org.eclipse.tracecompass.ctf.core / src / org / eclipse / tracecompass / internal / ctf / core / event / metadata / tsdl / string / StringDeclarationParser.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 package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.string;
10
11 import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError;
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.isAnyUnaryString;
14
15 import java.util.List;
16
17 import org.antlr.runtime.tree.CommonTree;
18 import org.eclipse.jdt.annotation.NonNull;
19 import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
20 import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
21 import org.eclipse.tracecompass.ctf.parser.CTFParser;
22 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser;
23 import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
24
25 /**
26 * Strings are an array of bytes of variable size and are terminated by a '\0'
27 * “NULL” character. Their encoding is described in the TSDL metadata. In
28 * absence of encoding attribute information, the default encoding is UTF-8.
29 *
30 * TSDL metadata representation of a named string type:
31 *
32 * <pre>
33 * typealias string {
34 * encoding = /* UTF8 OR ASCII * /;
35 * } := name;
36 * </pre>
37 *
38 * A nameless string type can be declared as a field type:
39 *
40 * <pre>
41 * string field_name; /* use default UTF8 encoding * /
42 * </pre>
43 *
44 * Strings are always aligned on byte size.
45 *
46 * @author Matthew Khouzam
47 * @author Efficios - Javadoc Preable
48 *
49 */
50 public class StringDeclarationParser implements ICommonTreeParser {
51
52 /**
53 * Instance
54 */
55 public static final StringDeclarationParser INSTANCE = new StringDeclarationParser();
56
57 private static final @NonNull String ENCODING = "encoding"; //$NON-NLS-1$
58
59 private StringDeclarationParser() {
60 }
61
62 /**
63 * Parse a string declaration node and return a {@link StringDeclaration}
64 *
65 * @param string
66 * the string declaration AST node
67 * @param unused
68 * unused
69 * @return a {@link StringDeclaration} describing the string layout
70 * @throws ParseException
71 * the AST is malformed
72 */
73 @Override
74 public StringDeclaration parse(CommonTree string, ICommonTreeParserParameter unused) throws ParseException {
75 List<CommonTree> children = string.getChildren();
76 StringDeclaration stringDeclaration = null;
77
78 if (children == null) {
79 stringDeclaration = StringDeclaration.getStringDeclaration(Encoding.UTF8);
80 } else {
81 Encoding encoding = Encoding.UTF8;
82 for (CommonTree child : children) {
83 switch (child.getType()) {
84 case CTFParser.CTF_EXPRESSION_VAL:
85 /*
86 * An assignment expression must have 2 children, left and
87 * right
88 */
89
90 CommonTree leftNode = (CommonTree) child.getChild(0);
91 CommonTree rightNode = (CommonTree) child.getChild(1);
92
93 List<CommonTree> leftStrings = leftNode.getChildren();
94
95 if (!isAnyUnaryString(leftStrings.get(0))) {
96 throw new ParseException("Left side of ctf expression must be a string"); //$NON-NLS-1$
97 }
98 String left = concatenateUnaryStrings(leftStrings);
99
100 if (left.equals(ENCODING)) {
101 encoding = EncodingParser.INSTANCE.parse(rightNode, null);
102 } else {
103 throw new ParseException("String: unknown attribute " //$NON-NLS-1$
104 + left);
105 }
106
107 break;
108 default:
109 throw childTypeError(child);
110 }
111 }
112
113 stringDeclaration = StringDeclaration.getStringDeclaration(encoding);
114 }
115
116 return stringDeclaration;
117 }
118
119 }
This page took 0.033123 seconds and 4 git commands to generate.