Click here to Skip to main content
13,797,167 members
Click here to Skip to main content


248 bookmarked
Posted 22 Oct 2011
Licenced MIT

Implementing Programming Languages Using C# 4.0

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

namespace Diggins.Jigsaw
    public class ParserState
        public string input;
        public int pos;
        public List<Node> nodes = new List<Node>();
        private Dictionary<int, Dictionary<Rule, Node>> cache = new Dictionary<int,Dictionary<Rule, Node>>();

        public string Current
            get { return input.Substring(pos); }
        public ParserState Clone()
            return new ParserState { 
                input = input, 
                pos = pos, 
                nodes = new List<Node>(nodes) };
        public void Assign(ParserState state)
            input = state.input; 
            pos = state.pos; 
            nodes = state.nodes;
        public void RestoreAfter(Action action)
            var state = Clone(); 
        public override string ToString()
            return String.Format("{0}/{1}", pos, input.Length);
        public void CacheResult(Rule rule, int pos, Node node)
            if (!cache.ContainsKey(pos)) 
                cache.Add(pos, new Dictionary<Rule, Node>());
            var tmp = cache[pos];
            if (!tmp.ContainsKey(rule))
                tmp.Add(rule, node);
        public bool GetCachedResult(NodeRule rule, out Node node)
            node = null;
            if (!cache.ContainsKey(pos))
                return false;
            if (cache[pos].ContainsKey(rule))
                node = cache[pos][rule];
                return true;
            return false;

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.


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
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...

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