Code Monkey home page Code Monkey logo

intech-jsonc's Introduction

JSONC

Exercise July 12, 2016

A solution to parse JSON with comments (JSONC).

Exercise objectives

Parse a subset of JSON (objects, arrays, strings and integers) with the addition of single-line and multi-line comments.

For example:

{
    //Comment 1
    a = 1,
    b = [1, 2, 3], //Comment 2
    c = {
            /* Comment 3 on
                several lines */
            d = 1,
            e = "Hello World"
            // Comment 4
        }
}

The lexer

It produces tokens from an input string. Tokens are generic and have a type and a value.

It handles format errors the same way as int.Parse(...) does: if a character is not recognized, an UnknownTokenException is thrown.

Parsing

The goal it to produce an AST that represents the structure of the JSONC, where comments become meta-data to a node of the tree according to the following rules:

  • becomes meta-data for the following element
  • if there is no following element, becomes meta-data for the containing element

Example:

{
    // description for the following line
    a = 1,
    b = [1, 2, 3], // description for the following line
    c = {
            /* Long description
                for the following line */
            d = 1,
            e = 2
            // comment for the block
        }
}

Unit tests

The test cases data are provided by the TestsData class.

They pass:

tests passing image

Misc

The AST produced by the parser can easily be rendered as JSON because the comments are not stored on specific nodes but as meta-data of the node representing the element they are related to.

The JSON Visitor can just print the content of the nodes. Another visitor willing to take into account the comments will just have to access the Comments properties of the node.

intech-jsonc's People

Watchers

 avatar  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.