|
1 | 1 | package io.github.json5.kotlin |
2 | 2 |
|
3 | 3 | /** |
4 | | - * Represents a token in the JSON5 syntax |
| 4 | + * Represents a token in the JSON5 syntax. |
| 5 | + * Each token has a type, a value (if applicable), and its line and column number in the source. |
| 6 | + * |
| 7 | + * Example usage: |
| 8 | + * ``` |
| 9 | + * // Null token: null |
| 10 | + * val nullToken = Token.NullToken(1, 5) |
| 11 | + * |
| 12 | + * // Boolean token: true |
| 13 | + * val trueToken = Token.BooleanToken(true, 2, 3) |
| 14 | + * |
| 15 | + * // String token: "hello" |
| 16 | + * val stringToken = Token.StringToken("hello", 3, 7) |
| 17 | + * |
| 18 | + * // Numeric token: 42, 3.14, 0xFF, Infinity, NaN |
| 19 | + * val intToken = Token.NumericToken(42.0, 4, 2) |
| 20 | + * val floatToken = Token.NumericToken(3.14, 4, 7) |
| 21 | + * val hexToken = Token.NumericToken(255.0, 4, 12) |
| 22 | + * val infToken = Token.NumericToken(Double.POSITIVE_INFINITY, 5, 1) |
| 23 | + * val nanToken = Token.NumericToken(Double.NaN, 5, 10) |
| 24 | + * |
| 25 | + * // Punctuator token: { } [ ] : , |
| 26 | + * val leftBrace = Token.PunctuatorToken("{", 6, 1) |
| 27 | + * val comma = Token.PunctuatorToken(",", 6, 2) |
| 28 | + * |
| 29 | + * // Identifier token: keyName, _foo, $bar |
| 30 | + * val identToken = Token.IdentifierToken("keyName", 7, 3) |
| 31 | + * |
| 32 | + * // End of file token |
| 33 | + * val eofToken = Token.EOFToken(8, 1) |
| 34 | + * ``` |
5 | 35 | */ |
6 | 36 | sealed class Token(val type: TokenType, val value: Any?, val line: Int, val column: Int) { |
| 37 | + /** |
| 38 | + * Represents a null literal token. |
| 39 | + * Example: `null` |
| 40 | + */ |
7 | 41 | class NullToken(line: Int, column: Int) : Token(TokenType.NULL, null, line, column) |
| 42 | + |
| 43 | + /** |
| 44 | + * Represents a boolean literal token. |
| 45 | + * Example: `true`, `false` |
| 46 | + * @param boolValue The boolean value (true or false) |
| 47 | + */ |
8 | 48 | class BooleanToken(val boolValue: Boolean, line: Int, column: Int) : Token(TokenType.BOOLEAN, boolValue, line, column) |
| 49 | + |
| 50 | + /** |
| 51 | + * Represents a string literal token. |
| 52 | + * Example: `"hello"`, `'world'` |
| 53 | + * @param stringValue The string value |
| 54 | + */ |
9 | 55 | class StringToken(val stringValue: String, line: Int, column: Int) : Token(TokenType.STRING, stringValue, line, column) |
| 56 | + |
| 57 | + /** |
| 58 | + * Represents a numeric literal token. |
| 59 | + * Example: `123`, `3.14`, `0xFF`, `Infinity`, `NaN` |
| 60 | + * @param numberValue The numeric value as Double |
| 61 | + */ |
10 | 62 | class NumericToken(val numberValue: Double, line: Int, column: Int) : Token(TokenType.NUMERIC, numberValue, line, column) |
| 63 | + |
| 64 | + /** |
| 65 | + * Represents a punctuator token. |
| 66 | + * Example: `{`, `}`, `[`, `]`, `:`, `,` |
| 67 | + * @param punctuator The punctuator character as string |
| 68 | + */ |
11 | 69 | class PunctuatorToken(val punctuator: String, line: Int, column: Int) : Token(TokenType.PUNCTUATOR, punctuator, line, column) |
| 70 | + |
| 71 | + /** |
| 72 | + * Represents an identifier token (unquoted property names). |
| 73 | + * Example: `foo`, `_bar`, `$baz` |
| 74 | + * @param identifierValue The identifier string |
| 75 | + */ |
12 | 76 | class IdentifierToken(val identifierValue: String, line: Int, column: Int) : Token(TokenType.IDENTIFIER, identifierValue, line, column) |
| 77 | + |
| 78 | + /** |
| 79 | + * Represents the end of the input stream. |
| 80 | + * Example: (no more tokens) |
| 81 | + */ |
13 | 82 | class EOFToken(line: Int, column: Int) : Token(TokenType.EOF, null, line, column) |
14 | 83 | } |
15 | 84 |
|
|
0 commit comments