@Override public ConstantExpression visitIntegerLiteralAlt(IntegerLiteralAltContext ctx) { String text = ctx.IntegerLiteral().getText(); Number num = null; try { num = Numbers.parseInteger(null, text); } catch (Exception e) { this.numberFormatError = new Tuple2<>(ctx, e); } ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR)); constantExpression.putNodeMetaData(IS_NUMERIC, true); constantExpression.putNodeMetaData(INTEGER_LITERAL_TEXT, text); return configureAST(constantExpression, ctx); }
@Override public ConstantExpression visitFloatingPointLiteralAlt(FloatingPointLiteralAltContext ctx) { String text = ctx.FloatingPointLiteral().getText(); Number num = null; try { num = Numbers.parseDecimal(text); } catch (Exception e) { this.numberFormatError = new Tuple2<>(ctx, e); } ConstantExpression constantExpression = new ConstantExpression(num, !text.startsWith(SUB_STR)); constantExpression.putNodeMetaData(IS_NUMERIC, true); constantExpression.putNodeMetaData(FLOATING_POINT_LITERAL_TEXT, text); return configureAST(constantExpression, ctx); }
@Override public List<Tuple2<String, Expression>> visitElementValues(ElementValuesContext ctx) { if (!asBoolean(ctx)) { return Collections.emptyList(); } List<Tuple2<String, Expression>> annotationElementValues = new LinkedList<>(); if (asBoolean(ctx.elementValuePairs())) { this.visitElementValuePairs(ctx.elementValuePairs()).forEach((key, value) -> annotationElementValues.add(new Tuple2<>(key, value))); } else if (asBoolean(ctx.elementValue())) { annotationElementValues.add(new Tuple2<>(VALUE_STR, this.visitElementValue(ctx.elementValue()))); } return annotationElementValues; }
private void breadthFirstRest(boolean preorder, int level, Closure c) { Stack<Tuple2<Object, Integer>> stack = new Stack<Tuple2<Object, Integer>>(); List nextLevelChildren = preorder ? getDirectChildren() : DefaultGroovyMethods.reverse(getDirectChildren()); while (!nextLevelChildren.isEmpty()) { List working = new NodeList(nextLevelChildren); nextLevelChildren = new NodeList(); for (Object child : working) { if (preorder) { callClosureForNode(c, child, level); } else { stack.push(new Tuple2<Object, Integer>(child, level)); } if (child instanceof Node) { Node childNode = (Node) child; List children = childNode.getDirectChildren(); if (children.size() > 1 || (children.size() == 1 && !(children.get(0) instanceof String))) nextLevelChildren.addAll(preorder ? children : DefaultGroovyMethods.reverse(children)); } } level++; } while (!stack.isEmpty()) { Tuple2<Object, Integer> next = stack.pop(); callClosureForNode(c, next.getFirst(), next.getSecond()); } }
public List<Tuple2<JsonEvent, JsonEvent>> indexRequests() { System.out.println("Size: " + events.size()); List<Tuple2<JsonEvent, JsonEvent>> indexEvents = new ArrayList<>(); for (int i = 0; i < events.size(); i++) { indexEvents.add(new Tuple2<>(events.get(i), events.get(i+1))); i++; } System.out.println("Return size: " + indexEvents.size()); return indexEvents; }
public static Tuple2<Integer, Integer> endPosition(Token token) { String stopText = token.getText(); int stopTextLength = 0; int newLineCnt = 0; if (null != stopText) { stopTextLength = stopText.length(); newLineCnt = (int) StringUtils.countChar(stopText, '\n'); } if (0 == newLineCnt) { return new Tuple2<>(token.getLine(), token.getCharPositionInLine() + 1 + token.getText().length()); } else { // e.g. GStringEnd contains newlines, we should fix the location info return new Tuple2<>(token.getLine() + newLineCnt, stopTextLength - stopText.lastIndexOf('\n')); } }
@Override public ForStatement visitForStmtAlt(ForStmtAltContext ctx) { Tuple2<Parameter, Expression> controlTuple = this.visitForControl(ctx.forControl()); Statement loopBlock = this.unpackStatement((Statement) this.visit(ctx.statement())); return configureAST( new ForStatement(controlTuple.getFirst(), controlTuple.getSecond(), asBoolean(loopBlock) ? loopBlock : EmptyStatement.INSTANCE), ctx); }
@Override public Tuple2<Parameter, Expression> visitForControl(ForControlContext ctx) { if (asBoolean(ctx.enhancedForControl())) { // e.g. for(int i in 0..<10) {} return this.visitEnhancedForControl(ctx.enhancedForControl()); } if (asBoolean(ctx.classicalForControl())) { // e.g. for(int i = 0; i < 10; i++) {} return this.visitClassicalForControl(ctx.classicalForControl()); } throw createParsingFailedException("Unsupported for control: " + ctx.getText(), ctx); }
@Override public Tuple2<Parameter, Expression> visitEnhancedForControl(EnhancedForControlContext ctx) { Parameter parameter = configureAST( new Parameter(this.visitType(ctx.type()), this.visitVariableDeclaratorId(ctx.variableDeclaratorId()).getName()), ctx.variableDeclaratorId()); // FIXME Groovy will ignore variableModifier of parameter in the for control // In order to make the new parser behave same with the old one, we do not process variableModifier* return new Tuple2<>(parameter, (Expression) this.visit(ctx.expression())); }
@Override public Tuple2<Parameter, Expression> visitClassicalForControl(ClassicalForControlContext ctx) { ClosureListExpression closureListExpression = new ClosureListExpression(); closureListExpression.addExpression(this.visitForInit(ctx.forInit())); closureListExpression.addExpression(asBoolean(ctx.expression()) ? (Expression) this.visit(ctx.expression()) : EmptyExpression.INSTANCE); closureListExpression.addExpression(this.visitForUpdate(ctx.forUpdate())); return new Tuple2<>(ForStatement.FOR_LOOP_DUMMY, closureListExpression); }
@Override public Tuple2<Token, Expression> visitSwitchLabel(SwitchLabelContext ctx) { if (asBoolean(ctx.CASE())) { return new Tuple2<>(ctx.CASE().getSymbol(), (Expression) this.visit(ctx.expression())); } else if (asBoolean(ctx.DEFAULT())) { return new Tuple2<>(ctx.DEFAULT().getSymbol(), EmptyExpression.INSTANCE); } throw createParsingFailedException("Unsupported switch label: " + ctx.getText(), ctx); }
@Override public AnnotationNode visitAnnotation(AnnotationContext ctx) { String annotationName = this.visitAnnotationName(ctx.annotationName()); AnnotationNode annotationNode = new AnnotationNode(ClassHelper.make(annotationName)); List<Tuple2<String, Expression>> annotationElementValues = this.visitElementValues(ctx.elementValues()); annotationElementValues.forEach(e -> annotationNode.addMember(e.getFirst(), e.getSecond())); return configureAST(annotationNode, ctx); }
@Override public Map<String, Expression> visitElementValuePairs(ElementValuePairsContext ctx) { return ctx.elementValuePair().stream() .map(this::visitElementValuePair) .collect(Collectors.toMap( Tuple2::getFirst, Tuple2::getSecond, (k, v) -> { throw new IllegalStateException(String.format("Duplicate key %s", k)); }, LinkedHashMap::new )); }
private static Tuple2<Throwable, Integer> create(Throwable t, int attempt) { return new Tuple2<>(t, attempt); }
public static <T extends ASTNode> void configureEndPosition(T astNode, Token token) { Tuple2<Integer, Integer> endPosition = endPosition(token); astNode.setLastLineNumber(endPosition.getFirst()); astNode.setLastColumnNumber(endPosition.getSecond()); }
@Override @SuppressWarnings({"unchecked"}) public List<Statement> visitSwitchBlockStatementGroup(SwitchBlockStatementGroupContext ctx) { int labelCnt = ctx.switchLabel().size(); List<Token> firstLabelHolder = new ArrayList<>(1); return (List<Statement>) ctx.switchLabel().stream() .map(e -> (Object) this.visitSwitchLabel(e)) .reduce(new ArrayList<Statement>(4), (r, e) -> { List<Statement> statementList = (List<Statement>) r; Tuple2<Token, Expression> tuple = (Tuple2<Token, Expression>) e; boolean isLast = labelCnt - 1 == statementList.size(); switch (tuple.getFirst().getType()) { case CASE: { if (!asBoolean(statementList)) { firstLabelHolder.add(tuple.getFirst()); } statementList.add( configureAST( new CaseStatement( tuple.getSecond(), // check whether processing the last label. if yes, block statement should be attached. isLast ? this.visitBlockStatements(ctx.blockStatements()) : EmptyStatement.INSTANCE ), firstLabelHolder.get(0))); break; } case DEFAULT: { BlockStatement blockStatement = this.visitBlockStatements(ctx.blockStatements()); blockStatement.putNodeMetaData(IS_SWITCH_DEFAULT, true); statementList.add( // this.configureAST(blockStatement, tuple.getKey()) blockStatement ); break; } } return statementList; }); }
@Override public Tuple2<String, Expression> visitElementValuePair(ElementValuePairContext ctx) { return new Tuple2<>(ctx.elementValuePairName().getText(), this.visitElementValue(ctx.elementValue())); }
public Tuple2<E, Integer> next() { if (!hasNext()) throw new NoSuchElementException(); return new Tuple2<E, Integer>(delegate.next(), index++); }
public Tuple2<Integer, E> next() { if (!hasNext()) throw new NoSuchElementException(); return new Tuple2<Integer, E>(index++, delegate.next()); }
/** * Zips an Iterable with indices in (value, index) order. * <p/> * Example usage: * <pre class="groovyTestCase"> * assert [["a", 0], ["b", 1]] == ["a", "b"].withIndex() * assert ["0: a", "1: b"] == ["a", "b"].withIndex().collect { str, idx -> "$idx: $str" } * </pre> * * @param self an Iterable * @return a zipped list with indices * @see #indexed(Iterable) * @since 2.4.0 */ public static <E> List<Tuple2<E, Integer>> withIndex(Iterable<E> self) { return withIndex(self, 0); }
/** * Zips an Iterable with indices in (value, index) order. * <p/> * Example usage: * <pre class="groovyTestCase"> * assert [["a", 5], ["b", 6]] == ["a", "b"].withIndex(5) * assert ["1: a", "2: b"] == ["a", "b"].withIndex(1).collect { str, idx -> "$idx: $str" } * </pre> * * @param self an Iterable * @param offset an index to start from * @return a zipped list with indices * @see #indexed(Iterable, int) * @since 2.4.0 */ public static <E> List<Tuple2<E, Integer>> withIndex(Iterable<E> self, int offset) { return toList(withIndex(self.iterator(), offset)); }
/** * Zips an iterator with indices in (value, index) order. * <p/> * Example usage: * <pre class="groovyTestCase"> * assert [["a", 0], ["b", 1]] == ["a", "b"].iterator().withIndex().toList() * assert ["0: a", "1: b"] == ["a", "b"].iterator().withIndex().collect { str, idx -> "$idx: $str" }.toList() * </pre> * * @param self an iterator * @return a zipped iterator with indices * @see #indexed(Iterator) * @since 2.4.0 */ public static <E> Iterator<Tuple2<E, Integer>> withIndex(Iterator<E> self) { return withIndex(self, 0); }
/** * Zips an iterator with indices in (index, value) order. * <p/> * Example usage: * <pre class="groovyTestCase"> * assert [[0, "a"], [1, "b"]] == ["a", "b"].iterator().indexed().collect{ tuple -> [tuple.first, tuple.second] } * assert ["0: a", "1: b"] == ["a", "b"].iterator().indexed().collect { idx, str -> "$idx: $str" }.toList() * </pre> * * @param self an iterator * @return a zipped iterator with indices * @see #withIndex(Iterator) * @since 2.4.0 */ public static <E> Iterator<Tuple2<Integer, E>> indexed(Iterator<E> self) { return indexed(self, 0); }
/** * Zips an iterator with indices in (value, index) order. * <p/> * Example usage: * <pre class="groovyTestCase"> * assert [["a", 5], ["b", 6]] == ["a", "b"].iterator().withIndex(5).toList() * assert ["1: a", "2: b"] == ["a", "b"].iterator().withIndex(1).collect { str, idx -> "$idx: $str" }.toList() * </pre> * * @param self an iterator * @param offset an index to start from * @return a zipped iterator with indices * @see #indexed(Iterator, int) * @since 2.4.0 */ public static <E> Iterator<Tuple2<E, Integer>> withIndex(Iterator<E> self, int offset) { return new ZipPostIterator<E>(self, offset); }
/** * Zips an iterator with indices in (index, value) order. * <p/> * Example usage: * <pre class="groovyTestCase"> * assert [[5, "a"], [6, "b"]] == ["a", "b"].iterator().indexed(5).toList() * assert ["a: 1", "b: 2"] == ["a", "b"].iterator().indexed(1).collect { idx, str -> "$str: $idx" }.toList() * </pre> * * @param self an iterator * @param offset an index to start from * @return a zipped iterator with indices * @see #withIndex(Iterator, int) * @since 2.4.0 */ public static <E> Iterator<Tuple2<Integer, E>> indexed(Iterator<E> self, int offset) { return new ZipPreIterator<E>(self, offset); }