Skip to content

Commit 22e3998

Browse files
committed
Complete Lexer
The last commit says that it finished the lexer, but this was a mistake. The literals still had to be added into the lexer. So this is "finishing" the lexer. It is not really "finished" as there are some small changes that can be made. (Like better error handling)
1 parent 83c1420 commit 22e3998

File tree

2 files changed

+48
-5
lines changed

2 files changed

+48
-5
lines changed

src/main/kotlin/lexer/StringJSONLexer.kt

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,23 @@ class StringJSONLexer(private val content: CharSequence) : JSONLexer {
2323
parseNumber(true)
2424
}
2525
'"' -> parseString()
26-
else -> TODO("Not implemented")
26+
't' -> parseLiteral("true", JSONLexemeType.TRUE)
27+
'f' -> parseLiteral("false", JSONLexemeType.FALSE)
28+
'n' -> parseLiteral("null", JSONLexemeType.NULL)
29+
else -> throw LexerException("Invalid token")
2730
}
2831
}
2932

33+
private fun parseLiteral(input: String, type: JSONLexemeType): JSONLexeme {
34+
for (i in input) {
35+
if(content.getChar(position) != i) {
36+
throw LexerException("Expected '$input', but got malformed input")
37+
}
38+
position++
39+
}
40+
return JSONLexeme(type)
41+
}
42+
3043
private fun charLexeme(type: JSONLexemeType): JSONLexeme {
3144
position++
3245
return JSONLexeme(type)
@@ -40,7 +53,7 @@ class StringJSONLexer(private val content: CharSequence) : JSONLexer {
4053
while (content.getChar(position) != '"' && content.getChar(position) != 0.toChar()) {
4154
if (content[position] == '\\') {
4255
position++
43-
builder.append(when(content.getChar(position)) {
56+
builder.append(when(val char = content.getChar(position)) {
4457
'"' -> '"'
4558
'\\' -> '\\'
4659
'/' -> '/'
@@ -50,7 +63,7 @@ class StringJSONLexer(private val content: CharSequence) : JSONLexer {
5063
'r' -> '\r'
5164
't' -> '\t'
5265
'u' -> parseUDigitNumber()
53-
else -> TODO()
66+
else -> throw LexerException("The character $char cannot be escaped") // TODO: Better error handling
5467
})
5568
position++
5669
continue

src/test/kotlin/lexer/StringJSONLexerTest.kt

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class StringJSONLexerTest {
7373
}
7474

7575
@Test
76-
fun `simple String test()`() {
76+
fun `simple String test`() {
7777
val inputs = arrayOf("\"\"", "\"Hello World\"")
7878

7979
inputs.forEach {
@@ -86,12 +86,42 @@ class StringJSONLexerTest {
8686
}
8787

8888
@Test
89-
fun `complex String test()`() {
89+
fun `complex String test`() {
9090
val input = "\"\\\" \\\\ \\/ \\b \\f \\n \\r \\t \\u000A\""
9191

9292
assertEquals(
9393
JSONLexeme(JSONLexemeType.STRING, "\" \\ / \b \u000C \n \r \t \u000A"),
9494
StringJSONLexer(input).getNext()
9595
)
9696
}
97+
98+
@Test
99+
fun `true literal test`() {
100+
val input = "true"
101+
102+
assertEquals(
103+
JSONLexeme(JSONLexemeType.TRUE),
104+
StringJSONLexer(input).getNext()
105+
)
106+
}
107+
108+
@Test
109+
fun `false literal test`() {
110+
val input = "false"
111+
112+
assertEquals(
113+
JSONLexeme(JSONLexemeType.FALSE),
114+
StringJSONLexer(input).getNext()
115+
)
116+
}
117+
118+
@Test
119+
fun `null literal test`() {
120+
val input = "null"
121+
122+
assertEquals(
123+
JSONLexeme(JSONLexemeType.NULL),
124+
StringJSONLexer(input).getNext()
125+
)
126+
}
97127
}

0 commit comments

Comments
 (0)