Skip to content

Commit

Permalink
Add visitors and folds
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Nov 1, 2024
1 parent c17c3cf commit afa8e96
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 9 deletions.
10 changes: 8 additions & 2 deletions .scalafmt.conf
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
runner.dialect = "scala3"
version = 3.5.8
version = 3.8.3

runner.dialect=scala3
runner.dialectOverride.allowSignificantIndentation = false
runner.dialectOverride.allowQuietSyntax = true

maxColumn = 100
align.preset = some

Expand Down Expand Up @@ -30,3 +34,5 @@ danglingParentheses.exclude = [
]
verticalMultiline.newlineAfterOpenParen = true
verticalMultiline.atDefnSite = true

assumeStandardLibraryStripMargin = true
10 changes: 6 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,12 @@ val commonJVMSettings = Seq(
Compile / doc / javacOptions -= "-Xlint:all",
Test / fork := true,
scalacOptions ++= {
if (scalaVersion.value.startsWith("2.13"))
Seq("-Wnonunit-statement")
else
Nil
Seq("-Wnonunit-statement") ++ {
if (scalaVersion.value.startsWith("3"))
Seq("-no-indent")
else
Nil
}
},
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,22 @@ trait Node {
def fields: Map[String, Node]
def startByte: Int
def endByte: Int

def visit[A](visitor: Node.Visitor[A]): A = visitor.onNode(this, this.children)

// A specialized form of visitor, where every node is provided already visited.
def fold[A](folder: Node.Folder[A]): A = folder.onNode(this, this.children.map(_.fold(folder)))

}

object Node {

trait Folder[A] {
def onNode(node: Node, children: List[A]): A
}

trait Visitor[A] {
def onNode(node: Node, children: List[Node]): A
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@
* limitations under the License.
*/

package org.polyvariant.treesitter4s
package org.polyvariant.treesitter4s.lowlevel

import weaver.*

object TreeSitterAPITest extends FunSuite {
object TreeSitterTest extends FunSuite {
test("Tree Sitter loads") {
try {
println(TreeSitterAPI)
println(TreeSitter.instance)
success
} catch {
case e: ExceptionInInitializerError =>
Expand All @@ -30,4 +30,5 @@ object TreeSitterAPITest extends FunSuite {
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import cats.implicits._
import org.polyvariant.treesitter4s.Tree
import weaver._
import org.polyvariant.treesitter4s.TreeSitterAPI
import org.polyvariant.treesitter4s.Node

object BindingTests extends FunSuite {
val ts = TreeSitterAPI.make("python")
Expand Down Expand Up @@ -47,6 +48,31 @@ object BindingTests extends FunSuite {
assert.eql(rootNode.children.lift(0).isDefined, true)
}

test("Node.fold") {
val rootNode = parseExample("""
|def foo():
| return 1
|
|def bar():
| def baz():
| return 2
| return baz()
|""".stripMargin)
.rootNode
.getOrElse(sys.error("missing root node"))

val functions = rootNode.fold[List[Node]] { (node, children) =>
if (node.tpe == "function_definition")
node :: children.flatten
else
children.flatten
}

val functionNames = functions.map(_.fields("name").source)

assert.eql(functionNames, "foo" :: "bar" :: "baz" :: Nil)
}

// test("root node child by index (out of range)") {
// val tree = parseExample("class Hello {}")
// val rootNode = tree.rootNode.getOrElse(sys.error("missing root node"))
Expand Down

0 comments on commit afa8e96

Please sign in to comment.