Click here to Skip to main content
Click here to Skip to main content

Implementing Programming Languages Using C# 4.0

By , 12 Jul 2012
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Diggins.Jigsaw
{
    public class JsonGrammar : SharedGrammar
    {
        new public static Rule Integer = Node(SharedGrammar.Integer);
        new public static Rule Float = Node(SharedGrammar.Float);
        public static Rule True = Node(MatchString("true"));
        public static Rule False = Node(MatchString("false"));
        public static Rule Null = Node(MatchString("null"));
        public static Rule UnicodeChar = MatchString("\\u") + HexDigit + HexDigit + HexDigit + HexDigit;
        public static Rule ControlChar = MatchChar('\\') + CharSet("\"\'\\/bfnt");
        public static Rule DoubleQuotedString = Node(MatchChar('"') + ZeroOrMore(UnicodeChar | ControlChar | ExceptCharSet("\"\\")) + MatchChar('"'));
        public static Rule SingleQuotedString = Node(MatchChar('\'') + ZeroOrMore(UnicodeChar | ControlChar | ExceptCharSet("'\\")) + MatchChar('\''));
        public static Rule String = Node(DoubleQuotedString | SingleQuotedString);
        public static Rule Number = Float | Integer;
        public static Rule Value = Node(Recursive(() => String | Number | Object | Array | True | False | Null));
        public static Rule Pair = Node(DoubleQuotedString + WS + CharToken(':') + Value + WS);
        public static Rule Array = Node(CharToken('[') + CommaDelimited(Value) + WS + CharToken(']'));
        public static Rule Object = Node(CharToken('{') + CommaDelimited(Pair) + WS + CharToken('}'));
        static JsonGrammar() { InitGrammar(typeof(JsonGrammar)); }
    }
}

By viewing downloads associated with this article you agree to the Terms of use and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The MIT License

About the Author

Christopher Diggins
Software Developer Autodesk
Canada Canada
Member
This article was written by Christopher Diggins, a computer science nerd who currently works at Autodesk as an SDK specialist.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130516.1 | Last Updated 12 Jul 2012
Article Copyright 2011 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid