@@ -49,10 +49,10 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
4949 ParserStructure (operations, followedBy.map { it.append(other) })
5050 }
5151
52- fun ParserStructure<T>.simplify (unconditionalModifications : List < UnconditionalModification < T >> ): ParserStructure <T > {
52+ fun ParserStructure<T>.simplify (): ParserStructure <T > {
5353 val newOperations = mutableListOf<ParserOperation <T >>()
5454 var currentNumberSpan: MutableList <NumberConsumer <T >>? = null
55- val unconditionalModificationsForTails = unconditionalModifications.toMutableList ()
55+ val unconditionalModificationsForTails = mutableListOf< UnconditionalModification < T >> ()
5656 // joining together the number consumers in this parser before the first alternative;
5757 // collecting the unconditional modifications to push them to the end of all the parser's branches.
5858 for (op in operations) {
@@ -73,7 +73,7 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
7373 }
7474 }
7575 val mergedTails = followedBy.flatMap {
76- val simplified = it.simplify(unconditionalModificationsForTails )
76+ val simplified = it.simplify()
7777 // parser `ParserStructure(emptyList(), p)` is equivalent to `p`,
7878 // unless `p` is empty. For example, ((a|b)|(c|d)) is equivalent to (a|b|c|d).
7979 // As a special case, `ParserStructure(emptyList(), emptyList())` represents a parser that recognizes an empty
@@ -82,38 +82,37 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
8282 simplified.followedBy.ifEmpty { listOf (simplified) }
8383 else
8484 listOf (simplified)
85- }.ifEmpty {
86- // preserving the invariant that `mergedTails` contains all unconditional modifications
87- listOf (ParserStructure (unconditionalModificationsForTails, emptyList()))
8885 }
8986 return if (currentNumberSpan == null ) {
9087 // the last operation was not a number span, or it was a number span that we are allowed to interrupt
88+ newOperations.addAll(unconditionalModificationsForTails)
9189 ParserStructure (newOperations, mergedTails)
9290 } else if (mergedTails.none {
9391 it.operations.firstOrNull()?.let { it is NumberSpanParserOperation } == true
9492 }) {
9593 // the last operation was a number span, but there are no alternatives that start with a number span.
9694 newOperations.add(NumberSpanParserOperation (currentNumberSpan))
95+ newOperations.addAll(unconditionalModificationsForTails)
9796 ParserStructure (newOperations, mergedTails)
9897 } else {
9998 val newTails = mergedTails.map {
10099 when (val firstOperation = it.operations.firstOrNull()) {
101100 is NumberSpanParserOperation -> {
102101 ParserStructure (
103- listOf (NumberSpanParserOperation (currentNumberSpan + firstOperation.consumers)) + it.operations.drop(
102+ listOf (NumberSpanParserOperation (currentNumberSpan + firstOperation.consumers)) + unconditionalModificationsForTails + it.operations.drop(
104103 1
105104 ),
106105 it.followedBy
107106 )
108107 }
109108
110109 null -> ParserStructure (
111- listOf (NumberSpanParserOperation (currentNumberSpan)),
110+ unconditionalModificationsForTails + listOf (NumberSpanParserOperation (currentNumberSpan)),
112111 it.followedBy
113112 )
114113
115114 else -> ParserStructure (
116- listOf (NumberSpanParserOperation (currentNumberSpan)) + it.operations,
115+ unconditionalModificationsForTails + listOf (NumberSpanParserOperation (currentNumberSpan)) + it.operations,
117116 it.followedBy
118117 )
119118 }
@@ -122,7 +121,7 @@ internal fun <T> List<ParserStructure<T>>.concat(): ParserStructure<T> {
122121 }
123122 }
124123 val naiveParser = foldRight(ParserStructure <T >(emptyList(), emptyList())) { parser, acc -> parser.append(acc) }
125- return naiveParser.simplify(emptyList() )
124+ return naiveParser.simplify()
126125}
127126
128127internal interface Copyable <Self > {
0 commit comments