Ejemplo n.º 1
0
        public static CharacterSet operator -(CharacterSet left, char right)
        {
            var result = new CharacterSet(left);

            result.Subtract(right);
            return(result);
        }
Ejemplo n.º 2
0
        public static CharacterSet operator +(CharacterSet left, IEnumerable <char> right)
        {
            var result = new CharacterSet(left);

            result.Add(right);
            return(result);
        }
Ejemplo n.º 3
0
        public Grammar()
        {
            // Constants.
            m_ht    = AddCharacterSet("HT", new CharacterSet('\x0009'));
            m_lf    = AddCharacterSet("LF", new CharacterSet('\x000A'));
            m_vt    = AddCharacterSet("VT", new CharacterSet('\x000B'));
            m_ff    = AddCharacterSet("FF", new CharacterSet('\x000C'));
            m_cr    = AddCharacterSet("CR", new CharacterSet('\x000D'));
            m_space = AddCharacterSet("Space", new CharacterSet('\x0020'));
            m_nbsp  = AddCharacterSet("NBSP", new CharacterSet('\x00A0'));
            m_ls    = AddCharacterSet("LS", new CharacterSet('\x2028'));
            m_ps    = AddCharacterSet("PS", new CharacterSet('\x2029'));

            // Common Character Sets.
            m_digit        = AddCharacterSet("Digit", new CharacterSet('0', '9'));
            m_letter       = AddCharacterSet("Letter", new CharacterSet('a', 'z') + new CharacterSet('A', 'Z'));
            m_alphaNumeric = AddCharacterSet("Alphanumeric", m_digit + m_letter);
            m_printable    = AddCharacterSet("Printable", new CharacterSet('\x0020', '\x007e') + '\x00A0');
            m_whitespace   = AddCharacterSet("Whitespace", new CharacterSet('\x0009', '\x000d') + '\x0020' + '\x00A0');

            // Common Symbols.
            AddSymbol(new SymbolEnd());
            AddSymbol(new SymbolError());
        }
Ejemplo n.º 4
0
 public Edge(int target, CharacterSet characterSet)
 {
     Target     = target;
     Characters = (characterSet?.Count > 0) ? characterSet : null;
 }
