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

Writing Your First Visual Studio Language Service

Rate me:
Please Sign up or sign in to vote.
4.95/5 (61 votes)
11 Dec 2009CPOL8 min read 233.8K   4K   157  
A guide to writing a language service for Visual Studio using Irony.
#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.Linq;
using System.Text;
using Irony.Runtime;

namespace Irony.Compiler.AST {
  public class BinExprNode : AstNode {
    public AstNode Left;
    public string Op;
    public AstNode Right;
    CallDispatcher _dispatcher;


    public BinExprNode(NodeArgs args, AstNode left, string op, AstNode right) : base(args) {
      ChildNodes.Clear();
      Left = left;
      AddChild("Arg", Left);
      Op = op; 
      Right = right;
      AddChild("Arg", Right);
      //Flags |= AstNodeFlags.TypeBasedDispatch;
    }
    public BinExprNode(NodeArgs args) 
      : this(args, args.ChildNodes[0], args.ChildNodes[1].GetContent(), args.ChildNodes[2]) {  }

    public override void OnCodeAnalysis(CodeAnalysisArgs args) {
      switch (args.Phase) {
        case CodeAnalysisPhase.Binding:
          _dispatcher = args.Context.Runtime.GetDispatcher(Op);
          if (_dispatcher == null)
            args.Context.ReportError(this.Location, "Operator " + Op + " not defined.");
          break;
      }//switch
      base.OnCodeAnalysis(args);
    }

    protected override void DoEvaluate(EvaluationContext context) {
      try {
        Left.Evaluate(context);
        object arg1 = context.CurrentResult;
        Right.Evaluate(context);
        context.Arg2 = context.CurrentResult;
        context.Arg1 = arg1;
        _dispatcher.Evaluate(context);
      } catch (RuntimeException e) {
        e.Location = this.Location;
        throw; 
      }
    }

    public override string ToString() {
      return Op + " (binary operation)";
    }
    protected override void XmlSetAttributes(System.Xml.XmlElement thisElement) {
      base.XmlSetAttributes(thisElement);
      thisElement.SetAttribute("Operation", this.Op); 
    }
  }//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 Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions