Git Product home page Git Product logo

Comments (13)

HerringtonDarkholme avatar HerringtonDarkholme commented on September 25, 2024 2

I have added --debug-query=ast in the CLI to dump tree-sitter output.

image

from ast-grep.

HerringtonDarkholme avatar HerringtonDarkholme commented on September 25, 2024 1

Hi! Thanks for the report! I don't have time to give it a look at the moment but I will give it back as soon as possible!
Thank you for the patience!

from ast-grep.

HerringtonDarkholme avatar HerringtonDarkholme commented on September 25, 2024 1

Thanks 👍 Having a cli debug output will be very helpful.
I will implement it in near future

from ast-grep.

HerringtonDarkholme avatar HerringtonDarkholme commented on September 25, 2024 1

thanks for the suggestion. But a rule YAML usually has many patterns and displaying them in one command is not straightforward. For example, there may be multiple patterns but only one pattern is interested. Using the playground has a better debugging experience.

from ast-grep.

HerringtonDarkholme avatar HerringtonDarkholme commented on September 25, 2024

The issue is

@Serializable data class $$$

is not parsed as class constructor in tree-sitter-kotlin 0.3.6

See the official parser page https://fwcd.dev/tree-sitter-kotlin/

image

However, ast-grep playground is using older kotlin parser so it somehow works.

UPDATE: I have updated the tree-sitter-kotlin wasm on playground

from ast-grep.

HerringtonDarkholme avatar HerringtonDarkholme commented on September 25, 2024

In the case of tree-sitter parser cannot parse your pattern, using relational + kind rule is easier.
See this example

image

I also tested it on command line it works across different parser versions.
image

from ast-grep.

orangain avatar orangain commented on September 25, 2024

@HerringtonDarkholme Thank you for your prompt response! I was able to extract the class parameters as intended with the rules you suggested.

The issue is

@Serializable data class $$$

is not parsed as class constructor in tree-sitter-kotlin 0.3.6

However, I am really sorry, but I feel that this explanation does not accurately describe the cause. Because the CLI does not show any parsing errors for pattern, and it seems natural that tree-sitter Kotlin cannot parse code with meta variables as it is.

Upon further reflection after receiving the response, I realized that I could make the reproducible code much smaller. In the example below, the relational rule is no longer used.

Still, the situation remains the same: no match in the CLI, but a match in the playground. Is there any other possible cause?


⏯ Playground Link

Playground link with relevant code

💻 Code

Code (Unchanged from the original):

package com.example

import kotlinx.serialization.Serializable
import java.util.UUID

@Serializable
data class Annotated(
    val a: Int,
    val b: UUID,
)

data class NotAnnotated(
    val a: Int,
    val b: UUID,
)

Pattern:

id: serializable-class
language: kotlin
rule:
  pattern: |
    @Serializable
    data class $$$

🙁 Actual behavior

The CLI does not match.

image

🙂 Expected behavior

The CLI should match with the annotated class as the playground does.

image

from ast-grep.

orangain avatar orangain commented on September 25, 2024

I have updated the title of the issue. My main concern is the difference between CLI and playground. Because such a difference would make rule development more difficult.

If the difference is due to differences in library versions, it would be nice if either of the following could be accomplished.

  • Both the CLI and the playground show the version of the ast-grep and depending libraries including the tree-sitter so that a user can notice the difference of the versions.
  • The CLI provides something like a debug mode that shows AST structure of the pattern and the input code so that a user can notice the difference of the parsed structure.

from ast-grep.

HerringtonDarkholme avatar HerringtonDarkholme commented on September 25, 2024

One more thing is to note is the behavior of treesitter.
At most time web and native tree sitter behave the same. However, when the input is not valid, which is commonly seen in pattern, web and native will behave differently due to tree sitter heuristics.

TS will try to recover from parsing error and the recovery is based on the input length. Sadly, web and native tree sitter have different string encoding so their length are different.

This makes different parsing results

from ast-grep.

StevenLove avatar StevenLove commented on September 25, 2024

That is a fascinating explanation for why tree sitter has different behavior in a browser vs natively.
Here's an example of tree sitter behaving differently for strings of different lengths:

GIF of tree sitter behavior

from ast-grep.

orangain avatar orangain commented on September 25, 2024

@HerringtonDarkholme @StevenLove Thank you for clarifying the difference between web and native. I understand that the parsing result depends on the length of the input.

I am interested in the different encodings that affect the length of the input. Personally, if there is an option to use the same encoding in the CLI and playground, I would prefer to use that, even if it slows down the execution. This is because I often refer to the playground results when developing ast-grep rules, and I consider the consistency of the results to be important.

from ast-grep.

HerringtonDarkholme avatar HerringtonDarkholme commented on September 25, 2024

Related documentation https://ast-grep.github.io/reference/cli/run.html#debug-query-format

from ast-grep.

orangain avatar orangain commented on September 25, 2024

@HerringtonDarkholme Thank you for adding the debugging feature so quickly! It will be useful for debugging patterns.

If possible, the same flag for the sg scan command would also make it easier to debug when we write the pattern in a yaml file.

from ast-grep.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.