-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IR (which doesn't help much but hey)
- Loading branch information
Showing
2 changed files
with
81 additions
and
65 deletions.
There are no files selected for viewing
28 changes: 26 additions & 2 deletions
28
modules/parser-gen/src/main/scala/playground/parsergen/IR.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,50 @@ | ||
package playground.parsergen | ||
|
||
import cats.data.NonEmptyList | ||
import treesittersmithy.FieldName | ||
import treesittersmithy.NodeType | ||
import treesittersmithy.TypeName | ||
|
||
enum Type { | ||
|
||
case ADT(name: TypeName, subtypes: NonEmptyList[Subtype]) | ||
case Product(name: TypeName, fields: List[Field], children: Option[Children]) | ||
} | ||
|
||
case class Field(name: FieldName, targetTypes: NonEmptyList[TypeName], repeated: Boolean) | ||
case class Children(targetTypes: NonEmptyList[TypeName], repeated: Boolean) | ||
|
||
case class Subtype(name: TypeName) | ||
|
||
object IR { | ||
|
||
def from(nt: NodeType): Type = | ||
if nt.subtypes.nonEmpty then fromADT(nt) | ||
else | ||
sys.error("todo") | ||
fromProduct(nt) | ||
|
||
private def fromADT(nt: NodeType): Type.ADT = Type.ADT( | ||
name = nt.tpe, | ||
subtypes = NonEmptyList.fromListUnsafe(nt.subtypes.map(subtype => Subtype(name = subtype.tpe))), | ||
) | ||
|
||
private def fromProduct(nt: NodeType): Type.Product = Type.Product( | ||
name = nt.tpe, | ||
fields = | ||
nt.fields | ||
.map { (fieldName, fieldInfo) => | ||
Field( | ||
name = fieldName, | ||
targetTypes = NonEmptyList.fromListUnsafe(fieldInfo.types.map(_.tpe)), | ||
repeated = fieldInfo.multiple, | ||
) | ||
} | ||
.toList, | ||
children = nt.children.map { children => | ||
Children( | ||
targetTypes = NonEmptyList.fromListUnsafe(children.types.map(_.tpe)), | ||
repeated = children.multiple, | ||
) | ||
}, | ||
) | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters