@@ -41,32 +41,24 @@ internal class ParserStructure<in Output>(
4141 " ${operations.joinToString(" , " )} (${followedBy.joinToString(" ;" )} )"
4242}
4343
44- // TODO: O(size of the resulting parser ^ 2), but can be O(size of the resulting parser)
4544internal fun <T > List<ParserStructure<T>>.concat (): ParserStructure <T > {
46- fun <T > ParserStructure<T>.append (other : ParserStructure <T >): ParserStructure <T > = if (followedBy.isEmpty()) {
47- ParserStructure (operations + other.operations, other.followedBy)
48- } else {
49- ParserStructure (operations, followedBy.map { it.append(other) })
50- }
51-
5245 fun mergeOperations (
5346 baseOperations : List <ParserOperation <T >>,
5447 numberSpan : List <NumberConsumer <T >>? ,
5548 unconditionalModifications : List <UnconditionalModification <T >>,
56- operationsToMerge : List <ParserOperation <T >>,
57- followedBy : List <ParserStructure <T >>
49+ simplifiedParserStructure : ParserStructure <T >,
5850 ): ParserStructure <T > {
5951 val operations = buildList {
6052 addAll(baseOperations)
61- when (val firstOperation = operationsToMerge .firstOrNull()) {
53+ when (val firstOperation = simplifiedParserStructure.operations .firstOrNull()) {
6254 is NumberSpanParserOperation -> {
6355 if (numberSpan != null ) {
6456 add(NumberSpanParserOperation (numberSpan + firstOperation.consumers))
6557 } else {
6658 add(firstOperation)
6759 }
6860 addAll(unconditionalModifications)
69- addAll(operationsToMerge .drop(1 ))
61+ addAll(simplifiedParserStructure.operations .drop(1 ))
7062 }
7163 null -> {
7264 if (numberSpan != null ) {
@@ -79,11 +71,11 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
7971 add(NumberSpanParserOperation (numberSpan))
8072 }
8173 addAll(unconditionalModifications)
82- addAll(operationsToMerge )
74+ addAll(simplifiedParserStructure.operations )
8375 }
8476 }
8577 }
86- return ParserStructure (operations, followedBy)
78+ return ParserStructure (operations, simplifiedParserStructure. followedBy)
8779 }
8880
8981 fun ParserStructure<T>.simplifyAndAppend (other : ParserStructure <T >): ParserStructure <T > {
@@ -122,7 +114,7 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
122114 listOf (simplified)
123115 }.ifEmpty {
124116 if (other.operations.isNotEmpty()) {
125- return mergeOperations(newOperations, currentNumberSpan, unconditionalModifications, other.operations, other.followedBy )
117+ return mergeOperations(newOperations, currentNumberSpan, unconditionalModifications, other)
126118 }
127119 other.followedBy
128120 }
@@ -139,7 +131,7 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
139131 ParserStructure (newOperations, mergedTails)
140132 } else {
141133 val newTails = mergedTails.map {
142- mergeOperations(emptyList(), currentNumberSpan, unconditionalModifications, it.operations, it.followedBy )
134+ mergeOperations(emptyList(), currentNumberSpan, unconditionalModifications, it)
143135 }
144136 ParserStructure (newOperations, newTails)
145137 }
0 commit comments