Click here to Skip to main content
15,886,137 members
Articles / Containers / Virtual Machine

Parsing Expression Grammar Support for C# 3.0 Part 1 - PEG Lib and Parser Generator

Rate me:
Please Sign up or sign in to vote.
4.95/5 (49 votes)
7 Oct 2008CPOL40 min read 202.9K   2.1K   118  
Introduction to the parsing method PEG with library and parser generator
/*Author:Martin.Holzherr;Date:20080922;Context:"PEG Support for C#";Licence:CPOL
 * <<History>> 
 *  20080922;V1.0 created
 * <</History>>
*/
using System;
using Peg.Base;
using System.IO;
using System.Diagnostics;

namespace PEG_Console_Parser
{

    
    class SamplePegConsoleParser
    {
        class NodePrinter
        {
            internal NodePrinter(PegBaseParser parser)
            {
                parser_ = parser;
            }
            internal string GetNodeName(PegNode n)
            {
                return parser_.GetRuleNameFromId(n.id_);
            }
            PegBaseParser parser_;
        }
        static int Main(string[] args)
        {
            if (args.Length < 1 || !File.Exists(args[0]))
            {
                Console.WriteLine("FATAL: First command line parameter must be an existing file");
                return -1;
            }
            try
            {
                FileLoader loader = new FileLoader(json_tree.json_tree.encodingClass,json_tree.json_tree.unicodeDetection, args[0]);
                Debug.Assert(!loader.IsBinaryFile());
                string src;
                if (loader.LoadFile(out src))
                {
                    json_tree.json_tree jsonParser = new json_tree.json_tree(src, Console.Out);
                    bool bMatches = jsonParser.json_text();
                    if (bMatches)
                    {
                        Console.WriteLine("SUCCESS: Json Parser matched input file '{0}'", args[0]);
                        TreePrint tprint = new TreePrint(Console.Out, src, 60, new NodePrinter(jsonParser).GetNodeName, false);
                        tprint.PrintTree(jsonParser.GetRoot(), 0, 0);
                    }
                    else
                    {
                        Console.WriteLine("FAILURE: Json Parser did not match input file '{0]'", args[0]);
                    }
                    return 0;
                }
                else
                {
                    Console.WriteLine("FATAL: File '{0}' could not be loaded", args[0]);
                    return -1;
                }
                
            }
            catch (Exception e)
            {
                Console.WriteLine("FATAL: Program terminated by exception '{0}'",e.Message);
                return -1;
            }
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service 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 Code Project Open License (CPOL)


Written By
Switzerland Switzerland
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions