Git Product home page Git Product logo

typed-bnf-csharp-demo's Introduction

A JSON parser generated by typed bnf, which gives your cross-language semantic actions, and static type checking before codeg generation, and automatic type inference!

You can copy all .cs files from this project to get a complete Json parser. Such parser produces a JsonValue, in case you needs to handle Json serialization for variant types:

interface JsonValue
class JInt : JsonValue
class JFloat : JsonValue
class JList : JsonValue
...
class JNull : JsonValue

Running Code Generation

  1. Install
    • typed-bnf(requires Python 3.10+!)
    • dotnet runtime 4+
    • antlr4 command line tool
  2. bash build.sh(see concrete commands at build.sh)
  3. dotnet run to see the results.

Check out generated antlr g4 source at testjson.g4.

Overview

shape token {
    Text : str
}

// Using type alias is a workaround for C# codegen.
// C# has no type alias so we cannot refer to types
// via language-independent names.

// Some declarations. they specifies what you need to
// provide for generating this parser.
val nil : forall 'a. () -> list['a]
val append : forall 'a. (list['a], 'a) -> list['a]
val json_dict: (list[(str, JsonValue)]) -> JsonValue
val parse_int: (str) -> int
val parse_float: (str) -> float
val unesc_string: (str) -> str
val json_list: (list[JsonValue]) -> JsonValue
val json_int : (int) -> JsonValue
val json_float : (float) -> JsonValue
val json_string : (str) -> JsonValue
val json_bool : (int) -> JsonValue
val json_null : () -> JsonValue

start : json <EOF>      { $1 }


// parametric rules!
list[a] : a { [$1] }
        | list[a] a     { append($1, $2) }
        

seplist[sep, elt] : seplist[sep, elt] sep elt  { append($1, $3) }
                  | elt { [$1] }

nullable[a]  : a { $1 }
             |   { [] }
             
             
str : <str> { unesc_string($1.Text) }
pair : str ":" json   { ($1, $3) }


json : "[" nullable[seplist[",", json]] "]" { json_list($2) }
| "{" nullable[seplist[",", pair]] "}" { json_dict($2) }
| <int>             { json_int(parse_int($1.Text)) }
| <FLOAT>           { json_float(parse_float($1.Text)) }
| str               { json_string($1) }
| "null"            { json_null() }
| "true"            { json_bool(1) }
| "false"            { json_bool(0) }

%ignore <ws>


<ESCAPED_QUOTE> : "\\\""
<str> :   "\"" ( ESCAPED_QUOTE | !"\"")* "\""
<ws> : ("\r" | "\t" | "\n" | " ")
<exp> : ("E"|"e") DIGIT+
<int> : "-"? DIGIT+ exp?
<DIGIT> : [0 .. 9]
<FLOAT> : "-"? int "." int exp?

You can check JsonParse.cs to see how we interface with the Antlr4-generated parser.

typed-bnf-csharp-demo's People

Contributors

thautwarm avatar

Stargazers

Maple avatar vtheno avatar Zhihao Yuan avatar  avatar

Watchers

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