Java Code Examples for org.antlr.runtime.tree.CommonTree#getType()

The following examples show how to use org.antlr.runtime.tree.CommonTree#getType() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IOStructGen.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Parses a declaration at the root level.
 *
 * @param declaration
 *            The declaration subtree.
 * @throws ParseException
 */
private void parseRootDeclaration(CommonTree declaration)
        throws ParseException {

    List<CommonTree> children = declaration.getChildren();

    for (CommonTree child : children) {
        switch (child.getType()) {
        case CTFParser.TYPEDEF:
            TypedefParser.INSTANCE.parse(child, new TypedefParser.Param(fTrace, fRoot));
            break;
        case CTFParser.TYPEALIAS:
            TypeAliasParser.INSTANCE.parse(child, new TypeAliasParser.Param(fTrace, fRoot));
            break;
        case CTFParser.TYPE_SPECIFIER_LIST:
            TypeSpecifierListParser.INSTANCE.parse(child, new TypeSpecifierListParser.Param(fTrace, null, null, fRoot));
            break;
        default:
            throw childTypeError(child);
        }
    }
}
 
Example 2
Source File: TraceScopeParser.java    From tracecompass with Eclipse Public License 2.0 6 votes vote down vote up
/**
 * Parse a trace scope to get a concatenated scope name. Like
 * "trace.version.minor"
 *
 * @param unused
 *            unused
 * @param param
 *            a {@link Param} containing the list of ASTs to make the scope
 * @return a {@link String} of the scope
 * @throws ParseException
 *             an AST was malformed
 *
 */
@Override
public String parse(CommonTree unused, ICommonTreeParserParameter param) throws ParseException {
    if (!(param instanceof Param)) {
        throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
    }
    List<@NonNull CommonTree> lengthChildren = ((Param) param).fList;
    CommonTree nextElem = (CommonTree) lengthChildren.get(1).getChild(0);
    switch (nextElem.getType()) {
    case CTFParser.IDENTIFIER:
        return concatenateUnaryStrings(lengthChildren.subList(1, lengthChildren.size()));
    case CTFParser.STREAM:
        return StreamScopeParser.INSTANCE.parse(null, new StreamScopeParser.Param(lengthChildren.subList(1, lengthChildren.size())));
    default:
        throw new ParseException("Unsupported scope trace." + nextElem); //$NON-NLS-1$
    }
}
 
Example 3
Source File: CFilterRuleParser.java    From binnavi with Apache License 2.0 6 votes vote down vote up
/**
 * Converts an ANTLR AST into a filter AST.
 * 
 * @param ast The ANTRL AST to convert.
 * 
 * @return The converted AST.
 * 
 * @throws RecognitionException Thrown if the AST could not be converted.
 */
private static IAbstractNode convert(final CommonTree ast) throws RecognitionException {
  if (ast.getType() == FilterParser.PREDICATE) {
    return new CPredicateExpression(ast.getText());
  } else if (ast.getType() == FilterParser.AND) {
    return convertAnd(ast);
  } else if (ast.getType() == FilterParser.OR) {
    return convertOr(ast);
  } else if (ast.getType() == 0) {
    throw new RecognitionException();
  } else if (ast.getType() == FilterParser.SUB_EXPRESSION) {
    return convert((CommonTree) ast.getChild(0));
  }

  throw new IllegalStateException("IE00960: Not yet implemented (" + ast.getType() + ")");
}
 
Example 4
Source File: CMISFTSQueryParser.java    From alfresco-data-model with GNU Lesser General Public License v3.0 6 votes vote down vote up
static private Constraint buildFTSTest(CommonTree argNode, QueryModelFactory factory, FunctionEvaluationContext functionEvaluationContext,
        Selector selector, Map<String, Column> columnMap, String defaultField)
{
    CommonTree testNode = argNode;
    switch (testNode.getType())
    {
    case CMIS_FTSParser.DISJUNCTION:
    case CMIS_FTSParser.CONJUNCTION:
        return buildFTSConnective(testNode, factory, functionEvaluationContext, selector, columnMap, defaultField);
    case CMIS_FTSParser.TERM:
        return buildTerm(testNode, factory, functionEvaluationContext, selector, columnMap);
    case CMIS_FTSParser.PHRASE:
        return buildPhrase(testNode, factory, functionEvaluationContext, selector, columnMap);
    default:
        throw new FTSQueryException("Unsupported FTS option " + testNode.getText());
    }
}
 
