public void TerminalLexemShouldWhileAcceptedContinuesToMatch() { var terminalLexeme = new TerminalLexeme( new CharacterTerminal('c'), new TokenType("c"), 0); Assert.IsFalse(terminalLexeme.IsAccepted()); Assert.IsTrue(terminalLexeme.Scan('c')); Assert.IsTrue(terminalLexeme.IsAccepted()); Assert.IsFalse(terminalLexeme.Scan('c')); }
public void TerminalLexemeResetShouldClearPreExistingValues() { var terminalLexeme = new TerminalLexeme( new CharacterTerminal('c'), new TokenType("c"), 0); Assert.IsTrue(terminalLexeme.Scan('c')); terminalLexeme.Reset( new TerminalLexerRule(new CharacterTerminal('a'), new TokenType("a")), 10); Assert.AreEqual(10, terminalLexeme.Position); }
public void TerminalLexemShouldWhileAcceptedContinuesToMatch() { var input = "c"; var segment = input.AsCapture(); var terminalLexeme = new TerminalLexeme( new CharacterTerminal('c'), new TokenType("c"), segment, 0); Assert.IsFalse(terminalLexeme.IsAccepted()); Assert.IsTrue(terminalLexeme.Scan()); Assert.IsTrue(terminalLexeme.IsAccepted()); Assert.IsFalse(terminalLexeme.Scan()); }
public override bool Scan() { // get expected lexems // PERF: Avoid Linq where, let and select expressions due to lambda allocation var expectedLexemes = SharedPools.Default <List <TerminalLexeme> >().AllocateAndClear(); var expectedLexerRules = _parseEngine.GetExpectedLexerRules(); foreach (var rule in expectedLexerRules) { if (rule.LexerRuleType == TerminalLexerRule.TerminalLexerRuleType) { expectedLexemes.Add( new TerminalLexeme( rule as ITerminalLexerRule, Capture, _parseEngine.Location)); } } // filter on first rule to pass (since all rules are one character per lexeme) // PERF: Avoid Linq FirstOrDefault due to lambda allocation TerminalLexeme firstPassingRule = null; foreach (var lexeme in expectedLexemes) { if (lexeme.Scan()) { firstPassingRule = lexeme; break; } } SharedPools.Default <List <TerminalLexeme> >() .ClearAndFree(expectedLexemes); if (firstPassingRule is null) { return(false); } var result = _parseEngine.Pulse(firstPassingRule); if (!result) { return(false); } return(Capture.Grow()); }
public void TerminalLexemeResetShouldClearPreExistingValues() { var input = "c"; var segment = input.AsCapture(); var terminalLexeme = new TerminalLexeme( new CharacterTerminal('c'), new TokenType("c"), segment, 0); Assert.IsTrue(terminalLexeme.Scan()); terminalLexeme.Reset( new TerminalLexerRule(new CharacterTerminal('a'), new TokenType("a")), 0); Assert.AreEqual(0, terminalLexeme.Position); }
public void TerminalLexemeShouldInitializeCatpureToEmptyString() { var terminalLexeme = new TerminalLexeme(new CharacterTerminal('c'), new TokenType("c"), 0); Assert.AreEqual(string.Empty, terminalLexeme.Value); }