Click here to Skip to main content
15,893,904 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.9K   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.Text;
using Irony.Compiler;


namespace Irony.Runtime {

  public class Frame {
    public string MethodName; //for debugging purposes
    public Frame Parent; //Lexical parent - not the same as the caller
    public Frame Caller;
    public AstNode Node;
    public object[] Locals; //includes parameters and local variables

    public Frame(string methodName, AstNode node, Frame caller, Frame parent, object[] args) {
      MethodName = methodName;
      Node = node; 
      Caller = caller;
      Parent = parent;
      Locals = args; 
      //When allocating an array for parameters, we reserve extra 8 elements for locals - this should be enough in most cases
      // If not, we resize the array
      int argCount = args.Length;
      int localCount = node.Scope.Slots.Count;
      if (localCount > args.Length) {
        Array.Resize(ref Locals, localCount);
        for (int i = argCount + 1; i < localCount; i++)
          Locals[i] = Unassigned.Value;
      }
    }

    public int ScopeLevel {
      get { return Node.Scope.Level; }
    }

    public object GetValue(SlotInfo slot) {
      Frame targetFrame = GetFrame(slot.Scope.Level);
      object result = targetFrame.Locals[slot.Index];
      if (result == Unassigned.Value)
        throw new RuntimeException("Access to unassigned variable " + slot.Name);
      return result; 
    }

    public void SetValue(SlotInfo slot, object value) {
      Frame f = GetFrame(slot.Scope.Level);
      f.Locals[slot.Index] = value;
    }

    public Frame GetFrame(int scopeLevel) {
      Frame result = this;
      while (result.ScopeLevel != scopeLevel)
        result = result.Parent;
      return result;
    }//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 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