Example usage for org.antlr.v4.runtime ParserRuleContext getStart

List of usage examples for org.antlr.v4.runtime ParserRuleContext getStart

Introduction

In this page you can find the example usage for org.antlr.v4.runtime ParserRuleContext getStart.

Prototype

public Token getStart() 

Source Link

Document

Get the initial token in this context.

Usage

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create a filter predicate with will compare the left-hand side to the right-hand side both assumed to be present
 * in the supplied ANTLR context.//from w  ww . jav  a2 s. co  m
 *
 * @param filterPredicateFactoryClass
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForComparison(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    Predicate<T> filter = null;
    String fieldName = ctx.getStart().getText();

    if (hasCompareTo(ctx)) {
        String operator = invokeCompareTo(ctx).getText();
        if (hasINT(ctx)) {
            Integer rhs = Integer.valueOf(invokeINT(ctx).getText());
            filter = createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory,
                    fieldName, operator, rhs);
        } else if (hasNumber(ctx)) {
            Float rhs = Float.valueOf(invokeNumber(ctx).getText());
            filter = createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory,
                    fieldName, operator, rhs);
        } else if (hasString(ctx)) {
            /* if (isNamedValue(fieldName)) {
                filter = createFilterPredicateForComparisonOfNamedValue(filterPredicateFactoryClass, filterPredicateFactory, ctx);
            } else { */
            ExpressionFilterParser.StringContext stringCtx = invokeString(ctx);
            if (stringCtx.number() != null) {
                Integer rhs = Integer.valueOf(extractString(stringCtx));
                filter = createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory,
                        fieldName, operator, rhs);
            } else if (stringCtx.STRING() != null) {
                String rhs = stringCtx.getText();
                filter = createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory,
                        fieldName, operator, rhs);
            }
            // }
        } else {
            throw new IllegalArgumentException();
        }
    }
    return filter;
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create a filter predicate with will test the left-side value assumed to be present
 * in the supplied ANTLR context against the list or range supplied on the right-hand side.
 *
 * @param filterPredicateFactoryClass//from w  w  w  . j  ava2  s.co  m
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForRangeOrList(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    Predicate<T> filter = null;
    String fieldName = ctx.getStart().getText();
    if (hasIntList(ctx)) {
        Integer[] ints = extractIntegers(invokeIntList(ctx).INT());
        filter = createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName,
                ints);
    } else if (hasStringList(ctx)) {
        String[] strings = extractStrings(invokeStringList(ctx).string());
        filter = createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName,
                strings);
    } else if (hasIntRange(ctx)) {
        int min = Integer.valueOf(invokeIntRange(ctx).INT().get(0).getText());
        int max = Integer.valueOf(invokeIntRange(ctx).INT().get(1).getText());
        filter = createFilterPredicateForRange(filterPredicateFactoryClass, filterPredicateFactory, fieldName,
                min, max);
    } else if (hasNumberRange(ctx)) {
        float min = Float.valueOf(invokeNumberRange(ctx).number().get(0).getText());
        float max = Float.valueOf(invokeNumberRange(ctx).number().get(1).getText());
        filter = createFilterPredicateForRange(filterPredicateFactoryClass, filterPredicateFactory, fieldName,
                min, max);
    } else {
        throw new IllegalArgumentException();
    }
    return checkNegate(ctx, filter);
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create predicate for comparisons of strings, including the LIKE operator.
 * @param filterPredicateFactoryClass/*from ww  w .  j av a2s.  c o m*/
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForStringComparison(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    Predicate<T> filter = null;
    if (hasString(ctx)) {
        String fieldName = ctx.getStart().getText();
        String string = invokeString(ctx).getText();
        if (hasCompareTo(ctx)) {
            ExpressionFilterParser.CompareToContext compareToContext = invokeCompareTo(ctx);
            String operator = compareToContext.getText();
            filter = createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory,
                    fieldName, operator, string);
        } else if (hasLIKE(ctx)) {
            filter = createFilterPredicateForMatch(filterPredicateFactoryClass, filterPredicateFactory,
                    fieldName, string);
        }
    }
    return filter;
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create predicate for comparison of ship types (as numbers or named in strings).
 * @param filterPredicateFactoryClass//from   ww w .j  a  v a 2 s .c o  m
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForComparisonOfShipType(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    if (hasCompareTo(ctx) && hasString(ctx)) {
        ExpressionFilterParser.StringContext rhsCtx = invokeString(ctx);
        if (rhsCtx.STRING() != null) {
            Set<Integer> shipTypes = getShipTypes(extractString(rhsCtx));
            String fieldName = ctx.getStart().getText();
            String operator = invokeCompareTo(ctx).getText();
            if (!"=".equals(operator)) {
                throw new IllegalArgumentException(
                        "Sorry, only '=' operator supported for comparison on named ship type.");
            }
            Predicate<T> filter = createFilterPredicateForList(filterPredicateFactoryClass,
                    filterPredicateFactory, fieldName, shipTypes.toArray(new Integer[shipTypes.size()]));
            return filter;
        } else {
            return createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory, ctx);
        }
    } else {
        throw new IllegalArgumentException(ctx.toStringTree());
    }
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create predicate for testing against lists of ship types (as numbers or named in strings).
 * @param filterPredicateFactoryClass//from   w  w  w .jav  a  2 s . c o  m
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForListOfShipType(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    if (hasStringList(ctx)) {
        Set<Integer> shipTypes = getShipTypes(extractStrings(invokeStringList(ctx).string()));
        String fieldName = ctx.getStart().getText();
        return createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName,
                shipTypes.toArray(new Integer[shipTypes.size()]));
    } else {
        return createFilterPredicateForRangeOrList(filterPredicateFactoryClass, filterPredicateFactory, ctx);
    }
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create a predicate for comparing navigational statuses (as numbers or named in strings).
 * @param filterPredicateFactoryClass/*from w  w w  .  j  av a  2 s  .c  o m*/
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForComparisonOfNavigationalStatus(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    if (hasCompareTo(ctx) && hasString(ctx)) {
        ExpressionFilterParser.StringContext rhsCtx = invokeString(ctx);
        if (rhsCtx.STRING() != null) {
            Set<Integer> navstats = getNavigationalStatuses(new String[] { extractString(rhsCtx) });
            String fieldName = ctx.getStart().getText();
            String operator = invokeCompareTo(ctx).getText();
            if (!operator.equals("=")) {
                throw new IllegalArgumentException(
                        "Sorry, only '=' operator supported for comparison on named navigational status.");
            }
            return createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName,
                    navstats.toArray(new Integer[navstats.size()]));
        } else {
            return createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory, ctx);
        }
    } else {
        throw new IllegalArgumentException(ctx.toStringTree());
    }
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create predicate for testing against lists of navigational statuses (as numbers or named in strings).
 * @param filterPredicateFactoryClass//from   w  ww . j av a 2s. com
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForListOfNavigationalStatus(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    if (hasStringList(ctx)) {
        Set<Integer> navstats = getNavigationalStatuses(extractStrings(invokeStringList(ctx).string()));
        String fieldName = ctx.getStart().getText();
        return createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName,
                navstats.toArray(new Integer[navstats.size()]));
    } else {
        return createFilterPredicateForRangeOrList(filterPredicateFactoryClass, filterPredicateFactory, ctx);
    }
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create predicate for testing against lists of countries.
 * @param filterPredicateFactoryClass//  ww  w  .  j av a 2s.c  om
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForListOfCountry(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    if (hasStringList(ctx)) {
        Country[] countries = getCountries(extractStrings(invokeStringList(ctx).string()));
        String fieldName = ctx.getStart().getText();
        return checkNegate(ctx, createFilterPredicateForList(filterPredicateFactoryClass,
                filterPredicateFactory, fieldName, countries));
    } else {
        throw new IllegalArgumentException(ctx.toStringTree());
    }
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create predicate for testing against lists of source types.
 * @param filterPredicateFactoryClass//  w  w w .jav  a2  s.  com
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForListOfSourceType(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    if (hasStringList(ctx)) {
        SourceType[] sourceTypes = getSourceTypes(extractStrings(invokeStringList(ctx).string()));
        String fieldName = ctx.getStart().getText();
        return checkNegate(ctx, createFilterPredicateForList(filterPredicateFactoryClass,
                filterPredicateFactory, fieldName, sourceTypes));
    } else {
        throw new IllegalArgumentException(ctx.toStringTree());
    }
}

From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java

License:Apache License

/**
 * Create predicate for comparing months (as numbers or named in strings).
 * @param filterPredicateFactoryClass//  w w w.  java  2  s .co  m
 * @param filterPredicateFactory
 * @param ctx
 * @param <T>
 * @return
 */
protected static <T> Predicate<T> createFilterPredicateForComparisonOfMonth(
        Class<? extends FilterPredicateFactory> filterPredicateFactoryClass,
        FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) {
    if (hasCompareTo(ctx)) {
        if (hasString(ctx)) {
            int month = mapStringToCalendarMonth(extractString(invokeString(ctx)));
            String fieldName = ctx.getStart().getText();
            String operator = invokeCompareTo(ctx).getText();
            return createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory,
                    fieldName, operator, month);
        } else {
            return createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory, ctx);
        }
    } else {
        throw new IllegalArgumentException(ctx.toStringTree());
    }
}