kotlinx / ast

Generic AST parsing library for kotlin multiplatform
Apache License 2.0
326 stars 23 forks source link

ParseCancellationException - string interpolated functions #31

Closed arkadiuszpalka closed 3 years ago

arkadiuszpalka commented 3 years ago

Hi, I'm not sure that is it ast library issue but I've got following file:

package com.example.app.ui

internal class FeatureFragment :
    BaseFragment<FeatureViewModel, FeatureFragmentBinding>() {

    private fun handleViewStateChanges(viewState: FeatureViewModel.ViewState): Unit =
        with(
            requireBinding
        ) {
            dateRangePropertyView.text =
                "${monthDayYearDateFormat.format(viewState.dateRange.startDate)} - ${
                    monthDayYearDateFormat.format(viewState.dateRange.endDate)
                }"
        }
}

I'm testing the file as usual:

kotlinFile.summary(attachRawAst = false)
    .onSuccess { astList ->
        astList.forEach(Ast::print)
    }.onFailure { errors ->
        errors.forEach(::println)
    }

and parser throws an exception:

Exception in thread "main" org.antlr.v4.kotlinruntime.misc.ParseCancellationException at kotlinx.ast.parser.antlr.kotlin.AntlrKotlinErrorListener.syntaxError(AntlrKotlinErrorListener.kt:17) at org.antlr.v4.kotlinruntime.ProxyErrorListener.syntaxError(ProxyErrorListener.kt:34) at org.antlr.v4.kotlinruntime.Parser.notifyErrorListeners(Parser.kt:564) at org.antlr.v4.kotlinruntime.DefaultErrorStrategy.reportUnwantedToken(DefaultErrorStrategy.kt:376) at org.antlr.v4.kotlinruntime.DefaultErrorStrategy.sync(DefaultErrorStrategy.kt:271) at kotlinx.ast.grammar.kotlin.target.antlr.kotlin.generated.KotlinParser.kotlinFile(KotlinParser.kt:1108) at kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParserExtractor$extractor$1.invoke(KotlinGrammarAntlrKotlinParserAstParserType.kt:18) at kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParserExtractor$extractor$1.invoke(KotlinGrammarAntlrKotlinParserAstParserType.kt:9) at kotlinx.ast.common.AstParserExtractor$DefaultImpls.extract(AstParserExtractor.kt:5) at kotlinx.ast.parser.antlr.kotlin.AntlrKotlinParserExtractor$DefaultImpls.extract(AntlrKotlinParserExtractor.kt) at kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParserExtractor.extract(KotlinGrammarAntlrKotlinParserAstParserType.kt:9) at kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParserExtractor.extract(KotlinGrammarAntlrKotlinParserAstParserType.kt:9) at kotlinx.ast.parser.antlr.kotlin.AntlrKotlinAstParserImplKt.antlrKotlinParser(AntlrKotlinAstParserImpl.kt:165) at kotlinx.ast.parser.antlr.kotlin.AntlrKotlinAstParserImplKt.antlrKotlinParser(AntlrKotlinAstParserImpl.kt:121) at kotlinx.ast.parser.antlr.kotlin.AntlrKotlinParser.parse(AntlrKotlinParser.kt:20) at kotlinx.ast.grammar.kotlin.common.KotlinGrammarParser$DefaultImpls.parseKotlinFile(KotlinGrammarParser.kt:9) at kotlinx.ast.grammar.kotlin.target.antlr.kotlin.KotlinGrammarAntlrKotlinParser.parseKotlinFile(KotlinGrammarAntlrKotlinParser.kt:10) at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated$ScratchFileRunnerGenerated.(tmp.kt:22) at org.jetbrains.kotlin.idea.scratch.generated.ScratchFileRunnerGenerated.main(tmp.kt:33)

The problem has to be the line break after ${ because, without that lines, the execution goes smooth

            dateRangePropertyView.text =
                "${monthDayYearDateFormat.format(viewState.dateRange.startDate)} - ${
                    monthDayYearDateFormat.format(viewState.dateRange.endDate)
                }"

Library version

com.github.kotlinx.ast:grammar-kotlin-parser-antlr-kotlin-jvm:a96e681f90

drieks commented 3 years ago

Hi @arkadiuszpalka, I will check this. Thank you for reporting it!

drieks commented 3 years ago

Hi @arkadiuszpalka,

you found an error in the kotlin antlr grammar file. should be working now (in version https://github.com/kotlinx/ast/commit/0b853e90aa412f21567626b069747b654956261a)

drieks commented 3 years ago

There is a build problem, https://github.com/kotlinx/ast/commit/0b853e90aa412f21567626b069747b654956261a is not working, sorry. I will fix this in the next few days.

drieks commented 3 years ago

Hi @arkadiuszpalka, please try version 57556f1603, it should work now.

arkadiuszpalka commented 3 years ago

@drieks Hi, thank you for your involvement! I will check it out and give a feedback.

arkadiuszpalka commented 3 years ago

It's working and I'm closing the issue.