Click here to Skip to main content
15,892,161 members
Articles / Programming Languages / C#

Irony - .NET Compiler Construction Kit

Rate me:
Please Sign up or sign in to vote.
4.97/5 (86 votes)
4 Jan 2008MIT19 min read 295.3K   3.2K   201  
Introduction to Irony - a new technology of parser/compiler construction for .NET.
#region License
/* **********************************************************************************
 * Copyright (c) Roman Ivantsov
 * This source code is subject to terms and conditions of the MIT License
 * for Irony. A copy of the license can be found in the License.txt file
 * at the root of this distribution. 
 * By using this source code in any fashion, you are agreeing to be bound by the terms of the 
 * MIT License.
 * You must not remove this notice from this software.
 * **********************************************************************************/
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using Irony.Compiler;

namespace Irony.Compiler {
  //Root compiler class
  public class LanguageCompiler {
    public LanguageCompiler(Grammar grammar) {
      Grammar = grammar;
      long startTime = Environment.TickCount;
      GrammarDataBuilder bld = new GrammarDataBuilder();
      Data = bld.Build(grammar);
      Parser = new Parser(Data); 
      Scanner = new Scanner(Data);
      InitTime = Environment.TickCount - startTime;
    }

    public readonly Grammar Grammar;
    public readonly GrammarData Data;
    public readonly Scanner Scanner;
    public readonly Parser Parser;
    public readonly long InitTime;

    public long CompileTime  {
      get {return _compileTime;}
    } long  _compileTime;

    public CompilerContext Context  {
      get {return _context;}
    } CompilerContext  _context;

    public AstNode Parse(string source) {
      return Parse(new CompilerContext(), new SourceFile(source, "Source"));
    }

    public AstNode Parse(CompilerContext context, SourceFile source) {
      _context = context;
      int start = Environment.TickCount;
      IEnumerable<Token> tokenStream = Scanner.BeginScan(context, source);
      //chain all token filters
      foreach (TokenFilter filter in Grammar.TokenFilters) {
        tokenStream = filter.BeginFiltering(context, tokenStream);
      }
      //finally, parser takes token stream and produces root Ast node
      AstNode rootNode = Parser.Parse(context, tokenStream);
      _compileTime = Environment.TickCount - start;
      return rootNode;
    }//method
    
  }//class

}//namespace

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


Written By
Software Developer (Senior) Microsoft
United States United States
25 years of professional experience. .NET/c#, databases, security.
Currently Senior Security Engineer, Cloud Security, Microsoft

Comments and Discussions