Coder Social home page Coder Social logo

json_parser's Introduction

json_parser

A JSON parser written in Rust. Just for fun.

AST

{
  "hello": "world"
}

The corresponding AST is:

Object(
    ObjectAst {
        value: [
            PropertyAst {
                key: IdentifierAst {
                    value: StringAst {
                        value: "hello",
                        span: Span {
                            start: Loc {
                                line: 2,
                                column: 3,
                                offset: 4,
                            },
                            end: Loc {
                                line: 2,
                                column: 10,
                                offset: 11,
                            },
                        },
                    },
                    span: Span {
                        start: Loc {
                            line: 2,
                            column: 3,
                            offset: 4,
                        },
                        end: Loc {
                            line: 2,
                            column: 10,
                            offset: 11,
                        },
                    },
                },
                value: String(
                    StringAst {
                        value: "world",
                        span: Span {
                            start: Loc {
                                line: 2,
                                column: 12,
                                offset: 13,
                            },
                            end: Loc {
                                line: 2,
                                column: 19,
                                offset: 20,
                            },
                        },
                    },
                ),
                span: Span {
                    start: Loc {
                        line: 2,
                        column: 3,
                        offset: 4,
                    },
                    end: Loc {
                        line: 2,
                        column: 19,
                        offset: 20,
                    },
                },
            },
        ],
        span: Span {
            start: Loc {
                line: 1,
                column: 1,
                offset: 0,
            },
            end: Loc {
                line: 3,
                column: 2,
                offset: 22,
            },
        },
    },
)

Visit

You can visit or modify the nodes of a JSON AST by implementing the visit trait.

struct Visitor {
  pub property_pos: (usize, usize), // (start, end)
  pub merged_string: String, // xxx_yyy
}

impl Visit for Visitor {
  fn visit_property(&mut self, ast: &mut parser::PropertyAst) {
    self.property_pos = (ast.span.start.offset, ast.span.end.offset);

    self.visit_identifier(&mut ast.key);
    self.visit_property_value(&mut ast.value);
  }

  fn visit_string(&mut self, ast: &mut parser::StringAst) {
    if self.merged_string.is_empty() {
      self.merged_string.push_str(&ast.value);
    } else {
      self.merged_string.push_str(&format!("_{}", ast.value));
    }
  }
}

fn test_visit() {
  let mut json = "{\"hello\":\"world\"}".parse::<Json>().unwrap();

  let mut visitor = Visitor {
    property_pos: (0, 0),
    merged_string: String::new(),
  };

  visitor.visit_json(&mut json);
  assert_eq!(visitor.property_pos, (1, 16));
  assert_eq!(visitor.merged_string, "hello_world");
}

json_parser's People

Contributors

codpoe avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.