public void DataHandle(byte data) { switch (satus) { case parseType.wait0xaa: if (data == 0xaa) { satus = parseType.wait0x55; pd = new PictureData(); } break; case parseType.wait0x55: if (data == 0x55) { satus = parseType.waitdatalen; } break; case parseType.waitdatalen: pd.dataLength += (data << (pd.dataLengthstep * 8)); pd.dataLengthstep--; if (pd.dataLengthstep < 0) { pd.data = new byte[pd.dataLength]; satus = parseType.waitdata; } break; case parseType.waitdata: pd.data[pd.dataIndex++] = data; if (pd.dataIndex == pd.dataLength) { satus = parseType.waittail; } break; case parseType.waittail: if (data == 0xa5) { satus = parseType.dataready; } break; default: break; } if (satus == parseType.dataready) { //接受到完整数据包 satus = parseType.wait0xaa; } }
private void parseTokens(Token[] tokens) { VariableObject outVariable = null; for(int i = 0; i < tokens.Length && tokens[i].token != null; i++) { Debug.drawDebugLine(debugState.Debug, "Token {0}: Current state: {1}, {2} type: {3}", i, this.currentType.ToString(), tokens[i].token, tokens[i].type); if(currentType == parseType.Function) { if(tokens[i].type == TokenState.Token_Decimal || tokens[i].type == TokenState.Token_Float || tokens[i].type == TokenState.Token_String || tokens[i].type == TokenState.Token_Chars) { LocalArguments.addArgument(new ParameterObject(this.tokenToSystemObject(tokens[i]))); // operation.Add(tokens[i]); } else if(tokens[i].type == TokenState.Token_Variable) { LocalArguments.addArgument(new VariableObject(tokens[i].token)); } else if(tokens[i].type == TokenState.Token_Brackets_Close) { object result = null; if(outVariable == null && Impulse.hasResult()) { Debug.drawDebugLine(debugState.Warning, "Function has a result type, calling without a destination."); } try { result = Impulse.callFunction(); } catch(Exception ex) { Debug.drawDebugLine(debugState.Error, "Function '{0}' error: {1}", Impulse.getFunctionName(), ex.Message); } if(result != null) { Debug.drawDebugLine(debugState.Info, "Result type {0} value {1}.", result.GetType().ToString(), result); if(outVariable != null) { outVariable.setValue(result); } } //if(operation[0].type != TokenState.Token_Keyword) { // Debug.drawDebugLine(debugState.Warning, "Not a function! ({0}). :(", operation[0].type.ToString()); //} //else { // Debug.drawDebugLine(debugState.Info, "Method '{0}' call.", operation[0].token); // try { // // Impulse.callStdMethod(arguments[0].token, arguments.Skip(1).ToArray()); // } // catch(Exception ex) { // Debug.drawDebugLine(debugState.Error, "Function '{0}' error: {1}", operation[0].token, ex.Message); // } //} // operation.Clear(); LocalArguments.clearArguments(); outVariable = null; currentType = parseType.Unknown; } } else if(this.currentType == parseType.Variable_operation) { Debug.drawDebugLine(debugState.Info, "Current token: {0}.", tokens[i].token); if(tokens[i].type == TokenState.Token_Keyword) { try { Impulse.requestMethod(tokens[i].token); this.currentType = parseType.Function; } catch(Exception e) { Debug.drawDebugLine(debugState.Error, "Request function '{0}' error: {1}", tokens[i].token, e.Message); this.currentType = parseType.Unknown; } } else if(tokens[i].type == TokenState.Token_Chars || tokens[i].type == TokenState.Token_Decimal || tokens[i].type == TokenState.Token_Float || tokens[i].type == TokenState.Token_String) { outVariable.setValue(this.tokenToSystemObject(tokens[i])); this.currentType = parseType.Unknown; } } else if(this.currentType == parseType.Unknown && tokens[i].type == TokenState.Token_Keyword) { // if(tokens[i].token.ToLower() == "define") { // this.currentType = parseType.Definition; // } // else if(tokens[i].token.ToLower() == "import") { // this.currentType = parseType.Import; // } if(this.currentType == parseType.Unknown && tokens[i].token.ToLower() == "new") { this.currentType = parseType.Variable_definition; } //else if(Regex.Match(tokens[i].token, "^[_a-z]+[a-zA-Z]+?$").Success) { else { if(tokens.Length > (i + 1)) { // if(tokens[i + 1].type == TokenState.Token_Reference) { // this.currentType = parseType.ClassReference; // } if(tokens[i + 1].type == TokenState.Token_Brackets) { this.currentType = parseType.Function; } else { Debug.drawDebugLine(debugState.Warning, "Unknown keyword declaration token."); this.currentType = parseType.Unknown; continue; } try { Impulse.requestMethod(tokens[i].token); } catch(Exception e) { Debug.drawDebugLine(debugState.Error, "Request function '{0}' error: {1}", tokens[i].token, e.Message); this.currentType = parseType.Unknown; } // operation.Add(tokens[i]); } } // else Debug.drawDebugLine(debugState.Error, "Can not parse keyword {0}, token type {1}", tokens[i].token, tokens[i].type.ToString()); } else if(this.currentType == parseType.Unknown && tokens[i].type == TokenState.Token_Variable) { if(tokens.Length > (i + 1)) { // operation.Add(tokens[i]); outVariable = new VariableObject(tokens[i].token); if(tokens[i + 1].type == TokenState.Token_Operator) { // operation.Add(tokens[++i]); this.currentType = parseType.Variable_operation; i++; } else { Debug.drawDebugLine(debugState.Warning, "What should I do with the variable?"); } } } else if(this.currentType == parseType.Variable_definition) { if(tokens[i].type == TokenState.Token_Variable) { Debug.drawDebugLine(debugState.Debug, "Variable definition: {0}", tokens[i].token); if(tokens.Length > (i + 1) && tokens[i + 1].type == TokenState.Token_Operator) { Debug.drawDebugLine(debugState.Info, "-> Variable assignment"); outVariable = new VariableObject(tokens[i++].token, true); this.currentType = parseType.Variable_operation; } else { Variables.setValue(tokens[i].token, null); this.currentType = parseType.Unknown; } } else { Debug.drawDebugLine(debugState.Error, "Variable definition is ambiguous!"); } } } }
// private List<Token> operation; public Parser() { lex = new Lexer(); currentType = parseType.Unknown; // operation = new List<Token>(); }
//other packet data goes here. this is so that //we can send all notification messages to remote handler. public packet(string input, parseType action, DateTime t) { data = input; result = action; toc = t; }