| 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; |
| 10 | |
| 11 | import static org.eclipse.tracecompass.internal.ctf.core.event.metadata.tsdl.TsdlUtils.childTypeError; |
| 12 | |
| 13 | import org.antlr.runtime.tree.CommonTree; |
| 14 | import org.eclipse.tracecompass.ctf.parser.CTFParser; |
| 15 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ICommonTreeParser; |
| 16 | import org.eclipse.tracecompass.internal.ctf.core.event.metadata.ParseException; |
| 17 | |
| 18 | /** |
| 19 | * Type specifier list name parser (is it a bool? a string... ) |
| 20 | * |
| 21 | * @author Matthew Khouzam |
| 22 | * |
| 23 | */ |
| 24 | public final class TypeSpecifierListNameParser implements ICommonTreeParser { |
| 25 | |
| 26 | /** |
| 27 | * Instance |
| 28 | */ |
| 29 | public static final TypeSpecifierListNameParser INSTANCE = new TypeSpecifierListNameParser(); |
| 30 | |
| 31 | private TypeSpecifierListNameParser() { |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Creates the string representation of a type specifier. |
| 36 | * |
| 37 | * @param typeSpecifier |
| 38 | * A TYPE_SPECIFIER node. |
| 39 | * |
| 40 | * @return param unused |
| 41 | * @throws ParseException |
| 42 | * invalid node |
| 43 | */ |
| 44 | @Override |
| 45 | public StringBuilder parse(CommonTree typeSpecifier, ICommonTreeParserParameter param) throws ParseException { |
| 46 | StringBuilder sb = new StringBuilder(); |
| 47 | switch (typeSpecifier.getType()) { |
| 48 | case CTFParser.FLOATTOK: |
| 49 | case CTFParser.INTTOK: |
| 50 | case CTFParser.LONGTOK: |
| 51 | case CTFParser.SHORTTOK: |
| 52 | case CTFParser.SIGNEDTOK: |
| 53 | case CTFParser.UNSIGNEDTOK: |
| 54 | case CTFParser.CHARTOK: |
| 55 | case CTFParser.DOUBLETOK: |
| 56 | case CTFParser.VOIDTOK: |
| 57 | case CTFParser.BOOLTOK: |
| 58 | case CTFParser.COMPLEXTOK: |
| 59 | case CTFParser.IMAGINARYTOK: |
| 60 | case CTFParser.CONSTTOK: |
| 61 | case CTFParser.IDENTIFIER: |
| 62 | parseSimple(typeSpecifier, sb); |
| 63 | break; |
| 64 | case CTFParser.STRUCT: { |
| 65 | parseStruct(typeSpecifier, sb); |
| 66 | break; |
| 67 | } |
| 68 | case CTFParser.VARIANT: { |
| 69 | parseVariant(typeSpecifier, sb); |
| 70 | break; |
| 71 | } |
| 72 | case CTFParser.ENUM: { |
| 73 | parseEnum(typeSpecifier, sb); |
| 74 | break; |
| 75 | } |
| 76 | case CTFParser.FLOATING_POINT: |
| 77 | case CTFParser.INTEGER: |
| 78 | case CTFParser.STRING: |
| 79 | throw new ParseException("CTF type found in createTypeSpecifierString"); //$NON-NLS-1$ |
| 80 | default: |
| 81 | throw childTypeError(typeSpecifier); |
| 82 | } |
| 83 | return sb; |
| 84 | |
| 85 | } |
| 86 | |
| 87 | private static void parseEnum(CommonTree typeSpecifier, StringBuilder sb) throws ParseException { |
| 88 | CommonTree enumName = (CommonTree) typeSpecifier.getFirstChildWithType(CTFParser.ENUM_NAME); |
| 89 | if (enumName == null) { |
| 90 | throw new ParseException("nameless enum found in createTypeSpecifierString"); //$NON-NLS-1$ |
| 91 | } |
| 92 | |
| 93 | CommonTree enumNameIdentifier = (CommonTree) enumName.getChild(0); |
| 94 | |
| 95 | parseSimple(enumNameIdentifier, sb); |
| 96 | } |
| 97 | |
| 98 | private static void parseVariant(CommonTree typeSpecifier, StringBuilder sb) throws ParseException { |
| 99 | CommonTree variantName = (CommonTree) typeSpecifier.getFirstChildWithType(CTFParser.VARIANT_NAME); |
| 100 | if (variantName == null) { |
| 101 | throw new ParseException("nameless variant found in createTypeSpecifierString"); //$NON-NLS-1$ |
| 102 | } |
| 103 | |
| 104 | CommonTree variantNameIdentifier = (CommonTree) variantName.getChild(0); |
| 105 | |
| 106 | parseSimple(variantNameIdentifier, sb); |
| 107 | } |
| 108 | |
| 109 | private static void parseSimple(CommonTree typeSpecifier, StringBuilder sb) { |
| 110 | sb.append(typeSpecifier.getText()); |
| 111 | } |
| 112 | |
| 113 | private static void parseStruct(CommonTree typeSpecifier, StringBuilder sb) throws ParseException { |
| 114 | CommonTree structName = (CommonTree) typeSpecifier.getFirstChildWithType(CTFParser.STRUCT_NAME); |
| 115 | if (structName == null) { |
| 116 | throw new ParseException("nameless struct found in createTypeSpecifierString"); //$NON-NLS-1$ |
| 117 | } |
| 118 | |
| 119 | CommonTree structNameIdentifier = (CommonTree) structName.getChild(0); |
| 120 | |
| 121 | parseSimple(structNameIdentifier, sb); |
| 122 | } |
| 123 | |
| 124 | } |