private void UpdateTokenState(AsciiAtom command) { if (command.Category != AtomType.CONTROL && command.Category != AtomType.DELIMITER) { this.currentToken = this.currentToken + command.Symbol; } else if (this.CurrentState == LexicalMachineState.QUOTED_STRING_TOKEN) { this.currentToken = this.currentToken + command.Symbol; } }
public void MoveNext(AsciiAtom command) { LexicalMachineState nextState = GetNext(command.Category); if (nextState == LexicalMachineState.ARRAY_TOKEN && this.CurrentState == LexicalMachineState.VAR_TOKEN) { this.arrayToken = this.currentToken; this.ClearToken(); this.CurrentState = nextState; return; } else if (this.CurrentState == LexicalMachineState.INDEX_OF_ARRAY_TOKEN && command.Category != AtomType.DIGIT) { this.indexOrSize = int.Parse(this.currentToken); this.ClearToken(); this.OnTokenIdentified(command); this.CurrentState = nextState; return; } else if (nextState == LexicalMachineState.VAR_TOKEN && this.CurrentState == LexicalMachineState.EMPTY) { this.UpdateTokenState(command); this.CurrentState = nextState; return; } else if (nextState == LexicalMachineState.EMPTY && this.CurrentState == LexicalMachineState.VAR_TOKEN || nextState == LexicalMachineState.EMPTY && this.CurrentState == LexicalMachineState.QUOTED_STRING_TOKEN) { this.UpdateTokenState(command); this.OnTokenIdentified(command); this.ClearToken(); this.CurrentState = nextState; return; } else if (nextState == LexicalMachineState.EMPTY && command.Category == AtomType.CONTROL || nextState == LexicalMachineState.EMPTY && this.CurrentState != LexicalMachineState.EMPTY || nextState == LexicalMachineState.SPECIAL_TOKEN && (this.CurrentState == LexicalMachineState.STRING_TOKEN || this.CurrentState == LexicalMachineState.INT_TOKEN || this.CurrentState == LexicalMachineState.SPECIAL_TOKEN) || this.CurrentState == LexicalMachineState.SPECIAL_TOKEN && (nextState == LexicalMachineState.INT_TOKEN || nextState == LexicalMachineState.STRING_TOKEN)) { this.OnTokenIdentified(command); this.ClearToken(); } this.UpdateTokenState(command); this.CurrentState = nextState; }
protected virtual void OnTokenIdentified(AsciiAtom command) { if (this.CurrentState == LexicalMachineState.INDEX_OF_ARRAY_TOKEN) { NotifyTokenIdentified?.Invoke(new Token(this.arrayToken, this.indexOrSize, false)); } else if (command.Category == AtomType.CONTROL) { NotifyTokenIdentified?.Invoke(new Token(this.CurrentState, this.currentToken, command)); NotifyTokenIdentified?.Invoke(new Token()); } else { Console.WriteLine("Lexical: sent token: " + this.currentToken); NotifyTokenIdentified?.Invoke(new Token(this.CurrentState, this.currentToken, command)); } }
public Token(LexicalMachineState state, string tokenString, AsciiAtom command) { this.Text = tokenString; if (state == LexicalMachineState.STRING_TOKEN) { this.Type = this.Text.ToUpper() switch { "PRINT" => TokenType.PRINT, "DIM" => TokenType.DIM, "LET" => TokenType.LET, "READ" => TokenType.READ, "DATA" => TokenType.DATA, "FOR" => TokenType.FOR, "TO" => TokenType.TO, "NEXT" => TokenType.NEXT, "GO" => TokenType.GO, "GOTO" => TokenType.GOTO, "GOSUB" => TokenType.GOSUB, "RETURN" => TokenType.RETURN, "REM" => TokenType.REMARK, "IF" => TokenType.IF, "STEP" => TokenType.STEP, "THEN" => TokenType.THEN, "END" => TokenType.FINAL, _ => TokenType.STRING }; } else if (state == LexicalMachineState.INT_TOKEN) { this.Type = TokenType.INT; } else if (state == LexicalMachineState.SPECIAL_TOKEN) { this.Type = this.Text switch { "=" => TokenType.EQUALS, ":=" => TokenType.EQUALS, ">" => TokenType.GREATER, ">=" => TokenType.GREATER_OR_EQUAL, "<" => TokenType.LESS, "<=" => TokenType.LESS_OR_EQUAL, "<>" => TokenType.NOT_EQUAL, "(" => TokenType.OPENING_BRACES, ")" => TokenType.CLOSING_BRACES, "," => TokenType.COMMA, "+" => TokenType.PLUS, "-" => TokenType.MINUS, "*" => TokenType.MULT, "/" => TokenType.DIV, _ => TokenType.ERROR }; } else if (state == LexicalMachineState.VAR_TOKEN) { this.Type = TokenType.VAR; } else if (state == LexicalMachineState.QUOTED_STRING_TOKEN) { this.Type = TokenType.QUOTED_STRING; } else if (command.Category == AtomType.CONTROL) { this.Type = TokenType.END; } else { this.Type = TokenType.ERROR; } }
public void ConsumeCategorizedSymbolEvent(AsciiAtom symbol) { this.MoveNext(symbol); }