Example 5
Source File: TsdlUtils.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Concatenates a list of unary strings separated by arrows (->) or dots.
 *
 * @param strings
 *            A list, first element being an unary string, subsequent
 *            elements being ARROW or DOT nodes with unary strings as child.
 * @return The string representation of the unary string chain.
 * @throws ParseException
 *             If the strings list contains a non-string element
 */
public static @NonNull String concatenateUnaryStrings(List<CommonTree> strings) throws ParseException {

    StringBuilder sb = new StringBuilder();

    CommonTree first = strings.get(0);
    sb.append(STRING_PARSER.parse(first, null));

    boolean isFirst = true;

    for (CommonTree ref : strings) {
        if (isFirst) {
            isFirst = false;
            continue;
        }

        CommonTree id = (CommonTree) ref.getChild(0);

        if (ref.getType() == CTFParser.ARROW) {
            sb.append("->"); //$NON-NLS-1$
        } else { /* DOT */
            sb.append('.');
        }

        sb.append(STRING_PARSER.parse(id, null));
    }

    return sb.toString();
}
 
Example 6
Source File: IOStructGen.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
private void parseTrace(CommonTree traceNode) throws ParseException {

        CTFTrace trace = fTrace;
        List<CommonTree> children = traceNode.getChildren();
        if (children == null) {
            throw new ParseException("Trace block is empty"); //$NON-NLS-1$
        }

        for (CommonTree child : children) {
            switch (child.getType()) {
            case CTFParser.TYPEALIAS:
                TypeAliasParser.INSTANCE.parse(child, new TypeAliasParser.Param(trace, fRoot));
                break;
            case CTFParser.TYPEDEF:
                TypedefParser.INSTANCE.parse(child, new TypedefParser.Param(trace, fRoot));
                break;
            case CTFParser.CTF_EXPRESSION_TYPE:
            case CTFParser.CTF_EXPRESSION_VAL:
                TraceDeclarationParser.INSTANCE.parse(child, new TraceDeclarationParser.Param(fTrace, fRoot));
                break;
            default:
                throw childTypeError(child);
            }
        }

        /*
         * If trace byte order was not specified and not using packet based
         * metadata
         */
        if (fTrace.getByteOrder() == null) {
            throw new ParseException("Trace byte order not set"); //$NON-NLS-1$
        }
    }
 
Example 7
Source File: IOStructGen.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
private void parseIncompleteRoot(CommonTree root) throws ParseException {
    if (!fHasBeenParsed) {
        throw new ParseException("You need to run generate first"); //$NON-NLS-1$
    }
    List<CommonTree> children = root.getChildren();
    List<CommonTree> events = new ArrayList<>();
    Collection<CTFCallsite> callsites = new ArrayList<>();
    for (CommonTree child : children) {
        final int type = child.getType();
        switch (type) {
        case CTFParser.DECLARATION:
            parseRootDeclaration(child);
            break;
        case CTFParser.TRACE:
            throw new ParseException("Trace block defined here, please use generate and not generateFragment to parse this fragment"); //$NON-NLS-1$
        case CTFParser.STREAM:
            StreamParser.INSTANCE.parse(child, new StreamParser.Param(fTrace, fRoot));
            break;
        case CTFParser.EVENT:
            events.add(child);
            break;
        case CTFParser.CLOCK:
            CTFClock ctfClock = ClockParser.INSTANCE.parse(child, null);
            String nameValue = ctfClock.getName();
            fTrace.addClock(nameValue, ctfClock);
            break;
        case CTFParser.ENV:
            fTrace.setEnvironment(EnvironmentParser.INSTANCE.parse(child, null));
            break;
        case CTFParser.CALLSITE:
            callsites.add(CallSiteParser.INSTANCE.parse(child, null));
            break;
        default:
            throw childTypeError(child);
        }
    }
    parseEvents(events, callsites, !Iterables.isEmpty(fTrace.getStreams()));
}
 
