private IEnumerable StringMode(Itr itr) { if (currentItem.Length == 0) { currentItem.Append('"'); } currentItem.Append(itr.Current()); if (SwitchMode(itr.Mode)) { yield return(PopString(currentItem)); } else { if (itr.IsFinalIndex()) { currentItem.Append('"'); yield return(PopString(currentItem)); } else if ('\\'.Equals(itr.Current())) { currentItem.Append(itr.Peek(1)); itr.MoveNext(); //don't check next character since already added } } }
/// <summary> /// Switches lexing mode if necessary, returns true if switch was performed /// </summary> private bool SwitchMode(LexerMode mode) { if (mode == LexerMode.Normal) { if ('"'.Equals(itr.Current())) { itr.Mode = LexerMode.String; return(true); } if ('/'.Equals(itr.Current())) { if ('/'.Equals(itr.Peek(1))) { itr.Mode = LexerMode.Comment; return(true); } if ('*'.Equals(itr.Peek(1))) { itr.Mode = LexerMode.MultiComment; return(true); } } } else if (mode == LexerMode.String) { if (itr.Current() == '"') { itr.Mode = LexerMode.Normal; return(true); } } else { if (mode == LexerMode.Comment) { if (itr.IsFinalIndex()) { itr.Mode = LexerMode.Normal; return(true); } } else if (mode == LexerMode.MultiComment) { if (itr == "*/") { itr.Mode = LexerMode.Normal; itr.MoveNext(); //move to '/' return(true); } } } return(false); }
private IEnumerable <string> NormalMode(Itr itr) { if (!SwitchMode(itr.Mode)) { if (' '.Equals(itr.Current())) { if (currentItem.Length > 0) { yield return(PopString(currentItem)); } } else if (SplitOnDot(itr)) { if (currentItem.Length > 0) { yield return(PopString(currentItem)); } yield return(itr.Current().ToString()); } else if (MatchDoubleSymbolDelimiter(__multi_symbol_delimiters__, itr)) { if (currentItem.Length > 0) { yield return(PopString(currentItem)); } currentItem.Append(itr.Current()); currentItem.Append(itr.Peek(1)); itr.MoveNext(); } else if (MatchDelimiter(__Updated_Delimiters__, itr)) { if (currentItem.Length > 0) { yield return(PopString(currentItem)); } yield return(itr.Current().ToString()); } else { currentItem.Append(itr.Current()); } if (itr.IsFinalIndex() && currentItem.Length > 0) { yield return(PopString(currentItem)); } } else if (itr.Current() != ' ' && currentItem.Length > 0) { yield return(PopString(currentItem)); } }
private static bool SplitOnDot(Itr itr) { if (itr == ".") { if (itr.IsFirstIndex() || itr.Peek(-1) == ' ') //dot is first index or white space { if (!itr.IsFinalIndex()) //not final index { if (Regex.IsMatch(itr.Peek(1).ToString(), @"[\d]")) //digit { return(false); } } } if (itr.IsFirstIndex() || itr.Peek(-1) == '.') { return(true); } if (Regex.IsMatch(itr.Peek(-1).ToString(), @"[\d]")) //if it's got a digit before { int i = -2; while (true) { if (Regex.IsMatch(itr.Peek(i).ToString(), @"[\D]")) //not a digit { if (Regex.IsMatch(itr.Peek(i).ToString(), @"[\w]")) //is a word character { return(true); } } if (itr.Index + --i < 0) { break; } } } } return(false); }
private static bool MatchDelimiter(Delimiter[] delimiters, Itr itr) { foreach (Delimiter del in delimiters) { if (itr != del.toMatch) { continue; } if (del.RegexPostMatch != null) { if (itr.IsFinalIndex()) { return(false); } if (!Regex.IsMatch(itr.Peek(1).ToString(), del.RegexPostMatch)) { continue; } } else if (del.PostMatch != null) { var match = false; foreach (string str in del.PostMatch) { if (itr.PeekAndEquals(del.toMatch.Length, str)) { match = true; break; } } if (!match) { continue; } } if (del.RegexPreMatch != null) { if (itr.IsFirstIndex()) { return(false); } if (!Regex.IsMatch(itr.Peek(-1).ToString(), del.RegexPreMatch)) { continue; } } else if (del.PreMatch != null) { var match = false; foreach (string str in del.PreMatch) { if (itr.PeekAndEquals(-str.Length, str)) { match = true; break; } } if (!match) { continue; } } return(true); } return(false); }