Construct graphs from parsed source code

Overview

tree-sitter-graph

The tree-sitter-graph library defines a DSL for constructing arbitrary graph structures from source code that has been parsed using tree-sitter.

This package can be used either as a library or command-line program.

To use it as a library, add the following to your Cargo.toml:

[dependencies]
tree-sitter-graph = "0.2"

To use it as a program, install it via cargo install:

$ cargo install tree-sitter-graph

Check out our documentation for more details.

Comments
  • Add control statements and improve handling of pattern suffixes

    Add control statements and improve handling of pattern suffixes

    This PR introduces control statements for conditionals and iteration, and improves the handling of patterns with suffixes indicating list or optional outputs.

    Can be reviewed commit by commit.

    opened by hendrikvanantwerpen 7
  • Pass globals as command-line options

    Pass globals as command-line options

    • [x] Fixes #76.
    • ~~How should types other than String be handled?~~ See #86.
    • ~~String::split_once() seems not to match when the rhs (after the delimiter) is empty. How should we handle empty globals, e.g. empty strings?~~ See #87.
    opened by robrix 4
  • Example TSG files for languages?

    Example TSG files for languages?

    I tried piecing together a python tsg file from https://docs.rs/tree-sitter-graph/0.5.1/tree_sitter_graph/reference/ but I get errors:

    Caused by:
        0: Invalid query pattern: [0, "string", 0, #true, @id]
            ^
        1: Query error at 7:2. Invalid syntax:
           [0, "string", 0, #true, @id]```
    
    Is there a full tsg file available for any languages to make it a bit easier to get started testing out functionality?
    opened by InTheCloudDan 4
  • Support capture existence testing and simple control flow

    Support capture existence testing and simple control flow

    Adds support for if statements and a prefix operator ? that can be used to test for the presence of a given capture.

    Thus, a stanza like

    (module . (expression_statement)? @first) @root
    {
      node @root.node
      if ?@first {
        attr (@root.node) first_expr = @first
      }
    }
    

    will set the first_expr attribute only in the case where the first statement in the input is in fact an expression.

    In addition to this, this PR also adds functions for the usual boolean operators and, or, and not.

    Can be reviewed commit-by-commit.

    opened by tausbn 4
  • Graph nodes not implicitly linked to syntax nodes

    Graph nodes not implicitly linked to syntax nodes

    This is an alternative version of #1 that doesn't use the "tag name" mechanism to implicitly attach each graph node to exactly one syntax node. Instead you can just create graph nodes whenever you want, and use the existing "scoped variables" mechanism to attach them to syntax nodes whenever you want.

    opened by dcreager 4
  • Bad error message (and possible execution bug?) with typo

    Bad error message (and possible execution bug?) with typo

    The .tsg below has two bugs:

    1. Typo on line 3 (@ehre should be @here)
    2. Use of @path.context on line 10 which is never defined on that syntax node
    [
      (struct_item name: (_) @here)
      (enum_item name: (_) @ehre) ;; oops, typo
    ] {
      let @here.context = "%pattern"
    }
    
    (scoped_type_identifier) @path {
      node s
      attr (s) symbol = @path.context ;; oops, not defined
    }
    

    I ran it on this test case.

    pub enum A {
        M(std::string::String),
    }
    

    Expected behavior: Either some error message for bug 1, or the usual message for bug 2 (Undefined scoped variable [syntax node scoped_identifier (2, 7)].context)

    Actual behavior:

    $ ./bin/tree-sitter-stack-graphs test --grammar . --tsg ./bug.tsg ./testcase.rs
    Error: Error running test ./testcase.rs
    
    Caused by:
        0: Executing attr ((load 1)) symbol = (scoped [syntax node scoped_type_identifier (2, 7)] 'context) at (10, 3)
        1: Expected a syntax node  got #null
    

    This seems like a bug to me because I can't think of any legit way for #null to get into the computation at line 10.

    To reproduce:

    git clone [email protected]:jorendorff/tree-sitter-rust.git
    cd tree-sitter-rust
    git checkout jorendorff/internal-error
    script/setup
    ./bin/tree-sitter-stack-graphs test --grammar . --tsg ./bug.tsg ./testcase.rs
    
    opened by jorendorff 3
  • relax parse error behavior

    relax parse error behavior

    • Improve display of parse errors
    • Move parse error check to CLI
    • Add flag to allow parse errors
    • Skip tree traversal unless there are errors

    Example verbose error:

    Unexpected syntax on line 3 column 17:
    
    | type U = { X: { f number } };
                      ^
    
    

    Example compact error:

    Unexpected syntax on line 3 column 17: f
    

    Can be reviewed commit-by-commit

    opened by hendrikvanantwerpen 3
  • Compile error

    Compile error

    When I build the project,

    $ cargo install --features cli tree-sitter-graph
    error[E0658]: unions with non-`Copy` fields are unstable
       --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-1.8.0/src/lib.rs:406:1
        |
    406 | / union SmallVecData<A: Array> {
    407 | |     inline: core::mem::ManuallyDrop<MaybeUninit<A>>,
    408 | |     heap: (*mut A::Item, usize),
    409 | | }
        | |_^
        |
        = note: see issue #55149 <https://github.com/rust-lang/rust/issues/55149> for more information
    
    error: aborting due to previous error
    

    How can i do for it?

    Thanks!

    opened by cyw3 2
  • Add execute_into method

    Add execute_into method

    This variant allows you to execute a TSG file into an existing Graph instance, which is useful if you need to create predefined nodes and/or edges before executing.

    opened by dcreager 2
  • Include statement location in error messages

    Include statement location in error messages

    Include statement location in error messages to improve debugging.

    Resulting error messages look as follows:

    Error: Could not execute TSG file ../../../tsg-python/python.tsg
    
    Caused by:
        0: Executing attr (@assign.node) value = @value.node at (541, 5)
        1: Undefined variable @value.node on node [syntax node attribute (7, 15)]
    
    Error: Could not execute TSG file ../../../tsg-python/python.tsg
    
    Caused by:
        0: Executing edge @parent.node -> @stmt.node at (178, 5)
        1: Undefined variable @stmt.node on node [syntax node function_definition (43, 0)]
    

    Depends on

    • [x] #10
    • [x] #11
    opened by hendrikvanantwerpen 2
  • Indicate node type in duplicate variable errors involving syntax nodes

    Indicate node type in duplicate variable errors involving syntax nodes

    I've got an error in my rules indicating a duplicate variable @block.after_scope where @block is (of course) a syntax node. It would be very helpful to know what kind of syntax tree node it is, and where it occurs in the source, just like we show for undefined variables.

    duplicate enhancement 
    opened by robrix 1
  • Namespacing

    Namespacing

    I think we're going to want to be able to run the union of multiple .tsg programs at a time, and to do that successfully and not have them clobber each other I think their variable and attr names will need to be namespaced.

    I also think we're going to want to be able to explicitly use another namespace to e.g. extend one tsg program with another.

    enhancement 
    opened by robrix 0
  • Allow duplicate attrs

    Allow duplicate attrs

    Like #85, but for attributes: using something like propagators, allow redundant attributes &c. if and only if their values are compatible (for most purposes we can think of this as "equal-valued").

    enhancement 
    opened by robrix 0
  • Graph schemas

    Graph schemas

    I'd like to be able to provide some sort of schema for the kinds of nodes I'm constructing within a graph, and where they can be found in relation to the syntax tree.

    (This would give us a nice way to describe stack graph or eval rules for example, in a way that's machine-checkable.)

    Talked with @dcreager about this, and he suggested testing against such as a midway point to actually verifying.

    In addition to verification/validation, this could also be used to give us constructors for subgraphs, which could be quite convenient when building stack graphs for example.

    enhancement 
    opened by robrix 0
  • "Scoped variable" is a misnomer

    In the old DSL, scoped variables were available within the lexical scope of the node they were attached to—its entire subtree.

    This is no longer the case, leading me to believe that the name is a misnomer, and that we should try to come up with something more indicative of what they mean.

    bug 
    opened by robrix 0
  • --global rejects empty string values

    --global rejects empty string values

    As of #78, we can pass --global var=value arguments on the CLI to specify globals. However, the parser rejects var= as a value, which should correctly specify a value of the empty string. Either way, there is currently no way to supply an empty-valued string global.

    bug 
    opened by robrix 0
  • Pass globals of non-string types on CLI

    Pass globals of non-string types on CLI

    #78 allows us to provide --global var=value arguments on the CLI. This is limited to passing strings, however. It would be nice to support other, non-string globals.

    One possibility would be type-specific flags like --global-int. I'm not sure how we'd want to handle collections.

    enhancement 
    opened by robrix 0
Releases(v0.7.0)
Owner
null
Python library for embedding large graphs in 2D space, using force-directed layouts.

Graph Force A python/rust library for embedding graphs in 2D space, using force-directed layouts. Installation pip install graph_force Usage The first

Niko Abeler 159 Dec 29, 2022
Generate QR code easily for free - QR Code Generation as a Service.

QRcode.show Generate QR code easily for free - QR Code Generation as a Service. INPUT: curl qrcode.show/INPUT curl qrcode.show -d INPUT curl qrcode.sh

Arijit Basu 572 Dec 24, 2022
Baryon is a compact 3D engine focused on fast prototyping in code.

baryon Baryon is a compact 3D engine focused on fast prototyping in code. No big dependencies, no fancy run-times, GUI editors, or other magic. Depend

Dzmitry Malyshau 63 Jan 1, 2023
The library provides basic functions to work with Graphviz dot lang from rust code.

Description The library provides the basic access to the graphs in graphviz format with ability to import into or export from it. Base examples: Parse

Boris 28 Dec 16, 2022
A version of `sort | uniq -c` with output that updates in real-time as each line is parsed

uniqtoo A version of sort | uniq -c with output that updates in real-time as each line is parsed. Usage Pipe something line-based into uniqtoo the sam

Jake Wharton 62 Oct 14, 2022
Uindex is a data store, for data that can be parsed as sentences in some context-free language.

Uindex - Universal index Uindex is a data store, for data that can be parsed as sentences in some context-free language.

Enrique Pérez Arnaud 3 Jul 20, 2021
lemmy-help is a emmylua parser as well as a CLI which takes that parsed tree and converts it into vim help docs.

lemmy-help is a emmylua parser as well as a CLI which takes that parsed tree and converts it into vim help docs.

Vikas Raj 117 Jan 3, 2023
Rust crate: Overloaded Literals to construct your datatypes without boilerplate and with compile-time validation.

overloaded_literals   Overloaded Literals to construct your datatypes without boilerplate and with compile-time validation. Features Compile-time vali

Qqwy / Marten 6 Apr 14, 2023
plugy empowers you to construct agnostic dynamic plugin systems using Rust and WebAssembly.

plugy plugy is a plugin system designed to enable the seamless integration of Rust-based plugins into your application. It provides a runtime environm

Geoffrey Mureithi 22 Aug 12, 2023
Construct complex structures within single call + simple compile-time meta-inheritance model with mixins.

Introduction constructivism is a Rust sample-library designed to simplify the construction of structured data by defining and manipulating sequences o

polako.rs 5 Oct 24, 2023
A web service that generates images of dependency graphs for crates hosted on crates.io

crate-deps A web service that generates images of dependency graphs for crates hosted on crates.io This project is built entirely in Rust using these

Corey Farwell 20 Nov 9, 2020
A utility that can download JavaScript and TypeScript module graphs and store them locally in a special zip file.

eszip A utility that can download JavaScript and TypeScript module graphs and store them locally in a special zip file. To create a new archive: > esz

Deno Land 162 Dec 24, 2022
HNSW ANN from the paper "Efficient and robust approximate nearest neighbor search using Hierarchical Navigable Small World graphs"

hnsw Hierarchical Navigable Small World Graph for fast ANN search Enable the serde feature to serialize and deserialize HNSW. Tips A good default for

Rust Computer Vision 93 Dec 30, 2022
Tool to draw low-resolution graphs in terminal

lowcharts Tool to draw low-resolution graphs in terminal. lowcharts is meant to be used in those scenarios where we have numerical data in text files

juanleon lahoz 114 Dec 31, 2022
A cargo subcommand for creating GraphViz DOT files and dependency graphs

cargo-graph Linux: A cargo subcommand for building GraphViz DOT files of dependency graphs. This subcommand was originally based off and inspired by t

Kevin K. 213 Nov 24, 2022
Tiny Rust library to draw pretty line graphs using ascii characters.

rasciigraph Tiny Rust library to draw pretty line graphs using ascii characters. Usage Add this to your Cargo.toml [dependencies] rasciigraph = "0.1.1

Orhan Balci 54 Jan 6, 2023
A simple 2D plotting library that outputs graphs to SVG that can be styled using CSS.

Poloto graphs can be stylized using css either directly in the SVG, or from inside of html with an embedded svg. The latter allows the user to dynamically match the svg to their website's theme.

Ken Reed 119 Dec 13, 2022
Utilities for creating strictly ordered execution graphs of systems for the Bevy game engine

bevy_system_graph This crate provides the utilities for creating strictly ordered execution graphs of systems for the Bevy game engine. Bevy Version S

Hourai Teahouse 19 Dec 2, 2022
Lightweight graphs (sparklines) for use with Embedded Rust

Sparklines for Rust's Embedded-graphics Sparklines are small, high resolution graphics embedded in a context of words, numbers or images". Edward Tuft

Bernard Kobos 4 Aug 28, 2022
Track and query Cargo dependency graphs.

cargo-guppy: track and query dependency graphs This repository contains the source code for: guppy: a library for performing queries on Cargo dependen

guppy 42 Dec 30, 2022