private void HandleExtractBlock(char c) { switch (c) { case '\n': throw new ParseException("')' is expected here", this.globalIndex); case '(': this.stack += c; this.bracketCount++; break; case ')': if (this.bracketCount == 0) { RawExpression rawExpression = new RawExpression(this.globalIndex - this.stack.Length, this.stack); this.expressions.Add(new Expression(rawExpression)); this.state = ParseStates.Start; } else { this.bracketCount--; this.stack += c; } break; default: this.stack += c; break; } }
private void HandleParsePowerDigit(char c) { if (!Char.IsDigit(c) || c == '\n') { if (String.IsNullOrEmpty(this.stack)) { throw new ParseException("Only digits are allowed here", this.globalIndex); } int power = int.Parse(this.stack); if (this.expressions.Count == 0) { throw new ParseException("Power operation should follow some expression", this.globalIndex); } Expression lastExpression = this.expressions.Last(); for (int i = 0; i < power - 1; i++) { this.expressions.Add(lastExpression.Clone()); } this.state = ParseStates.Start; this.HandleStart(c); } else { this.stack += c; } }
private string ParseDialogueText(string input) { char [] chars = input.ToCharArray(); string output = ""; string temp = ""; ParseStates state = ParseStates.Normal; for (int i = 0; i < chars.Length; i++) { if (chars[i] == '{' && state == ParseStates.Normal) { state = ParseStates.Response; temp = ""; } else if (chars[i] == '}' && state == ParseStates.Response) { state = ParseStates.Normal; string response = GameManager.Inst.DBManager.DBHandlerDialogue.GetGlobalResponse(temp, GetSpeakerName()); output = output + response; } else if (state == ParseStates.Response) { temp = temp + chars[i]; } else if (state == ParseStates.Normal) { output = output + chars[i]; } } return(output); }
private void HandleStart(char c) { if (c != ' ') // ignore all whitespaces { switch (c) { case '-': this.state = ParseStates.WaitSymbols; this.coeff = -1; break; case '+': throw new ParseException("'+' sign is not expected to be here", this.globalIndex); case '(': this.state = ParseStates.EscapeSymbols; this.blockOffset = this.index; break; default: // here we hit some symbol of expression this.state = ParseStates.WaitOperand; this.blockOffset = this.index; break; } } }
private void reset() { this.index = 0; this.globalIndex = this.rawMultiplier.offset; this.state = ParseStates.Start; this.expressions = new List <Expression>(); this.stack = String.Empty; this.bracketCount = 0; }
private void reset() { this.state = ParseStates.Start; this.index = 0; this.blockOffset = 0; this.globalIndex = rawExpression.offset; this.coeff = 1; this.bracketCounter = 0; }
internal void clear() { state = ParseStates.init; method = ParseMethods.init; bytePos = 0; bytesUsed = 0; charPos = 0; charsUsed = 0; sb = new StringBuilder(); }
private void HandleStart(char c) { this.stack = String.Empty; switch (c) { case '\n': break; case ' ': // ignore whitespaces break; case '(': this.state = ParseStates.ExtractBlock; break; case '^': this.state = ParseStates.ParsePowerDigit; break; default: if (Char.IsLetter(c)) { if (Char.IsUpper(c)) { throw new ParseException("Only lower case letters are allowed", this.globalIndex); } this.addSummand(c); } else { if (Char.IsDigit(c)) { this.stack += c; this.state = ParseStates.ParseDigit; } else { throw new ParseException($"'{c}' symbol is not allowed here", this.globalIndex); } } break; } }
private void ProcessCommandData() { switch (_parseState) { case ParseStates.NAME: _name = System.Text.ASCIIEncoding.ASCII.GetString(_parseBuffer, 0, _parseBufferCount); break; case ParseStates.FIRMWARE: _firmware = System.Text.ASCIIEncoding.ASCII.GetString(_parseBuffer, 0, _parseBufferCount); break; case ParseStates.STATS: // TODO: Parse stats break; } _parseBufferCount = 0; _parseState = ParseStates.COMMAND; }
private void HandleEscapeSymbols(char c) { if (c == ')') { if (this.bracketCounter == 0) { this.state = ParseStates.WaitOperand; } else { this.bracketCounter--; } } else { if (c == '(') { this.bracketCounter++; } } }
private void ProcessCommand(byte command) { switch (command) { case 101: _devices.Clear(); _parseState = ParseStates.ENUMERATIONCOUNT; break; case 102: _parseState = ParseStates.FIRMWARE; break; case 105: _parseState = ParseStates.STATS; break; case 109: _parseState = ParseStates.MODE; break; case 110: _parseState = ParseStates.NAME; break; case 112: _parseState = ParseStates.PACKET; break; case 113: // Timeout case 114: // Bus Error if (State == EosBusState.WAITING_RESPONSE) { OnResponseReceived((EosPacket)null); State = EosBusState.POLLING; } break; } _parseBufferCount = 0; }
private void HandleParseDigit(char c) { if (Char.IsDigit(c) || c == '.') { // check whether we have already one dot in stack string if (c == '.' && this.stack.IndexOf('.') != -1) { throw new ParseException("Illigal symbol here", this.globalIndex); } this.stack += c; } else { double coeff = double.Parse(this.stack); List <Summand> summands = new List <Summand>(); summands.Add(new Summand(coeff, new Dictionary <char, int>())); this.expressions.Add(new Expression(summands)); this.state = ParseStates.Start; this.HandleStart(c); } }
private void ProcessMode(byte mode) { switch (mode) { case 112: State = EosBusState.POLLING; break; case 115: State = EosBusState.SCANNING; break; case 119: State = EosBusState.WAITING_RESPONSE; break; case 105: State = EosBusState.IDLE; break; } _parseState = ParseStates.COMMAND; }
private void HandleWaitSymbols(char c) { switch (c) { case '+': case '-': throw new ParseException("Summand symbols are expected here, not operand", this.globalIndex); case ' ': break; case '(': this.blockOffset = index; this.state = ParseStates.EscapeSymbols; break; default: this.blockOffset = index; this.state = ParseStates.WaitOperand; break; } }
private RawMultiplier HandleWaitOperand(char c) { if (c == '+' || c == '-') { string rawBlockString = this.rawExpression .rawString.Substring(this.blockOffset, index - this.blockOffset); RawMultiplier output = new RawMultiplier(this.blockOffset, rawBlockString, this.coeff); // define coefficent this.coeff = (c == '-') ? -1 : 1; this.state = ParseStates.WaitSymbols; return(output); } else { if (c == '(') { this.state = ParseStates.EscapeSymbols; } } return(null); }
/// <summary> /// Process the indicated number of bytes and sent data to the transport /// stream as needed. /// </summary> /// <param name="buffer">Some buffer.</param> /// <param name="start">First byte to process.</param> /// <param name="length">Total number of bytes to process.</param> public virtual void AddPayload( byte[] buffer, int start, int length ) { // Not active if (IgnoreInput) return; // All for (int i = start, l = length; l-- > 0; ) { // To test byte test = buffer[i++]; // Count total number of bytes ++Position; // Check mode if (ParseStates.Synchronize == State) { // Wait for zero if (test == 0) State = ParseStates.Found0; // Next continue; } // Check mode if (ParseStates.Found00 == State) { // Wait for one if (test == 1) { // Next State = ParseStates.Found001; // Next continue; } // Fall back State = ParseStates.Found0; } // Check mode if (ParseStates.Found0 == State) { // Set State = (test == 0) ? ParseStates.Found00 : ParseStates.Synchronize; // Next continue; } // Check mode if (ParseStates.Found001 == State) { // Check for start code if (StartCode != 0) { // Set State = (test == StartCode) ? ParseStates.LenHigh : ((0 == test) ? ParseStates.Found0 : ParseStates.Synchronize); } else if (IsValidStartCode( test )) { // Remember LastStartCode = test; // Set State = ParseStates.LenHigh; } else { // Restart State = (test == 0) ? ParseStates.Found0 : ParseStates.Synchronize; } // Next continue; } // Length high byte if (ParseStates.LenHigh == State) { // Remember LastLenHigh = test; // Advance State = ParseStates.LenLow; // Go on continue; } // Length low byte if (ParseStates.LenLow == State) { // Remember LastLenLow = test; // Advance State = ParseStates.Found; } // Not found if (ParseStates.Found != State) continue; // Construct length int pesLength = LastLenLow + 256 * LastLenHigh; // Validate if (!IsValidLength( pesLength )) { // Reset state if (LastLenLow == 0) if (LastLenHigh == 0) State = ParseStates.Found00; else State = ParseStates.Found0; else State = ParseStates.Synchronize; // Go on continue; } // Length to process int found = i - start; // Flush buffers Flush( buffer, start, found, 6 ); // Time to adjust delayed buffer if (StartCode == 0) { // Copy to the very beginning Array.Copy( Delayed, DelayedBytes - 6, Delayed, 0, 6 ); // Reset DelayedBytes = 6; } // Next write will be a start IsFirst = true; // Readjust buffer start = i; length -= found; // Remember StartCode = LastStartCode; // Reset State = ParseStates.Synchronize; } // Set delay mode int delay = 0; // Check state switch (State) { case ParseStates.Found0: delay = 1; break; case ParseStates.Found00: delay = 2; break; case ParseStates.Found001: delay = 3; break; case ParseStates.LenHigh: delay = 4; break; case ParseStates.LenLow: delay = 5; break; } // All but these Flush( buffer, start, length, delay ); }
public void parseJson(string json, string parentName = "", bool tryParseInvalidJson = false, SOType forceType = SOType.Undefined) { var currentObject = this.root; if (parentName != "") { currentObject = this.find(parentName, true, root); } currentObject.name = parentName; ParseStates state = ParseStates.findValueStart; bool ignoreNextChar = false; StringBuilder currentStringContent = new StringBuilder(); StringBuilder currentNumberContent = new StringBuilder(); StringBuilder currentSpecialWordContent = new StringBuilder(); StringBuilder currentChildName = new StringBuilder(); int currLine = 1; int currCol = 1; int max = json.Length; char curr = ' '; for (int cont = 0; cont < max; cont++) { curr = json[cont]; currCol++; if (curr == '\n') { currCol = 1; currLine++; } switch (state) { case ParseStates.findingStart: if (curr == '"') { if (currentObject.isArray()) { state = ParseStates.prepareArray; } else { state = ParseStates.readingName; } currentChildName.Clear(); } else if ((curr == ',') /* || (curr == '[') || (curr == '{')*/) { if (currentObject.isArray()) { state = ParseStates.prepareArray; } } else if ((curr == '}') || (curr == ']')) { if (parentName.Contains('.')) { parentName = parentName.Substring(0, parentName.LastIndexOf('.')); currentObject = currentObject.parent; } else { parentName = ""; currentObject = root; } } break; case ParseStates.readingName: if (curr == '"') { state = ParseStates.waitingKeyValueSep; currentObject = this.find(currentChildName.ToString(), true, currentObject, forceType); currentObject.name = currentChildName.ToString(); parentName = parentName + (parentName != "" ? "." : "") + currentChildName; } else { currentChildName.Append(curr); } break; case ParseStates.waitingKeyValueSep: if (curr == ':') { state = ParseStates.findValueStart; } break; case ParseStates.findValueStart: if (curr == '"') { state = ParseStates.readingContentString; currentStringContent.Clear(); } else if (curr == '{') { state = ParseStates.findingStart; } else if (curr == '[') { state = ParseStates.prepareArray; } else if ("0123456789-+.".Contains(curr)) { state = ParseStates.readingContentNumber; currentNumberContent.Clear(); cont--; currCol--; } else if ("untf".Contains(curr)) { state = ParseStates.readingContentSpecialWord; currentSpecialWordContent.Clear(); cont--; currCol--; } else if (curr == ']') { //delete currenObject var temp = currentObject; if (parentName.Contains('.')) { parentName = parentName.Substring(0, parentName.LastIndexOf('.')); currentObject = currentObject.parent; } else { parentName = ""; currentObject = root; } currentObject.delete(temp.name); cont--; currCol--; state = ParseStates.findingStart; } else if (!" \t\r\n".Contains(curr)) { if (!tryParseInvalidJson) { throw new Exception("SintaxError at line " + currLine + " and column " + currCol + ". Expected ' '(space), \t, \r or \n, but found " + curr + "."); } } break; case ParseStates.prepareArray: //state = "findingStart"; currentChildName.Clear(); currentChildName.Append(currentObject.__getChildsNames().Count.ToString()); currentObject = this.find(currentChildName.ToString(), true, currentObject, forceType); currentObject.name = currentChildName.ToString(); parentName = parentName + (parentName != "" ? "." : "") + currentChildName; state = ParseStates.findValueStart; cont--; currCol--; break; case ParseStates.readingContentString: if (ignoreNextChar) { currentStringContent.Append(curr); ignoreNextChar = false; } else if (curr == '\\') { ignoreNextChar = true; currentStringContent.Append(curr); } else if (curr == '"') { currentObject.setSingleValue(currentStringContent.ToString()); //return to parent Object if (parentName.Contains('.')) { parentName = parentName.Substring(0, parentName.LastIndexOf('.')); currentObject = currentObject.parent; } else { parentName = ""; currentObject = root; } state = ParseStates.findingStart; } else { currentStringContent.Append(curr); } break; case ParseStates.readingContentNumber: if ("0123456789.-+".Contains(curr)) { currentNumberContent.Append(curr); } else { currentObject.setSingleValue(currentNumberContent.ToString()); //return to parent Object if (parentName.Contains('.')) { parentName = parentName.Substring(0, parentName.LastIndexOf('.')); currentObject = currentObject.parent; } else { parentName = ""; currentObject = root; } cont--; state = ParseStates.findingStart; } break; case ParseStates.readingContentSpecialWord: if ("truefalseundefindednul".Contains(curr)) { currentSpecialWordContent.Append(curr); } else { string strTemp = currentSpecialWordContent.ToString(); if ((strTemp == "true") || (strTemp == "false") || (strTemp == "null") || (strTemp == "undefined")) { currentObject.setSingleValue(strTemp); //return to parent Object if (parentName.Contains('.')) { parentName = parentName.Substring(0, parentName.LastIndexOf('.')); currentObject = currentObject.parent; } else { parentName = ""; currentObject = root; } cont--; state = ParseStates.findingStart; } else { if (!tryParseInvalidJson) { throw new Exception("Invalid simbol at line " + currLine + " and column " + currCol + ": " + currentSpecialWordContent); } } } break; } } }
/// <summary> /// Process the indicated number of bytes and sent data to the transport /// stream as needed. /// </summary> /// <param name="buffer">Some buffer.</param> /// <param name="start">First byte to process.</param> /// <param name="length">Total number of bytes to process.</param> public virtual void AddPayload(byte[] buffer, int start, int length) { // Not active if (IgnoreInput) { return; } // All for (int i = start, l = length; l-- > 0;) { // To test byte test = buffer[i++]; // Count total number of bytes ++Position; // Check mode if (ParseStates.Synchronize == State) { // Wait for zero if (test == 0) { State = ParseStates.Found0; } // Next continue; } // Check mode if (ParseStates.Found00 == State) { // Wait for one if (test == 1) { // Next State = ParseStates.Found001; // Next continue; } // Fall back State = ParseStates.Found0; } // Check mode if (ParseStates.Found0 == State) { // Set State = (test == 0) ? ParseStates.Found00 : ParseStates.Synchronize; // Next continue; } // Check mode if (ParseStates.Found001 == State) { // Check for start code if (StartCode != 0) { // Set State = (test == StartCode) ? ParseStates.LenHigh : ((0 == test) ? ParseStates.Found0 : ParseStates.Synchronize); } else if (IsValidStartCode(test)) { // Remember LastStartCode = test; // Set State = ParseStates.LenHigh; } else { // Restart State = (test == 0) ? ParseStates.Found0 : ParseStates.Synchronize; } // Next continue; } // Length high byte if (ParseStates.LenHigh == State) { // Remember LastLenHigh = test; // Advance State = ParseStates.LenLow; // Go on continue; } // Length low byte if (ParseStates.LenLow == State) { // Remember LastLenLow = test; // Advance State = ParseStates.Found; } // Not found if (ParseStates.Found != State) { continue; } // Construct length int pesLength = LastLenLow + 256 * LastLenHigh; // Validate if (!IsValidLength(pesLength)) { // Reset state if (LastLenLow == 0) { if (LastLenHigh == 0) { State = ParseStates.Found00; } else { State = ParseStates.Found0; } } else { State = ParseStates.Synchronize; } // Go on continue; } // Length to process int found = i - start; // Flush buffers Flush(buffer, start, found, 6); // Time to adjust delayed buffer if (StartCode == 0) { // Copy to the very beginning Array.Copy(Delayed, DelayedBytes - 6, Delayed, 0, 6); // Reset DelayedBytes = 6; } // Next write will be a start IsFirst = true; // Readjust buffer start = i; length -= found; // Remember StartCode = LastStartCode; // Reset State = ParseStates.Synchronize; } // Set delay mode int delay = 0; // Check state switch (State) { case ParseStates.Found0: delay = 1; break; case ParseStates.Found00: delay = 2; break; case ParseStates.Found001: delay = 3; break; case ParseStates.LenHigh: delay = 4; break; case ParseStates.LenLow: delay = 5; break; } // All but these Flush(buffer, start, length, delay); }
public override void ProcessData(byte[] buffer, int offset, int length) { for (int i = 0; i < length; i++) { byte data = buffer[offset + i]; switch (_parseState) { case ParseStates.COMMAND: ProcessCommand(data); break; case ParseStates.MODE: ProcessMode(data); break; case ParseStates.PACKET: _parseBuffer[_parseBufferCount++] = data; if ((_parseBufferCount == 4 && _parseBuffer[3] == 0) || (_parseBufferCount > 4 && _parseBufferCount == (4 + _parseBuffer[3]))) { EosPacket packet = new EosPacket(); packet.Destination = _parseBuffer[0]; packet.Source = _parseBuffer[1]; packet.Command = _parseBuffer[2]; for (int j = 0; j < _parseBuffer[3]; j++) { packet.Add(_parseBuffer[4 + j]); } OnPacketReceived(packet); if (State == EosBusState.WAITING_RESPONSE) { OnResponseReceived(packet); } if (packet.Command == 127 && packet.Data.Count > 0) { EosDevice device = Devices.GetByAddress(packet.Source); device.UpdateState(packet); OnDeviceUpdated(device); } _parseState = ParseStates.COMMAND; //Console.WriteLine(); } break; case ParseStates.ENUMERATIONCOUNT: if (data > 0) { _parseBuffer[0] = data; _parseState = ParseStates.ENUMERATION; } else { _parseState = ParseStates.COMMAND; } break; case ParseStates.ENUMERATION: _parseBuffer[1 + _parseBufferCount++] = data; if (_parseBufferCount == 21) { List <byte> deviceData = new List <byte>(20); for (int j = 0; j < 20; j++) { deviceData.Add(_parseBuffer[2 + j]); } _devices.Add(new EosDevice(this, _parseBuffer[1], deviceData)); _parseBuffer[0]--; _parseBufferCount = 0; if (_parseBuffer[0] == 0) { _parseState = ParseStates.COMMAND; OnBusReset(); //Console.WriteLine(); } } break; default: if (data == 58) { ProcessCommandData(); //Console.WriteLine(); } else { _parseBuffer[_parseBufferCount++] = data; } break; } } }