Skip to content

Commit a53ce2e

Browse files
committed
Pretty refactoring
1 parent 0b47bc4 commit a53ce2e

File tree

10 files changed

+149
-146
lines changed

10 files changed

+149
-146
lines changed

solver/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ dependencies {
1313
implementation("org.jetbrains.kotlinx:kotlinx-cli:0.3.5")
1414
implementation("com.fasterxml.jackson.core:jackson-core:2.14.0")
1515
implementation("com.fasterxml.jackson.core:jackson-databind:2.14.0")
16+
implementation ("io.github.oshai:kotlin-logging-jvm:5.1.0")
1617

1718
testImplementation(kotlin("test"))
1819
testImplementation("org.junit.jupiter:junit-jupiter:5.8.1")

solver/src/main/kotlin/org/ucfs/descriptors/Descriptor.kt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,17 @@ data class Descriptor<InputNodeType>(
2727
*/
2828
val sppfNode: RangeSppfNode<InputNodeType>,
2929

30-
)
3130

31+
) {
32+
// debug only property
33+
val id = lastId++
34+
override fun toString(): String {
35+
return "${id}\t;" +
36+
"${inputPosition}\t;" +
37+
"${rsmState.id}\t;" +
38+
"(${gssNode.inputPosition}, ${gssNode.rsm.id})\t;" +
39+
"sppf: ${sppfNode.id} "
40+
}
41+
}
42+
43+
var lastId = 0

solver/src/main/kotlin/org/ucfs/descriptors/DescriptorsStorage.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package org.ucfs.descriptors
22

3+
import io.github.oshai.kotlinlogging.KotlinLogging
34
import org.ucfs.parser.ParsingException
45

