Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
NewLineTerminal.cs
Go to the documentation of this file.
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.Parsing {
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.ErrorAlias = Resources.LabelLineBreak; // "[line break]";
24  this.Flags |= TermFlags.IsPunctuation;
25  }
26 
27  public string LineTerminators = "\n\r\v";
28 
29  #region overrides: Init, GetFirsts, TryMatch
30  public override void Init(GrammarData grammarData) {
31  base.Init(grammarData);
32  //Remove new line chars from whitespace
33  foreach(char t in LineTerminators)
34  grammarData.Grammar.WhitespaceChars = grammarData.Grammar.WhitespaceChars.Replace(t.ToString(), string.Empty);
35  }
36  public override IList<string> GetFirsts() {
37  StringList firsts = new StringList();
38  foreach(char t in LineTerminators)
39  firsts.Add(t.ToString());
40  return firsts;
41  }
42  public override Token TryMatch(ParsingContext context, ISourceStream source) {
43  char current = source.PreviewChar;
44  if (!LineTerminators.Contains(current)) return null;
45  //Treat \r\n as a single terminator
46  bool doExtraShift = (current == '\r' && source.NextPreviewChar == '\n');
47  source.PreviewPosition++; //main shift
48  if (doExtraShift)
49  source.PreviewPosition++;
50  Token result = source.CreateToken(this.OutputTerminal);
51  return result;
52  }
53 
54  #endregion
55 
56 
57  }//class
58 }//namespace
TokenCategory
Token category.
Definition: Token.cs:29
static string LabelLineBreak
Looks up a localized string similar to [line break].
override Token TryMatch(ParsingContext context, ISourceStream source)
Interface for Terminals to access the source stream and produce tokens.
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
override void Init(GrammarData grammarData)
override IList< string > GetFirsts()
newLine, indent, dedent