Example 8
Source File: TreeToQuery.java    From cuba with Apache License 2.0 5 votes vote down vote up
private boolean isGroupByItem(CommonTree node) {
    if (node.parent != null && node.parent.getType() == JPA2Lexer.T_GROUP_BY) {
        if (node.getType() != JPA2Lexer.BY && node.getType() != JPA2Lexer.GROUP) {
            return true;
        }
    }
    return false;
}
 
Example 9
Source File: TreeToQuery.java    From cuba with Apache License 2.0 5 votes vote down vote up
private boolean isDecimal(CommonTree node) {
    if (node != null) {
        if (Objects.equals(".", node.getText())) {
            return true;
        }
        if (node.getType() == JPA2Lexer.INT_NUMERAL && node.childIndex > 0) {
            Tree prevNode = node.parent.getChild(node.childIndex - 1);
            return prevNode != null && Objects.equals(".", prevNode.getText());
        }
    }
    return false;
}
 
Example 10
Source File: VariantBodyParser.java    From tracecompass with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Parse the variant body, fills the variant with the results.
 *
 * @param variantBody
 *            the variant body AST node
 * @param param
 *            the {@link Param} parameter object
 * @return a populated {@link VariantDeclaration}
 * @throws ParseException
 *             if the AST is malformed
 */
@Override
public VariantDeclaration parse(CommonTree variantBody, ICommonTreeParserParameter param) throws ParseException {
    if (!(param instanceof Param)) {
        throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
    }

    String variantName = ((Param) param).fName;
    VariantDeclaration variantDeclaration = ((Param) param).fVariantDeclaration;
    List<CommonTree> variantDeclarations = variantBody.getChildren();

    final DeclarationScope scope = new DeclarationScope(((Param) param).fDeclarationScope, variantName == null ? MetadataStrings.VARIANT : variantName);
    CTFTrace trace = ((Param) param).fTrace;
    for (CommonTree declarationNode : variantDeclarations) {
        switch (declarationNode.getType()) {
        case CTFParser.TYPEALIAS:
            TypeAliasParser.INSTANCE.parse(declarationNode, new TypeAliasParser.Param(trace, scope));
            break;
        case CTFParser.TYPEDEF:
            Map<String, IDeclaration> decs = TypedefParser.INSTANCE.parse(declarationNode, new TypedefParser.Param(trace, scope));
            for (Entry<String, IDeclaration> declarationEntry : decs.entrySet()) {
                variantDeclaration.addField(declarationEntry.getKey(), declarationEntry.getValue());
            }
            break;
        case CTFParser.SV_DECLARATION:
            VariantDeclarationParser.INSTANCE.parse(declarationNode, new VariantDeclarationParser.Param(variantDeclaration, trace, scope));
            break;
        default:
            throw childTypeError(declarationNode);
        }
    }

    return variantDeclaration;
}
 
Example 11
Source File: TypeSpecifierListNameParser.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Creates the string representation of a type specifier.
 *
 * @param typeSpecifier
 *            A TYPE_SPECIFIER node.
 *
 * @return param unused
 * @throws ParseException
 *             invalid node
 */