Ejemplo n.º 5
0
        public GoldParserGrammar()
        {
            // Character Sets.
            m_parameterCh   = AddCharacterSet("Parameter Ch", Printable - "'\"");
            m_terminalCh    = AddCharacterSet("Terminal Ch", AlphaNumeric + "_-.");
            m_nonTerminalCh = AddCharacterSet("Nonterminal Ch", m_terminalCh + Space);
            m_literalCh     = AddCharacterSet("Literal Ch", Printable - '\'');
            m_setLiteralCh  = AddCharacterSet("Set Literal Ch", Printable - "[]'");
            m_setNameCh     = AddCharacterSet("Set Name Ch", Printable - "{}");
            m_whitespaceCh  = AddCharacterSet("Whitespace Ch", Whitespace - CR - LF);
            m_idHead        = AddCharacterSet("Id Head", Letter + "_");
            m_idTail        = AddCharacterSet("Id Tail", m_idHead + Digit);
            m_hexDigit      = AddCharacterSet("HexDigit Ch", Digit + "abcdefABCDEF");

            // Comment container.
            m_comment = AddSymbol(new SymbolNoise("Comment", null));

            // Whitespace = {Whitespace Ch}+
            m_whiteTerminal = AddSymbol(new SymbolNoise("Whitespace", new Expression
            {
                new Sequence
                {
                    { EKleene.OneOrMore, m_whitespaceCh },
                }
            }));

            // Comment line/start/end
            m_commentLine = AddSymbol(new SymbolGroupStart("!", new Expression {
                "!"
            }));
            m_commentStart = AddSymbol(new SymbolGroupStart("!*", new Expression {
                "!*"
            }));
            m_commentEnd = AddSymbol(new SymbolGroupEnd("*!", new Expression {
                "*!"
            }));

            // simple symbols.
            m_minus = AddSymbol(new SymbolTerminal("-", new Expression {
                "-"
            }));
            m_lparen = AddSymbol(new SymbolTerminal("(", new Expression {
                "("
            }));
            m_rparen = AddSymbol(new SymbolTerminal(")", new Expression {
                ")"
            }));
            m_times = AddSymbol(new SymbolTerminal("*", new Expression {
                "*"
            }));
            m_comma = AddSymbol(new SymbolTerminal(",", new Expression {
                ","
            }));
            m_dotDot = AddSymbol(new SymbolTerminal("..", new Expression {
                ".."
            }));
            m_colonColonEq = AddSymbol(new SymbolTerminal("::=", new Expression {
                "::="
            }));
            m_question = AddSymbol(new SymbolTerminal("?", new Expression {
                "?"
            }));
            m_ateq = AddSymbol(new SymbolTerminal("@=", new Expression {
                "@="
            }));
            m_lbrace = AddSymbol(new SymbolTerminal("{", new Expression {
                "{"
            }));
            m_pipe = AddSymbol(new SymbolTerminal("|", new Expression {
                "|"
            }));
            m_rbrace = AddSymbol(new SymbolTerminal("}", new Expression {
                "}"
            }));
            m_plus = AddSymbol(new SymbolTerminal("+", new Expression {
                "+"
            }));
            m_ltgt = AddSymbol(new SymbolTerminal("<>", new Expression {
                "<>"
            }));
            m_eq = AddSymbol(new SymbolTerminal("=", new Expression {
                "="
            }));

            // DecNumber = {Digit}+
            m_decNumber = AddSymbol(new SymbolTerminal("DecNumber", new Expression
            {
                new Sequence
                {
                    { EKleene.OneOrMore, CharacterSets["Digit"] }
                }
            }));

            // HexNumber = 0x{Hex Digit}+
            m_hexNumber = AddSymbol(new SymbolTerminal("HexNumber", new Expression
            {
                new Sequence
                {
                    { EKleene.OneOrMore, new CharacterSet('0') },
                    { EKleene.OneOrMore, new CharacterSet('x') },
                    { EKleene.OneOrMore, m_hexDigit }
                }
            }));

            // Identifier = {Id Head}{Id Tail}*
            m_identifier = AddSymbol(new SymbolTerminal("Identifier", new Expression
            {
                new Sequence
                {
                    { EKleene.None, m_idHead },
                    { EKleene.ZeroOrMore, m_idTail },
                }
            }));

            // Literal = '' {Literal Ch}* ''
            m_literal = AddSymbol(new SymbolTerminal("Literal", new Expression
            {
                new Sequence
                {
                    { EKleene.None, new CharacterSet('\'') },
                    { EKleene.ZeroOrMore, m_literalCh },
                    { EKleene.None, new CharacterSet('\'') }
                },
            }));

            // NewLine    = {CR}{LF} | {CR} | {LF}
            m_newLine = AddSymbol(new SymbolTerminal("NewLine", new Expression
            {
                new Sequence
                {
                    { EKleene.None, CharacterSets["CR"] },
                    { EKleene.None, CharacterSets["LF"] }
                },
                new Sequence
                {
                    { EKleene.None, CharacterSets["CR"] },
                },
                new Sequence
                {
                    { EKleene.None, CharacterSets["LF"] },
                }
            }));

            // NonTerminal = '<' {Nonterminal Ch}+ '>'
            m_nonTerminal = AddSymbol(new SymbolTerminal("NonTerminal", new Expression
            {
                new Sequence
                {
                    { EKleene.None, new CharacterSet('<') },
                    { EKleene.OneOrMore, m_nonTerminalCh },
                    { EKleene.None, new CharacterSet('>') }
                }
            }));

            // ParameterName = '"' {Parameter Ch}+ '"'
            m_parameterName = AddSymbol(new SymbolTerminal("ParameterName", new Expression
            {
                new Sequence
                {
                    { EKleene.None, new CharacterSet('"') },
                    { EKleene.OneOrMore, m_parameterCh },
                    { EKleene.None, new CharacterSet('"') }
                }
            }));

            // SetLiteral = '[' ({Set Literal Ch} | '' {Literal Ch}* '' )+ ']'
            m_setLiteral = AddSymbol(new SymbolTerminal("SetLiteral", new Expression
            {
                new Sequence
                {
                    { EKleene.None, new CharacterSet('[') },
                    { EKleene.OneOrMore, new Expression
                      {
                          new Sequence
                          {
                              { EKleene.None, m_setLiteralCh },
                          },
                          new Sequence
                          {
                              { EKleene.None, new CharacterSet('\'') },
                              { EKleene.ZeroOrMore, m_literalCh },
                              { EKleene.None, new CharacterSet('\'') }
                          },
                      } },
                    { EKleene.None, new CharacterSet(']') }
                },
            }));

            // Setup comment blocks.
            Group lineGroup = AddGroup(new Group
            {
                Name      = "Comment Line",
                Container = m_comment,
                Start     = m_commentLine,
                End       = m_newLine,
                Advance   = EAdvanceMode.CharacterAdvanceMode,
                Ending    = EEndingMode.OpenEndingMode,
            });
            Group blockGroup = AddGroup(new Group
            {
                Name      = "Comment Block",
                Container = m_comment,
                Start     = m_commentStart,
                End       = m_commentEnd,
                Advance   = EAdvanceMode.CharacterAdvanceMode,
                Ending    = EEndingMode.OpenEndingMode,
            });

            blockGroup.Nesting.Add(lineGroup);
            blockGroup.Nesting.Add(blockGroup);

            // Define all non terminal symbols.
            m_attributeDecl = AddSymbol(new SymbolNonTerminal("Attribute Decl"));
            m_attributeItem = AddSymbol(new SymbolNonTerminal("Attribute Item"));
            m_attributeList = AddSymbol(new SymbolNonTerminal("Attribute List"));
            m_charcodeItem  = AddSymbol(new SymbolNonTerminal("Charcode Item"));
            m_charcodeList  = AddSymbol(new SymbolNonTerminal("Charcode List"));
            m_charcodeValue = AddSymbol(new SymbolNonTerminal("Charcode Value"));
            m_content       = AddSymbol(new SymbolNonTerminal("Content"));
            m_definition    = AddSymbol(new SymbolNonTerminal("Definition"));
            m_grammar       = AddSymbol(new SymbolNonTerminal("Grammar"));
            m_groupDecl     = AddSymbol(new SymbolNonTerminal("Group Decl"));
            m_groupItem     = AddSymbol(new SymbolNonTerminal("Group Item"));
            m_handle        = AddSymbol(new SymbolNonTerminal("Handle"));
            m_handles       = AddSymbol(new SymbolNonTerminal("Handles"));
            m_idSeries      = AddSymbol(new SymbolNonTerminal("ID Series"));
            m_kleeneOpt     = AddSymbol(new SymbolNonTerminal("Kleene Opt"));
            m_nl            = AddSymbol(new SymbolNonTerminal("nl"));
            m_nlo           = AddSymbol(new SymbolNonTerminal("nlo"));
            m_param         = AddSymbol(new SymbolNonTerminal("Param"));
            m_paramBody     = AddSymbol(new SymbolNonTerminal("Param Body"));
            m_regExpItem    = AddSymbol(new SymbolNonTerminal("Reg Exp Item"));
            m_regExpSeq     = AddSymbol(new SymbolNonTerminal("Reg Exp Seq"));
            m_ruleDecl      = AddSymbol(new SymbolNonTerminal("Rule Decl"));
            m_setDecl       = AddSymbol(new SymbolNonTerminal("Set Decl"));
            m_setExp        = AddSymbol(new SymbolNonTerminal("Set Exp"));
            m_setItem       = AddSymbol(new SymbolNonTerminal("Set Item"));
            m_subRegExp     = AddSymbol(new SymbolNonTerminal("Sub Reg Exp"));
            m_symbol        = AddSymbol(new SymbolNonTerminal("Symbol"));
            m_symbols       = AddSymbol(new SymbolNonTerminal("Symbols"));
            m_terminalBody  = AddSymbol(new SymbolNonTerminal("Terminal Body"));
            m_terminalDecl  = AddSymbol(new SymbolNonTerminal("Terminal Decl"));
            m_terminalName  = AddSymbol(new SymbolNonTerminal("Terminal Name"));
            m_valueItem     = AddSymbol(new SymbolNonTerminal("Value Item"));
            m_valueItems    = AddSymbol(new SymbolNonTerminal("Value Items"));
            m_valueList     = AddSymbol(new SymbolNonTerminal("Value List"));

            // "Start Symbol" = <Grammar>
            StartSymbol = m_grammar;

            // <Grammar>
            //     ::= <nlo> <Content>
            AddProduction(m_grammar, m_nlo, m_content);

            //<Content>
            //    ::= <Content> <Definition>
            //    |   <Definition>
            AddProduction(m_content, m_content, m_definition);
            AddProduction(m_content, m_definition);

            //<Definition>
            //    ::= <Param>
            //    |   <Attribute Decl>
            //    |   <Set Decl>
            //    |   <Group Decl>
            //    |   <Terminal Decl>
            //    |   <Rule Decl>
            AddProduction(m_definition, m_param);
            AddProduction(m_definition, m_attributeDecl);
            AddProduction(m_definition, m_setDecl);
            AddProduction(m_definition, m_groupDecl);
            AddProduction(m_definition, m_terminalDecl);
            AddProduction(m_definition, m_ruleDecl);

            //<nlo>
            //    ::= Newline <nlo>
            //    |
            AddProduction(m_nlo, m_newLine, m_nlo);
            AddProduction(m_nlo);

            //<nl>
            //    ::= Newline <nl>
            //    |   Newline
            AddProduction(m_nl, m_newLine, m_nl);
            AddProduction(m_nl, m_newLine);

            //<Terminal Name>
            //    ::= Identifier
            //    |   Literal
            AddProduction(m_terminalName, m_identifier);
            AddProduction(m_terminalName, m_literal);

            //<Value List>
            //    ::= <Value List> ',' <nlo> <Value Items>
            //    |   <Value Items>
            AddProduction(m_valueList, m_valueList, m_comma, m_nlo, m_valueItems);
            AddProduction(m_valueList, m_valueItems);

            //<Value Items>
            //    ::= <Value Items> <Value Item>
            //    |   <Value Item>
            AddProduction(m_valueItems, m_valueItems, m_valueItem);
            AddProduction(m_valueItems, m_valueItem);

            //<Value Item>
            //    ::= Identifier
            //    |   Nonterminal
            //    |   Literal
            AddProduction(m_valueItem, m_identifier);
            AddProduction(m_valueItem, m_nonTerminal);
            AddProduction(m_valueItem, m_literal);

            //<Param>
            //    ::= ParameterName <nlo> '=' <Param Body> <nl>
            AddProduction(m_param, m_parameterName, m_nlo, m_eq, m_paramBody, m_nl);

            //<Param Body>
            //    ::= <Param Body> <nlo> '|' <Value List>
            //    |   <Value List>
            AddProduction(m_paramBody, m_paramBody, m_nlo, m_pipe, m_valueList);
            AddProduction(m_paramBody, m_valueList);

            //<Attribute Decl>
            //    ::= <Terminal Name> <nlo> '@=' '{' <Attribute List> '}' <nl>
            //    |   <Terminal Name> Identifier <nlo> '@=' '{' <Attribute List> '}' <nl>
            AddProduction(m_attributeDecl, m_terminalName, m_nlo, m_ateq, m_lbrace, m_attributeList, m_rbrace, m_nl);
            AddProduction(m_attributeDecl, m_terminalName, m_identifier, m_nlo, m_ateq, m_lbrace, m_attributeList, m_rbrace, m_nl);

            //<Attribute List>
            //    ::= <Attribute List> ',' <nlo> <Attribute Item>
            //    |   <Attribute Item>
            AddProduction(m_attributeList, m_attributeList, m_comma, m_nlo, m_attributeItem);
            AddProduction(m_attributeList, m_attributeItem);

            //<Attribute Item>
            //    ::= Identifier '=' Identifier
            //    |   Identifier '=' '{' <Value List> '}'
            AddProduction(m_attributeItem, m_identifier, m_eq, m_identifier);
            AddProduction(m_attributeItem, m_identifier, m_eq, m_lbrace, m_valueList, m_rbrace);

            //<Set Decl>
            //    ::= '{' <ID Series> '}' <nlo> '=' <Set Exp> <nl>
            AddProduction(m_setDecl, m_lbrace, m_idSeries, m_rbrace, m_nlo, m_eq, m_setExp, m_nl);

            //<Set Exp>
            //    ::= <Set Exp> <nlo> '+' <Set Item>
            //    |   <Set Exp> <nlo> '-' <Set Item>
            //    |   <Set Item>
            AddProduction(m_setExp, m_setExp, m_nlo, m_plus, m_setItem);
            AddProduction(m_setExp, m_setExp, m_nlo, m_minus, m_setItem);
            AddProduction(m_setExp, m_setItem);

            //<Set Item>
            //    ::= SetLiteral
            //    |   '{' <ID Series> '}'
            //    |   '{' <Charcode List> '}'
            AddProduction(m_setItem, m_setLiteral);
            AddProduction(m_setItem, m_lbrace, m_idSeries, m_rbrace);
            AddProduction(m_setItem, m_lbrace, m_charcodeList, m_rbrace);

            //<ID Series>
            //    ::= <ID Series> Identifier
            //    |   Identifier
            AddProduction(m_idSeries, m_idSeries, m_identifier);
            AddProduction(m_idSeries, m_identifier);

            //<Charcode List>
            //    ::= <Charcode List> ',' <nlo> <Charcode Item>
            //    |   <Charcode Item>
            AddProduction(m_charcodeList, m_charcodeList, m_comma, m_nlo, m_charcodeItem);
            AddProduction(m_charcodeList, m_charcodeItem);

            //<Charcode Item>
            //    ::= <Charcode Value>
            //    |   <Charcode Value> '..' <Charcode Value>
            AddProduction(m_charcodeItem, m_charcodeValue);
            AddProduction(m_charcodeItem, m_charcodeValue, m_dotDot, m_charcodeValue);

            //<Charcode Value>
            //    ::= HexNumber
            //    |   DecNumber
            AddProduction(m_charcodeValue, m_hexNumber);
            AddProduction(m_charcodeValue, m_decNumber);

            //<Group Decl>
            //    ::= <Terminal Name> Identifier <nlo> '=' <Group Item> <nl>
            AddProduction(m_groupDecl, m_terminalName, m_identifier, m_nlo, m_eq, m_groupItem, m_nl);

            //<Group Item>
            //    ::= Identifier
            //    |   Literal
            AddProduction(m_groupItem, m_identifier);
            AddProduction(m_groupItem, m_literal);

            //<Terminal Decl>
            //    ::= <Terminal Name> <nlo> '=' <Terminal Body> <nl>
            AddProduction(m_terminalDecl, m_terminalName, m_nlo, m_eq, m_terminalBody, m_nl);

            //<Terminal Body>
            //    ::= <Terminal Body> <nlo> '|' <Reg Exp Seq>
            //    |   <Reg Exp Seq>
            AddProduction(m_terminalBody, m_terminalBody, m_nlo, m_pipe, m_regExpSeq);
            AddProduction(m_terminalBody, m_regExpSeq);

            //<Reg Exp Seq>
            //    ::= <Reg Exp Seq> <Reg Exp Item>
            //    |   <Reg Exp Item>
            AddProduction(m_regExpSeq, m_regExpSeq, m_regExpItem);
            AddProduction(m_regExpSeq, m_regExpItem);

            //<Reg Exp Item>
            //    ::= <Set Item> <Kleene Opt>
            //    |   Literal <Kleene Opt>
            //    |   Identifier <Kleene Opt>
            //    |   '(' <Sub Reg Exp> ')' <Kleene Opt>
            AddProduction(m_regExpItem, m_setItem, m_kleeneOpt);
            AddProduction(m_regExpItem, m_literal, m_kleeneOpt);
            AddProduction(m_regExpItem, m_identifier, m_kleeneOpt);
            AddProduction(m_regExpItem, m_lparen, m_subRegExp, m_rparen, m_kleeneOpt);

            //<Sub Reg Exp>
            //    ::= <Sub Reg Exp> '|' <Reg Exp Seq>
            //    |   <Reg Exp Seq>
            AddProduction(m_subRegExp, m_subRegExp, m_pipe, m_regExpSeq);
            AddProduction(m_subRegExp, m_regExpSeq);

            //<Kleene Opt>
            //    ::= '+'
            //    |   '?'
            //    |   '*'
            //    |
            AddProduction(m_kleeneOpt, m_plus);
            AddProduction(m_kleeneOpt, m_question);
            AddProduction(m_kleeneOpt, m_times);
            AddProduction(m_kleeneOpt);

            //<Rule Decl>
            //    ::= Nonterminal <nlo> '::=' <Handles> <nl>
            AddProduction(m_ruleDecl, m_nonTerminal, m_nlo, m_colonColonEq, m_handles, m_nl);

            //<Handles>
            //    ::= <Handles> <nlo> '|' <Handle>
            //    |   <Handle>
            AddProduction(m_handles, m_handles, m_nlo, m_pipe, m_handle);
            AddProduction(m_handles, m_handle);

            //<Handle>
            //    ::= <Symbols>
            //    |   '<>'
            AddProduction(m_handle, m_symbols);
            AddProduction(m_handle, m_ltgt);

            //<Symbols>
            //    ::= <Symbols> <Symbol>
            //    |
            AddProduction(m_symbols, m_symbols, m_symbol);
            AddProduction(m_symbols);

            //<Symbol>
            //    ::= <Terminal Name>
            //    |   NonTerminal
            AddProduction(m_symbol, m_terminalName);
            AddProduction(m_symbol, m_nonTerminal);
        }
Ejemplo n.º 6
0
 public void Add(EKleene kleene, CharacterSet set)
 {
     m_items.Add(new CharacterSetNode(kleene, set));
 }
Ejemplo n.º 7
0
 public CharacterSet AddCharacterSet(string name, CharacterSet set)
 {
     m_characterSets.Add(name, set);
     return(set);
 }