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
9
* You must not remove this notice from this software.
10
* **********************************************************************************/
14
using System.Collections.Generic;
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";
26
public string LineTerminators = "\n\r\v";
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);
35
public override IList<string> GetFirsts() {
36
StringList firsts = new StringList();
37
foreach(char t in LineTerminators)
38
firsts.Add(t.ToString());
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
49
Token result = new Token(this, source.TokenStart, source.GetLexeme(), null);