-
-
Notifications
You must be signed in to change notification settings - Fork 481
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: extract common lexer functions from version parser #1615
- Loading branch information
1 parent
0b1be93
commit 739a40d
Showing
4 changed files
with
89 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
56 changes: 56 additions & 0 deletions
56
core/support/src/main/kotlin/au/com/dius/pact/core/support/parsers/StringLexer.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package au.com.dius.pact.core.support.parsers | ||
|
||
class StringLexer(private val buffer: String) { | ||
var index = 0 | ||
private set | ||
|
||
val empty: Boolean | ||
get() = index >= buffer.length | ||
|
||
val remainder: String | ||
get() = buffer.substring(index) | ||
|
||
fun nextChar(): Char? { | ||
val c = peekNextChar() | ||
if (c != null) { | ||
index++ | ||
} | ||
return c | ||
} | ||
|
||
fun peekNextChar(): Char? { | ||
return if (empty) { | ||
null | ||
} else { | ||
buffer[index] | ||
} | ||
} | ||
|
||
fun advance() { | ||
advance(1) | ||
} | ||
|
||
fun advance(count: Int) { | ||
for (i in 0 until count) { | ||
index++ | ||
} | ||
} | ||
|
||
fun skipWhitespace() { | ||
var next = peekNextChar() | ||
while (next != null && Character.isWhitespace(next)) { | ||
advance() | ||
next = peekNextChar() | ||
} | ||
} | ||
|
||
fun matchRegex(regex: Regex): String? { | ||
return when (val result = regex.find(buffer.substring(index))) { | ||
null -> null | ||
else -> { | ||
index += result.value.length | ||
result.value | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters