示例#1
0
        static NumberLexerRule()
        {
            var states = new DfaState[5];
            for (int i = 0; i < states.Length; i++)
                states[i] = new DfaState(i==4 || i== 2);

            var zeroThroughNine = new RangeTerminal('0', '9');

            var plusOrMinusTo1 = new DfaTransition(new SetTerminal('+', '-'), states[1]);
            var dotTo3 = new DfaTransition(new CharacterTerminal('.'), states[3]);
            var zeroThroughNineTo2 = new DfaTransition(zeroThroughNine, states[2]);
            var zeroThroughNineTo4 = new DfaTransition(zeroThroughNine, states[4]);

            states[0].AddTransition(dotTo3);
            states[0].AddTransition(plusOrMinusTo1);
            states[0].AddTransition(zeroThroughNineTo2);

            states[1].AddTransition(dotTo3);
            states[1].AddTransition(zeroThroughNineTo2);

            states[2].AddTransition(zeroThroughNineTo2);
            states[2].AddTransition(dotTo3);

            states[3].AddTransition(zeroThroughNineTo4);

            states[4].AddTransition(zeroThroughNineTo4);

            _start = states[0];
        }
示例#2
0
 static WhitespaceLexerRule()
 {
     _start = new DfaState();
     var end = new DfaState(isFinal: true);
     var transition = new DfaTransition(
         new WhitespaceTerminal(),
         end);
     _start.AddTransition(transition);
     end.AddTransition(transition);
 }
示例#3
0
 private static ILexerRule Whitespace()
 {
     var start = new DfaState();
     var end = new DfaState(isFinal: true);
     var transition = new DfaTransition(
         new WhitespaceTerminal(),
         end);
     start.AddTransition(transition);
     end.AddTransition(transition);
     return new DfaLexerRule(start, "\\w+");
 }
示例#4
0
        public void DfaLexemeShouldMatchOneOrMoreRandomWhitespaceCharacters()
        {
            var randomWhitespace = "\t\f\v \r\n";
            var dfa = new DfaState();
            var final = new DfaState(true);
            dfa.AddTransition(new DfaTransition(new WhitespaceTerminal(), final));
            final.AddTransition(new DfaTransition(new WhitespaceTerminal(), final));

            var dfaLexerRule = new DfaLexerRule(dfa, new TokenType("whitespace"));
            var whitespaceLexeme = new DfaLexeme(dfaLexerRule);
            for (int i = 0; i < randomWhitespace.Length; i++)
                Assert.IsTrue(whitespaceLexeme.Scan(randomWhitespace[i]));
        }
示例#5
0
        public void DfaLexemeGivenCharacerLexemeNumberShouldFail()
        {
            var numberInput = "0";
            var dfa = new DfaState();
            var final = new DfaState(true);
            dfa.AddTransition(new DfaTransition(new RangeTerminal('a', 'z'), final));
            final.AddTransition(new DfaTransition(new RangeTerminal('a', 'z'), final));

            var dfaLexerRule = new DfaLexerRule(dfa, new TokenType("lowerCase"));
            var letterLexeme = new DfaLexeme(dfaLexerRule);
            Assert.IsFalse(letterLexeme.Scan(numberInput[0]));
            Assert.AreEqual(string.Empty, letterLexeme.Capture);
        }
示例#6
0
        public void DfaLexemeShouldMatchMixedCaseWord()
        {
            var wordInput = "t90vAriabl3";
            var dfa = new DfaState();
            var final = new DfaState(true);
            dfa.AddTransition(new DfaTransition(new RangeTerminal('a', 'z'), final));
            dfa.AddTransition(new DfaTransition(new RangeTerminal('A', 'Z'), final));
            final.AddTransition(new DfaTransition(new RangeTerminal('a', 'z'), final));
            final.AddTransition(new DfaTransition(new RangeTerminal('A', 'Z'), final));
            final.AddTransition(new DfaTransition(new DigitTerminal(), final));

            var dfaLexerRule = new DfaLexerRule(dfa, new TokenType("Identifier"));
            var indentifierLexeme = new DfaLexeme(dfaLexerRule);
            for (int i = 0; i < wordInput.Length; i++)
                Assert.IsTrue(indentifierLexeme.Scan(wordInput[i]));
        }
示例#7
0
 private static BaseLexerRule CreateWhitespaceLexerRule()
 {
     var whitespaceTerminal = new WhitespaceTerminal();
     var startWhitespace = new DfaState();
     var finalWhitespace = new DfaState(true);
     startWhitespace.AddTransition(new DfaTransition(whitespaceTerminal, finalWhitespace));
     finalWhitespace.AddTransition(new DfaTransition(whitespaceTerminal, finalWhitespace));
     var whitespace = new DfaLexerRule(startWhitespace, "whitespace");
     return whitespace;
 }
示例#8
0
 private static BaseLexerRule CreateSettingIdentifierLexerRule()
 {
     // /:[a-zA-Z][a-zA-Z0-9]*/
     var start = new DfaState();
     var oneLetter = new DfaState();
     var zeroOrMoreLetterOrDigit = new DfaState(true);
     start.AddTransition(
        new DfaTransition(
            new CharacterTerminal(':'),
            oneLetter));
     oneLetter.AddTransition(
          new DfaTransition(
             new CharacterClassTerminal(
                 new RangeTerminal('a', 'z'),
                 new RangeTerminal('A', 'Z')),
             zeroOrMoreLetterOrDigit));
     zeroOrMoreLetterOrDigit.AddTransition(
         new DfaTransition(
             new CharacterClassTerminal(
                 new RangeTerminal('a', 'z'),
                 new RangeTerminal('A', 'Z'),
                 new DigitTerminal()),
             zeroOrMoreLetterOrDigit));
     return new DfaLexerRule(start, "settingIdentifier");
 }
