/
TypeAliasAliasParser.java
112 lines (96 loc) · 4.42 KB
/
TypeAliasAliasParser.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
112
/*******************************************************************************
* 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;
import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError;
import java.util.LinkedList;
import java.util.List;
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;
/**
* The "typealias" declaration can be used to give a name (including pointer
* declarator specifier) to a type. It should also be used to map basic C types
* (float, int, unsigned long, ...) to a CTF type. Typealias is a superset of
* "typedef": it also allows assignment of a simple variable identifier to a
* type.
*
* @author Matthew Khouzam - Inital API and implementation
* @author Efficios - Documentation
*
*/
public final class TypeAliasAliasParser implements ICommonTreeParser {
/**
* Instance
*/
public static final TypeAliasAliasParser INSTANCE = new TypeAliasAliasParser();
private TypeAliasAliasParser() {
}
/**
* Parses the alias part of a typealias. It parses the underlying specifier
* list and declarator and creates the string representation that will be
* used to register the type. In typealias integer{ blabla } := int, the
* alias is the <em>integer{ blabla }</em> part, and the target is the
* <em>int</em> part.
*
* @param typeSpecifier
* A TYPEALIAS_ALIAS node.
* @param param
* unused
*
* @return The string representation of the alias.
* @throws ParseException
* if an alias is not allowed here
*/
@Override
public String parse(ICTFMetadataNode typeSpecifier, ICommonTreeParserParameter param) throws ParseException {
List<ICTFMetadataNode> children = typeSpecifier.getChildren();
ICTFMetadataNode typeSpecifierList = null;
ICTFMetadataNode typeDeclaratorList = null;
ICTFMetadataNode typeDeclarator = null;
List<ICTFMetadataNode> pointers = new LinkedList<>();
for (ICTFMetadataNode child : children) {
String type = child.getType();
if (CTFParser.tokenNames[CTFParser.TYPE_SPECIFIER_LIST].equals(type)) {
typeSpecifierList = child;
} else if (CTFParser.tokenNames[CTFParser.TYPE_DECLARATOR_LIST].equals(type)) {
typeDeclaratorList = child;
} else {
throw childTypeError(child);
}
}
/* If there is a type declarator list, extract the pointers */
if (typeDeclaratorList != null) {
/*
* Only allow one declarator
*
* eg: "typealias uint8_t := puint8_t *, **;" is not permitted.
*/
if (typeDeclaratorList.getChildCount() != 1) {
throw new ParseException("Only one type declarator is allowed in the typealias alias"); //$NON-NLS-1$
}
typeDeclarator = typeDeclaratorList.getChild(0);
List<ICTFMetadataNode> typeDeclaratorChildren = typeDeclarator.getChildren();
for (ICTFMetadataNode child : typeDeclaratorChildren) {
String type = child.getType();
if (CTFParser.tokenNames[CTFParser.POINTER].equals(type)) {
pointers.add(child);
} else if (CTFParser.tokenNames[CTFParser.IDENTIFIER].equals(type)) {
throw new ParseException("Identifier (" + child.getText() //$NON-NLS-1$
+ ") not expected in the typealias target"); //$NON-NLS-1$
} else {
throw childTypeError(child);
}
}
}
return TypeDeclarationStringParser.INSTANCE.parse(typeSpecifierList, new TypeDeclarationStringParser.Param(pointers));
}
}