Click here to Skip to main content
12,445,482 members (62,554 online)
Click here to Skip to main content

Stats

128K views
3K downloads
244 bookmarked
Posted

Implementing Programming Languages Using C# 4.0

, 12 Jul 2012 MIT
An introduction to creating programming language tools using C# 4.0.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Diggins.Jigsaw
{
    public class SharedGrammar : Grammar
    {
        public static Rule MatchAnyString(params string[] xs) { return Choice(xs.Select(x => MatchString(x)).ToArray()); }
        public static Rule MatchStringSet(string s) { return MatchAnyString(s.Split(' ')); }

        public static Rule FullComment      = MatchString("/*") + AdvanceWhileNot(MatchString("*/"));
        public static Rule LineComment      = MatchString("//") + AdvanceWhileNot(MatchChar('\n'));
        public static Rule WS               = Pattern(@"\s*");
        public static Rule Digit            = MatchChar(Char.IsDigit);
        public static Rule Digits           = OneOrMore(Digit);
        public static Rule Letter           = MatchChar(Char.IsLetter);
        public static Rule LetterOrDigit    = MatchChar(Char.IsLetterOrDigit);
        public static Rule E                = (MatchChar('e') | MatchChar('E')) + Opt(MatchChar('+') | MatchChar('-'));
        public static Rule IdentFirstChar   = MatchChar(c => Char.IsLetter(c) || c == '_');
        public static Rule IdentNextChar    = MatchChar(c => Char.IsLetterOrDigit(c) || c == '_');
        public static Rule Identifier       = IdentFirstChar + ZeroOrMore(IdentNextChar);
        public static Rule Exp              = E + Digits;
        public static Rule Frac             = MatchChar('.') + Digits;
        public static Rule Integer          = Digits + Not(MatchChar('.'));
        public static Rule Float            = Digits + ((Frac + Opt(Exp)) | Exp);
        public static Rule HexDigit         = Digit | CharRange('a', 'f') | CharRange('A', 'F');
            
        public static Rule CharToken(char c) { return MatchChar(c) + WS; }
        public static Rule StringToken(string s) { return MatchString(s) + WS; } 
        public static Rule CommaDelimited(Rule r) { return Opt(r + (ZeroOrMore(CharToken(',') + r) + Opt(CharToken(',')))); }

        public static Rule Comma = CharToken(',');
        public static Rule Eos = CharToken(';');
        public static Rule Eq = CharToken('=');
        public static Rule Dot = CharToken('.');
        public static Rule Keyword(string s) { return MatchString(s) + Not(LetterOrDigit) + WS; } 
        public static Rule Parenthesize(Rule r) { return CharToken('(') + r + WS + CharToken(')'); }

        static SharedGrammar() { InitGrammar(typeof(SharedGrammar)); }
    }
}

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 MIT License

Share

About the Author

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

You may also be interested in...

Pro
Pro
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160811.3 | Last Updated 12 Jul 2012
Article Copyright 2011 by Christopher Diggins
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid