Click here to Skip to main content
15,881,172 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.4K   2.1K   118  
Introduction to the parsing method PEG with library and parser generator
/* created on 15.09.2008 10:44:55 from peg generator V1.0*/

using Peg.Base;
using System;
using System.IO;
using System.Text;
namespace json
{
      
      enum Ejson{json_text= 1, expect_file_end= 2, top_element= 3, @object= 4, 
                  members= 5, pair= 6, array= 7, elements= 8, value= 9, @string= 10, 
                  @char= 11, escape= 12, number= 13, @int= 14, frac= 15, exp= 16, 
                  control_chars= 17, unicode_char= 18, S= 19};
      class json : PegCharParser 
      {
        
         #region Input Properties
        public static EncodingClass encodingClass = EncodingClass.unicode;
        public static UnicodeDetection unicodeDetection = UnicodeDetection.FirstCharIsAscii;
        #endregion Input Properties
        #region Constructors
        public json()
            : base()
        {
            
        }
        public json(string src,TextWriter FerrOut)
			: base(src,FerrOut)
        {
            
        }
        #endregion Constructors
        #region Overrides
        public override string GetRuleNameFromId(int id)
        {
            try
            {
                   Ejson ruleEnum = (Ejson)id;
                    string s= ruleEnum.ToString();
                    int val;
                    if( int.TryParse(s,out val) ){
                        return base.GetRuleNameFromId(id);
                    }else{
                        return s;
                    }
            }
            catch (Exception)
            {
                return base.GetRuleNameFromId(id);
            }
        }
        public override void GetProperties(out EncodingClass encoding, out UnicodeDetection detection)
        {
            encoding = encodingClass;
            detection = unicodeDetection;
        } 
        #endregion Overrides
		#region Grammar Rules
        public bool json_text()    /*[1]json_text:		S top_element expect_file_end			;*/
        {

           return And(()=>    S() && top_element() && expect_file_end() );
		}
        public bool expect_file_end()    /*[2]expect_file_end:	!./ WARNING<"non-json stuff before end of file">;*/
        {

           return   
                     Not(()=> Any() )
                  || Warning("non-json stuff before end of file");
		}
        public bool top_element()    /*[3]top_element:		object / array  /
			FATAL<"json file must start with '{' or '['">	;*/
        {

           return   
                     @object()
                  || array()
                  || Fatal("json file must start with '{' or '['");
		}
        public bool @object()    /*[4]object:  		'{' S (&'}'/members)  @'}' S			;*/
        {

           return And(()=>  
                     Char('{')
                  && S()
                  && (    Peek(()=> Char('}') ) || members())
                  && (    Char('}') || Fatal("<<'}'>> expected"))
                  && S() );
		}
        public bool members()    /*[5]members: 		pair S (',' S pair S)*				;*/
        {

           return And(()=>  
                     pair()
                  && S()
                  && OptRepeat(()=>    
                      And(()=>    Char(',') && S() && pair() && S() ) ) );
		}
        public bool pair()    /*[6]pair:		@string S @':' S value				;*/
        {

           return And(()=>  
                     (    @string() || Fatal("<<string>> expected"))
                  && S()
                  && (    Char(':') || Fatal("<<':'>> expected"))
                  && S()
                  && value() );
		}
        public bool array()    /*[7]array:  		'[' S (&']'/elements)  @']' S			;*/
        {

           return And(()=>  
                     Char('[')
                  && S()
                  && (    Peek(()=> Char(']') ) || elements())
                  && (    Char(']') || Fatal("<<']'>> expected"))
                  && S() );
		}
        public bool elements()    /*[8]elements: 		value S (','S  value S)*			;*/
        {

           return And(()=>  
                     value()
                  && S()
                  && OptRepeat(()=>    
                      And(()=>    Char(',') && S() && value() && S() ) ) );
		}
        public bool value()    /*[9]value:    		@(string / number / object / 
				array / 'true' / 'false' / 'null')	;*/
        {

           return   
                     (    
                         @string()
                      || number()
                      || @object()
                      || array()
                      || Char('t','r','u','e')
                      || Char('f','a','l','s','e')
                      || Char('n','u','l','l'))
                  || Fatal("<<(string  or  number  or  object  or  array  or  'true'  or  'false'  or  'null')>> expected");
		}
        public bool @string()    /*[10]string:	 	'"' char* @'"'					;*/
        {

           return And(()=>  
                     Char('"')
                  && OptRepeat(()=> @char() )
                  && (    Char('"') || Fatal("<<'\"'>> expected")) );
		}
        public bool @char()    /*[11]char:  		escape / !(["\\]/control_chars)unicode_char	;*/
        {

           return   
                     escape()
                  || And(()=>    
                         Not(()=>     OneOf("\"\\") || control_chars() )
                      && unicode_char() );
		}
        public bool escape()    /*[12]escape:		'\\' ( ["\\/bfnrt] / 
			'u' ([0-9A-Fa-f]{4}/FATAL<"4 hex digits expected">)/
			     FATAL<"illegal escape">);*/
        {

           return And(()=>  
                     Char('\\')
                  && (    
                         OneOf(optimizedCharset0)
                      || And(()=>      
                               Char('u')
                            && (        
                                       ForRepeat(4,4,()=> In('0','9', 'A','F', 'a','f') )
                                    || Fatal("4 hex digits expected")) )
                      || Fatal("illegal escape")) );
		}
        public bool number()    /*[13]number:		'-'? int frac? exp?				;*/
        {

           return And(()=>  
                     Option(()=> Char('-') )
                  && @int()
                  && Option(()=> frac() )
                  && Option(()=> exp() ) );
		}
        public bool @int()    /*[14]int:		'0'/ [1-9][0-9]*				;*/
        {

           return   
                     Char('0')
                  || And(()=>    
                         In('1','9')
                      && OptRepeat(()=> In('0','9') ) );
		}
        public bool frac()    /*[15]frac:		'.' [0-9]+					;*/
        {

           return And(()=>    Char('.') && PlusRepeat(()=> In('0','9') ) );
		}
        public bool exp()    /*[16]exp:		[eE] [-+] [0-9]+				;*/
        {

           return And(()=>  
                     OneOf("eE")
                  && OneOf("-+")
                  && PlusRepeat(()=> In('0','9') ) );
		}
        public bool control_chars()    /*[17]control_chars:	[#x0-#x1F]					;*/
        {

           return In('\u0000','\u001f');
		}
        public bool unicode_char()    /*[18]unicode_char:	[#x0-#xFFFF]					;*/
        {

           return In('\u0000','\uffff');
		}
        public bool S()    /*[19]S:  		[ \t\r\n]*					;*/
        {

           return OptRepeat(()=> OneOf(" \t\r\n") );
		}
		#endregion Grammar Rules

        #region Optimization Data 
        internal static OptimizedCharset optimizedCharset0;
        
        
        static json()
        {
            {
               char[] oneOfChars = new char[]    {'"','\\','/','b','f'
                                                  ,'n','r','t'};
               optimizedCharset0= new OptimizedCharset(null,oneOfChars);
            }
            
            
            
        }
        #endregion Optimization Data 
           }
}

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