示例#9
0
 private static BaseLexerRule CreateNotSingleQuoteLexerRule()
 {
     var start = new DfaState();
     var final = new DfaState(true);
     var terminal = new NegationTerminal(new CharacterTerminal('\''));
     var edge = new DfaTransition(terminal, final);
     start.AddTransition(edge);
     final.AddTransition(edge);
     return new DfaLexerRule(start, new TokenType(@"([^']|(\\.))+"));
 }
示例#10
0
        private static BaseLexerRule CreateNotDoubleQuoteLexerRule()
        {
            // ([^"]|(\\.))*
            var start = new DfaState();
            var escape = new DfaState();
            var final = new DfaState(true);

            var notDoubleQuoteTerminal = new NegationTerminal(
                new CharacterTerminal('"'));
            var escapeTerminal = new CharacterTerminal('\\');
            var anyTerminal = new AnyTerminal();

            var notDoubleQuoteEdge = new DfaTransition(notDoubleQuoteTerminal, final);
            start.AddTransition(notDoubleQuoteEdge);
            final.AddTransition(notDoubleQuoteEdge);

            var escapeEdge = new DfaTransition(escapeTerminal, escape);
            start.AddTransition(escapeEdge);
            final.AddTransition(escapeEdge);

            var anyEdge = new DfaTransition(anyTerminal, final);
            escape.AddTransition(anyEdge);

            return new DfaLexerRule(start, new TokenType(@"([^""]|(\\.))+"));
        }
示例#11
0
        private static BaseLexerRule CreateMultiLineCommentLexerRule()
        {
            var pattern = @"\/[*]([*][^\/]|[^*])*[*][\/]";

            var states = new DfaState[5];
            for (int i = 0; i < states.Length; i++)
                states[i] = new DfaState();

            var slash = new CharacterTerminal('/');
            var star = new CharacterTerminal('*');
            var notStar = new NegationTerminal(star);
            var notSlash = new NegationTerminal(slash);

            var firstSlash = new DfaTransition(slash, states[1]);
            var firstStar = new DfaTransition(star, states[2]);
            var repeatNotStar = new DfaTransition(notStar, states[2]);
            var lastStar = new DfaTransition(star, states[3]);
            var goBackNotSlash = new DfaTransition(notSlash, states[2]);
            var lastSlash = new DfaTransition(slash, states[4]);

            states[0].AddTransition(firstSlash);
            states[1].AddTransition(firstStar);
            states[2].AddTransition(repeatNotStar);
            states[2].AddTransition(lastStar);
            states[3].AddTransition(goBackNotSlash);
            states[3].AddTransition(lastSlash);

            return new DfaLexerRule(states[0], pattern);
        }
示例#12
0
 private static BaseLexerRule CreateIdentifierLexerRule()
 {
     // /[a-zA-Z][a-zA-Z0-9-_]*/
     var identifierState = new DfaState();
     var zeroOrMoreLetterOrDigit = new DfaState(true);
     identifierState.AddTransition(
         new DfaTransition(
             new CharacterClassTerminal(
                 new RangeTerminal('a', 'z'),
                 new RangeTerminal('A', 'Z')),
             zeroOrMoreLetterOrDigit));
     zeroOrMoreLetterOrDigit.AddTransition(
         new DfaTransition(
             new CharacterClassTerminal(
                 new RangeTerminal('a', 'z'),
                 new RangeTerminal('A', 'Z'),
                 new DigitTerminal(),
                 new SetTerminal('-', '_')),
             zeroOrMoreLetterOrDigit));
     var identifier = new DfaLexerRule(identifierState, "identifier");
     return identifier;
 }
示例#13
0
 private static BaseLexerRule CreateEscapeCharacterLexerRule()
 {
     var start = new DfaState();
     var escape = new DfaState();
     var final = new DfaState(true);
     start.AddTransition(new DfaTransition(new CharacterTerminal('\\'), escape));
     escape.AddTransition(new DfaTransition(new AnyTerminal(), final));
     return new DfaLexerRule(start, "escape");
 }
示例#14
0
 private static BaseLexerRule CreateWhitespaceLexerRule()
 {
     var whitespaceTerminal = new WhitespaceTerminal();
     var startState = new DfaState();
     var finalState = new DfaState(true);
     var whitespaceTransition = new DfaTransition(whitespaceTerminal, finalState);
     startState.AddTransition(whitespaceTransition);
     finalState.AddTransition(whitespaceTransition);
     return new DfaLexerRule(startState, new TokenType("[\\s]+"));
 }
示例#15
0
 private static ILexerRule CreateRuleNameLexerRule()
 {
     var ruleNameState = new DfaState();
     var zeroOrMoreLetterOrDigit = new DfaState(true);
     ruleNameState.AddTransition(
         new DfaTransition(
             new CharacterClassTerminal(
                 new RangeTerminal('a', 'z'),
                 new RangeTerminal('A', 'Z')),
             zeroOrMoreLetterOrDigit));
     zeroOrMoreLetterOrDigit.AddTransition(
         new DfaTransition(
             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;
 }
 public NfaClosure(HashSet<INfaState> closure, bool isFinal)
 {
     Closure = closure;
     _hashCode = HashCode.ComputeHash(closure);
     State = new DfaState(isFinal);
 }