56
/**
67
* Collection of default descriptors
78
* @param VertexType - type of vertex in input graph
89
*/
910
open class DescriptorsStorage<VertexType> {
11+
private val logger = KotlinLogging.logger {}
12+
1013
/**
1114
* Collection of already handled descriptors, accessible via descriptor's hashcode
1215
*/
@@ -31,10 +34,12 @@ open class DescriptorsStorage<VertexType> {
3134
}
3235

3336
fun add(descriptor: Descriptor<VertexType>) {
34-
if(!handledDescriptors.contains(descriptor)){
37+
logger.debug { "+d:${descriptor.id}" }
38+
if (!handledDescriptors.contains(descriptor)) {
3539
descriptorsToHandle.addLast(descriptor)
3640
}
3741
}
42+
3843
/**
3944
* Gets next descriptor to handle
4045
* @return default descriptor if there is available one, null otherwise

solver/src/main/kotlin/org/ucfs/parser/Gll.kt

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ucfs.parser
22

3+
import io.github.oshai.kotlinlogging.KotlinLogging
34
import org.ucfs.descriptors.Descriptor
45
import org.ucfs.gss.GssEdge
56
import org.ucfs.input.IInputGraph
@@ -18,6 +19,7 @@ import org.ucfs.sppf.node.*
1819
class Gll<VertexType, LabelType : ILabel> private constructor(
1920
override var ctx: Context<VertexType, LabelType>, private val engine: IIntersectionEngine
2021
) : IGll<VertexType, LabelType> {
22+
val logger = KotlinLogging.logger {}
2123

2224
companion object {
2325
/**
@@ -29,7 +31,7 @@ class Gll<VertexType, LabelType : ILabel> private constructor(
2931
fun <VertexType, LabelType : ILabel> gll(
3032
startState: RsmState, inputGraph: IInputGraph<VertexType, LabelType>
3133
): Gll<VertexType, LabelType> {
32-
val finalState = startState.outgoingEdges.get(0).destinationState
34+
val finalState = startState.outgoingEdges[0].destinationState
3335
return Gll(Context(startState, finalState, inputGraph), IntersectionEngine)
3436
}
3537
}
@@ -67,41 +69,47 @@ class Gll<VertexType, LabelType : ILabel> private constructor(
6769
ctx.descriptors.add(newDescriptor)
6870
}
6971

70-
fun isParseResult(descriptor: Descriptor<VertexType>, matchedRange: RangeSppfNode<VertexType>): Boolean {
72+
private fun isParseResult(matchedRange: RangeSppfNode<VertexType>): Boolean {
7173
return matchedRange.inputRange!!.from in ctx.input.getInputStartVertices()
7274
&& matchedRange.rsmRange!!.from == ctx.fictiveStartState
7375
&& matchedRange.rsmRange.to == ctx.fictiveFinalState
7476
}
77+
7578
/**
7679
* Processes descriptor
7780
* @param descriptor - descriptor to process
7881
*/
7982
override fun handleDescriptor(descriptor: Descriptor<VertexType>) {
8083
ctx.descriptors.addToHandled(descriptor)
84+
logger.debug { "\n${descriptor}\t" }
8185
if (descriptor.rsmState.isFinal) {
82-
val matchedRange = if (descriptor.sppfNode.type is EmptyType) {
83-
val node = getEpsilonRange(descriptor)
84-
//TODO fix
85-
// dirty hack: in fact it's equivavelnt descriptors
86-
// but only initial was added in handled set
87-
ctx.descriptors.addToHandled(Descriptor(descriptor.inputPosition,
88-
descriptor.gssNode, descriptor.rsmState, node))
89-
node
90-
} else {
91-
descriptor.sppfNode
92-
}
93-
for (poppedEdge in ctx.gss.pop(descriptor, matchedRange)) {
94-
handlePoppedGssEdge(poppedEdge, descriptor, matchedRange)
95-
}
96-
if (isParseResult(descriptor, matchedRange)) {
97-
98-
if(ctx.parseResult == null) {
99-
ctx.parseResult = matchedRange
100-
}
101-
ctx.parseResults.add(matchedRange)
102-
}
86+
handleTerminalRsmState(descriptor)
10387
}
10488
engine.handleEdges(this, descriptor)
10589
}
90+
91+
private fun handleTerminalRsmState(descriptor: Descriptor<VertexType>) {
92+
val matchedRange = if (descriptor.sppfNode.type is EmptyType) {
93+
val node = getEpsilonRange(descriptor)
94+
//TODO fix
95+
// dirty hack: in fact it's equivavelnt descriptors
96+
// but only initial was added in handled set
97+
ctx.descriptors.addToHandled(
98+
Descriptor(
99+
descriptor.inputPosition,
100+
descriptor.gssNode, descriptor.rsmState, node
101+
)
102+
)
103+
node
104+
} else {
105+
descriptor.sppfNode
106+
}
107+
for (poppedEdge in ctx.gss.pop(descriptor, matchedRange)) {
108+
handlePoppedGssEdge(poppedEdge, descriptor, matchedRange)
109+
}
110+
if (isParseResult(matchedRange)) {
111+
ctx.parseResults.add(matchedRange)
112+
}
113+
}
106114
}
107115

solver/src/main/kotlin/org/ucfs/parser/IGll.kt

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ interface IGll<InputNodeType, LabelType : ILabel> {
2525
* Main parsing loop. Iterates over available descriptors and processes them
2626
* @return Pair of derivation tree root and collection of reachability pairs
2727
*/
28-
fun parse(): RangeSppfNode<InputNodeType>? {
29-
ctx.parseResult = null
28+
fun parse(): Set<RangeSppfNode<InputNodeType>> {
29+
ctx.parseResults = HashSet()
3030
initDescriptors(ctx.input)
3131

3232
var curDescriptor = ctx.descriptors.nextToHandle()
@@ -36,9 +36,7 @@ interface IGll<InputNodeType, LabelType : ILabel> {
3636
curDescriptor = ctx.descriptors.nextToHandle()
3737
}
3838

39-
// assert(ctx.parseResult != null)
40-
// assert(ctx.parseResult!!.children.size == 1)
41-
return ctx.parseResult!!.children.get(0)
39+
return ctx.parseResults
4240
}
4341

4442
/**
@@ -56,7 +54,7 @@ interface IGll<InputNodeType, LabelType : ILabel> {
5654

5755
val gssNode = ctx.gss.getOrCreateNode(startVertex, ctx.fictiveStartState)
5856
val startDescriptor = Descriptor(
59-
startVertex, gssNode, ctx.fictiveStartState, getEmptyRange(true)
57+
startVertex, gssNode, ctx.fictiveStartState, getEmptyRange()
6058
)
6159
ctx.descriptors.add(startDescriptor)
6260
}
@@ -78,16 +76,16 @@ interface IGll<InputNodeType, LabelType : ILabel> {
7876
for (rangeToPop in positionToPops) {
7977
val leftSubRange = descriptor.sppfNode
8078
val rightSubRange = ctx.sppfStorage.addNonterminalNode(
81-
rangeToPop.inputRange!!, RsmRange(
82-
descriptor.rsmState, destinationRsmState
83-
), rsmStartState
84-
)
79+
rangeToPop.inputRange!!, RsmRange(
80+
descriptor.rsmState, destinationRsmState
81+
), rsmStartState
82+
)
8583

8684
val newSppfNode = ctx.sppfStorage.addIntermediateNode(leftSubRange, rightSubRange)
8785

8886
//TODO why these parameters???
8987
newDescriptor = Descriptor(
90-
rangeToPop.inputRange!!.to, descriptor.gssNode, destinationRsmState, newSppfNode
88+
rangeToPop.inputRange.to, descriptor.gssNode, destinationRsmState, newSppfNode
9189
)
9290
ctx.descriptors.add(newDescriptor)
9391
}
@@ -100,7 +98,7 @@ interface IGll<InputNodeType, LabelType : ILabel> {
10098
destinationRsmState: RsmState,
10199
terminal: ITerminal
102100
) {
103-
var terminalSppfNode = ctx.sppfStorage.addNode(
101+
val terminalSppfNode = ctx.sppfStorage.addNode(
104102
InputRange(
105103
descriptor.inputPosition,
106104
inputEdge.targetVertex,

solver/src/main/kotlin/org/ucfs/parser/context/Context.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import org.ucfs.sppf.node.RangeSppfNode
1313
* @param InputNodeType - type of vertex in input graph
1414
* @param LabelType - type of label on edges in input graph
1515
*/
16-
class Context<InputNodeType, LabelType : ILabel> (
16+
class Context<InputNodeType, LabelType : ILabel>(
1717
/**
1818
* Starting state of accepting Nonterminal in RSM
1919
*/
@@ -33,9 +33,8 @@ class Context<InputNodeType, LabelType : ILabel> (
3333
* Derivation trees storage
3434
*/
3535
val sppfStorage: SppfStorage<InputNodeType> = SppfStorage()
36-
36+
3737
val gss: GraphStructuredStack<InputNodeType> = GraphStructuredStack()
3838

39-
var parseResult: RangeSppfNode<InputNodeType>? = null
40-
var parseResults = ArrayList<RangeSppfNode<InputNodeType>>()
39+
var parseResults = HashSet<RangeSppfNode<InputNodeType>>()
4140
}

solver/src/main/kotlin/org/ucfs/sppf/SppfStorage.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.ucfs.sppf
22

3+
import io.github.oshai.kotlinlogging.KotlinLogging
34
import org.ucfs.rsm.RsmState
45
import org.ucfs.rsm.symbol.ITerminal
56
import org.ucfs.sppf.node.*
@@ -8,14 +9,19 @@ import org.ucfs.sppf.node.*
89
* @param InputEdgeType - type of vertex in input graph
910
*/
1011
open class SppfStorage<InputEdgeType> {
12+
private val logger = KotlinLogging.logger {}
13+
1114
/**
1215
* Collection of created sppfNodes with access and search in O(1) time
1316
*/
1417
private val createdSppfNodes: HashMap<RangeSppfNode<InputEdgeType>, RangeSppfNode<InputEdgeType>> = HashMap()
1518

1619

1720
private fun addNode(node: RangeSppfNode<InputEdgeType>): RangeSppfNode<InputEdgeType> {
18-
return createdSppfNodes.getOrPut(node, { node })
21+
22+
val sppfNode = createdSppfNodes.getOrPut(node) { node }
23+
logger.debug{"+sppf:${sppfNode.id}, "}
24+
return sppfNode
1925
}
2026

2127
/**
@@ -37,7 +43,8 @@ open class SppfStorage<InputEdgeType> {
3743
rsmState: RsmState
3844
): RangeSppfNode<InputEdgeType> {
3945
return addNode(
40-
input, rsmRange, EpsilonNonterminalType(rsmState))
46+
input, rsmRange, EpsilonNonterminalType(rsmState)
47+
)
4148
}
4249

4350
/**
@@ -79,8 +86,8 @@ open class SppfStorage<InputEdgeType> {
7986
if (!rangeNode.children.contains(valueNode)) {
8087
rangeNode.children.add(valueNode)
8188
}
82-
for(child in children){
83-
if (!valueNode.children.contains(child)){
89+
for (child in children) {
90+
if (!valueNode.children.contains(child)) {
8491
valueNode.children.add(child)
8592
}
8693
}

solver/src/main/kotlin/org/ucfs/sppf/node/RangeSppfNode.kt

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,52 +15,51 @@ import org.ucfs.rsm.symbol.ITerminal
1515
* May be used as extended packed sppfNode.
1616
* @param VertexType - type of vertex in input graph
1717
*/
18-
var lastId = 0
19-
2018
data class RangeSppfNode<VertexType>(
2119
val inputRange: InputRange<VertexType>?,
2220
val rsmRange: RsmRange?,
2321
val type: RangeType,
2422
) {
2523
val id: Int = lastId++
2624
val children = ArrayList<RangeSppfNode<VertexType>>()
25+
override fun toString(): String {
26+
return when (type) {
27+
is TerminalType<*> -> "Terminal $inputRange ${type.terminal}"
28+
is Range -> "Range $inputRange $rsmRange"
29+
is NonterminalType -> "Nonterminal ${type.startState.nonterminal.name} $inputRange $rsmRange"
30+
is IntermediateType<*> -> "Intermediate i:${type.inputPosition} rsm:${type.grammarSlot.id}"
31+
is EpsilonNonterminalType -> "Epsilon ${type.startState.id}"
32+
is EmptyType -> "Empty node"
33+
else -> "Unknown sppf node type!"
34+
}
35+
}
2736
}
2837

29-
fun <VertexType> getEmptyRange(isStart: Boolean = false): RangeSppfNode<VertexType> {
30-
val type = EmptyType()
31-
if(isStart) {
32-
type.isStart = isStart
33-
}
34-
return RangeSppfNode(null, null, type)
38+
fun <VertexType> getEmptyRange(): RangeSppfNode<VertexType> {
39+
return RangeSppfNode(null, null, EmptyType())
3540
}
3641

3742
data class InputRange<VertexType>(
3843
val from: VertexType,
3944
val to: VertexType,
40-
)
45+
) {
46+
override fun toString(): String = "input:[$from;$to]"
47+
}
4148

4249
data class RsmRange(
4350
val from: RsmState,
4451
val to: RsmState,
45-
)
52+
) {
53+
override fun toString(): String = "rsm:[${from.id};${to.id}]"
54+
}
4655

4756
interface RangeType
48-
var lastEmptyId = 0
57+
4958
object Range : RangeType
5059
data class TerminalType<T : ITerminal>(val terminal: T) : RangeType
5160
data class NonterminalType(val startState: RsmState) : RangeType
5261
data class EpsilonNonterminalType(val startState: RsmState) : RangeType
5362
data class IntermediateType<VertexType>(val grammarSlot: RsmState, val inputPosition: VertexType) : RangeType
54-
class EmptyType : RangeType {
55-
var isStart = false
56-
57-
@Override
58-
override fun equals(other: Any?): Boolean {
59-
return other is EmptyType
60-
}
63+
class EmptyType : RangeType
6164

62-
@Override
63-
override fun hashCode(): Int {
64-
return 12
65-
}
66-
}
65+
var lastId = 0

0 commit comments

Comments
 (0)