Skip to content

Commit

Permalink
Documentation for rule language syntax (#567)
Browse files Browse the repository at this point in the history
  • Loading branch information
danieltrt authored Aug 3, 2023
1 parent 282643b commit c0d64bf
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
Binary file added site/docs/reference/dsl/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
id: rules
label: Piranha DSL
title: Piranha DSL
id: graphs
label: Graph Language
title: Graph Language
---

Piranha Polyglot uses a domain-specific language (DSL) to specify program transformations. The DSL is used to define rules, scopes, and edges between rules.
Expand Down
34 changes: 34 additions & 0 deletions site/docs/reference/dsl/rules.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
---
id: rules
label: Rule Languages
title: Rule Languages
---

Piranha offers support for three distinct rule languages for matching code.

# Tree-sitter Queries

The Tree-sitter queries language is one of the rule languages that Piranha supports. For a detailed understanding of the syntax, refer to the [Tree-sitter Syntax Reference](https://tree-sitter.github.io/tree-sitter/syntax-highlighting#queries).

# Regular Expressions (Regex)

Regex forms another rule language supported by Piranha. To create a rule in regex, prepend your query with `rgx `. For instance: `rgx <your regex query>`. Piranha supports the regex syntax derived from the [regex](https://docs.rs/regex/) crate.

# Concrete Syntax

Piranha's Concrete Syntax is a custom rule language designed for matching and replacing code. Concrete Syntax operates at the parse tree level, similar to [comby](https://comby.dev/), yet with a distinct difference: it will only match a node if it is possible to traverse the corresponding its parse tree from start to finish using the concrete syntax template.

Template variables `:[x], :[y], ...` are used as placeholders to match arbitrary nodes (i.e., syntactically meaningful constructs).

Consider the following code snippet:
```java
exp.isTreated("SHOW_MENU")
```
To match this code snippet, we can use the concrete syntax (cs) template:
```java
cs :[object].isTreated(:[string])
```

which matches the method invocation nodes as follows:

![example.png](example.png)
10 changes: 9 additions & 1 deletion site/sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,15 @@ const sidebars = {
'reference/getting-started/demos',
],
},
'reference/rules',
{
type: 'category',
label: 'Piranha DSL',
collapsed: false,
items: [
'reference/dsl/graphs',
'reference/dsl/rules',
],
},
'reference/api',
'reference/cli',
{
Expand Down

0 comments on commit c0d64bf

Please sign in to comment.