Paradox Game Engine  v1.0.0 beta06
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Events Macros Pages
ImpliedSymbolTerminal.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  //In some grammars there is a situation when some operator symbol can be skipped in source text and should be implied by parser.
8  // In arithmetics, we often imply "*" operator in formulas:
9  // x y => x * y.
10  // The SearchGrammar in Samples provides another example: two consequtive terms imply "and" operator and should be treated as such:
11  // x y => x AND y
12  // We could use a simple nullable Non-terminal terminal in this case, but the problem is that we cannot associate precedence
13  // and associativity with non-terminal, only with terminals. Precedence is important here because the implied symbol identifies binary
14  // operation, so parser should be able to use precedence value(s) when resolving shift/reduce ambiguity.
15  // So here comes ImpliedSymbolTerminal - it is a terminal that produces a token with empty text.
16  // It relies on scanner-parser link enabled - so the implied symbol token is created ONLY
17  // when the current parser state allows it and there are no other alternatives (hence lowest priority value).
18  // See SearchGrammar as an example of use of this terminal.
20  public ImpliedSymbolTerminal(string name) : base(name) {
21  this.Priority = Terminal.LowestPriority; //This terminal should be tried after all candidate terminals failed.
22  }
23 
24  public override void Init(Irony.Parsing.GrammarData grammarData) {
25  base.Init(grammarData);
26  //Check that Parser-scanner link is enabled - this terminal can be used only if this link is enabled
27  if (Grammar.FlagIsSet(LanguageFlags.DisableScannerParserLink))
28  grammarData.Language.Errors.Add(GrammarErrorLevel.Error, null, Resources.ErrImpliedOpUseParserLink, this.Name);
29  //"ImpliedSymbolTerminal cannot be used in grammar with DisableScannerParserLink flag set"
30  }
31 
32  public override Token TryMatch(ParsingContext context, ISourceStream source) {
33  return source.CreateToken(this); //Create an empty token representing an implied symbol.
34  }
35 
36  }//class
37 }//namespace
bool FlagIsSet(LanguageFlags flag)
Definition: Grammar.cs:42
A strongly-typed resource class, for looking up localized strings, etc.
Interface for Terminals to access the source stream and produce tokens.
override void Init(Irony.Parsing.GrammarData grammarData)
override Token TryMatch(ParsingContext context, ISourceStream source)
Tokens are produced by scanner and fed to parser, optionally passing through Token filters in between...
Definition: Token.cs:74
static string ErrImpliedOpUseParserLink
Looks up a localized string similar to ImpliedSymbolTerminal cannot be used in grammar with DisableSc...