Example #1
0
        public GoldGrammar()
            : base("gold")
        {
            Parser oldSeparator = DefaultSeparator;
            // Special Terminals

            ExceptParser      parameterCh   = Terminals.Printable - Terminals.Set("\"'");
            AlternativeParser nonterminalCh = Terminals.LetterOrDigit | Terminals.Set("_-. ");
            AlternativeParser terminalCh    = Terminals.LetterOrDigit | Terminals.Set("_-.");
            ExceptParser      literalCh     = Terminals.Printable - Terminals.Set('\'');
            ExceptParser      setLiteralCh  = Terminals.Printable - Terminals.Set("[]'");
            ExceptParser      setNameCh     = Terminals.Printable - Terminals.Set("{}");

            SequenceParser    parameterName = ('"' & (+parameterCh).WithName("value") & '"').Separate();
            SequenceParser    nonterminal   = ('<' & (+nonterminalCh).WithName("value") & '>').Separate();
            AlternativeParser terminal      =
                ((+terminalCh).WithName("terminal") | ('\'' & (-literalCh).WithName("literal") & '\'')).Separate();
            SequenceParser setLiteral =
                ('[' & +(setLiteralCh.WithName("ch") | '\'' & (-literalCh).WithName("ch") & '\'') & ']').WithName(
                    "setLiteral");
            SequenceParser setName = ('{' & (+setNameCh).WithName("value") & '}').WithName("setName");

            // Line-Based Grammar Declarations

            var    comments = new GroupParser("!*", "*!", "!");
            Parser newline  = Terminals.Eol;

            whitespace       = -(Terminals.SingleLineWhiteSpace | comments);
            DefaultSeparator = whitespace;
            RepeatParser      nlOpt = -newline;
            AlternativeParser nl    = +newline | Terminals.End;

            // Parameter Definition

            AlternativeParser parameterItem = parameterName | terminal | setLiteral | setName | nonterminal;

            RepeatParser parameterItems = +parameterItem;

            SequenceParser parameterBody = parameterItems & -(nlOpt & '|' & parameterItems);

            parameter =
                (parameterName.Named("name") & nlOpt & '=' & parameterBody.WithName("body") & nl).WithName("parameter");

            // Set Definition

            AlternativeParser setItem = setLiteral | setName;

            var setExp = new AlternativeParser {
                Name = "setExp"
            };

            setExp.Add((setExp & nlOpt & '+' & setItem).WithName("add"),
                       (setExp & nlOpt & '-' & setItem).WithName("sub"),
                       setItem);


            setDecl = (setName & nlOpt & '=' & setExp & nl).WithName("setDecl");

            //  Terminal Definition

            var regExp2 = new SequenceParser();

            OptionalParser kleeneOpt = (~((Parser)'+' | '?' | '*')).WithName("kleene");

            regExpItem = ((setLiteral & kleeneOpt)
                          | (setName & kleeneOpt)
                          | (terminal.Named("terminal") & kleeneOpt)
                          | ('(' & regExp2.Named("regExp2") & ')' & kleeneOpt)).WithName("regExpItem");

            RepeatParser regExpSeq = (+regExpItem).WithName("regExpSeq");

            regExp2.Items.Add(regExpSeq);
            regExp2.Items.Add(-('|' & regExpSeq));

            regExp = (regExpSeq & -(nlOpt & '|' & regExpSeq)).WithName("regExp");

            RepeatParser terminalName = +terminal;

            terminalDecl = (terminalName.Named("name") & nlOpt & '=' & regExp & nl).WithName("terminalDecl");

            // Rule Definition
            symbol = (terminal.Named("terminal") | nonterminal.Named("nonterminal")).WithName("symbol");

            handle = (-symbol).WithName("handle");
            SequenceParser handles = handle & -(nlOpt & '|' & handle);

            ruleDecl = (nonterminal.Named("name") & nlOpt & "::=" & handles & nl).WithName("ruleDecl");

            // Rules

            AlternativeParser definitionDecl = parameter | setDecl | terminalDecl | ruleDecl;

            RepeatParser content = -definitionDecl;

            Inner = nlOpt & content & nlOpt;

            DefaultSeparator = oldSeparator;
            AttachEvents();
        }