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]; }
static WhitespaceLexerRule() { _start = new DfaState(); var end = new DfaState(isFinal: true); var transition = new DfaTransition( new WhitespaceTerminal(), end); _start.AddTransition(transition); end.AddTransition(transition); }
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+"); }
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])); }
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); }
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])); }
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; }
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"); }
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(@"([^']|(\\.))+")); }
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(@"([^""]|(\\.))+")); }
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); }
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; }
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"); }
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]+")); }
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); }