@Override
public StringBuilder parse(CommonTree typeSpecifier, ICommonTreeParserParameter param) throws ParseException {
    StringBuilder sb = new StringBuilder();
    switch (typeSpecifier.getType()) {
    case CTFParser.FLOATTOK:
    case CTFParser.INTTOK:
    case CTFParser.LONGTOK:
    case CTFParser.SHORTTOK:
    case CTFParser.SIGNEDTOK:
    case CTFParser.UNSIGNEDTOK:
    case CTFParser.CHARTOK:
    case CTFParser.DOUBLETOK:
    case CTFParser.VOIDTOK:
    case CTFParser.BOOLTOK:
    case CTFParser.COMPLEXTOK:
    case CTFParser.IMAGINARYTOK:
    case CTFParser.CONSTTOK:
    case CTFParser.IDENTIFIER:
        parseSimple(typeSpecifier, sb);
        break;
    case CTFParser.STRUCT: {
        parseStruct(typeSpecifier, sb);
        break;
    }
    case CTFParser.VARIANT: {
        parseVariant(typeSpecifier, sb);
        break;
    }
    case CTFParser.ENUM: {
        parseEnum(typeSpecifier, sb);
        break;
    }
    case CTFParser.FLOATING_POINT:
    case CTFParser.INTEGER:
    case CTFParser.STRING:
        throw new ParseException("CTF type found in createTypeSpecifierString"); //$NON-NLS-1$
    default:
        throw childTypeError(typeSpecifier);
    }
    return sb;

}
 
Example 12
Source File: TypeSpecifierListParser.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Parses a type specifier list and returns the corresponding declaration.
 *
 * @param typeSpecifierList
 *            A TYPE_SPECIFIER_LIST node.
 * @param param
 *            The paramer object
 *
 * @return The corresponding declaration.
 * @throws ParseException
 *             If the type has not been defined or if there is an error
 *             creating the declaration.
 */
@Override
public IDeclaration parse(CommonTree typeSpecifierList, ICommonTreeParserParameter param) throws ParseException {
    if (!(param instanceof Param)) {
        throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
    }
    final DeclarationScope scope = ((Param) param).fDeclarationScope;
    List<@NonNull CommonTree> pointerList = ((Param) param).fListNode;
    CTFTrace trace = ((Param) param).fTrace;
    CommonTree identifier = ((Param) param).fIdentifier;
    IDeclaration declaration = null;

    /*
     * By looking at the first element of the type specifier list, we can
     * determine which type it belongs to.
     */
    CommonTree firstChild = (CommonTree) typeSpecifierList.getChild(0);

    switch (firstChild.getType()) {
    case CTFParser.FLOATING_POINT:
        declaration = FloatDeclarationParser.INSTANCE.parse(firstChild, new FloatDeclarationParser.Param(trace));
        break;
    case CTFParser.INTEGER:
        declaration = IntegerDeclarationParser.INSTANCE.parse(firstChild, new IntegerDeclarationParser.Param(trace));
        break;
    case CTFParser.STRING:
        declaration = StringDeclarationParser.INSTANCE.parse(firstChild, null);
        break;
    case CTFParser.STRUCT:
        declaration = StructParser.INSTANCE.parse(firstChild, new StructParser.Param(trace, identifier, scope));
        StructDeclaration structDeclaration = (StructDeclaration) declaration;
        if (structDeclaration.hasField(MetadataStrings.ID)) {
            IDeclaration idEnumDecl = structDeclaration.getField(MetadataStrings.ID);
            if (idEnumDecl instanceof EnumDeclaration) {
                EnumDeclaration enumDeclaration = (EnumDeclaration) idEnumDecl;
                ByteOrder bo = enumDeclaration.getContainerType().getByteOrder();
                if (EventHeaderCompactDeclaration.getEventHeader(bo).isCompactEventHeader(structDeclaration)) {
                    declaration = EventHeaderCompactDeclaration.getEventHeader(bo);
                } else if (EventHeaderLargeDeclaration.getEventHeader(bo).isLargeEventHeader(structDeclaration)) {
                    declaration = EventHeaderLargeDeclaration.getEventHeader(bo);
                }
            }
        }
        break;
    case CTFParser.VARIANT:
        declaration = VariantParser.INSTANCE.parse(firstChild, new VariantParser.Param(trace, scope));
        break;
    case CTFParser.ENUM:
        declaration = EnumParser.INSTANCE.parse(firstChild, new EnumParser.Param(trace, scope));
        break;
    case CTFParser.IDENTIFIER:
    case CTFParser.FLOATTOK:
    case CTFParser.INTTOK:
    case CTFParser.LONGTOK:
    case CTFParser.SHORTTOK:
    case CTFParser.SIGNEDTOK:
    case CTFParser.UNSIGNEDTOK:
    case CTFParser.CHARTOK:
    case CTFParser.DOUBLETOK:
    case CTFParser.VOIDTOK:
    case CTFParser.BOOLTOK:
    case CTFParser.COMPLEXTOK:
    case CTFParser.IMAGINARYTOK:
        declaration = TypeDeclarationParser.INSTANCE.parse(typeSpecifierList, new TypeDeclarationParser.Param(pointerList, scope));
        break;
    default:
        throw childTypeError(firstChild);
    }

    return declaration;
}
 
