Source:
public void generator_expression() { setRootRule(PythonGrammar.TEST); ComprehensionExpression generator = (ComprehensionExpression) parse("(x*x for x in range(10))", treeMaker::expression); assertThat(generator.getKind()).isEqualTo(Tree.Kind.GENERATOR_EXPR); assertThat(generator.children()).hasSize(4); assertThat(((Token) generator.children().get(0)).value()).isEqualTo("("); assertThat(((Token) generator.children().get(3)).value()).isEqualTo(")"); assertThat(generator.firstToken().value()).isEqualTo("("); assertThat(generator.lastToken().value()).isEqualTo(")"); assertThat(generator.resultExpression().getKind()).isEqualTo(Tree.Kind.MULTIPLICATION); assertThat(generator.comprehensionFor().iterable().getKind()).isEqualTo(Tree.Kind.CALL_EXPR); setRootRule(PythonGrammar.CALL_EXPR); CallExpression call = (CallExpression) parse("foo(x*x for x in range(10))", treeMaker::expression); assertThat(call.arguments()).hasSize(1); Expression firstArg = call.arguments().get(0).expression(); assertThat(firstArg.getKind()).isEqualTo(Tree.Kind.GENERATOR_EXPR); call = (CallExpression) parse("foo((x*x for x in range(10)))", treeMaker::expression); assertThat(call.arguments()).hasSize(1); firstArg = call.arguments().get(0).expression(); assertThat(firstArg.getKind()).isEqualTo(Tree.Kind.GENERATOR_EXPR); try { parse("foo(1, x*x for x in range(10))", treeMaker::expression); fail("generator expression must be parenthesized unless it's the unique argument in arglist"); } catch (RecognitionException re) { assertThat(re).hasMessage("Parse error at line 1: Generator expression must be parenthesized if not sole argument."); } }

Target:
public void generator_expression() { setRootRule(PythonGrammar.TEST); ComprehensionExpression generator = (ComprehensionExpression) parse("(x*x for x in range(10))", treeMaker::expression); assertThat(generator.getKind()).isEqualTo(Tree.Kind.GENERATOR_EXPR); assertThat(generator.children()).hasSize(4); assertThat(generator.children().get(0)).isEqualTo(generator.firstToken()); assertThat(generator.children().get(3)).isEqualTo(generator.lastToken()); assertThat(generator.firstToken().value()).isEqualTo("("); assertThat(generator.lastToken().value()).isEqualTo(")"); assertThat(generator.resultExpression().getKind()).isEqualTo(Tree.Kind.MULTIPLICATION); assertThat(generator.comprehensionFor().iterable().getKind()).isEqualTo(Tree.Kind.CALL_EXPR); setRootRule(PythonGrammar.CALL_EXPR); CallExpression call = (CallExpression) parse("foo(x*x for x in range(10))", treeMaker::expression); assertThat(call.arguments()).hasSize(1); Expression firstArg = call.arguments().get(0).expression(); assertThat(firstArg.getKind()).isEqualTo(Tree.Kind.GENERATOR_EXPR); call = (CallExpression) parse("foo((x*x for x in range(10)))", treeMaker::expression); assertThat(call.arguments()).hasSize(1); firstArg = call.arguments().get(0).expression(); assertThat(firstArg.getKind()).isEqualTo(Tree.Kind.GENERATOR_EXPR); try { parse("foo(1, x*x for x in range(10))", treeMaker::expression); fail("generator expression must be parenthesized unless it's the unique argument in arglist"); } catch (RecognitionException re) { assertThat(re).hasMessage("Parse error at line 1: Generator expression must be parenthesized if not sole argument."); } }