private void ReturnIdOrTokenOrLabel(LexerState lexerState, Symbol symbol) { if (_tokens.Contains(CurrentToken.ToString())) { CurrentToken.TokenIndex = _tokens.IndexOf(CurrentToken.ToString()) + 1; Log(LogEventLevel.Information, "Found token {0}", false, CurrentToken); ReturnToken(CurrentToken, symbol); } else if (symbol.Class?.Class == Class.Colon || Parsed.Last().Substring == "goto") { //Label ReturnLabel(new StateMachine.Transition(lexerState, LexerState.LabelDefinition, symbol)); } else { Log(LogEventLevel.Information, "Not found token - treat as ID: {0}", false, CurrentToken); var identifier = Identifiers.FirstOrDefault(x => x.Name == CurrentToken.ToString())?.Clone() as IdentifierToken; if (identifier == null) { identifier = new IdentifierToken(CurrentToken.ToString()) { TokenIndex = IdIndex }; Identifiers.Add(identifier); } identifier.Line = Line; ReturnToken(identifier, symbol); } }
private void ReturnToken(Token token, Symbol trigger) { if (_tokens.Contains(CurrentToken.ToString()) && CurrentToken.TokenIndex == null) { CurrentToken.TokenIndex = _tokens.IndexOf(CurrentToken.ToString()) + 1; } Log(LogEventLevel.Information, "Added token: {0}", false, CurrentToken.Escaped); Parsed.Add(token); CurrentToken = new StringToken { Line = Line }; _machine.Reset(); if (trigger != Symbol.Space && trigger.Value != null) { _machine.Fire(trigger); } }
private void ReturnConst(LexerState state, Symbol trigger) { Log(LogEventLevel.Information, "Found a constant"); var value = ConstantToken <float> .Parse(CurrentToken.ToString()); var con = Constants.FirstOrDefault(x => Math.Abs(x.Value - value) < 1E-5)?.Clone() as ConstantToken <float>; if (con == null) { con = new ConstantToken <float>(CurrentToken.ToString()) { TokenIndex = ConstIndex, Substring = CurrentToken.ToString() }; Constants.Add(con); } else { Log(LogEventLevel.Information, "The constant is already processed"); } con.Line = Line; ReturnToken(con, trigger); }
private ExpressionNode ParseLeaf() { if (IsLiteral(CurrentToken.TokenType)) { var node = new ExpressionNode(CurrentToken); Index++; return(node); } if (CurrentToken.TokenType == TokenType.OpenParens) { Index++; var node = ParseAddSub(); if (CurrentToken.TokenType != TokenType.CloseParens) { AddError("error at " + CurrentToken.Index.ToString() + " missing closing parenthesis"); return(null); } Index++; return(node); } if (CurrentTokenType == TokenType.Field) { var node = new ExpressionNode(CurrentToken); Index++; return(node); } else if (CurrentTokenType == TokenType.Variable) { var node = new ExpressionNode(CurrentToken); Index++; return(node); } AddError("error at " + CurrentToken.Index.ToString() + " unexpected token: " + CurrentToken.ToString()); return(null); }