Пример #1
0
 public IRuleBuilder Rule(params object[] symbols)
 {
     var symbolList = new List<ISymbol>();
     if (symbols != null)
     {
         foreach (var symbol in symbols)
         {
             if (symbol is char)
             {
                 var terminal = new Terminal((char)symbol);
                 var lexerRule = new TerminalLexerRule(
                     terminal,
                     new TokenType(terminal.ToString()));
                 symbolList.Add(lexerRule);
             }
             else if (symbol is ITerminal)
             {
                 var terminal = symbol as ITerminal;
                 var lexerRule = new TerminalLexerRule(
                     terminal,
                     new TokenType(terminal.ToString()));
                 symbolList.Add(lexerRule);
             }
             else if (symbol is ILexerRule)
             {
                 symbolList.Add(symbol as ILexerRule);
             }
             else if (symbol is string)
             {
                 var nonTerminal = new NonTerminal(symbol as string);
                 symbolList.Add(nonTerminal);
             }
             else if (symbol == null)
             { }
             else { throw new ArgumentException("unrecognized terminal or nonterminal"); }
         }
     }
     _rules.Add(symbolList);
     return this;
 }
Пример #2
0
        private static ILexerRule CreateNotDoubleQuoteLexerRule()
        {
            // ( [^"\\] | (\\ .) ) +
            var start = new DfaState();
            var escape = new DfaState();
            var final = new DfaState(true);

            var notQuoteTerminal = new NegationTerminal(
                new SetTerminal('"', '\\'));
            var escapeTerminal = new Terminal('\\');
            var anyTerminal = new AnyTerminal();

            var notQuoteEdge = new DfaEdge(notQuoteTerminal, final);
            start.AddEdge(notQuoteEdge);
            final.AddEdge(notQuoteEdge);

            var escapeEdge = new DfaEdge(escapeTerminal, escape);
            start.AddEdge(escapeEdge);
            final.AddEdge(escapeEdge);

            var anyEdge = new DfaEdge(anyTerminal, final);
            escape.AddEdge(anyEdge);

            return new DfaLexerRule(start, new TokenType("not-double-quote"));
        }
Пример #3
0
        public IRuleBuilder Rule1(params object[] symbols)
        {
            var symbolList = new List<ISymbol>();
            var terminalNeighborList = new List<ITerminal>();

            if (symbols.IsNullOrEmpty())
                return this;

            foreach (var symbol in symbols)
            {
                var flushTerminals = false;
                if (symbol is INonTerminal)
                {
                    flushTerminals = true;
                    symbolList.Add(symbol as INonTerminal);
                }
                else if (symbol is string)
                {
                    flushTerminals = true;
                    var nonTerminal = new NonTerminal(symbol as string);
                    symbolList.Add(nonTerminal);
                }
                else if (symbol is char)
                {
                    var terminal = new Terminal((char)symbol);
                    terminalNeighborList.Add(terminal);
                }
                else if (symbol is ITerminal)
                {
                    terminalNeighborList.Add(symbol as ITerminal);
                }
                else if (symbol is ILexerRule)
                {
                    flushTerminals = true;
                    symbolList.Add(symbol as ILexerRule);
                }
                else if (symbol == null)
                { }
                else { throw new ArgumentException("unrecognized terminal or nonterminal"); }

                if (flushTerminals)
                {
                    var grammarLexerRule = CreateGrammarLexerRule(terminalNeighborList);
                    symbolList.Add(grammarLexerRule);
                    terminalNeighborList.Clear();
                }
            }

            if (terminalNeighborList.Count > 0)
            {
                var grammarLexerRule = CreateGrammarLexerRule(terminalNeighborList);
                symbolList.Add(grammarLexerRule);
                terminalNeighborList.Clear();
            }

            _rules.Add(symbolList);
            return this;
        }