public void buildingWorks(string regex, string expected) { StringCursor stringCursor = new StringCursor(regex); NodeAST node = Regex.MatchRegex(stringCursor); Assert.That(node.ToString(), Is.EqualTo(expected)); }
public bool TryCreateFragment(StringCursor text, out Fragment fragment) { ITextSplitter textSplitter = new SeparatorTextSplitter(text, new[] {'\r', '\n'}); fragment = new DelimitedFragment(textSplitter, _fragmentFactory); return true; }
public void RegexMatchWorks(string regex, bool expected) { StringCursor stringCursor = new StringCursor(regex); NodeAST node = Regex.MatchRegex(stringCursor); Assert.That(node.Type != NodeASTType.Error, Is.EqualTo(expected)); }
public bool TryCreateFragment(StringCursor text, out Fragment fragment) { if (text.Count == 0) { fragment = default(Fragment); return false; } return _fragmentFactory.TryCreateFragment(text, out fragment); }
/// <summary> /// Execute the parser on the text /// </summary> /// <param name="parser"></param> /// <param name="text"></param> /// <typeparam name="TResult"></typeparam> /// <returns></returns> public static Result <Cursor <char>, TResult> Execute <TResult>(this Parser <char, TResult> parser, string text) { var cursor = new StringCursor(text); return(parser.Parse(cursor)); }
public DelimiterTextSplitter(StringCursor text, char delimiter) { if (text == null) throw new ArgumentNullException("text"); _delimiter = delimiter; _text = text.Data; _parseOffset = text.Offset; _parseEnd = text.Offset + text.Count; _elements = new List<StringCursor>(); }
public bool TryGetText(int index, out StringCursor text) { if (index < 0) throw new ArgumentOutOfRangeException("index", "Index must be >= 0"); if (index < _elements.Count) { text = _elements[index]; return true; } while ((_parseEnd - _parseOffset) > 0 && index >= _elements.Count) { int lineEnd = _text.IndexOf(_delimiter, _parseOffset, _parseEnd - _parseOffset); if (lineEnd < 0) lineEnd = _parseEnd; _elements.Add(new LocationStringCursor(_text, _parseOffset, lineEnd - _parseOffset, _elements.Count)); if (lineEnd == _parseEnd) { _parseOffset = lineEnd; break; } _parseOffset = lineEnd + 1; if (_parseEnd == _parseOffset) { _elements.Add(new LocationStringCursor(_text, _parseOffset, 0, _elements.Count)); } } if (index < _elements.Count) { text = _elements[index]; return true; } text = default(StringCursor); return false; }
public bool TryCreateFragment(StringCursor text, out Fragment fragment) { fragment = new ParsedFragment(text); return true; }
public ParsedFragment(StringCursor text) { _text = text; }
public bool TryGetText(int index, out StringCursor text) { if (index < 0) throw new ArgumentOutOfRangeException("index", "Index must be >= 0"); if (index < _elements.Count) { text = _elements[index]; return true; } while ((_parseEnd - _parseOffset) > 0 && index >= _elements.Count) { int lineEnd = _text.IndexOfAny(_separator, _parseOffset); if (lineEnd < 0) lineEnd = _parseEnd; _elements.Add(new LocationStringCursor(_text, _parseOffset, lineEnd - _parseOffset, _elements.Count)); if (lineEnd == _parseEnd) { _parseOffset = lineEnd; break; } for (int i = 0; i < _separator.Length && lineEnd < _parseEnd; i++, lineEnd++) { if (_text[lineEnd] != _separator[i]) break; } _parseOffset = lineEnd; if (_parseEnd == _parseOffset) { _elements.Add(new LocationStringCursor(_text, _parseOffset, 0, _elements.Count)); } } if (index < _elements.Count) { text = _elements[index]; return true; } text = default(StringCursor); return false; }
public SeparatorTextSplitter(StringCursor text, char[] separator = null) { if (text == null) throw new ArgumentNullException("text"); _separator = separator ?? new[] {'\r', '\n'}; _text = text.Data; _parseOffset = text.Offset; _parseEnd = text.Offset + text.Count; _elements = new List<StringCursor>(); }