Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
WikiTextTerminal.cs
Go to the documentation of this file.
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 
6 namespace Irony.Parsing {
7  //Handles plain text
9  public const char NoEscape = '\0';
10  public char EscapeChar = NoEscape;
11  private char[] _stopChars;
12 
13  public WikiTextTerminal(string name) : base(name, WikiTermType.Text, string.Empty, string.Empty, string.Empty) {
14  this.Priority = Terminal.LowestPriority;
15  }
16 
17  public override void Init(GrammarData grammarData) {
18  base.Init(grammarData);
19  var stopCharSet = new CharHashSet();
20  foreach(var term in grammarData.Terminals) {
21  var firsts = term.GetFirsts();
22  if (firsts == null) continue;
23  foreach (var first in firsts)
24  if (!string.IsNullOrEmpty(first))
25  stopCharSet.Add(first[0]);
26  }//foreach term
27  if (EscapeChar != NoEscape)
28  stopCharSet.Add(EscapeChar);
29  _stopChars = stopCharSet.ToArray();
30  }
31 
32  //override to WikiTerminalBase's method to return null, indicating there are no firsts, so it is a fallback terminal
33  public override IList<string> GetFirsts() {
34  return null;
35  }
36 
37  public override Token TryMatch(ParsingContext context, ISourceStream source) {
38  bool isEscape = source.PreviewChar == EscapeChar && EscapeChar != NoEscape;
39  if(isEscape) {
40  //return a token containing only escaped char
41  var value = source.NextPreviewChar.ToString();
42  source.PreviewPosition += 2;
43  return source.CreateToken(this.OutputTerminal, value);
44  }
45  var stopIndex = source.Text.IndexOfAny(_stopChars, source.Location.Position + 1);
46  if (stopIndex == source.Location.Position) return null;
47  if (stopIndex < 0) stopIndex = source.Text.Length;
48  source.PreviewPosition = stopIndex;
49  return source.CreateToken(this.OutputTerminal);
50  }//method
51 
52  }//class
53 
54 }
override IList< string > GetFirsts()
Interface for Terminals to access the source stream and produce tokens.
SourceLocation Location
Current start location (position, row, column) of the new token
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 Token TryMatch(ParsingContext context, ISourceStream source)