1 /*******************************************************************************
2 * Copyright (c) 2015 Ericsson
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
;
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
;
15 import java
.util
.List
;
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
;
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.
30 * TSDL metadata representation of a named string type:
34 * encoding = /* UTF8 OR ASCII * /;
38 * A nameless string type can be declared as a field type:
41 * string field_name; /* use default UTF8 encoding * /
44 * Strings are always aligned on byte size.
46 * @author Matthew Khouzam
47 * @author Efficios - Javadoc Preable
50 public final class StringDeclarationParser
implements ICommonTreeParser
{
55 public static final StringDeclarationParser INSTANCE
= new StringDeclarationParser();
57 private static final @NonNull String ENCODING
= "encoding"; //$NON-NLS-1$
59 private StringDeclarationParser() {
63 * Parse a string declaration node and return a {@link StringDeclaration}
66 * the string declaration AST node
69 * @return a {@link StringDeclaration} describing the string layout
70 * @throws ParseException
71 * the AST is malformed
74 public StringDeclaration
parse(CommonTree string
, ICommonTreeParserParameter unused
) throws ParseException
{
75 List
<CommonTree
> children
= string
.getChildren();
76 StringDeclaration stringDeclaration
= null;
78 if (children
== null) {
79 stringDeclaration
= StringDeclaration
.getStringDeclaration(Encoding
.UTF8
);
81 Encoding encoding
= Encoding
.UTF8
;
82 for (CommonTree child
: children
) {
83 switch (child
.getType()) {
84 case CTFParser
.CTF_EXPRESSION_VAL
:
86 * An assignment expression must have 2 children, left and
90 CommonTree leftNode
= (CommonTree
) child
.getChild(0);
91 CommonTree rightNode
= (CommonTree
) child
.getChild(1);
93 List
<CommonTree
> leftStrings
= leftNode
.getChildren();
95 if (!isAnyUnaryString(leftStrings
.get(0))) {
96 throw new ParseException("Left side of ctf expression must be a string"); //$NON-NLS-1$
98 String left
= concatenateUnaryStrings(leftStrings
);
100 if (left
.equals(ENCODING
)) {
101 encoding
= EncodingParser
.INSTANCE
.parse(rightNode
, null);
103 throw new ParseException("String: unknown attribute " //$NON-NLS-1$
109 throw childTypeError(child
);
113 stringDeclaration
= StringDeclaration
.getStringDeclaration(encoding
);
116 return stringDeclaration
;