File tree Expand file tree Collapse file tree 2 files changed +48
-5
lines changed Expand file tree Collapse file tree 2 files changed +48
-5
lines changed Original file line number Diff line number Diff line change @@ -23,10 +23,23 @@ class StringJSONLexer(private val content: CharSequence) : JSONLexer {
23
23
parseNumber(true )
24
24
}
25
25
' "' -> 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" )
27
30
}
28
31
}
29
32
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
+
30
43
private fun charLexeme (type : JSONLexemeType ): JSONLexeme {
31
44
position++
32
45
return JSONLexeme (type)
@@ -40,7 +53,7 @@ class StringJSONLexer(private val content: CharSequence) : JSONLexer {
40
53
while (content.getChar(position) != ' "' && content.getChar(position) != 0 .toChar()) {
41
54
if (content[position] == ' \\ ' ) {
42
55
position++
43
- builder.append(when (content.getChar(position)) {
56
+ builder.append(when (val char = content.getChar(position)) {
44
57
' "' -> ' "'
45
58
' \\ ' -> ' \\ '
46
59
' /' -> ' /'
@@ -50,7 +63,7 @@ class StringJSONLexer(private val content: CharSequence) : JSONLexer {
50
63
' r' -> ' \r '
51
64
' t' -> ' \t '
52
65
' u' -> parseUDigitNumber()
53
- else -> TODO ()
66
+ else -> throw LexerException ( " The character $char cannot be escaped " ) // TODO: Better error handling
54
67
})
55
68
position++
56
69
continue
Original file line number Diff line number Diff line change @@ -73,7 +73,7 @@ class StringJSONLexerTest {
73
73
}
74
74
75
75
@Test
76
- fun `simple String test() ` () {
76
+ fun `simple String test` () {
77
77
val inputs = arrayOf(" \"\" " , " \" Hello World\" " )
78
78
79
79
inputs.forEach {
@@ -86,12 +86,42 @@ class StringJSONLexerTest {
86
86
}
87
87
88
88
@Test
89
- fun `complex String test() ` () {
89
+ fun `complex String test` () {
90
90
val input = " \"\\\" \\\\ \\ / \\ b \\ f \\ n \\ r \\ t \\ u000A\" "
91
91
92
92
assertEquals(
93
93
JSONLexeme (JSONLexemeType .STRING , " \" \\ / \b \u000C \n \r \t \u000A " ),
94
94
StringJSONLexer (input).getNext()
95
95
)
96
96
}
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
+ }
97
127
}
You can’t perform that action at this time.
0 commit comments