示例#1
0
        public BnfGrammar(bool enhanced = true)
            : base("bnf")
        {
            if (enhanced)
            {
                foreach (var property in typeof(Terminals).GetProperties())
                {
                    if (typeof(Parser).IsAssignableFrom(property.PropertyType))
                    {
                        var parser = property.GetValue(null, null) as Parser;
                        baseLookup[property.Name] = parser.Named(property.Name);
                    }
                }
            }

            var lineEnd = sws & +(sws & Terminals.Eol);

            literal = (
                (sq & (+!sq).Named("value").Optional() & sq)
                | (dq & (+!dq).Named("value").Optional() & dq)
            ).Named("parser");

            RuleNameParser = "<" & Terminals.Set('>').Inverse().Repeat().Named("name") & ">";

            RuleParser = new AlternativeParser(); // defined later

            TermParser = literal | (ruleName = RuleNameParser.Named("parser"));
            if (enhanced)
            {
                TermParser.Items.Add('(' & sws & RuleParser & sws & ')');
                TermParser.Items.Add(repeatRule = ('{' & sws & RuleParser & sws & '}').Named("parser"));
                TermParser.Items.Add(optionalRule = ('[' & sws & RuleParser & sws & ']').Named("parser"));
            }

            list = (TermParser.Named("term") & -(~((+Terminals.SingleLineWhiteSpace).Named("ws")) & TermParser.Named("term"))).Named("parser");

            listRepeat = (list.Named("list") & ws & '|' & sws & ~(RuleParser.Named("expression"))).Named("parser");
            RuleParser.Items.Add(listRepeat);
            RuleParser.Items.Add(list);

            rule = (~lineEnd & sws & RuleNameParser.Named("ruleName") & ws & ruleSeparator & sws & RuleParser & lineEnd).Named("parser");
            Expresssions = new AlternativeParser();
            Expresssions.Items.Add(rule);

            this.Inner = ws & +Expresssions & ws;

            AttachEvents();
        }
示例#2
0
		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();
		}