Click here to Skip to main content
15,881,413 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 232.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 CondFormNode : AstNode {
    public AstNodeList Clauses;
    public AstNode ElseClause;

    public CondFormNode(NodeArgs args, AstNodeList clauses, AstNode elseClause) : base(args) {
      ChildNodes.Clear();
      Clauses = clauses;
      foreach (AstNode clause in clauses) {
        clause.Role = "Arg";
        ChildNodes.Add(clause);
      }
      ElseClause = elseClause;
      if (ElseClause != null) {
        ElseClause.Role = "else";
        ChildNodes.Add(ElseClause);
      }
    }

    public override void OnCodeAnalysis(CodeAnalysisArgs args) {
      switch (args.Phase) {
        case CodeAnalysisPhase.MarkTailCalls:
          if (IsSet(AstNodeFlags.IsTail)) {
            foreach (CondClauseNode clause in Clauses)
              clause.Flags |= AstNodeFlags.IsTail;
            ElseClause.Flags |= AstNodeFlags.IsTail;
          }
          break;
      }
      base.OnCodeAnalysis(args);
    }

    protected override void DoEvaluate(EvaluationContext context) {
      foreach (CondClauseNode clause in Clauses) {
        clause.Test.Evaluate(context);
        if (context.Runtime.IsTrue(context.CurrentResult)) {
          clause.Expressions.Evaluate(context);
          return;
        }
      }//foreach
      if (ElseClause != null)
        ElseClause.Evaluate(context);
    }
  }
}

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