private static void AssertLexerRuleMatches(DfaLexerRule lexerRule, string input) { var lexeme = lexerRule.CreateLexeme(0); for (var i = 0; i < input.Length; i++) { Assert.IsTrue(lexeme.Scan(input[i]), $"character '{input[i]}' not recognized at position {i}."); } Assert.IsTrue(lexeme.IsAccepted(), $"input {input} not accepted."); }
public void RegexToDfaShouldConvertCharacterRegexToDfa() { var pattern = "a"; var dfa = CreateDfaFromRegexPattern(pattern); Assert.IsNotNull(dfa); var lexerRule = new DfaLexerRule(dfa, "a"); var lexeme = lexerRule.CreateLexeme(0); Assert.IsTrue(lexeme.Scan('a')); }
public void DfaLexemeGivenCharacerLexemeNumberShouldFail() { var numberInput = "0"; var dfa = DfaState.Inner(); var final = DfaState.Final(); dfa.AddTransition(new RangeTerminal('a', 'z'), final); final.AddTransition(new RangeTerminal('a', 'z'), final); var dfaLexerRule = new DfaLexerRule(dfa, new TokenName("lowerCase")); var letterLexeme = dfaLexerRule.CreateLexeme(0); Assert.IsFalse(letterLexeme.Scan(numberInput[0])); Assert.AreEqual(string.Empty, letterLexeme.Value); }
public void DfaLexemeShouldMatchOneOrMoreRandomWhitespaceCharacters() { var randomWhitespace = "\t\f\v \r\n"; var dfa = DfaState.Inner(); var final = DfaState.Final(); dfa.AddTransition(WhitespaceTerminal.Instance, final); final.AddTransition(WhitespaceTerminal.Instance, final); var dfaLexerRule = new DfaLexerRule(dfa, new TokenName("whitespace")); var whitespaceLexeme = dfaLexerRule.CreateLexeme(0); foreach (var character in randomWhitespace) { Assert.IsTrue(whitespaceLexeme.Scan(character)); } }
public void DfaLexemeShouldMatchMixedCaseWord() { var wordInput = "t90vAriabl3"; var dfa = DfaState.Inner(); var final = DfaState.Final(); dfa.AddTransition(new RangeTerminal('a', 'z'), final); dfa.AddTransition(new RangeTerminal('A', 'Z'), final); final.AddTransition(new RangeTerminal('a', 'z'), final); final.AddTransition(new RangeTerminal('A', 'Z'), final); final.AddTransition(DigitTerminal.Instance, final); var dfaLexerRule = new DfaLexerRule(dfa, new TokenName("Identifier")); var indentifierLexeme = dfaLexerRule.CreateLexeme(0); foreach (var character in wordInput) { Assert.IsTrue(indentifierLexeme.Scan(character)); } }