https://github.com/xirzo/jsonlexer
JSON Lexer that can be used to parse json files
https://github.com/xirzo/jsonlexer
json lexer parsing
Last synced: 2 months ago
JSON representation
JSON Lexer that can be used to parse json files
- Host: GitHub
- URL: https://github.com/xirzo/jsonlexer
- Owner: xirzo
- License: gpl-3.0
- Created: 2025-01-26T17:01:20.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2025-02-25T13:09:53.000Z (over 1 year ago)
- Last Synced: 2025-03-23T01:28:27.923Z (over 1 year ago)
- Topics: json, lexer, parsing
- Language: C
- Homepage:
- Size: 21.5 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# JSON Lexer ππ
This is a simple lexer written in C that tokenizes JSON files. It breaks down JSON input into tokens, making it easier to parse and process data. π
---
## How It Works π οΈ
The lexer reads a JSON string character by character and identifies tokens based on the JSON syntax. It uses a `Lexer` struct to keep track of the current state and a `Token` struct to represent each token.
### Key Functions
- `init_lexer`: Initializes the lexer with the input JSON string.
- `next_token`: Returns the next token in the input.
---
## Token Types π·οΈ
The lexer recognizes the following token types:
| Token Type | Example | Description |
|--------------|---------------|--------------------------------------|
| `TOKEN_LBR` | `{` | Left brace |
| `TOKEN_RBR` | `}` | Right brace |
| `TOKEN_COLON`| `:` | Colon |
| `TOKEN_COMMA`| `,` | Comma |
| `TOKEN_STRING`| `"key"` | String literal |
| `TOKEN_NUMBER`| `123` | Number literal |
| `TOKEN_BOOLEAN`| `true`/`false` | Boolean literal |
| `TOKEN_NULL` | `null` | Null literal |
| `TOKEN_EOF` | N/A | End of file/input |
| `TOKEN_ILLEGAL`| N/A | Illegal/unknown token |
---
## Example Usage π₯οΈ
Hereβs how you can use the lexer in your code:
```c
#include "lexer.h"
int main() {
const char* json = "{\"key\": \"value\"}";
struct Lexer lexer;
init_lexer(&lexer, json);
Token token;
do {
token = next_token(&lexer);
printf("Token: %s, Literal: %s\n", token_type_to_string(token.type), token.literal);
} while (token.type != TOKEN_EOF);
return 0;
}
```
---
## Running Tests π§ͺ
The project includes a suite of tests to verify the lexer's functionality. Run the tests to ensure everything works as expected:
### Test Cases
1. **Empty Object** `{}` β
2. **Key-Number Pair** `{"key": 123}` β
3. **Key-String Pair** `{"key": "value"}` β
4. **Key-Boolean Pair** `{"key": true}` β
5. **Key-Null Pair** `{"key": null}` β
---
## Project Structure π
```
json-lexer/
βββ lexer.h # Lexer header file
βββ lexer.c # Lexer implementation
βββ token.h # Token header file
βββ token.c # Token implementation
βββ main.c # Main program and tests
```