List of usage examples for org.antlr.v4.runtime ParserRuleContext getStart
public Token getStart()
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) or * ranges of months (as numbers).//from w w w . j av a 2 s . c om * * @param filterPredicateFactoryClass * @param filterPredicateFactory * @param ctx * @param <T> * @return */ protected static <T> Predicate<T> createFilterPredicateForRangeOrListOfMonth( Class<? extends FilterPredicateFactory> filterPredicateFactoryClass, FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) { Predicate<T> filter = null; String fieldName = ctx.getStart().getText(); Set<Integer> months; if (hasStringList(ctx)) { months = mapStringsToCalendarMonths(extractStrings(invokeStringList(ctx).string())); filter = checkNegate(ctx, createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName, months.toArray(new Integer[months.size()]))); } else if (hasIntList(ctx)) { months = ImmutableSet.copyOf(extractIntegers(invokeIntList(ctx).INT())); filter = checkNegate(ctx, createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName, months.toArray(new Integer[months.size()]))); } else if (hasIntRange(ctx)) { int min = Integer.parseInt(invokeIntRange(ctx).INT(0).getText()); int max = Integer.parseInt(invokeIntRange(ctx).INT(1).getText()); filter = checkNegate(ctx, createFilterPredicateForRange(filterPredicateFactoryClass, filterPredicateFactory, fieldName, min, max)); } return filter; }
From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java
License:Apache License
/** * Create predicate for comparing weekdays (as numbers (monday = 1) or named in strings (in English)). * @param filterPredicateFactoryClass//ww w . j av a2s .c o m * @param filterPredicateFactory * @param ctx * @param <T> * @return */ protected static <T> Predicate<T> createFilterPredicateForComparisonOfWeekday( Class<? extends FilterPredicateFactory> filterPredicateFactoryClass, FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) { if (hasCompareTo(ctx)) { if (hasString(ctx)) { int dow = mapStringToCalendarDayOfWeek(extractString(invokeString(ctx))); String fieldName = ctx.getStart().getText(); String operator = invokeCompareTo(ctx).getText(); return createFilterPredicateForComparison(filterPredicateFactoryClass, filterPredicateFactory, fieldName, operator, dow); } 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 weekdays (as numbers (monday = 1) or named in strings (in * English))) or ranges of months (as numbers). * * @param filterPredicateFactoryClass//from w w w .j a v a2s .c o m * @param filterPredicateFactory * @param ctx * @param <T> * @return */ protected static <T> Predicate<T> createFilterPredicateForRangeOrListOfWeekday( Class<? extends FilterPredicateFactory> filterPredicateFactoryClass, FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) { Predicate<T> filter = null; String fieldName = ctx.getStart().getText(); Set<Integer> weekdays; if (hasStringList(ctx)) { weekdays = mapStringsToCalendarDaysOfWeek(extractStrings(invokeStringList(ctx).string())); filter = checkNegate(ctx, createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName, weekdays.toArray(new Integer[weekdays.size()]))); } else if (hasIntList(ctx)) { weekdays = ImmutableSet.copyOf(extractIntegers(invokeIntList(ctx).INT())); filter = checkNegate(ctx, createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName, weekdays.toArray(new Integer[weekdays.size()]))); } else if (hasIntRange(ctx)) { int min = Integer.parseInt(invokeIntRange(ctx).INT(0).getText()); int max = Integer.parseInt(invokeIntRange(ctx).INT(1).getText()); filter = checkNegate(ctx, createFilterPredicateForRange(filterPredicateFactoryClass, filterPredicateFactory, fieldName, min, max)); } return filter; }
From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java
License:Apache License
protected static <T> Predicate<T> createFilterPredicateForRangeOrListOfMessageReceiveTime( Class<? extends FilterPredicateFactory> filterPredicateFactoryClass, FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) { Predicate<T> filter = null; String filterPredicateFactoryMethodName = mapFieldTokenToFilterPredicateFactoryMethodName( ctx.getStart().getText()); try {/*from www .j a v a 2s.c o m*/ Method filterPredicateFactoryMethod = filterPredicateFactoryClass .getDeclaredMethod(filterPredicateFactoryMethodName, int.class, int.class); if (hasIntList(ctx)) { String fieldName = ctx.getStart().getText(); Set<Integer> intList = ImmutableSet.copyOf(extractIntegers(invokeIntList(ctx).INT())); filter = checkNegate(ctx, createFilterPredicateForList(filterPredicateFactoryClass, filterPredicateFactory, fieldName, intList.toArray(new Integer[intList.size()]))); } else if (hasIntRange(ctx)) { int min = Integer.parseInt(invokeIntRange(ctx).INT(0).getText()); int max = Integer.parseInt(invokeIntRange(ctx).INT(1).getText()); filter = (Predicate<T>) filterPredicateFactoryMethod.invoke(filterPredicateFactory, min, max); } } catch (NoSuchMethodException e) { e.printStackTrace(System.err); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return filter; }
From source file:dk.dma.ais.packet.ExpressionFilterParserBase.java
License:Apache License
/** * *//*from w ww . j a v a2 s. c o m*/ protected static <T> Predicate<T> createFilterPredicateForPositionWithin(Class filterPredicateFactoryClass, FilterPredicateFactory filterPredicateFactory, ParserRuleContext ctx) { Predicate<T> filter = null; Area area = null; if (hasBbox(ctx)) { List<ExpressionFilterParser.NumberContext> params = invokeBbox(ctx).number(); float latitude1 = Float.valueOf(params.get(0).getText()); float longitude1 = Float.valueOf(params.get(1).getText()); float latitude2 = Float.valueOf(params.get(2).getText()); float longitude2 = Float.valueOf(params.get(3).getText()); Position corner1 = Position.create(latitude1, longitude1); Position corner2 = Position.create(latitude2, longitude2); area = BoundingBox.create(corner1, corner2, CoordinateSystem.CARTESIAN); } else if (hasCircle(ctx)) { List<ExpressionFilterParser.NumberContext> params = invokeCircle(ctx).number(); float latitude = Float.valueOf(params.get(0).getText()); float longitude = Float.valueOf(params.get(1).getText()); float radius = Float.valueOf(params.get(2).getText()); area = new Circle(latitude, longitude, radius, CoordinateSystem.CARTESIAN); } if (area != null) { String filterPredicateFactoryMethodName = mapFieldTokenToFilterPredicateFactoryMethodName( ctx.getStart().getText()); try { Method filterPredicateFactoryMethod = filterPredicateFactoryClass .getDeclaredMethod(filterPredicateFactoryMethodName, Area.class); filter = (Predicate<T>) filterPredicateFactoryMethod.invoke(filterPredicateFactory, area); } catch (NoSuchMethodException e) { e.printStackTrace(System.err); } catch (InvocationTargetException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return filter; }
From source file:edu.clemson.cs.r2jt.absynnew.TreeBuildingVisitor.java
License:Open Source License
private InitFinalAST buildImplModuleInitFinal(ParserRuleContext ctx, List<ResolveParser.VariableDeclGroupContext> v, ParseTree requires, ParseTree ensures, Type t) { sanityCheckInitFinal(ctx);/*from w w w . j av a2 s .co m*/ List<VariableAST> variables = new ArrayList<VariableAST>(); for (ResolveParser.VariableDeclGroupContext grp : v) { variables.addAll(getAll(VariableAST.class, grp.Identifier())); } return new InitFinalAST(ctx.getStart(), ctx.getStop(), get(ExprAST.class, requires), get(ExprAST.class, ensures), variables, new ArrayList<StmtAST>(), t); }
From source file:edu.clemson.cs.r2jt.absynnew.TreeBuildingVisitor.java
License:Open Source License
/** * <p>Raises hell if the user provided more than a single module level * initialization or finalization section.</p> *//*from ww w . j a v a 2s.c om*/ private void sanityCheckInitFinal(ParserRuleContext ctx) { if (mySeenModuleInitFlag) { throw new SrcErrorException("only one module level initialization" + " section per module is permitted", ctx.getStart()); } if (mySeenModuleFinalFlag) { throw new SrcErrorException("only one module level finalization" + " section per module is permitted", ctx.getStart()); } }
From source file:edu.clemson.cs.rsrg.parsing.TreeBuildingListener.java
License:Open Source License
/** * <p>Create a location for the current parser rule * we are visiting.</p>//from w w w . j ava 2 s. co m * * @param ctx The visiting ANTLR4 parser rule. * * @return A {@link Location} for the rule. */ private Location createLocation(ParserRuleContext ctx) { return createLocation(ctx.getStart()); }
From source file:eric.bottard.tis100.AbstractTis100Compiler.java
License:Apache License
/** * Return the 0-based line number of a program construct from the * parsing context./* w ww .ja va 2s. c o m*/ */ protected int lineOf(ParserRuleContext ctx) { return ctx.getStart().getLine() - 1; }
From source file:illarion.easynpc.parser.Utils.java
License:Open Source License
@Nonnull private static <T extends Enum<T>> T getEnumValue(@Nullable ParserRuleContext node, @Nonnull Class<T> enumClass, @Nonnull T defaultValue) {/*w w w. ja v a 2s.c o m*/ if (node == null) { LOGGER.warn("Expected node for enumerator {} not found.", enumClass.getSimpleName()); return defaultValue; } String string = removeQuotes(node.getText()); try { return Enum.valueOf(enumClass, string); } catch (IllegalArgumentException e) { node.addErrorNode(node.getStart()); LOGGER.warn("Failed to resolve {} to enumerator {}", string, enumClass.getSimpleName()); return defaultValue; } }