diff --git a/modules/parser-gen/src/main/scala/playground/parsergen/ParserGen.scala b/modules/parser-gen/src/main/scala/playground/parsergen/ParserGen.scala index 85f53b7b..716920e8 100644 --- a/modules/parser-gen/src/main/scala/playground/parsergen/ParserGen.scala +++ b/modules/parser-gen/src/main/scala/playground/parsergen/ParserGen.scala @@ -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 } diff --git a/modules/parser/src/main/scala/playground/generated/nodes/InputNode.scala b/modules/parser/src/main/scala/playground/generated/nodes/InputNode.scala index a39391be..a7b49118 100644 --- a/modules/parser/src/main/scala/playground/generated/nodes/InputNode.scala +++ b/modules/parser/src/main/scala/playground/generated/nodes/InputNode.scala @@ -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 diff --git a/modules/parser/src/main/scala/playground/smithyql/parser/ParserTreeSitterDemo.scala b/modules/parser/src/main/scala/playground/smithyql/parser/ParserTreeSitterDemo.scala index 548db8d1..5c0b7b11 100644 --- a/modules/parser/src/main/scala/playground/smithyql/parser/ParserTreeSitterDemo.scala +++ b/modules/parser/src/main/scala/playground/smithyql/parser/ParserTreeSitterDemo.scala @@ -23,5 +23,5 @@ object ParserTreeSitterDemo extends App { .find(_.key.source == "x") .get - println(bind.value.asNumber.source) + println(bind.value.asNumber.get.source) } diff --git a/modules/parser/src/main/scala/playground/smithyql/parser/demo.worksheet.sc b/modules/parser/src/main/scala/playground/smithyql/parser/demo.worksheet.sc index 8abfbd1e..6af30fc6 100644 --- a/modules/parser/src/main/scala/playground/smithyql/parser/demo.worksheet.sc +++ b/modules/parser/src/main/scala/playground/smithyql/parser/demo.worksheet.sc @@ -34,4 +34,4 @@ val bind = bind.key.source -bind.value.asNumber.source +bind.value.asNumber.get.source