Click here to Skip to main content
11,493,391 members (65,724 online)
Click here to Skip to main content
Add your own
alternative version

Implementing Programming Languages Using C# 4.0

, 12 Jul 2012 MIT 105.4K 2.6K 242
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
{
    class CSharpExprGrammar : CSharpLiteralsGrammar
    {
        public static Rule RecExpr = Recursive(() => Expr);

        public static Rule Block = Node(CharToken('{') + ZeroOrMore(Recursive(() => Statement)) + WS + CharToken('}'));
        public static Rule ExprStatement = Node(RecExpr + WS + Eos);
        public static Rule ReturnStatement = Node(Keyword("return") + Opt(RecExpr) + WS + Eos);
        public static Rule Statement = Node(Block | ExprStatement | ReturnStatement);

        public static Rule SymbolChar = CharSet("~!^&*<>/+-=%?"); 
        public static Rule TypeName = Node(Identifier + WS + ZeroOrMore(Dot + Identifier + WS));
        public static Rule TypeArgs = Node(Opt(CharToken('<') + CommaDelimited(Recursive(() => TypeExpr)) + CharToken('>')));
        public static Rule TypeArrayIndicator = Node(CharToken('[') + CharToken(']'));
        public static Rule TypeExpr = Node(TypeName + Opt(TypeArgs) + Opt(TypeArrayIndicator));
        public static Rule TypedLambdaParam = Node(TypeExpr + WS + Identifier + WS);
        public static Rule UntypedLambdaParam = Node(Identifier + WS);
        public static Rule LambdaParam = TypedLambdaParam | UntypedLambdaParam;
        public static Rule LambdaParams = Node(UntypedLambdaParam | Parenthesize(CommaDelimited(LambdaParam)));
        public static Rule LambdaExpr = Node(LambdaParams + WS + MatchString("=>") + WS + (Block | RecExpr));
        public static Rule ArgList = Node(CharToken('(') + CommaDelimited(RecExpr) + CharToken(')'));
        public static Rule Indexer = Node(CharToken('[') + RecExpr + CharToken(']'));
        public static Rule Selector = Node(CharToken('.') + Identifier);
        public static Rule Inc = Node(MatchString("++"));
        public static Rule Dec = Node(MatchString("--"));
        public static Rule PostfixOp = Node(Inc | Dec | Indexer | ArgList | Selector);
        public static Rule PrefixOp = Node(At(SymbolChar) + MatchStringSet("++ -- ! - ~"));
        public static Rule BinaryOp = Node(At(SymbolChar) + MatchStringSet(">>= <<= <= >= == != << >> += -= *= %= /= && || ?? < > & | + - * % / ="));
        public static Rule ParenthesizedExpr = Node(Parenthesize(RecExpr));
        public static Rule FieldInitializer = Node(Identifier + WS + Eq + RecExpr);
        public static Rule TypeInitializerField = Node(FieldInitializer | Recursive(() => TypeInitializer) | RecExpr);
        public static Rule TypeInitializer = Node(CharToken('{') + CommaDelimited(TypeInitializerField) + CharToken('}'));
        public static Rule NewExpr = Node(Keyword("new") + Opt(TypeExpr) + WS + Opt(ArgList) + WS + Opt(TypeInitializer));
        public static Rule LeafExpr = Node((LambdaExpr | ParenthesizedExpr | NewExpr | Identifier | Integer | Float | String) + WS);
        public static Rule PrefixExpr = Node(PrefixOp + WS + Recursive(() => PrefixExpr) | LeafExpr);
        public static Rule UnaryExpr = Node(PrefixExpr + ZeroOrMore(PostfixOp + WS));
        public static Rule BinaryExpr = Node(UnaryExpr + ZeroOrMore(BinaryOp + WS + UnaryExpr));
        public static Rule TertiaryExpr = Node(BinaryExpr + Opt(CharToken('?') + RecExpr + WS + CharToken(':') + RecExpr));
        public static Rule Expr = Node(TertiaryExpr);

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

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.
Follow on   Twitter   Google+   LinkedIn

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