void GenerateSpecialSequences() { // special sequences for each terminal foreach (var terminal in Terminals.GetTerminals()) { var name = "Terminals." + terminal.Item1; specialLookup[name] = terminal.Item2; } }
public BnfGrammar(bool enhanced = true) : base("bnf") { if (enhanced) { foreach (var terminal in Terminals.GetTerminals()) { baseLookup[terminal.Item1] = terminal.Item2.Named(terminal.Item1); } } literal = ( (sq & (+!sq).WithName("value").Optional() & sq) | (dq & (+!dq).WithName("value").Optional() & dq) | ((+(Terminals.WhiteSpace.Inverse().Except(Terminals.Set("<[{(|)}]>"))))).WithName("value") ).WithName("parser"); RuleNameParser = "<" & Terminals.Set("<>").Inverse().Repeat().WithName("name") & ">"; RuleParser = new AlternativeParser(); // defined later TermParser = ((ruleName = RuleNameParser.Named("parser")).NotFollowedBy(ows & ruleSeparator)) | literal; TermParser.Name = "term"; if (enhanced) { TermParser.Items.Add('(' & ows & RuleParser & ows & ')'); TermParser.Items.Add(repeatRule = ('{' & ows & RuleParser & ows & '}').WithName("parser")); TermParser.Items.Add(optionalRule = ('[' & ows & RuleParser & ows & ']').WithName("parser")); } TermParser.Items.Add((ows & RuleNameParser & ows & ruleSeparator).Not() & Terminals.Set("<[{(}]>").WithName("value").Named("parser")); list = (TermParser & -(~(rws.Named("ws")) & TermParser)).WithName("parser"); listRepeat = (list.Named("list") & ows & '|' & ~(ows & RuleParser.Named("expression"))).WithName("parser"); RuleParser.Items.Add(listRepeat); RuleParser.Items.Add(list); rule = (RuleNameParser.Named("ruleName") & ows & ruleSeparator & ows & RuleParser).WithName("parser"); Expresssions = new AlternativeParser(); Expresssions.Items.Add(rule); this.Inner = ows & (+Expresssions).SeparatedBy(rws) & ows; AttachEvents(); }
public BnfGrammar(BnfStyle style = BnfStyle.All) : base("bnf") { Parser ows = Terminals.WhiteSpace.Repeat(0); Parser rws = Terminals.WhiteSpace.Repeat(1); if (style.HasFlag(BnfStyle.CommonTerminals)) { foreach (var terminal in Terminals.GetTerminals()) { baseLookup[terminal.Item1] = terminal.Item2; } } if (style.HasFlag(BnfStyle.Comments)) { // allow ebnf comments var comment = new GroupParser("(*", "*)"); ows = (Terminals.WhiteSpace | comment).Repeat(0); rws = (Terminals.WhiteSpace | comment).Repeat(1); } literal = ( (sq & (+!sq).WithName("value").Optional() & sq) | (dq & (+!dq).WithName("value").Optional() & dq) | (+Terminals.Set(" \n\r<[{(|)}]>").Inverse()).WithName("value") ).WithName("parser"); RuleNameParser = "<" & Terminals.Set("<>\n\r").Inverse().Repeat().WithName("name") & ">"; RuleParser = new AlternativeParser(); // defined later TermParser = new AlternativeParser(); TermParser.Name = "term"; TermParser.Add((ruleName = RuleNameParser.Named("parser")).NotFollowedBy(ows & ruleSeparator)); if (style.HasFlag(BnfStyle.Cardinality)) { TermParser.Items.Add('(' & ows & RuleParser & ows & ')'); TermParser.Items.Add(repeatRule = ('{' & ows & RuleParser & ows & '}').WithName("parser")); TermParser.Items.Add(optionalRule = ('[' & ows & RuleParser & ows & ']').WithName("parser")); } TermParser.Items.Add(literal); var rep = -(~((+Terminals.WhiteSpace).WithName("ws")) & TermParser); rep.Name = "rep"; rep.AddMatch = false; list = (TermParser & rep).WithName("parser"); listRepeat = (list.Named("list") & ows & '|' & ~(ows & RuleParser.Named("expression"))).WithName("parser"); RuleParser.Items.Add(listRepeat); RuleParser.Items.Add(list); RuleParser.Items.Add((ows & RuleNameParser & ows & ruleSeparator).Not() & Terminals.WhiteSpace.Inverse().Repeat().WithName("value").Named("parser")); rule = (RuleNameParser.Named("ruleName") & ows & ruleSeparator & ows & RuleParser).WithName("parser"); Expresssions = new AlternativeParser(); Expresssions.Items.Add(rule); this.Inner = ows & (+Expresssions).SeparatedBy(rws) & ows; AttachEvents(); }