Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
RegExBasedTerminal.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;
15 using System.Collections.Generic;
16 using System.Text;
17 using System.Text.RegularExpressions;
18 
19 namespace Irony.Parsing {
20 
21  //Note: this class was not tested at all
22  // Based on contributions by CodePlex user sakana280
23  // 12.09.2008 - breaking change! added "name" parameter to the constructor
24  public class RegexBasedTerminal : Terminal {
25  public RegexBasedTerminal(string pattern, params string[] prefixes)
26  : base("name") {
27  Pattern = pattern;
28  if (prefixes != null)
29  Prefixes.AddRange(prefixes);
30  }
31  public RegexBasedTerminal(string name, string pattern, params string[] prefixes) : base(name) {
32  Pattern = pattern;
33  if (prefixes != null)
34  Prefixes.AddRange(prefixes);
35  }
36 
37  #region public properties
38  public readonly string Pattern;
39  public readonly StringList Prefixes = new StringList();
40 
41  public Regex Expression {
42  get { return _expression; }
43  } Regex _expression;
44  #endregion
45 
46  public override void Init(GrammarData grammarData) {
47  base.Init(grammarData);
48  string workPattern = @"\G(" + Pattern + ")";
49  RegexOptions options = (Grammar.CaseSensitive ? RegexOptions.None : RegexOptions.IgnoreCase);
50  _expression = new Regex(workPattern, options);
51  if (this.EditorInfo == null)
52  this.EditorInfo = new TokenEditorInfo(TokenType.Unknown, TokenColor.Text, TokenTriggers.None);
53  }
54 
55  public override IList<string> GetFirsts() {
56  return Prefixes;
57  }
58 
59  public override Token TryMatch(ParsingContext context, ISourceStream source) {
60  Match m = _expression.Match(source.Text, source.PreviewPosition);
61  if (!m.Success || m.Index != source.PreviewPosition)
62  return null;
63  source.PreviewPosition += m.Length;
64  return source.CreateToken(this.OutputTerminal);
65  }
66 
67  }//class
68 
69 
70 
71 
72 }//namespace
override void Init(GrammarData grammarData)
Interface for Terminals to access the source stream and produce tokens.
override Token TryMatch(ParsingContext context, ISourceStream source)
RegexBasedTerminal(string pattern, params string[] prefixes)
int PreviewPosition
Gets or sets the current preview position in the source file. Must be greater or equal to Location...
override IList< string > GetFirsts()
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
RegexBasedTerminal(string name, string pattern, params string[] prefixes)