Example 13
Source File: FloatDeclarationParser.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Parses a float node and returns a floating point declaration of the
 * type @link {@link FloatDeclaration}.
 *
 * @param floatingPoint
 *            AST node of type FLOAT
 * @param param
 *            parameter containing the trace
 * @return the float declaration
 * @throws ParseException
 *             if a float AST is malformed
 */
@Override
public FloatDeclaration parse(CommonTree floatingPoint, ICommonTreeParserParameter param) throws ParseException {
    if (!(param instanceof Param)) {
        throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
    }
    CTFTrace trace = ((Param) param).fTrace;
    List<CommonTree> children = floatingPoint.getChildren();

    /*
     * If the integer has no attributes, then it is missing the size
     * attribute which is required
     */
    if (children == null) {
        throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE);
    }

    /* The return value */
    ByteOrder byteOrder = trace.getByteOrder();
    long alignment = 0;

    int exponent = DEFAULT_FLOAT_EXPONENT;
    int mantissa = DEFAULT_FLOAT_MANTISSA;

    /* Iterate on all integer children */
    for (CommonTree child : children) {
        switch (child.getType()) {
        case CTFParser.CTF_EXPRESSION_VAL:
            /*
             * An assignment expression must have 2 children, left and right
             */

            CommonTree leftNode = (CommonTree) child.getChild(0);
            CommonTree rightNode = (CommonTree) child.getChild(1);

            List<CommonTree> leftStrings = leftNode.getChildren();

            if (!isAnyUnaryString(leftStrings.get(0))) {
                throw new ParseException(IDENTIFIER_MUST_BE_A_STRING);
            }
            String left = concatenateUnaryStrings(leftStrings);

            if (left.equals(MetadataStrings.EXP_DIG)) {
                exponent = UnaryIntegerParser.INSTANCE.parse((CommonTree) rightNode.getChild(0), null).intValue();
            } else if (left.equals(MetadataStrings.BYTE_ORDER)) {
                byteOrder = ByteOrderParser.INSTANCE.parse(rightNode, new ByteOrderParser.Param(trace));
            } else if (left.equals(MetadataStrings.MANT_DIG)) {
                mantissa = UnaryIntegerParser.INSTANCE.parse((CommonTree) rightNode.getChild(0), null).intValue();
            } else if (left.equals(MetadataStrings.ALIGN)) {
                alignment = AlignmentParser.INSTANCE.parse(rightNode, null);
            } else {
                throw new ParseException(FLOAT_UNKNOWN_ATTRIBUTE + left);
            }

            break;
        default:
            throw childTypeError(child);
        }
    }
    int size = mantissa + exponent;
    if (size == 0) {
        throw new ParseException(FLOAT_MISSING_SIZE_ATTRIBUTE);
    }

    if (alignment == 0) {
        alignment = 1;
    }

    return new FloatDeclaration(exponent, mantissa, byteOrder, alignment);

}
 
Example 14
Source File: TypeDeclaratorParser.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isTrace(CommonTree first) {
    return first.getType() == CTFParser.TRACE;
}
 
Example 15
Source File: IntegerDeclarationParser.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Parses an integer declaration node.
 *
 * @param parameter
 *            parent trace, for byte orders
 *
 * @return The corresponding integer declaration.
 */
