-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from samtkit/type-aliases
Type aliases
- Loading branch information
Showing
17 changed files
with
427 additions
and
77 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package tools.samt.cli | ||
|
||
import com.github.ajalt.mordant.rendering.TextColors.* | ||
import com.github.ajalt.mordant.rendering.TextStyles.bold | ||
import tools.samt.semantic.Package | ||
|
||
internal object TypePrinter { | ||
fun dump(samtPackage: Package): String = buildString { | ||
appendLine(blue(samtPackage.name.ifEmpty { "<root>" })) | ||
for (enum in samtPackage.enums) { | ||
appendLine(" ${bold("enum")} ${yellow(enum.name)}") | ||
} | ||
for (record in samtPackage.records) { | ||
appendLine(" ${bold("record")} ${yellow(record.name)}") | ||
} | ||
for (alias in samtPackage.aliases) { | ||
appendLine(" ${bold("alias")} ${yellow(alias.name)} = ${gray(alias.fullyResolvedType?.humanReadableName ?: "Unknown")}") | ||
} | ||
for (service in samtPackage.services) { | ||
appendLine(" ${bold("service")} ${yellow(service.name)}") | ||
} | ||
for (provider in samtPackage.providers) { | ||
appendLine(" ${bold("provider")} ${yellow(provider.name)}") | ||
} | ||
for (consumer in samtPackage.consumers) { | ||
appendLine(" ${bold("consumer")} for ${yellow(consumer.provider.humanReadableName)}") | ||
} | ||
|
||
val childDumps: List<String> = samtPackage.subPackages.map { dump(it) } | ||
|
||
childDumps.forEachIndexed { childIndex, child -> | ||
var firstLine = true | ||
child.lineSequence().forEach { line -> | ||
if (line.isNotEmpty()) { | ||
if (childIndex != childDumps.lastIndex) { | ||
if (firstLine) { | ||
append("${white("├─")}$line") | ||
} else { | ||
append("${white("│ ")}$line") | ||
} | ||
} else { | ||
if (firstLine) { | ||
append("${white("└─")}$line") | ||
} else { | ||
append(" $line") | ||
} | ||
} | ||
|
||
appendLine() | ||
} | ||
|
||
firstLine = false | ||
} | ||
} | ||
} | ||
} |
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
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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package tools.samt.cli | ||
|
||
import tools.samt.common.DiagnosticController | ||
import tools.samt.common.SourceFile | ||
import tools.samt.lexer.Lexer | ||
import tools.samt.parser.FileNode | ||
import tools.samt.parser.Parser | ||
import tools.samt.semantic.SemanticModelBuilder | ||
import java.net.URI | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.test.assertFalse | ||
|
||
class TypePrinterTest { | ||
@Test | ||
fun `correctly formats an AST dump`() { | ||
val stuffPackage = parse(""" | ||
package test.stuff | ||
record A { | ||
name: String (10..20, pattern("hehe")) | ||
age: Int (0..150) | ||
} | ||
enum E { A, B, C } | ||
@Description("This is a service") | ||
service MyService { | ||
testmethod(foo: A): E | ||
} | ||
provide MyEndpoint { | ||
implements MyService | ||
transport HTTP | ||
} | ||
""".trimIndent()) | ||
val consumerPackage = parse(""" | ||
package test.other.company | ||
record Empty | ||
consume test.stuff.MyEndpoint { | ||
uses test.stuff.MyService | ||
} | ||
""".trimIndent()) | ||
|
||
val controller = DiagnosticController(URI("file:///tmp")) | ||
val samtPackage = SemanticModelBuilder.build(listOf(stuffPackage, consumerPackage), controller) | ||
assertFalse(controller.hasErrors()) | ||
|
||
val dump = TypePrinter.dump(samtPackage) | ||
val dumpWithoutColorCodes = dump.replace(Regex("\u001B\\[[;\\d]*m"), "") | ||
|
||
assertEquals(""" | ||
<root> | ||
└─test | ||
├─stuff | ||
│ enum E | ||
│ record A | ||
│ service MyService | ||
│ provider MyEndpoint | ||
└─other | ||
└─company | ||
record Empty | ||
consumer for MyEndpoint | ||
""".trimIndent().trim(), dumpWithoutColorCodes.trimIndent().trim()) | ||
} | ||
|
||
private fun parse(source: String): FileNode { | ||
val filePath = URI("file:///tmp/ASTPrinterTest.samt") | ||
val sourceFile = SourceFile(filePath, source) | ||
val diagnosticController = DiagnosticController(URI("file:///tmp")) | ||
val diagnosticContext = diagnosticController.getOrCreateContext(sourceFile) | ||
val stream = Lexer.scan(source.reader(), diagnosticContext) | ||
val fileTree = Parser.parse(sourceFile, stream, diagnosticContext) | ||
assertFalse(diagnosticContext.hasErrors(), "Expected no errors, but had errors") | ||
return fileTree | ||
} | ||
} |
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
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
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
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
Oops, something went wrong.