~halega/+junk/sharpdevelop

« back to all changes in this revision

Viewing changes to src/AddIns/Misc/Reports/Irony/CompilerServices/Terminals/NewLineTerminal.cs

  • Committer: sk
  • Date: 2011-09-10 05:17:57 UTC
  • Revision ID: halega@halega.com-20110910051757-qfouz1llya9m6boy
4.1.0.7915 Release Candidate 1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#region License
 
2
/* **********************************************************************************
 
3
 * Copyright (c) Roman Ivantsov
 
4
 * This source code is subject to terms and conditions of the MIT License
 
5
 * for Irony. A copy of the license can be found in the License.txt file
 
6
 * at the root of this distribution. 
 
7
 * By using this source code in any fashion, you are agreeing to be bound by the terms of the 
 
8
 * MIT License.
 
9
 * You must not remove this notice from this software.
 
10
 * **********************************************************************************/
 
11
#endregion
 
12
 
 
13
using System;
 
14
using System.Collections.Generic;
 
15
using System.Linq;
 
16
using System.Text;
 
17
 
 
18
namespace Irony.CompilerServices {
 
19
  //This is a simple NewLine terminal recognizing line terminators for use in grammars for line-based languages like VB
 
20
  // instead of more complex alternative of using CodeOutlineFilter. 
 
21
  public class NewLineTerminal : Terminal {
 
22
    public NewLineTerminal(string name) : base(name, TokenCategory.Outline) {
 
23
      base.DisplayName = "Line break";
 
24
    }
 
25
 
 
26
    public string LineTerminators = "\n\r\v";
 
27
 
 
28
    #region overrides: Init, GetFirsts, TryMatch
 
29
    public override void Init(GrammarData grammarData) {
 
30
      base.Init(grammarData);
 
31
      //Remove new line chars from whitespace
 
32
      foreach(char t in LineTerminators)
 
33
        grammarData.Grammar.WhitespaceChars = grammarData.Grammar.WhitespaceChars.Replace(t.ToString(), string.Empty);
 
34
    }
 
35
    public override IList<string> GetFirsts() {
 
36
      StringList firsts = new StringList();
 
37
      foreach(char t in LineTerminators)
 
38
        firsts.Add(t.ToString());
 
39
      return firsts;
 
40
    }
 
41
    public override Token TryMatch(CompilerContext context, ISourceStream source) {
 
42
      char current = source.CurrentChar;
 
43
      if (!LineTerminators.Contains(current)) return null;
 
44
      //Treat \r\n as a single terminator
 
45
      bool doExtraShift = (current == '\r' && source.NextChar == '\n');
 
46
      source.Position++; //main shift
 
47
      if (doExtraShift)
 
48
        source.Position++;
 
49
      Token result = new Token(this, source.TokenStart, source.GetLexeme(), null);
 
50
      return result;
 
51
    }
 
52
 
 
53
    #endregion
 
54
 
 
55
    
 
56
  }//class
 
57
}//namespace