@Override
public IntegerDeclaration parse(CommonTree integer, ICommonTreeParserParameter parameter) throws ParseException {
    if (!(parameter instanceof Param)) {
        throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
    }
    CTFTrace trace = ((Param) parameter).fTrace;
    List<CommonTree> children = integer.getChildren();

    /*
     * If the integer has no attributes, then it is missing the size
     * attribute which is required
     */
    if (children == null) {
        throw new ParseException("integer: missing size attribute"); //$NON-NLS-1$
    }

    /* The return value */
    IntegerDeclaration integerDeclaration = null;
    boolean signed = false;
    ByteOrder byteOrder = trace.getByteOrder();
    long size = 0;
    long alignment = 0;
    int base = DEFAULT_INT_BASE;
    @NonNull
    String clock = EMPTY_STRING;

    Encoding encoding = Encoding.NONE;

    /* Iterate on all integer children */
    for (CommonTree child : children) {
        switch (child.getType()) {
        case CTFParser.CTF_EXPRESSION_VAL:
            /*
             * An assignment expression must have 2 children, left and right
             */

            CommonTree leftNode = (CommonTree) child.getChild(0);
            CommonTree rightNode = (CommonTree) child.getChild(1);

            List<CommonTree> 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);

            switch (left) {
            case SIGNED:
                signed = SignedParser.INSTANCE.parse(rightNode, null);
                break;
            case MetadataStrings.BYTE_ORDER:
                byteOrder = ByteOrderParser.INSTANCE.parse(rightNode, new ByteOrderParser.Param(trace));
                break;
            case SIZE:
                size = SizeParser.INSTANCE.parse(rightNode, null);
                break;
            case MetadataStrings.ALIGN:
                alignment = AlignmentParser.INSTANCE.parse(rightNode, null);
                break;
            case BASE:
                base = BaseParser.INSTANCE.parse(rightNode, null);
                break;
            case ENCODING:
                encoding = EncodingParser.INSTANCE.parse(rightNode, null);
                break;
            case MAP:
                clock = ClockMapParser.INSTANCE.parse(rightNode, null);
                break;
            default:
                Activator.log(IStatus.WARNING, Messages.IOStructGen_UnknownIntegerAttributeWarning + " " + left); //$NON-NLS-1$
                break;
            }

            break;
        default:
            throw childTypeError(child);
        }
    }

    if (size <= 0) {
        throw new ParseException("Invalid size attribute in Integer: " + size); //$NON-NLS-1$
    }

    if (alignment == 0) {
        alignment = 1;
    }

    integerDeclaration = IntegerDeclaration.createDeclaration((int) size, signed, base,
            byteOrder, encoding, clock, alignment);

    return integerDeclaration;
}
 
Example 16
Source File: EventParser.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Parses an enum declaration and returns the corresponding declaration.
 *
 * @param eventNode
 *            An event node.
 * @param param
 *            the parameter object
 *
 * @return The corresponding enum declaration.
 * @throws ParseException
 *             event stream was badly defined
 */
@Override
public EventDeclaration parse(CommonTree eventNode, ICommonTreeParserParameter param) throws ParseException {
    if (!(param instanceof Param)) {
        throw new IllegalArgumentException("Param must be a " + Param.class.getCanonicalName()); //$NON-NLS-1$
    }
    Param parameter = (Param) param;
    CTFTrace trace = ((Param) param).fTrace;
    List<CommonTree> children = eventNode.getChildren();
    if (children == null) {
        throw new ParseException("Empty event block"); //$NON-NLS-1$
    }

    EventDeclaration event = new EventDeclaration();

    DeclarationScope scope = new DeclarationScope(parameter.fCurrentScope, MetadataStrings.EVENT);

    for (CommonTree child : children) {
        switch (child.getType()) {
        case CTFParser.TYPEALIAS:
            TypeAliasParser.INSTANCE.parse(child, new TypeAliasParser.Param(trace, scope));
            break;
        case CTFParser.TYPEDEF:
            TypedefParser.INSTANCE.parse(child, new TypedefParser.Param(trace, scope));
            break;
        case CTFParser.CTF_EXPRESSION_TYPE:
        case CTFParser.CTF_EXPRESSION_VAL:
            EventDeclarationParser.INSTANCE.parse(child, new EventDeclarationParser.Param(trace, event, scope));
            break;
        default:
            throw childTypeError(child);
        }
    }

    if (!event.nameIsSet()) {
        throw new ParseException("Event name not set"); //$NON-NLS-1$
    }

    /*
     * If the event did not specify a stream, then the trace must be single
     * stream
     */
    if (!event.streamIsSet()) {
        if (trace.nbStreams() > 1) {
            throw new ParseException("Event without stream_id with more than one stream"); //$NON-NLS-1$
        }

        /*
         * If the event did not specify a stream, the only existing stream
         * must not have an id. Note: That behavior could be changed, it
         * could be possible to just get the only existing stream, whatever
         * is its id.
         */
        ICTFStream iStream = trace.getStream(null);
        if (iStream instanceof CTFStream) {
            CTFStream ctfStream = (CTFStream) iStream;
            event.setStream(ctfStream);
        } else {
            throw new ParseException("Event without stream_id, but there is no stream without id"); //$NON-NLS-1$
        }
    }

    /*
     * Add the event to the stream.
     */
    event.getStream().addEvent(event);

    return event;
}
 
