-
Notifications
You must be signed in to change notification settings - Fork 13
/
StringDeclarationParser.java
111 lines (99 loc) · 4.29 KB
/
StringDeclarationParser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
/*******************************************************************************
* Copyright (c) 2015, 2023 Ericsson
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*******************************************************************************/
package org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.string;
import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError;
import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.concatenateUnaryStrings;
import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.isAnyUnaryString;
import java.util.List;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.tracecompass.ctf.core.event.types.Encoding;
import org.eclipse.tracecompass.ctf.core.event.types.StringDeclaration;
import org.eclipse.tracecompass.ctf.parser.CTFParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser;
import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException;
import org.eclipse.tracecompass.internal.ctf.core.event.types.ICTFMetadataNode;
/**
* Strings are an array of bytes of variable size and are terminated by a '\0'
* “NULL” character. Their encoding is described in the TSDL metadata. In
* absence of encoding attribute information, the default encoding is UTF-8.
*
* TSDL metadata representation of a named string type:
*
* <pre>
* typealias string {
* encoding = /* UTF8 OR ASCII * /;
* } := name;
* </pre>
*
* A nameless string type can be declared as a field type:
*
* <pre>
* string field_name; /* use default UTF8 encoding * /
* </pre>
*
* Strings are always aligned on byte size.
*
* @author Matthew Khouzam
* @author Efficios - Javadoc Preable
*
*/
public final class StringDeclarationParser implements ICommonTreeParser {
/**
* Instance
*/
public static final StringDeclarationParser INSTANCE = new StringDeclarationParser();
private static final @NonNull String ENCODING = "encoding"; //$NON-NLS-1$
private StringDeclarationParser() {
}
/**
* Parse a string declaration node and return a {@link StringDeclaration}
*
* @param string
* the string declaration AST node
* @param unused
* unused
* @return a {@link StringDeclaration} describing the string layout
* @throws ParseException
* the AST is malformed
*/
@Override
public StringDeclaration parse(ICTFMetadataNode string, ICommonTreeParserParameter unused) throws ParseException {
List<ICTFMetadataNode> children = string.getChildren();
StringDeclaration stringDeclaration = null;
if (children == null) {
stringDeclaration = StringDeclaration.getStringDeclaration(Encoding.UTF8);
} else {
Encoding encoding = Encoding.UTF8;
for (ICTFMetadataNode child : children) {
String type = child.getType();
if (CTFParser.tokenNames[CTFParser.CTF_EXPRESSION_VAL].equals(type)) {
ICTFMetadataNode leftNode = child.getChild(0);
ICTFMetadataNode rightNode = child.getChild(1);
List<ICTFMetadataNode> leftStrings = leftNode.getChildren();
if (!isAnyUnaryString(leftStrings.get(0))) {
throw new ParseException("Left side of ctf expression must be a string"); //$NON-NLS-1$
}
String left = concatenateUnaryStrings(leftStrings);
if (left.equals(ENCODING)) {
encoding = EncodingParser.INSTANCE.parse(rightNode, null);
} else {
throw new ParseException("String: unknown attribute " //$NON-NLS-1$
+ left);
}
} else {
throw childTypeError(child);
}
}
stringDeclaration = StringDeclaration.getStringDeclaration(encoding);
}
return stringDeclaration;
}
}