Skip to content

Commit

Permalink
Improvements for more coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
kubukoz committed Nov 2, 2024
1 parent 3390152 commit 6f16fd5
Show file tree
Hide file tree
Showing 23 changed files with 108 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import org.polyvariant.treesitter4s.Node
import smithy4s.Blob
import smithy4s.Document
import smithy4s.json.Json
import treesittersmithy.FieldName
import treesittersmithy.NodeType
import treesittersmithy.NodeTypes
import treesittersmithy.TypeName
import util.chaining.*

import java.nio.file.Files
import java.nio.file.Paths
import scala.annotation.targetName
import scala.jdk.CollectionConverters.*
import scala.meta.Dialect

Expand All @@ -24,10 +26,16 @@ def debugDump(s: String): String =
""

extension (tn: TypeName) {
@targetName("renderTypeName")
def render: String = tn.value.smartCapitalize.ident
def asEnumCase: TypeName = TypeName(tn.value + "Case")
}

extension (fn: FieldName) {
@targetName("renderFieldName")
def render: String = fn.value.ident
}

extension (tpe: NodeType) {

def render: String =
Expand All @@ -52,7 +60,7 @@ def renderAdt(tpe: NodeType) = {
|enum $name {
|${enumCases.mkString_("\n").indentTrim(2)}
|
| def asNode: Node = this match {
| def node: Node = this match {
|${tpe
.subtypes
.map { nodeType =>
Expand All @@ -75,6 +83,18 @@ def renderAdt(tpe: NodeType) = {
.mkString_("\n")
.indentTrim(4)}
| }
|
| def unapply(node: Node): Boolean = node match {
|${tpe
.subtypes
.map { nodeType =>
show"""case node @ ${nodeType
.tpe
.render}() => true"""
}
.mkString_("\n")
.indentTrim(4)}
| }
|}
|
|/*
Expand All @@ -90,12 +110,13 @@ def renderClass(tpe: NodeType) = {
.fields
.toList
.map { (k, fieldType) =>
val singleFieldType = fieldType
val typeUnion = fieldType
.types
.map(tpe => show"${tpe.tpe.render}")
.reduceLeft(_ + " | " + _)
.reduceLeftOption(_ + " | " + _)
.getOrElse(sys.error(s"unexpected empty list of types: $k (in ${tpe.tpe})"))

val fieldTypeAnnotation = singleFieldType.pipe {
val fieldTypeAnnotation = typeUnion.pipe {
case s if fieldType.multiple => show"List[$s]"
case s => s
}
Expand All @@ -110,9 +131,12 @@ def renderClass(tpe: NodeType) = {
|${cases.mkString("\n").indentTrim(2)}
|}""".stripMargin
else
show"""${singleFieldType}($allFields.head)"""
// todo replace head with a stricter "only" check?
show"""$allFields.head match {
|${cases.mkString("\n").indentTrim(2)}
|}""".stripMargin

show"""def ${k.value}: ${fieldTypeAnnotation} = $fieldValue"""
show"""def ${k.render}: ${fieldTypeAnnotation} = $fieldValue"""
}

show"""// Generated code! Do not modify by hand.
Expand All @@ -130,7 +154,7 @@ def renderClass(tpe: NodeType) = {
|}
|
|object $name {
| def unapply(node: Node): scala.Boolean = node.tpe == ${tpe.tpe.value.literal}
| def unapply(node: Node): Boolean = node.tpe == ${tpe.tpe.value.literal}
|}
|
|/*
Expand All @@ -156,7 +180,14 @@ def renderClass(tpe: NodeType) = {

types
.filter(_.named)
.map(_.focus(_.fields.each.types).modify(_.filter(_.named)))
.map(
// only render field types that are named
_.focus(_.fields.each.types)
.modify(_.filter(_.named))
// don't render the field if it has no types
.focus(_.fields)
.modify(_.filter((_, v) => v.types.nonEmpty))
)
.fproduct(
_.render
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import org.polyvariant.treesitter4s.Node

case class Binding /* private */(node: Node) extends Node {

def key: Identifier = Identifier(node.fields("key").head)
def value: _Input_node = _Input_node(node.fields("value").head)
def key: Identifier = node.fields("key").head match {
case node @ Identifier() => Identifier(node)
}
def value: _Input_node = node.fields("value").head match {
case node @ _Input_node() => _Input_node(node)
}

export node.*
}

object Binding {
def unapply(node: Node): scala.Boolean = node.tpe == "binding"
def unapply(node: Node): Boolean = node.tpe == "binding"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Bindings /* private */(node: Node) extends Node {
}

object Bindings {
def unapply(node: Node): scala.Boolean = node.tpe == "bindings"
def unapply(node: Node): Boolean = node.tpe == "bindings"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Boolean_ /* private */(node: Node) extends Node {
}

object Boolean_ {
def unapply(node: Node): scala.Boolean = node.tpe == "boolean"
def unapply(node: Node): Boolean = node.tpe == "boolean"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Comment /* private */(node: Node) extends Node {
}

object Comment {
def unapply(node: Node): scala.Boolean = node.tpe == "comment"
def unapply(node: Node): Boolean = node.tpe == "comment"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Identifier /* private */(node: Node) extends Node {
}

object Identifier {
def unapply(node: Node): scala.Boolean = node.tpe == "identifier"
def unapply(node: Node): Boolean = node.tpe == "identifier"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Let_binding /* private */(node: Node) extends Node {
}

object Let_binding {
def unapply(node: Node): scala.Boolean = node.tpe == "let_binding"
def unapply(node: Node): Boolean = node.tpe == "let_binding"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import org.polyvariant.treesitter4s.Node

case class List_ /* private */(node: Node) extends Node {

def list_fields: List_fields = List_fields(node.fields("list_fields").head)
def list_fields: List_fields = node.fields("list_fields").head match {
case node @ List_fields() => List_fields(node)
}

export node.*
}

object List_ {
def unapply(node: Node): scala.Boolean = node.tpe == "list"
def unapply(node: Node): Boolean = node.tpe == "list"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class List_fields /* private */(node: Node) extends Node {
}

object List_fields {
def unapply(node: Node): scala.Boolean = node.tpe == "list_fields"
def unapply(node: Node): Boolean = node.tpe == "list_fields"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Null_ /* private */(node: Node) extends Node {
}

object Null_ {
def unapply(node: Node): scala.Boolean = node.tpe == "null"
def unapply(node: Node): Boolean = node.tpe == "null"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Number /* private */(node: Node) extends Node {
}

object Number {
def unapply(node: Node): scala.Boolean = node.tpe == "number"
def unapply(node: Node): Boolean = node.tpe == "number"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import org.polyvariant.treesitter4s.Node

case class Operation_call /* private */(node: Node) extends Node {

def input: Struct = Struct(node.fields("input").head)
def operation_name: Operation_name = Operation_name(node.fields("operation_name").head)
def input: Struct = node.fields("input").head match {
case node @ Struct() => Struct(node)
}
def operation_name: Operation_name = node.fields("operation_name").head match {
case node @ Operation_name() => Operation_name(node)
}

export node.*
}

object Operation_call {
def unapply(node: Node): scala.Boolean = node.tpe == "operation_call"
def unapply(node: Node): Boolean = node.tpe == "operation_call"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ case class Operation_name /* private */(node: Node) extends Node {
def identifier: List[Qualified_identifier] = node.fields("identifier").toList.collect {
case node @ Qualified_identifier() => Qualified_identifier(node)
}
def name: Identifier = Identifier(node.fields("name").head)
def name: Identifier = node.fields("name").head match {
case node @ Identifier() => Identifier(node)
}

export node.*
}

object Operation_name {
def unapply(node: Node): scala.Boolean = node.tpe == "operation_name"
def unapply(node: Node): Boolean = node.tpe == "operation_name"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ import org.polyvariant.treesitter4s.Node

case class Qualified_identifier /* private */(node: Node) extends Node {

def head: Identifier = Identifier(node.fields("head").head)
def selection: Identifier = Identifier(node.fields("selection").head)
def head: Identifier = node.fields("head").head match {
case node @ Identifier() => Identifier(node)
}
def selection: Identifier = node.fields("selection").head match {
case node @ Identifier() => Identifier(node)
}
def tail: List[Identifier] = node.fields("tail").toList.collect {
case node @ Identifier() => Identifier(node)
}
Expand All @@ -15,7 +19,7 @@ case class Qualified_identifier /* private */(node: Node) extends Node {
}

object Qualified_identifier {
def unapply(node: Node): scala.Boolean = node.tpe == "qualified_identifier"
def unapply(node: Node): Boolean = node.tpe == "qualified_identifier"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,18 @@ import org.polyvariant.treesitter4s.Node

case class Source_file /* private */(node: Node) extends Node {

def statements: Top_level_statement = Top_level_statement(node.fields("statements").head)
def use_clause: Use_clause = Use_clause(node.fields("use_clause").head)
def statements: Top_level_statement = node.fields("statements").head match {
case node @ Top_level_statement() => Top_level_statement(node)
}
def use_clause: Use_clause = node.fields("use_clause").head match {
case node @ Use_clause() => Use_clause(node)
}

export node.*
}

object Source_file {
def unapply(node: Node): scala.Boolean = node.tpe == "source_file"
def unapply(node: Node): Boolean = node.tpe == "source_file"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class String_ /* private */(node: Node) extends Node {
}

object String_ {
def unapply(node: Node): scala.Boolean = node.tpe == "string"
def unapply(node: Node): Boolean = node.tpe == "string"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import org.polyvariant.treesitter4s.Node

case class Struct /* private */(node: Node) extends Node {

def bindings: Bindings = Bindings(node.fields("bindings").head)
def bindings: Bindings = node.fields("bindings").head match {
case node @ Bindings() => Bindings(node)
}

export node.*
}

object Struct {
def unapply(node: Node): scala.Boolean = node.tpe == "struct"
def unapply(node: Node): Boolean = node.tpe == "struct"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Top_level_statement /* private */(node: Node) extends Node {
}

object Top_level_statement {
def unapply(node: Node): scala.Boolean = node.tpe == "top_level_statement"
def unapply(node: Node): Boolean = node.tpe == "top_level_statement"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import org.polyvariant.treesitter4s.Node

case class Use_clause /* private */(node: Node) extends Node {

def identifier: Qualified_identifier = Qualified_identifier(node.fields("identifier").head)
def identifier: Qualified_identifier = node.fields("identifier").head match {
case node @ Qualified_identifier() => Qualified_identifier(node)
}

export node.*
}

object Use_clause {
def unapply(node: Node): scala.Boolean = node.tpe == "use_clause"
def unapply(node: Node): Boolean = node.tpe == "use_clause"
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ case class Whitespace /* private */(node: Node) extends Node {
}

object Whitespace {
def unapply(node: Node): scala.Boolean = node.tpe == "whitespace"
def unapply(node: Node): Boolean = node.tpe == "whitespace"
}

/*
Expand Down
Loading

0 comments on commit 6f16fd5

Please sign in to comment.