private void ParseBuffer() { while (_parserBuffer.Count > 0) { var c = _parserBuffer.Dequeue(); var isLineDelimiter = (c == _parserConfigurations.LineDelimiter); var isColumnDelimiter = (c == _parserConfigurations.ColumnDelimiter); switch (_parserState) { case ParserStates.NotStarted: case ParserStates.LineEnded: if (isColumnDelimiter) { OnColumnFound(); _parserState = ParserStates.ColumnEnded; } else { _currentDataText.Append(c); _parserState = ParserStates.ColumnStarted; } break; case ParserStates.LineStarted: case ParserStates.ColumnStarted: case ParserStates.ColumnEnded: if (isLineDelimiter) { OnColumnFound(); OnLineFound(); _parserState = ParserStates.LineEnded; } else if (isColumnDelimiter) { OnColumnFound(); _parserState = ParserStates.ColumnEnded; } else { _currentDataText.Append(c); } break; default: _currentDataText.Append(c); break; } } }
public void Parse(byte[] array) { foreach (var ch in array) { try { switch (_parserState) { case ParserStates.ExpectingSOH: _currentMessage = new NetworkMessage(); _payloadIndex = 0; _parserState = ParserStates.ExpectingPIN1; break; case ParserStates.ExpectingPIN1: _currentMessage.CheckSum += ch; _currentMessage.Pin = ch; _parserState = ParserStates.ExpectingPIN2; break; case ParserStates.ExpectingPIN2: _currentMessage.CheckSum += ch; _currentMessage.Pin |= (short)(ch << 8); _parserState = ParserStates.ExpectingSN1; break; case ParserStates.ExpectingSN1: _currentMessage.CheckSum += ch; _currentMessage.SerialNumber = ch; _parserState = ParserStates.ExpectingSN2; break; case ParserStates.ExpectingSN2: _currentMessage.CheckSum += ch; _currentMessage.SerialNumber |= (short)(ch << 8); _parserState = ParserStates.ExpectingLEN1; break; case ParserStates.ExpectingLEN1: _currentMessage.CheckSum += ch; _currentMessage.PayloadLength = ch; _parserState = ParserStates.ExpectingLEN2; break; case ParserStates.ExpectingLEN2: _currentMessage.CheckSum += ch; _currentMessage.PayloadLength |= (short)(ch << 8); _parserState = ParserStates.ExpectingPayloadFormat; break; case ParserStates.ExpectingPayloadFormat: if (ch < (byte)PayloadFormats.JSON || ch > (byte)PayloadFormats.None) /* If Payload Formats are added, make sure you adjust here */ { _currentMessage = null; _parserState = ParserStates.ExpectingSOH; } else { _currentMessage.CheckSum += ch; _currentMessage.PayloadFormat = (PayloadFormats)ch; _parserState = ParserStates.ExpectingMessageTypeCode; } break; case ParserStates.ExpectingMessageTypeCode: _currentMessage.CheckSum += ch; _currentMessage.MessageTypeCode = ch; _parserState = ParserStates.ExpectingSTX; break; case ParserStates.ExpectingSTX: if (ch != STX) { _currentMessage = null; _parserState = ParserStates.ExpectingSOH; } else { _currentMessage.CheckSum += ch; if (_currentMessage.PayloadLength > 0) { _payloadIndex = 0; _currentMessage.Payload = new byte[_currentMessage.PayloadLength]; _parserState = ParserStates.ReadingMessage; } else { _parserState = ParserStates.ExpectingETX; } } break; case ParserStates.ReadingMessage: _currentMessage.CheckSum += ch; _currentMessage.Payload[_payloadIndex++] = ch; if (_payloadIndex == _currentMessage.PayloadLength) { _parserState = ParserStates.ExpectingETX; } break; case ParserStates.ExpectingETX: _currentMessage.CheckSum += ch; if (ch != ETX) { _currentMessage = null; _parserState = ParserStates.ExpectingSOH; } else { _parserState = ParserStates.ExpectingCS; break; } break; case ParserStates.ExpectingCS: var checkSum = ch; if (checkSum != _currentMessage.CheckSum) { _currentMessage = null; _parserState = ParserStates.ExpectingSOH; } else { _parserState = ParserStates.ExpectingEOT; } break; case ParserStates.ExpectingEOT: _parserState = ParserStates.ExpectingSOH; MessageReady?.Invoke(this, _currentMessage); _currentMessage = null; break; } } catch (Exception) { _parserState = ParserStates.ExpectingSOH; _currentMessage = null; } } }
public List <Token> GetTokenList() { List <Token> tokens = new List <Token>(); ParserStates state = ParserStates.DEFAULT; string current_token = ""; int source_lenght = source.Length; int line_number = 1; for (int i = 0; i < source_lenght; i++) { char current_char = source[i]; char next_char = ' '; int next_char_pointer = i + 1; if (next_char_pointer < source_lenght) { next_char = source[next_char_pointer]; } if (current_char.Equals('\n')) { line_number++; } switch (state) { case ParserStates.DEFAULT: if (current_char.Equals('-') && Char.IsDigit(next_char)) { current_token += current_char; state = ParserStates.NUMBER; break; } int index_of = char_tokens.IndexOf(current_char); if (index_of >= 0) { string oper = current_char.ToString(); string two_chars_operator = current_char.ToString() + next_char.ToString(); if (two_chars_operator.Equals("<>") || two_chars_operator.Equals("<=") || two_chars_operator.Equals(">=")) { oper = two_chars_operator; char[] chars = source.ToCharArray(); chars[next_char_pointer] = ' '; source = new string(chars); } tokens.Add(new Token(oper, char_tokens_types[index_of])); } else if (Char.IsLetter(current_char)) { current_token += current_char; state = ParserStates.WORD; } else if ((Char.IsDigit(current_char)) || ((current_char.Equals('-')) && (Char.IsDigit(next_char))) ) { current_token += current_char; state = ParserStates.NUMBER; } else if (current_char.Equals('"')) { state = ParserStates.STRING; } else if (current_char.Equals('\'')) { state = ParserStates.COMMENT; } else if (current_char.Equals('#')) { state = ParserStates.COMMENT; } break; case ParserStates.WORD: if (Char.IsLetterOrDigit(current_char)) { current_token += current_char; } else if (current_char.Equals(":")) { tokens.Add(new Token(current_token, TokenTypes.LABEL)); current_token = ""; state = ParserStates.DEFAULT; i--; } else if (Array.IndexOf(built_in_functions, current_token) >= 0) { tokens.Add(new Token(current_token, TokenTypes.FNCTION)); current_token = ""; state = ParserStates.DEFAULT; i--; } else { tokens.Add(new Token(current_token, TokenTypes.WORD)); current_token = ""; state = ParserStates.DEFAULT; i--; } break; case ParserStates.NUMBER: if ((Char.IsDigit(current_char)) || (current_char.Equals('.'))) { if ((current_char.Equals('.')) && (current_token.IndexOf('.') >= 0)) { throw new Exception("Bad Formed Number:[" + current_token + "] at line:" + line_number); } if ((current_char.Equals(".")) && (!Char.IsDigit(next_char))) { throw new Exception("Bad Formed Number:[" + current_token + "] at line:" + line_number); } current_token += current_char; } else { tokens.Add(new Token(current_token, TokenTypes.NUMBER)); current_token = ""; state = ParserStates.DEFAULT; i--; } break; case ParserStates.STRING: if (current_char.Equals('"')) { tokens.Add(new Token(current_token, TokenTypes.STRING)); current_token = ""; state = ParserStates.DEFAULT; //i--; } else if (current_char.Equals('\n')) { throw new Exception("Unterminated string:[" + current_token.Trim() + "...] at line:" + line_number); } else { current_token += current_char; } break; case ParserStates.COMMENT: if (current_char.Equals('\n')) { state = ParserStates.DEFAULT; } break; } } return(tokens); }
public static void Do_AddScreenToProject() { ProjectState++; ProviderState = ProviderStates.NoProvider; ParserState = ParserStates.NoParser; }
public static void Do_SelectProvidedParser() { ParserState = ParserStates.ProvidedParser; }
public static void Do_SelectDefaultParser() { ParserState = ParserStates.DefaultParser; }
static void Main() { ParserStates state = ParserStates.Normal; int n = int.Parse(Console.ReadLine()); for (int i = 0; i < n; i++) { if (state == ParserStates.SingleLineComment) { state = ParserStates.Normal; } string line = Console.ReadLine(); for (int j = 0; j < line.Length; j++) { char currChar = line[j]; if (currChar == '/') { switch (state) { case ParserStates.Normal: // Check if this is a start for singleLine Comment if (j < line.Length - 1 && line[j + 1] == '/') { state = ParserStates.SingleLineComment; } else if (j < line.Length - 1 && line[j + 1] == '*') { state = ParserStates.MultiLineComment; } break; case ParserStates.SingleLineComment: break; case ParserStates.MultiLineComment: if (j > 0 && line[j - 1] == '*') { state = ParserStates.Normal; continue; } break; case ParserStates.Quote: break; case ParserStates.FullEscape: break; default: break; } } else if (currChar == '"') { switch (state) { case ParserStates.Normal: if ((j > 1 && line[j - 1] != '\\') || (j > 1 && line[j - 2] != '\'')) { state = ParserStates.Quote; } break; case ParserStates.SingleLineComment: break; case ParserStates.MultiLineComment: break; case ParserStates.Quote: // if we are allready in doubleQuote State and we encounter another double quote // that is not escaped - we set the state to normal if (j > 0 && line[j - 1] != '\\' || (j > 1 && line[j - 1] == '\\' && line[j - 2] == '\\')) { state = ParserStates.Normal; } break; case ParserStates.FullEscape: // This is the end of the full escape - @ if (j < line.Length - 1 && line[j + 1] == ';') { state = ParserStates.Normal; } break; default: break; } } else if (currChar == '@') { switch (state) { case ParserStates.Normal: // If the next character is doubleQuote - we enter FullEscape State if (j < line.Length && line[j + 1] == '"') { state = ParserStates.FullEscape; } break; case ParserStates.SingleLineComment: break; case ParserStates.MultiLineComment: break; case ParserStates.Quote: break; case ParserStates.FullEscape: break; default: break; } } if (state != ParserStates.SingleLineComment && state != ParserStates.MultiLineComment) { // Add Character cleanBuilder.Append(currChar); } } if (i != n - 1 && state != ParserStates.MultiLineComment) { cleanBuilder.Append("\r\n"); } } string[] finalOutput = cleanBuilder.ToString().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); cleanBuilder.Clear(); for (int i = 0; i < finalOutput.Length; i++) { Match match = Regex.Match(finalOutput[i], @"^\s+$"); if (match.Success) { continue; } else { cleanBuilder.Append(finalOutput[i]); if (i != finalOutput.Length - 1) { cleanBuilder.AppendLine(); } } } //File.WriteAllText("../../output.txt",cleanBuilder.ToString()); Console.WriteLine(cleanBuilder); }