@@ -26,10 +26,62 @@ data class JSONLexeme(
26
26
val content : String? = null
27
27
)
28
28
29
+ /* *
30
+ * The lexer interface for a JSON parser.
31
+ *
32
+ * It contains two different methods:
33
+ * - [getNext] - Returns the next token
34
+ * - [getPosition] - Returns the data for a specific position
35
+ * It should only be used for error handling as it can be
36
+ * quite expensive to calculate this.
37
+ */
29
38
interface JSONLexer {
39
+ /* *
40
+ * Returns the next [JSONLexeme]. This lexeme is the next
41
+ * token that can be used by the `jsonParse` function.
42
+ *
43
+ * ## Functionality
44
+ *
45
+ * This function always returns the next token and won't return the same token
46
+ * twice (except if the last token is an `EOF` token). The tokenizer should
47
+ * disregard whitespace.
48
+ *
49
+ * If the token stream has ended an `EOF` (**E**nd **O**f **F**ile) token
50
+ * should be emitted.
51
+ *
52
+ * So if the source would be:
53
+ * ```json
54
+ * {
55
+ * "test": 42
56
+ * }
57
+ * ```
58
+ * it should be called `6` times and return the tokens:
59
+ * `CURLY_OPEN`, `STRING`, `COLON`, `NUMBER`, `CURLY_CLOSE`, `EOF`.
60
+ *
61
+ * ## Error handling
62
+ * If an error occurs whilst parsing the next token
63
+ * the `getNext` function will throw a [JSONLexerException].
64
+ *
65
+ * If the source code would be:
66
+ * ```json
67
+ * truw
68
+ * ```
69
+ * it should throw a [JSONLexerException] with the correct position
70
+ * data.
71
+ */
30
72
@Throws(JSONLexerException ::class )
31
73
fun getNext (): JSONLexeme
32
74
75
+ /* *
76
+ * Returns the position data for the given integer position.
77
+ *
78
+ * This should be used if there is an error as calculating the
79
+ * lines is a very expensive operation and does not need to be done
80
+ * in a different context.
81
+ *
82
+ * @param position The starting position where the error occurred
83
+ * @param length The length of the erroneous part
84
+ */
33
85
fun getPosition (position : Int , length : Int = 1): JSONPositionData
34
86
}
35
87
0 commit comments