Crates.io | json_parser_hibskyi |
lib.rs | json_parser_hibskyi |
version | 0.1.0 |
source | src |
created_at | 2024-11-10 23:42:47.606085 |
updated_at | 2024-11-10 23:42:47.606085 |
description | A simplified JSON parser written in Rust using the Pest parsing library. |
homepage | |
repository | |
max_upload_size | |
id | 1443289 |
size | 27,950 |
This project implements a simplified JSON parser in Rust using the Pest parsing library. The parser can recognize and validate basic JSON structures, including objects, arrays, strings, numbers, booleans, and null values. The goal is to provide a foundation for parsing JSON-like data formats with essential functionality, without handling complex edge cases, such as nested escape characters in strings.
The JSON parser uses a custom Pest grammar defined in json_grammar.pest
. This grammar outlines rules for recognizing each component of JSON syntax. Key rules include:
{}
.[]
.true
, false
, and null
.json
rule, the parser recursively processes the JSON structure, checking each component against its expected structure. If the input adheres to the JSON syntax, the parse tree is generated.The parsing results are intended to be used for:
This section describes the grammar rules used in the simplified JSON parser, designed to handle the basic structure of JSON values, including objects, arrays, strings, numbers, booleans, and null values.
This rule is the entry point for parsing a JSON structure. It parses a value inside the start-of-input (SOI) and end-of-input (EOI) markers, ensuring that the entire content adheres to JSON rules.
json = { SOI ~ value ~ EOI }
The value rule defines the possible types that a JSON value can take, including objects, arrays, strings, numbers, booleans, and null.
value = _{ object | array | string | number | boolean | null }
An object in JSON is a collection of key-value pairs enclosed in curly braces {}. Each field consists of a string (the key) followed by a colon : and the corresponding value.
object = { "{" ~ (field ~ ("," ~ field)*)? ~ "}" }
A field consists of a string followed by a colon : and a value, representing a key-value pair in a JSON object.
field = { string ~ ":" ~ value }
An array in JSON is a collection of values enclosed in square brackets [], separated by commas.
array = { "[" ~ (value ~ ("," ~ value)*)? ~ "]" }
A string is a sequence of characters enclosed in double quotes " and can contain any characters except the closing quote. It is parsed as a sequence of any characters that are not a double quote.
string = @{ "\"" ~ (!"\"" ~ ANY)* ~ "\"" }
A number can either be a positive or negative integer, with an optional decimal point and digits following it. The rule accounts for numbers like -12.34, 0, or 123.
number = @{ "-"? ~ ("0" | ASCII_NONZERO_DIGIT ~ ASCII_DIGIT*) ~ ("." ~ ASCII_DIGIT+)? }
A boolean value can be either true or false.
boolean = { "true" | "false" }
The null rule matches the literal string "null", representing a null value in JSON.
null = { "null" }