1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright (c) 2024 Hemashushu <hippospark@gmail.com>, All rights reserved.
//
// This Source Code Form is subject to the terms of
// the Mozilla Public License version 2.0 and additional exceptions,
// more details in file LICENSE, LICENSE.additional and CONTRIBUTING.

mod lexer;
mod parser;
mod peekable_iterator;
mod writer;

use std::fmt::Display;

use chrono::{DateTime, FixedOffset};
use lexer::{filter, lex};
use peekable_iterator::PeekableIterator;

#[derive(Debug, PartialEq, Clone, Copy)]
pub enum NumberLiteral {
    Byte(i8),
    UByte(u8),
    Short(i16),
    UShort(u16),
    Int(i32),
    UInt(u32),
    Long(i64),
    ULong(u64),
    Float(f32),
    Double(f64),
}

#[derive(Debug, PartialEq)]
pub struct NameValuePair {
    pub name: String,
    pub value: Box<AsonNode>,
}

#[derive(Debug, PartialEq)]
pub struct VariantItem {
    pub name: String,
    pub value: Option<Box<AsonNode>>,
}

#[derive(Debug, PartialEq)]
pub enum AsonNode {
    Number(NumberLiteral),
    Boolean(bool),
    Char(char),
    String_(String),
    Date(DateTime<FixedOffset>),
    Variant(VariantItem),
    ByteData(Vec<u8>),
    Array(Vec<AsonNode>),
    Tuple(Vec<AsonNode>),
    Object(Vec<NameValuePair>),
}

#[derive(Debug)]
pub struct ParseError {
    pub message: String,
}

impl ParseError {
    pub fn new(message: &str) -> Self {
        Self {
            message: message.to_owned(),
        }
    }
}

impl Display for ParseError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "Parse error: {}", self.message)
    }
}

impl std::error::Error for ParseError {}

pub fn parse(s: &str) -> Result<AsonNode, ParseError> {
    let mut chars = s.chars();
    let mut char_iter = PeekableIterator::new(&mut chars, 3);
    let tokens = lex(&mut char_iter)?;
    let effective_tokens = filter(tokens);
    let mut token_iter = effective_tokens.into_iter();
    let mut peekable_token_iter = PeekableIterator::new(&mut token_iter, 2);
    parser::parse(&mut peekable_token_iter)
}

pub fn write(n: &AsonNode) -> String {
    writer::write(n)
}