question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parse error: Muliple annotations with line comment

See original GitHub issue

That code can not be parsed:

@Suppress("unused") //
@JsName("daName")
fun myname(){
....

If the both annotations are switched, it works just fine.

–> The exception occurs if:

  • there are two annotations
  • there is a line comment between these two annotations

Stack trace: ` ava.lang.IllegalStateException: Unrecognized modifier: // public API

at kastree.ast.psi.Converter$convertModifiers$1.invoke(Converter.kt:367)
at kastree.ast.psi.Converter$convertModifiers$1.invoke(Converter.kt:16)
at kotlin.sequences.TransformingSequence$iterator$1.next(Sequences.kt:172)
at kotlin.sequences.FilteringSequence$iterator$1.calcNext(Sequences.kt:132)
at kotlin.sequences.FilteringSequence$iterator$1.hasNext(Sequences.kt:156)
at kotlin.sequences.SequencesKt___SequencesKt.toCollection(_Sequences.kt:702)
at kotlin.sequences.SequencesKt___SequencesKt.toMutableList(_Sequences.kt:732)
at kotlin.sequences.SequencesKt___SequencesKt.toList(_Sequences.kt:723)
at kastree.ast.psi.Converter.convertModifiers(Converter.kt:371)
at kastree.ast.psi.Converter.convertModifiers(Converter.kt:350)
at kastree.ast.psi.Converter.convertFunc(Converter.kt:304)
at kastree.ast.psi.Converter.convertDecl(Converter.kt:195)
at kastree.ast.psi.Converter.convertFile(Converter.kt:293)
at kastree.ast.psi.Parser.parseFile(Parser.kt:23)
at kastree.ast.psi.Parser.parseFile$default(Parser.kt:23)

`

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
martinflorekcommented, Oct 18, 2019

@drieks Now the parsing of “escaped” values in annotations work, but I have found another issue. It does not belong here, so I created a new issue in your repo https://github.com/kotlinx/ast/issues/1

@cretz I still prefer your parser, especially its speed, and it handles everything I have thrown at it except for the comments after annotations. Thank you for this library.

1reaction
driekscommented, Oct 15, 2019

Hi @martinflorek,

maybe my Kotlin parsing library can help you: https://github.com/kotlinx/ast

It is a generic AST parsing library, Kotlin is currently the only supported language. The library is designed that other languages can be easily added. kotlinx.ast does not use the kotlin compiler for paring, it is using ANTLR (the kotlin variant https://github.com/Strumenta/antlr-kotlin) using the official kotlin grammer (https://kotlinlang.org/docs/reference/grammar.html)

One Component is “Klass”, a collection of language independent data classes used to represent and easily access the AST.

Your example code will be parsed fine: https://github.com/kotlinx/ast/commit/d4da88af8bdbcc9d4b5f2a125ee0b985301905e0

  • MultiAnnotationsWithLineComment.kt.txt contains the source of your example
  • MultiAnnotationsWithLineComment.raw.txt contains the raw AST
  • MultiAnnotationsWithLineComment.summary.txt contains the “Klass” Representation you can use:
PackageHeader()
importList
KlassDeclaration(fun parse)
  KlassAnnotation(Annotation1)
    KlassArgument()
      KlassString
        "abc"
  KlassAnnotation(Annotation2)
    KlassArgument()
      KlassString
        "123"

using kotlin code, this is:

listOf(
  PackageHeader(),
  DefaultAstNode("importList"),
  KlassDeclaration(
    keyword = "fun",
    identifier = KlassIdentifier("parse"),
    annotations = listOf(
      KlassAnnotation(KlassIdentifier("Annotation1")),
      KlassAnnotation(KlassIdentifier("Annotation2"))
    )
)

There are some parts missing, for example the importList is not converted into an easy-to-use data class. But I’m already using the library for AST parsing. You can just open the project in IDEA or run ./gradlew clean check publishToMavenLocal if you want to use it locally.

Please let me know if you have any questions, or just open an issue 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Multiple annotations found at this line" - Stack Overflow
Multiple annotations found at this line: - error: Error parsing XML: not well-formed (invalid token) - The value of attribute "android:text" ...
Read more >
175193 – "Multiple annotations found at this line" error ... - Bugs
Bug 175193 - "Multiple annotations found at this line" error, even when syntax ... You need to log in before you can comment...
Read more >
Support multi-line # type: comments for functions #1102 - GitHub
The code works by converting Python 2.7 per-argument type comments to Python 3 annotations in the typed_ast's conversion module -- this allows ...
Read more >
What is the correct annotation syntax in Polyspace Bug Finder ...
I talked with a field application engineer for Polyspace and they confirmed that multi-line annotations are not valid with the current releases of...
Read more >
Appendix: YAML Techniques - Helm
Folded Multi-line Strings. Sometimes you want to represent a string in your YAML with multiple lines, but want it to be treated as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found