Example 17
Source File: IOStructGen.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
/**
 * Parse the root node.
 *
 * @param root
 *            A ROOT node.
 * @throws ParseException
 */
private void parseRoot(CommonTree root) throws ParseException {

    List<CommonTree> children = root.getChildren();

    CommonTree traceNode = null;
    boolean hasStreams = false;
    List<CommonTree> events = new ArrayList<>();
    Collection<CTFCallsite> callsites = new ArrayList<>();
    for (CommonTree child : children) {
        final int type = child.getType();
        switch (type) {
        case CTFParser.DECLARATION:
            parseRootDeclaration(child);
            break;
        case CTFParser.TRACE:
            if (traceNode != null) {
                throw new ParseException("Only one trace block is allowed"); //$NON-NLS-1$
            }
            traceNode = child;
            parseTrace(traceNode);
            break;
        case CTFParser.STREAM:
            StreamParser.INSTANCE.parse(child, new StreamParser.Param(fTrace, fRoot));
            hasStreams = true;
            break;
        case CTFParser.EVENT:
            events.add(child);
            break;
        case CTFParser.CLOCK:
            CTFClock ctfClock = ClockParser.INSTANCE.parse(child, null);
            String nameValue = ctfClock.getName();
            fTrace.addClock(nameValue, ctfClock);
            break;
        case CTFParser.ENV:
            fTrace.setEnvironment(EnvironmentParser.INSTANCE.parse(child, null));
            break;
        case CTFParser.CALLSITE:
            callsites.add(CallSiteParser.INSTANCE.parse(child, null));
            break;
        default:
            throw childTypeError(child);
        }
    }
    if (traceNode == null) {
        throw new ParseException("Missing trace block"); //$NON-NLS-1$
    }
    parseEvents(events, callsites, hasStreams);
    fHasBeenParsed = true;
}
 
Example 18
Source File: TypeDeclaratorParser.java    From tracecompass with Eclipse Public License 2.0 4 votes vote down vote up
private static boolean isEvent(CommonTree first) {
    return first.getType() == CTFParser.EVENT;
}
 
Example 19
Source File: TsdlUtils.java    From tracecompass with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Is the tree a unary string
 *
 * @param node
 *            The node to check.
 * @return True if the given node is an unary string.
 */
public static boolean isUnaryString(CommonTree node) {
    return ((node.getType() == CTFParser.UNARY_EXPRESSION_STRING));
}
 
Example 20
Source File: TsdlUtils.java    From tracecompass with Eclipse Public License 2.0 2 votes vote down vote up
/**
 * Is the tree a unary integer
 *
 * @param node
 *            The node to check.
 * @return True if the given node is an unary integer.
 */
public static boolean isUnaryInteger(CommonTree node) {
    return ((node.getType() == CTFParser.UNARY_EXPRESSION_DEC) ||
            (node.getType() == CTFParser.UNARY_EXPRESSION_HEX) || (node.getType() == CTFParser.UNARY_EXPRESSION_OCT));
}