1
1
package au.com.dius.pact.core.support
2
2
3
+ import au.com.dius.pact.core.support.parsers.StringLexer
3
4
import com.github.michaelbull.result.Err
4
5
import com.github.michaelbull.result.Ok
5
6
import com.github.michaelbull.result.Result
@@ -22,68 +23,52 @@ data class Version(
22
23
23
24
@JvmStatic
24
25
fun parse (version : String ): Result <Version , String > {
25
- var buffer = version
26
- var index = 0
26
+ val lexer = StringLexer (version)
27
27
28
- val major = when (val result = parseInt(buffer, index)) {
29
- is Ok -> {
30
- buffer = result.value.second
31
- index = result.value.third
32
- result.value.first
33
- }
28
+ val major = when (val result = parseInt(lexer)) {
29
+ is Ok -> result.value
34
30
is Err -> return result
35
31
}
36
32
37
- when (val dot = parseChar(' .' , buffer, index)) {
38
- is Ok -> {
39
- buffer = dot.value.first
40
- index = dot.value.second
41
- }
42
- is Err -> {
43
- return dot
44
- }
33
+ val err = parseChar(' .' , lexer)
34
+ if (err != null ) {
35
+ return Err (err)
45
36
}
46
37
47
- val minor = when (val result = parseInt(buffer, index)) {
48
- is Ok -> {
49
- buffer = result.value.second
50
- index = result.value.third
51
- result.value.first
52
- }
38
+ val minor = when (val result = parseInt(lexer)) {
39
+ is Ok -> result.value
53
40
is Err -> return result
54
41
}
55
42
56
- val dot = parseChar(' .' , buffer, index)
57
43
return when {
58
- dot is Ok -> {
59
- buffer = dot.value.first
60
- index = dot.value.second
61
- when (val result = parseInt(buffer, index)) {
62
- is Ok -> Ok (Version (major, minor, result.value.first))
44
+ lexer.peekNextChar() == ' .' -> {
45
+ lexer.advance()
46
+ when (val result = parseInt(lexer)) {
47
+ is Ok -> if (lexer.empty) {
48
+ Ok (Version (major, minor, result.value))
49
+ } else {
50
+ Err (" Unexpected characters '${lexer.remainder} ' at index ${lexer.index} " )
51
+ }
63
52
is Err -> result
64
53
}
65
54
}
66
- buffer.isEmpty() -> Ok (Version (major, minor))
67
- else -> Err (" Unexpected character '${buffer[ 0 ] } ' at index $index " )
55
+ lexer.empty -> Ok (Version (major, minor))
56
+ else -> Err (" Unexpected characters '${lexer.remainder } ' at index ${lexer. index} " )
68
57
}
69
58
}
70
59
71
- private fun parseChar (c : Char , buffer : String , index : Int ): Result <Pair <String , Int >, String> {
72
- return when {
73
- buffer.isNotEmpty() && buffer[0 ] == c -> {
74
- Ok (buffer.substring(1 ) to (index + 1 ))
75
- }
76
- else -> Err (" Was expecting a $c at index $index " )
60
+ private fun parseChar (c : Char , lexer : StringLexer ): String? {
61
+ return when (val ch = lexer.nextChar()) {
62
+ null -> " Was expecting a '$c ' at index ${lexer.index} but got the end of the input"
63
+ c -> null
64
+ else -> " Was expecting a '$c ' at index ${lexer.index - 1 } but got '$ch '"
77
65
}
78
66
}
79
67
80
- private fun parseInt (buffer : String , index : Int ): Result <Triple <Int , String , Int >, String> {
81
- return when (val result = INT .find(buffer)) {
82
- null -> Err (" Was expecting an integer at index $index " )
83
- else -> {
84
- val i = result.value.toInt()
85
- Ok (Triple (i, buffer.substring(result.value.length), index + result.value.length))
86
- }
68
+ private fun parseInt (lexer : StringLexer ): Result <Int , String > {
69
+ return when (val result = lexer.matchRegex(INT )) {
70
+ null -> Err (" Was expecting an integer at index ${lexer.index} " )
71
+ else -> Ok (result.toInt())
87
72
}
88
73
}
89
74
}
0 commit comments