Skip to content

Commit

Permalink
Fixes from usage:
Browse files Browse the repository at this point in the history
* Enable resolution of AuthorRefs on all definitions
* Make ReferenceMap.toString do something reasonable
* Fix bugs with attachments in RiddlFileEmitter
* Override toString in AST.LiteralString because the default doesn't work in JS.
  • Loading branch information
reidspencer committed Feb 22, 2025
1 parent f918015 commit 597f1e8
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,8 @@ object AST:
case class LiteralString(loc: At, s: String) extends RiddlValue:
override def format = s"\"$s\""

override def toString: String = format

/** Only empty if the string is empty too */
override def isEmpty: Boolean = s.isEmpty
end LiteralString
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,14 @@ case class RiddlFileEmitter(url: URL) extends FileBuilder {
end emitAuthorRef

private def emitStringAttachment(a: StringAttachment): this.type =
addIndent(a.identify).add(s" is \"${a.mimeType}\" as \"${a.value}\"")
addIndent("attachment " + a.id.format).add(s" is \"${a.mimeType}\" as ${a.value.format}")
end emitStringAttachment

private def emitFileAttachment(a: FileAttachment): this.type =
addIndent(a.identify).add(s" is \"${a.mimeType} in file \"${a.inFile}\"")
addIndent("attachment " + a.id.format).add(s" is \"${a.mimeType}\" in file ${a.inFile.format}")

private def emitULIDAttachment(ulid: ULIDAttachment): this.type =
addIndent(ulid.identify)
private def emitULIDAttachment(a: ULIDAttachment): this.type =
addIndent("attachment " + a.id.format).add(s" is \"${a.ulid.toString}\"")

def emitString(s: String_): this.type = {
(s.min, s.max) match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,45 @@ import com.ossuminc.riddl.utils.{PlatformContext, StringHelpers}
import scala.collection.mutable
import scala.reflect.{ClassTag, classTag}

/** The primary output of the [[ResolutionPass]]. It provides a mapping from a reference to the referenced definition.
* This is useful for fast resolution during validation and other Passes because the resolution logic doesn't need to
* be exercised again.
/** The primary output of the [[ResolutionPass]]. It provides a mapping from a reference to the
* referenced definition. This is useful for fast resolution during validation and other Passes
* because the resolution logic doesn't need to be exercised again.
* @param messages
* A message accumulator for collecting messages when member functions are invoked
*/
case class ReferenceMap(messages: Messages.Accumulator) {

private case class Key(path: String, in: Definition) {
override def toString: String = s"(k=$path,v=${in.identify})"
private case class Key(path: String, parent: Definition) {
override def toString: String = s"(k=$path,v=${parent.identify})"
}

private val map: mutable.HashMap[Key, Definition] = mutable.HashMap.empty

override def toString: String = {
StringHelpers.toPrettyString(this) ++ StringHelpers.toPrettyString(map)
}
override def toString: String =
map.keys.toSeq
.sortBy(_.path)
.map(k => s"${k.path} = ${map(k).identify}")
.mkString("ReferenceMap{\n", ",\n", "}\n")

def size: Int = map.size

def add[T <: Definition: ClassTag](ref: Reference[T], parent: Branch[?], definition: T): Unit = {
add(ref.pathId.format, parent, definition)
}

def add[T <: Definition: ClassTag](pathId: PathIdentifier, parent: Branch[?], definition: T): Unit = {
def add[T <: Definition: ClassTag](
pathId: PathIdentifier,
parent: Branch[?],
definition: T
): Unit = {
add(pathId.format, parent, definition)
}

private def add[T <: Definition: ClassTag](pathId: String, parent: Branch[?], definition: T): Unit = {
private def add[T <: Definition: ClassTag](
pathId: String,
parent: Branch[?],
definition: T
): Unit = {
val key = Key(pathId, parent)
val expected = classTag[T].runtimeClass
val actual = definition.getClass
Expand All @@ -52,20 +62,27 @@ case class ReferenceMap(messages: Messages.Accumulator) {
map.update(key, definition)
}

def definitionOf[T <: Definition: ClassTag](pathId: PathIdentifier)(using PlatformContext): Option[T] = {
def definitionOf[T <: Definition: ClassTag](
pathId: PathIdentifier
)(using PlatformContext): Option[T] = {
definitionOf[T](pathId.format)
}

def definitionOf[T <: Definition: ClassTag](pathId: String): Option[T] = {
val potentials = map.find(key => key._1.path == pathId)
println(potentials.map(_._1.path))
potentials match
case None => Option.empty[T]
case Some((_, definition)) =>
val klass = classTag[T].runtimeClass
println(s"klass=${klass.getSimpleName}")
println(s"definition.getClass=${definition.getClass.getSimpleName}")
if definition.getClass == klass then Some(definition.asInstanceOf[T]) else Option.empty[T]
}

def definitionOf[T <: Definition: ClassTag](pid: PathIdentifier, parent: Definition)(using PlatformContext): Option[T] = {
def definitionOf[T <: Definition: ClassTag](pid: PathIdentifier, parent: Definition)(using
PlatformContext
): Option[T] = {
val key = Key(pid.format, parent)
val value = map.get(key)
value match
Expand All @@ -75,12 +92,16 @@ case class ReferenceMap(messages: Messages.Accumulator) {
Some(x)
case Some(x) =>
val className = classTag[T].runtimeClass.getSimpleName
messages.addError(pid.loc, s"Path Id '${pid.format} found ${x.identify} but a $className was expected")
messages.addError(
pid.loc,
s"Path Id '${pid.format} found ${x.identify} but a $className was expected"
)
None
}

def definitionOf[T <: Definition: ClassTag](ref: Reference[T], parent: Branch[?])(using PlatformContext): Option[T]
= {
def definitionOf[T <: Definition: ClassTag](ref: Reference[T], parent: Branch[?])(using
PlatformContext
): Option[T] = {
definitionOf[T](ref.pathId, parent)
}
}
Expand Down
Loading

0 comments on commit 597f1e8

Please sign in to comment.