Skip to content

Commit 35e3e1b

Browse files
committed
jvm version is passing all tests
1 parent 2a106c5 commit 35e3e1b

File tree

11 files changed

+815
-54
lines changed

11 files changed

+815
-54
lines changed

src/commonMain/kotlin/io/zenwave360/zdl/ZdlParser.kt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package io.zenwave360.zdl
33
import io.zenwave360.zdl.antlr.*
44
import org.antlr.v4.kotlinruntime.CharStreams
55
import org.antlr.v4.kotlinruntime.CommonTokenStream
6+
import org.antlr.v4.kotlinruntime.tree.ParseTreeWalker
67

78
class ZdlParser {
89

@@ -35,8 +36,8 @@ class ZdlParser {
3536
val tokens = CommonTokenStream(lexer)
3637
val parser = io.zenwave360.zdl.antlr.ZdlParser(tokens)
3738
val listener = ZdlListenerImpl()
38-
parser.addParseListener(listener)
39-
parser.zdl() // parsing triggers listener callbacks
39+
val zdlRoot = parser.zdl()
40+
ParseTreeWalker.DEFAULT.walk(listener, zdlRoot)
4041

4142
var zdlModel = listener.model
4243
zdlModel = ZdlModelPostProcessor.postProcess(zdlModel)

src/commonMain/kotlin/io/zenwave360/zdl/antlr/FluentMap.kt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package io.zenwave360.zdl.antlr
22

33
class FluentMap private constructor(
4-
private val backingMap: MutableMap<String, Any?>
4+
private val backingMap: MutableMap<String, Any?> = linkedMapOf()
55
) : MutableMap<String, Any?> by backingMap {
66

77
companion object {
88
fun build(block: FluentMap.() -> Unit = {}): FluentMap =
9-
FluentMap(mutableMapOf()).apply(block)
9+
FluentMap(linkedMapOf()).apply(block)
1010
}
1111

1212
// Provide access to underlying Java Map (useful for JSONPath on JVM)
@@ -17,12 +17,12 @@ class FluentMap private constructor(
1717
fun putAllEntries(map: Map<String, Any?>): FluentMap = apply { backingMap.putAll(map) }
1818

1919
fun appendToMap(collection: String, key: String, value: Any?): FluentMap = apply {
20-
val nestedMap = backingMap.getOrPut(collection) { mutableMapOf<String, Any?>() } as MutableMap<String, Any?>
20+
val nestedMap = backingMap.getOrPut(collection) { linkedMapOf<String, Any?>() } as MutableMap<String, Any?>
2121
nestedMap[key] = value
2222
}
2323

2424
fun appendToMap(collection: String, map: Map<String, Any?>): FluentMap = apply {
25-
val nestedMap = backingMap.getOrPut(collection) { mutableMapOf<String, Any?>() } as MutableMap<String, Any?>
25+
val nestedMap = backingMap.getOrPut(collection) { linkedMapOf<String, Any?>() } as MutableMap<String, Any?>
2626
nestedMap.putAll(map)
2727
}
2828

src/commonMain/kotlin/io/zenwave360/zdl/antlr/JSONPath.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package io.zenwave360.zdl.antlr
22

33
expect object JSONPath {
4-
fun get(source: Any?, path: String): Any?
4+
fun <T> get(source: Any?, path: String): T?
55

66
fun <T> get(source: Any?, path: String, defaultValue: T?): T?
77
}

src/commonMain/kotlin/io/zenwave360/zdl/antlr/ZdlListenerImpl.kt

Lines changed: 42 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ class ZdlListenerImpl : ZdlBaseListener() {
2424
private val currentStack = ArrayDeque<FluentMap>()
2525
private var currentCollection: String? = null
2626

27+
// fun getModel(): ZdlModel = model
28+
2729
override fun enterZdl(ctx: ZdlParser.ZdlContext) {}
2830

2931
override fun enterSuffix_javadoc(ctx: ZdlParser.Suffix_javadocContext) {
@@ -46,7 +48,7 @@ class ZdlListenerImpl : ZdlBaseListener() {
4648
}
4749

4850
override fun enterConfig_option(ctx: ZdlParser.Config_optionContext) {
49-
val name = ctx.field_name()?.text ?: return
51+
val name = ctx.field_name().text
5052
val value = getComplexValue(ctx.complex_value())
5153
model.appendTo("config", name, value)
5254
}
@@ -123,9 +125,9 @@ class ZdlListenerImpl : ZdlBaseListener() {
123125

124126
override fun enterPolicie_body(ctx: ZdlParser.Policie_bodyContext) {
125127
val name = getText(ctx.policie_name())!!
126-
val value = ctx.policie_value()?.let { getValueText(it.simple()) }
127-
// Avoid accessing protected parent; store minimal info
128-
model.appendTo("policies", FluentMap.build().with(name, FluentMap.build().with("name", name).with("value", value)))
128+
val value = getValueText(ctx.policie_value().simple())
129+
val aggregate = (ctx.getParent()?.getParent() as ZdlParser.PoliciesContext).policy_aggregate()
130+
model.appendTo("policies", FluentMap.build().with(name, FluentMap.build().with("name", name).with("value", value).with("aggregate", aggregate)))
129131
super.enterPolicie_body(ctx)
130132
}
131133

@@ -197,6 +199,7 @@ class ZdlListenerImpl : ZdlBaseListener() {
197199
.with("isEnum", isEnum)
198200
.with("isEntity", isEntity)
199201
.with("isArray", isArray)
202+
.with("isComplexType", false)
200203
.with("options", FluentMap.build())
201204
.with("validations", validations)
202205
currentStack.last().appendTo("fields", name, field)
@@ -234,30 +237,30 @@ class ZdlListenerImpl : ZdlBaseListener() {
234237
}
235238

236239
override fun enterNested_field(ctx: ZdlParser.Nested_fieldContext) {
237-
val parentFieldCtx = (ctx as ParserRuleContext).getRuleContext(ZdlParser.FieldContext::class, 0) ?: throw IllegalStateException("No FieldContext parent")
238-
val parentEntity = currentStack.elementAt(currentStack.size - 2)
240+
val parent = ctx.getParent() as ZdlParser.FieldContext
241+
val parentEntity = currentStack[currentStack.size - 2]
239242
val parentEntityFields = parentEntity["fields"] as FluentMap
240-
val parentField = ArrayList(parentEntityFields.values).last()
241-
val entityName = parentFieldCtx.field_type().ID().text
242-
val entityJavadoc = javadoc(parentFieldCtx.javadoc())
243-
val tableName = getText(parentFieldCtx.entity_table_name())
243+
val parentField = ArrayList(parentEntityFields.values)[parentEntityFields.size - 1]
244+
val entityName = parent.field_type().ID()!!.text
245+
val entityJavadoc = javadoc(parent.javadoc())
246+
val tableName = getText(parent.entity_table_name())
244247
val validations = processNestedFieldValidations(ctx.nested_field_validations())
245248
(parentField as FluentMap).appendTo("validations", validations)
246249
currentStack.addLast(processEntity(entityName, entityJavadoc, tableName).with("type", currentCollection!!.split(".")[0]))
247250
currentStack.last().appendTo("options", "embedded", true)
248251
@Suppress("UNCHECKED_CAST")
249-
val parenFieldOptions = JSONPath.get(parentField, "options", mapOf<String, Any>()) as Map<String, Any>
250-
for ((k, v) in parenFieldOptions.entries) {
252+
val parentFieldOptions = JSONPath.get(parentField, "options", mapOf<String, Any>()) as Map<String, Any>
253+
for ((k, v) in parentFieldOptions.entries) {
251254
currentStack.last().appendTo("options", k, v)
252255
}
253256
model.appendTo(currentCollection!!, entityName, currentStack.last())
254257

255258
val entityLocation = "$currentCollection.$entityName"
256-
val startLocation = getLocations(parentFieldCtx.field_type())
259+
val startLocation = getLocations(parent.field_type())
257260
val endLocation = getLocations(ctx)
258261
model.setLocation(entityLocation, mergeLocations(startLocation, endLocation))
259-
model.setLocation("$entityLocation.name", getLocations(parentFieldCtx.field_type()))
260-
model.setLocation("$entityLocation.tableName", getLocations(parentFieldCtx.entity_table_name()))
262+
model.setLocation("$entityLocation.name", getLocations(parent.field_type()))
263+
model.setLocation("$entityLocation.tableName", getLocations(parent.entity_table_name()))
261264
model.setLocation("$entityLocation.body", getLocations(ctx))
262265
}
263266

@@ -313,8 +316,8 @@ class ZdlListenerImpl : ZdlBaseListener() {
313316
}
314317

315318
override fun enterRelationship(ctx: ZdlParser.RelationshipContext) {
316-
val relationshipsCtx = (ctx as ParserRuleContext).getRuleContext(ZdlParser.RelationshipsContext::class, 0)
317-
val relationshipType = relationshipsCtx?.relationship_type()?.text ?: ""
319+
val parent = ctx.getParent() as ZdlParser.RelationshipsContext
320+
val relationshipType = parent.relationship_type().text
318321
val relationshipName = removeJavadoc(relationshipType + "_" + relationshipDescription(ctx.relationship_from().relationship_definition()) + "_" + relationshipDescription(ctx.relationship_to().relationship_definition()))
319322

320323
val relationship = FluentMap.build().with("type", relationshipType).with("name", relationshipName)
@@ -463,7 +466,7 @@ class ZdlListenerImpl : ZdlBaseListener() {
463466
override fun exitAggregate(ctx: ZdlParser.AggregateContext) { currentStack.removeLast() }
464467

465468
override fun enterAggregate_command(ctx: ZdlParser.Aggregate_commandContext) {
466-
val aggregateName = (ctx as ParserRuleContext).getRuleContext(ZdlParser.AggregateContext::class, 0)?.aggregate_name()?.text ?: ""
469+
val aggregateName = getText((ctx.getParent() as ZdlParser.AggregateContext).aggregate_name())!!
467470
val commandName = getText(ctx.aggregate_command_name())!!
468471
val location = "aggregates.$aggregateName.commands.$commandName"
469472
val parameter = ctx.aggregate_command_parameter()?.ID()?.text
@@ -509,8 +512,7 @@ class ZdlListenerImpl : ZdlBaseListener() {
509512
override fun exitService(ctx: ZdlParser.ServiceContext) { currentStack.removeLast() }
510513

511514
override fun enterService_method(ctx: ZdlParser.Service_methodContext) {
512-
val serviceCtx = (ctx as ParserRuleContext).getRuleContext(ZdlParser.ServiceContext::class, 0)
513-
val serviceName = serviceCtx?.service_name()?.text ?: ""
515+
val serviceName = getText((ctx.getParent() as ZdlParser.ServiceContext).service_name())!!
514516
val methodName = getText(ctx.service_method_name())!!
515517
val location = "services.$serviceName.methods.$methodName"
516518
val naturalId = if (ctx.service_method_parameter_natural() != null) true else null
@@ -584,6 +586,26 @@ class ZdlListenerImpl : ZdlBaseListener() {
584586

585587
override fun exitEvent(ctx: ZdlParser.EventContext) { currentStack.removeLast() }
586588

589+
override fun enterInput(ctx: ZdlParser.InputContext) {
590+
val name = ctx.input_name().text
591+
val jd = javadoc(ctx.javadoc())
592+
currentStack.addLast(processEntity(name, jd, null).with("type", "inputs"))
593+
model.appendTo("inputs", name, currentStack.last())
594+
currentCollection = "inputs"
595+
}
596+
597+
override fun exitInput(ctx: ZdlParser.InputContext) { currentStack.removeLast() }
598+
599+
override fun enterOutput(ctx: ZdlParser.OutputContext) {
600+
val name = ctx.output_name().text
601+
val jd = javadoc(ctx.javadoc())
602+
currentStack.addLast(processEntity(name, jd, null).with("type", "outputs"))
603+
model.appendTo("outputs", name, currentStack.last())
604+
currentCollection = "outputs"
605+
}
606+
607+
override fun exitOutput(ctx: ZdlParser.OutputContext) { currentStack.removeLast() }
608+
587609
override fun exitEveryRule(ctx: ParserRuleContext) { super.exitEveryRule(ctx) }
588610
override fun visitTerminal(node: TerminalNode) { super.visitTerminal(node) }
589611
override fun visitErrorNode(node: ErrorNode) { super.visitErrorNode(node) }

0 commit comments

Comments
 (0)