public void PositiveSuccess1AtEnd() { RepetitionParser rp = +Parser; String s = "a"; StringScanner scan = new StringScanner(s); ParserMatch m = rp.Parse(scan); Assert.IsTrue(m.Success); Assert.AreEqual(1, m.Length); Assert.IsTrue(scan.AtEnd); }
/// <summary> /// Parse a string and return parse match /// </summary> /// <param name="s"></param> /// <returns></returns> public ParserMatch Parse(String s) { StringScanner sc = new StringScanner(s); ParserMatch match = this.expression.Parse(sc); if(!sc.AtEnd) { return ParserMatch.CreateFailureMatch(sc,0); } return match; }
public void ReadAndPeek() { StringScanner scanner = new StringScanner(Text); int i=0; while (!scanner.AtEnd) { Assert.Less(i, Text.Length); Assert.AreEqual(Text[i], scanner.Peek()); scanner.Read(); ++i; } Assert.AreEqual(Text.Length, i); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { long offset = scanner.Offset; ParserMatch m = FirstParser.Parse(scanner); if (m.Success) { StringScanner firstMatch = new StringScanner(m.Value); ParserMatch m2 = SecondParser.Parse(firstMatch); if (m2.Success) { return m; } } scanner.Seek(offset); return ParserMatch.CreateFailureMatch(scanner); }
public void InputMatchesFirstParserAndSecondParser_Actions_None() { IScanner letter = new StringScanner("a"); this._LetterOrDigitButNotLetterDifferenceParser.Parse(letter); Assert.IsFalse(_differenceParserSuccess); Assert.IsFalse(_letterOrDigitParserSuccess); // should not be called since it is ultimately not accepted Assert.IsFalse(_letterParserSuccess); // should not be called since the difference ultimately not accepted }
public void InputDoesNotMatchFirstParserAndDoesNotMatchSecondParser_Fail() { IScanner symbol = new StringScanner("+"); Assert.IsFalse(this._LetterOrDigitButNotLetterDifferenceParser.Parse(symbol).Success); }
public void InputMatchesFirstParserDoesNotMatchSecondParser_Actions_LetterOrDigitAndDiff() { IScanner digit = new StringScanner("1"); this._LetterOrDigitButNotLetterDifferenceParser.Parse(digit); Assert.IsTrue(_differenceParserSuccess); Assert.IsTrue(_letterOrDigitParserSuccess); Assert.IsFalse(_letterParserSuccess); }
public void Constructor2() { StringScanner scanner = new StringScanner(Text,Offset); Assert.AreEqual(Text, scanner.InputString); Assert.AreEqual(Offset, scanner.Offset); }
public void InputDoesNotMatchFirstParserButMatchesSecondParser_Fail() { IScanner symbol = new StringScanner("+"); DifferenceParser parser = new DifferenceParser(this._letterOrDigit, this._symbol); parser.Act += OnDifferenceParserSuccess; Assert.IsFalse(parser.Parse(symbol).Success); }
public void InputMatchesFirstParserDoesNotMatchSecondParser_ScanPosition_ForwardByMatchLength() { IScanner digit = new StringScanner("1 hello"); ParserMatch match = this._LetterOrDigitButNotLetterDifferenceParser.Parse(digit); Assert.AreEqual(1, match.Length); Assert.AreEqual(1, digit.Offset); }
public void InputDoesNotMatchFirstParserButMatchesSecondParser_ScanPosition_NoChange() { IScanner symbol = new StringScanner("+hello"); DifferenceParser parser = new DifferenceParser(this._letterOrDigit, this._symbol); parser.Parse(symbol); Assert.AreEqual(0, symbol.Offset); }
public void OptionalSuccess0() { RepetitionParser rp = !Parser; String s = ""; StringScanner scan = new StringScanner(s); ParserMatch m = rp.Parse(scan); Assert.IsTrue(m.Success); Assert.IsTrue(m.Empty); }
public void Offset_NotIncrementedAfterPeek() { StringScanner scanner = new StringScanner(Text); scanner.Peek(); Assert.AreEqual(0, scanner.Offset); }
public void KleneeSuccess0() { RepetitionParser rp = Ops.ZeroOrMore(Parser); String s = "b"; StringScanner scan = new StringScanner(s); ParserMatch m = rp.Parse(scan); Assert.IsTrue(m.Success); Assert.IsTrue(m.Empty); Assert.IsFalse(scan.AtEnd); }
public void OptionalSuccess() { RepetitionParser rp = !Parser; String s = "aa"; StringScanner scan = new StringScanner(s); ParserMatch m = rp.Parse(scan); Assert.IsTrue(m.Success); Assert.AreEqual(1,m.Length); Assert.AreEqual(1,scan.Offset); }
public void KleneeSuccessNotAtEnd() { RepetitionParser rp = Ops.ZeroOrMore(Parser); String s = "aaa "; StringScanner scan = new StringScanner(s); ParserMatch m = rp.Parse(scan); Assert.IsTrue(m.Success); Assert.AreEqual(3,m.Length); Assert.IsFalse(scan.AtEnd); }
public void PositiveFailure() { RepetitionParser rp = +Parser; String s = "b"; StringScanner scan = new StringScanner(s); ParserMatch m = rp.Parse(scan); Assert.IsFalse(m.Success); }
public void PositiveSequenceSuccessNotAtEnd() { RepetitionParser rp = +SequenceParser; String s = "ababab "; StringScanner scan = new StringScanner(s); ParserMatch m = rp.Parse(scan); Assert.IsTrue(m.Success); Assert.AreEqual(0, m.Offset); Assert.AreEqual(6, m.Length); Assert.IsFalse(scan.AtEnd); }
public void InputDoesNotMatchFirstParserButMatchesSecondParser_Actions_None() { IScanner symbol = new StringScanner("+"); DifferenceParser parser = new DifferenceParser(this._letterOrDigit, this._symbol); parser.Act += OnDifferenceParserSuccess; parser.Parse(symbol); Assert.IsFalse(_differenceParserSuccess); Assert.IsFalse(_letterOrDigitParserSuccess); Assert.IsFalse(_symbolParserSuccess); // should never call since first parser doesn't match }
public void InputMatchesFirstParserDoesNotMatchSecondParser_Success() { IScanner digit = new StringScanner("1"); Assert.IsTrue(this._LetterOrDigitButNotLetterDifferenceParser.Parse(digit).Success); }
public void InputDoesNotMatchFirstParserAndDoesNotMatchSecondParser_Actions_None() { IScanner symbol = new StringScanner("+"); this._LetterOrDigitButNotLetterDifferenceParser.Parse(symbol); Assert.IsFalse(_differenceParserSuccess); Assert.IsFalse(_letterOrDigitParserSuccess); Assert.IsFalse(_letterParserSuccess); }
public void InputMatchesFirstParserAndSecondParser_SecondParserLonger_Fail() { IScanner letter = new StringScanner("abc"); DifferenceParser parser = new DifferenceParser(this._letterOrDigit, Ops.Sequence(this._letterOrDigit, this._letterOrDigit)); Assert.IsFalse(parser.Parse(letter).Success); }
public void InputMatchesFirstParserAndSecondParser_ScanPosition_NoChange() { IScanner letter = new StringScanner("a hello"); this._LetterOrDigitButNotLetterDifferenceParser.Parse(letter); Assert.AreEqual(0, letter.Offset); }
public void Offset_IncrementedAfterRead() { StringScanner scanner = new StringScanner(Text); scanner.Read(); Assert.AreEqual(1,scanner.Offset); }
public void InputDoesNotMatchFirstParserAndDoesNotMatchSecondParser_ScanPosition_NoChange() { IScanner symbol = new StringScanner("+hello"); this._LetterOrDigitButNotLetterDifferenceParser.Parse(symbol); Assert.AreEqual(0, symbol.Offset); }
public void Substring() { StringScanner scanner = new StringScanner(Text,Offset); Assert.AreEqual(Text.Substring(3, 6), scanner.Substring(3, 6)); }
public void InputMatchesFirstParserAndSecondParser_SameLength_Fail() { IScanner letter = new StringScanner("abc"); Assert.IsFalse(this._LetterOrDigitButNotLetterDifferenceParser.Parse(letter).Success); }
/// <summary> /// Parse a string and return parse match /// </summary> /// <param name="rules"></param> /// <returns></returns> public string ConvertToIcuTailoringRule(String rules) { _result = string.Empty; _currentCollationElement = new StringBuilder(); _currentCollationLines = new Queue<string>(); _currentCollationGroups = new Queue<string>(); _currentCollationElements = new Queue<string>(); _usedCollationElements = new List<string>(); StringScanner sc = new StringScanner(rules); ParserMatch match = _collationRules.Parse(sc); Debug.Assert(match.Success); Debug.Assert(sc.AtEnd); return _result; }
public void InputMatchesFirstParserAndSecondParser_SecondParserShorter_Success() { IScanner letter = new StringScanner("abc"); DifferenceParser parser = new DifferenceParser(Ops.Sequence(this._letterOrDigit, this._letterOrDigit), this._letterOrDigit); Assert.IsTrue(parser.Parse(letter).Success); }
public bool ValidateSimpleCollationRules(string rules, out string message) { _currentCollationElement = new StringBuilder(); _currentCollationLines = new Queue<string>(); _currentCollationGroups = new Queue<string>(); _currentCollationElements = new Queue<string>(); _usedCollationElements = new List<string>(); StringScanner sc = new StringScanner(rules); message = null; try { ParserMatch match = _collationRules.Parse(sc); if (!match.Success || !sc.AtEnd) { message = "Invalid simple rules."; return false; } } catch (ParserErrorException e) { string errString = sc.InputString.Split(new char[] { '\n' })[e.ParserError.Line - 1]; int startingPos = Math.Max((int)e.ParserError.Column - 2, 0); errString = errString.Substring(startingPos, Math.Min(10, errString.Length - startingPos)); message = String.Format("{0}: '{1}'", e.ParserError.ErrorText, errString); return false; } catch (Exception e) { message = e.Message; return false; } return true; }