@@ -9,6 +9,8 @@ import au.com.dius.pact.core.model.matchingrules.EachValueMatcher
9
9
import au.com.dius.pact.core.model.matchingrules.EqualsMatcher
10
10
import au.com.dius.pact.core.model.matchingrules.IncludeMatcher
11
11
import au.com.dius.pact.core.model.matchingrules.MatchingRule
12
+ import au.com.dius.pact.core.model.matchingrules.MinTypeMatcher
13
+ import au.com.dius.pact.core.model.matchingrules.MaxTypeMatcher
12
14
import au.com.dius.pact.core.model.matchingrules.NotEmptyMatcher
13
15
import au.com.dius.pact.core.model.matchingrules.NumberTypeMatcher
14
16
import au.com.dius.pact.core.model.matchingrules.RegexMatcher
@@ -26,10 +28,13 @@ class MatcherDefinitionLexer(expression: String): StringLexer(expression) {
26
28
27
29
fun matchInteger () = matchRegex(INTEGER_LITERAL ).isNotEmpty()
28
30
31
+ fun matchWholeNumber () = matchRegex(NUMBER_LITERAL ).isNotEmpty()
32
+
29
33
fun matchBoolean () = matchRegex(BOOLEAN_LITERAL ).isNotEmpty()
30
34
31
35
companion object {
32
36
val INTEGER_LITERAL = Regex (" ^-?\\ d+" )
37
+ val NUMBER_LITERAL = Regex (" ^\\ d+" )
33
38
val DECIMAL_LITERAL = Regex (" ^-?\\ d+\\ .\\ d+" )
34
39
val BOOLEAN_LITERAL = Regex (" ^(true|false)" )
35
40
}
@@ -90,23 +95,15 @@ class MatcherDefinitionParser(private val lexer: MatcherDefinitionLexer) {
90
95
91
96
// matchingDefinitionExp returns [ MatchingRuleDefinition value ] :
92
97
// (
93
- // 'matching' LEFT_BRACKET matchingRule RIGHT_BRACKET {
94
- // if ($matchingRule.reference != null) {
95
- // $value = new MatchingRuleDefinition($matchingRule.value, $matchingRule.reference, $matchingRule.generator);
96
- // } else {
97
- // $value = new MatchingRuleDefinition($matchingRule.value, $matchingRule.rule, $matchingRule.generator);
98
- // }
99
- // }
100
- // | 'notEmpty' LEFT_BRACKET primitiveValue RIGHT_BRACKET { $value = new MatchingRuleDefinition($primitiveValue.value, NotEmptyMatcher.INSTANCE, null).withType($primitiveValue.type); }
101
- // | 'eachKey' LEFT_BRACKET e=matchingDefinitionExp RIGHT_BRACKET { if ($e.value != null) { $value = new MatchingRuleDefinition(null, new EachKeyMatcher($e.value), null); } }
102
- // | 'eachValue' LEFT_BRACKET e=matchingDefinitionExp RIGHT_BRACKET {
103
- // if ($e.value != null) {
104
- // $value = new MatchingRuleDefinition(null, ValueType.Unknown, List.of((Either<MatchingRule, MatchingReference>) new Either.A(new EachValueMatcher($e.value))), null);
105
- // }
106
- // }
98
+ // 'matching' LEFT_BRACKET matchingRule RIGHT_BRACKET
99
+ // | 'notEmpty' LEFT_BRACKET primitiveValue RIGHT_BRACKET
100
+ // | 'eachKey' LEFT_BRACKET e=matchingDefinitionExp RIGHT_BRACKET
101
+ // | 'eachValue' LEFT_BRACKET e=matchingDefinitionExp RIGHT_BRACKET
102
+ // | 'atLeast' LEFT_BRACKET DIGIT+ RIGHT_BRACKET
103
+ // | 'atMost' LEFT_BRACKET DIGIT+ RIGHT_BRACKET
107
104
// )
108
105
// ;
109
- @Suppress(" ReturnCount" )
106
+ @Suppress(" ReturnCount" , " LongMethod " )
110
107
fun matchingDefinitionExp (): Result <MatchingRuleDefinition , String > {
111
108
return when {
112
109
lexer.matchString(" matching" ) -> {
@@ -189,6 +186,38 @@ class MatcherDefinitionParser(private val lexer: MatcherDefinitionLexer) {
189
186
Result .Err (" Was expecting a '(' at index ${lexer.index} " )
190
187
}
191
188
}
189
+ lexer.matchString(" atLeast" ) -> {
190
+ if (matchChar(' (' )) {
191
+ when (val lengthResult = unsignedNumber()) {
192
+ is Result .Ok -> {
193
+ if (matchChar(' )' )) {
194
+ Result .Ok (MatchingRuleDefinition (" " , MinTypeMatcher (lengthResult.value), null ))
195
+ } else {
196
+ Result .Err (" Was expecting a ')' at index ${lexer.index} " )
197
+ }
198
+ }
199
+ is Result .Err -> return lengthResult
200
+ }
201
+ } else {
202
+ Result .Err (" Was expecting a '(' at index ${lexer.index} " )
203
+ }
204
+ }
205
+ lexer.matchString(" atMost" ) -> {
206
+ if (matchChar(' (' )) {
207
+ when (val lengthResult = unsignedNumber()) {
208
+ is Result .Ok -> {
209
+ if (matchChar(' )' )) {
210
+ Result .Ok (MatchingRuleDefinition (" " , MaxTypeMatcher (lengthResult.value), null ))
211
+ } else {
212
+ Result .Err (" Was expecting a ')' at index ${lexer.index} " )
213
+ }
214
+ }
215
+ is Result .Err -> return lengthResult
216
+ }
217
+ } else {
218
+ Result .Err (" Was expecting a '(' at index ${lexer.index} " )
219
+ }
220
+ }
192
221
else -> Result .Err (" Was expecting a matching rule definition type at index ${lexer.index} " )
193
222
}
194
223
}
@@ -346,6 +375,15 @@ class MatcherDefinitionParser(private val lexer: MatcherDefinitionLexer) {
346
375
Result .Err (" Was expecting a ',' at index ${lexer.index} " )
347
376
}
348
377
378
+ private fun unsignedNumber (): Result <Int , String > {
379
+ lexer.skipWhitespace()
380
+ return if (lexer.matchWholeNumber()) {
381
+ Result .Ok (lexer.lastMatch!! .toInt())
382
+ } else {
383
+ Result .Err (" Was expecting an unsigned number at index ${lexer.index} " )
384
+ }
385
+ }
386
+
349
387
private fun matchEqualOrType (equalTo : Boolean ) = if (matchChar(' ,' )) {
350
388
when (val primitiveValueResult = primitiveValue()) {
351
389
is Result .Ok -> {
@@ -435,10 +473,10 @@ class MatcherDefinitionParser(private val lexer: MatcherDefinitionLexer) {
435
473
}
436
474
437
475
// primitiveValue returns [ String value, ValueType type ] :
438
- // string { $value = $string.contents; $type = ValueType.String; }
439
- // | v=DECIMAL_LITERAL { $value = $v.getText(); $type = ValueType.Decimal; }
440
- // | v=INTEGER_LITERAL { $value = $v.getText(); $type = ValueType.Integer; }
441
- // | v=BOOLEAN_LITERAL { $value = $v.getText(); $type = ValueType.Boolean; }
476
+ // string
477
+ // | v=DECIMAL_LITERAL
478
+ // | v=INTEGER_LITERAL
479
+ // | v=BOOLEAN_LITERAL
442
480
// ;
443
481
fun primitiveValue (): Result <Pair <String ?, ValueType >, String> {
444
482
lexer.skipWhitespace()
@@ -490,7 +528,7 @@ class MatcherDefinitionParser(private val lexer: MatcherDefinitionLexer) {
490
528
}
491
529
}
492
530
493
- @Suppress(" ComplexMethod" )
531
+ @Suppress(" ComplexMethod" , " LongMethod " )
494
532
fun processRawString (rawString : String ): Result <String , String > {
495
533
val buffer = StringBuilder (rawString.length)
496
534
val chars = rawString.chars().iterator()
0 commit comments