Skip to content

Commit

Permalink
Private nodes + safe projections
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Nov 3, 2024
1 parent 0652614 commit 17d7538
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,12 @@ def renderAdt(tpe: NodeType) = {
val name = tpe.tpe.render

val enumCases = tpe.subtypes.map { nodeType =>
show"""case ${nodeType.tpe.asEnumCase.render}(value: ${nodeType.tpe.render})"""
show"""private case ${nodeType.tpe.asEnumCase.render}(value: ${nodeType.tpe.render})"""
}

val projections = tpe.subtypes.map { nodeType =>
// UNSAFE: inexhaustive match
// format: off
show"""def ${nodeType.tpe.renderProjection}: ${nodeType
.tpe
.render} = this match { case ${nodeType.tpe.asEnumCase.render}(v) => v }"""
show"""def ${nodeType.tpe.renderProjection}: Option[${nodeType.tpe.render}] = this match { case ${nodeType.tpe.asEnumCase.render}(v) => Some(v); case _ => None }"""
// format: on
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ package playground.generated.nodes
import org.polyvariant.treesitter4s.Node

enum InputNode {
case BooleanCase(value: Boolean_)
case ListCase(value: List_)
case NullCase(value: Null_)
case NumberCase(value: Number)
case StringCase(value: String_)
case StructCase(value: Struct)

def asBoolean: Boolean_ = this match { case BooleanCase(v) => v }
def asList: List_ = this match { case ListCase(v) => v }
def asNull: Null_ = this match { case NullCase(v) => v }
def asNumber: Number = this match { case NumberCase(v) => v }
def asString: String_ = this match { case StringCase(v) => v }
def asStruct: Struct = this match { case StructCase(v) => v }
private case BooleanCase(value: Boolean_)
private case ListCase(value: List_)
private case NullCase(value: Null_)
private case NumberCase(value: Number)
private case StringCase(value: String_)
private case StructCase(value: Struct)

def asBoolean: Option[Boolean_] = this match { case BooleanCase(v) => Some(v); case _ => None }
def asList: Option[List_] = this match { case ListCase(v) => Some(v); case _ => None }
def asNull: Option[Null_] = this match { case NullCase(v) => Some(v); case _ => None }
def asNumber: Option[Number] = this match { case NumberCase(v) => Some(v); case _ => None }
def asString: Option[String_] = this match { case StringCase(v) => Some(v); case _ => None }
def asStruct: Option[Struct] = this match { case StructCase(v) => Some(v); case _ => None }

def node: Node = this match {
case BooleanCase(value) => value.node
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ object ParserTreeSitterDemo extends App {
.find(_.key.source == "x")
.get

println(bind.value.asNumber.source)
println(bind.value.asNumber.get.source)
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@ val bind =

bind.key.source

bind.value.asNumber.source
bind.value.asNumber.get.source

0 comments on commit 17d7538

Please sign in to comment.