Ejemplo n.º 1
0
 public void Test_DfaLexeme_Given_Number_When_Character_Lexeme_Then_Failes_Match()
 {
     var numberInput = "0";
     var dfa = new DfaState();
     var final = new DfaState(true);
     dfa.AddEdge(new DfaEdge(new RangeTerminal('a', 'z'), final));
     final.AddEdge(new DfaEdge(new RangeTerminal('a', 'z'), final));
     var letterLexeme = new DfaLexeme(dfa, new TokenType("lowerCase"));
     Assert.IsFalse(letterLexeme.Scan(numberInput[0]));
     Assert.AreEqual(string.Empty, letterLexeme.Capture);
 }
Ejemplo n.º 2
0
        public void Test_DfaLexeme_That_Whitespace_One_Or_Many_Matches_Random_Whitespace()
        {
            var randomWhitespace = "\t\f\v \r\n";
            var dfa = new DfaState();
            var final = new DfaState(true);
            dfa.AddEdge(new DfaEdge(new WhitespaceTerminal(), final));
            final.AddEdge(new DfaEdge(new WhitespaceTerminal(), final));

            var whitespaceLexeme = new DfaLexeme(dfa, new TokenType("whitespace"));
            for (int i = 0; i < randomWhitespace.Length; i++)
                Assert.IsTrue(whitespaceLexeme.Scan(randomWhitespace[i]));
        }
Ejemplo n.º 3
0
        public void Test_DfaLexeme_Given_Mixed_Case_Word_When_Identifier_Lexeme_Then_Matches_Input()
        {
            var wordInput = "t90vAriabl3";
            var dfa = new DfaState();
            var final = new DfaState(true);
            dfa.AddEdge(new DfaEdge(new RangeTerminal('a', 'z'), final));
            dfa.AddEdge(new DfaEdge(new RangeTerminal('A', 'Z'), final));
            final.AddEdge(new DfaEdge(new RangeTerminal('a', 'z'), final));
            final.AddEdge(new DfaEdge(new RangeTerminal('A', 'Z'), final));
            final.AddEdge(new DfaEdge(new DigitTerminal(), final));

            var indentifierLexeme = new DfaLexeme(dfa, new TokenType("Identifier"));
            for (int i = 0; i < wordInput.Length; i++)
                Assert.IsTrue(indentifierLexeme.Scan(wordInput[i]));
        }
Ejemplo n.º 4
0
 private static ILexerRule CreateWhitespaceLexerRule()
 {
     var whitespaceTerminal = new WhitespaceTerminal();
     var startWhitespace = new DfaState();
     var finalWhitespace = new DfaState(true);
     startWhitespace.AddEdge(new DfaEdge(whitespaceTerminal, finalWhitespace));
     finalWhitespace.AddEdge(new DfaEdge(whitespaceTerminal, finalWhitespace));
     var whitespace = new DfaLexerRule(startWhitespace, new TokenType("whitespace"));
     return whitespace;
 }
Ejemplo n.º 5
0
 private static ILexerRule CreateRuleNameLexerRule()
 {
     var ruleNameState = new DfaState();
     var zeroOrMoreLetterOrDigit = new DfaState(true);
     ruleNameState.AddEdge(
         new DfaEdge(
             new CharacterClassTerminal(
                 new RangeTerminal('a', 'z'),
                 new RangeTerminal('A', 'Z')),
             zeroOrMoreLetterOrDigit));
     zeroOrMoreLetterOrDigit.AddEdge(
         new DfaEdge(
             new CharacterClassTerminal(
                 new RangeTerminal('a', 'z'),
                 new RangeTerminal('A', 'Z'),
                 new DigitTerminal(),
                 new SetTerminal('-', '_')),
             zeroOrMoreLetterOrDigit));
     var ruleName = new DfaLexerRule(ruleNameState, new TokenType("rule-name"));
     return ruleName;
 }
Ejemplo n.º 6
0
 private static ILexerRule CreateNotSingleQuoteLexerRule()
 {
     var start = new DfaState();
     var final = new DfaState(true);
     var terminal = new NegationTerminal(new Terminal('\''));
     var edge = new DfaEdge(terminal, final);
     start.AddEdge(edge);
     final.AddEdge(edge);
     return new DfaLexerRule(start, new TokenType("not-single-quote"));
 }
Ejemplo n.º 7
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"));
        }