/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { // save scanner state long offset = scanner.Offset; // apply the first parser ParserMatch m = FirstParser.Parse(scanner); // if m1 successful, do m2 if (m.Success) { return(m); } // not found try the next scanner.Seek(offset); // apply the second parser m = SecondParser.Parse(scanner); if (m.Success) { return(m); } scanner.Seek(offset); return(ParserMatch.CreateFailureMatch(scanner)); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { long offset = scanner.Offset; int len = 0; if (scanner.Peek() == '\r') // CR { scanner.Read(); ++len; } if (scanner.Peek() == '\n') // LF { scanner.Read(); ++len; } if (len > 0) { ParserMatch m = ParserMatch.CreateSuccessfulMatch(scanner, offset, len); return(m); } scanner.Seek(offset); return(ParserMatch.CreateFailureMatch(scanner)); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { // save scanner state long offset = scanner.Offset; // apply the first parser ParserMatch m = FirstParser.Parse(scanner); // if m1 successful, do m2 if (m.Success) { ParserMatch m2 = SecondParser.Parse(scanner); if (m2.Success) { m.Concat(m2); } else { m = ParserMatch.CreateFailureMatch(scanner, offset); } } // restoring parser failed, rewind scanner if (!m.Success) { scanner.Seek(offset); } return(m); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { if (scanner.AtEnd) { return(ParserMatch.CreateSuccessfulEmptyMatch(scanner)); } return(ParserMatch.CreateFailureMatch(scanner)); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { if (scanner == null) { throw new ArgumentNullException("scanner"); } return(ParserMatch.CreateFailureMatch(scanner)); }
/// <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); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { // save scanner state long startOffset = scanner.Offset; ParserMatch m = ParserMatch.CreateSuccessfulEmptyMatch(scanner); ParserMatch m_temp; // execution bound int count = 0; // lower bound, minimum number of executions while (count < LowerBound && !scanner.AtEnd) { m_temp = Parser.Parse(scanner); // stop if not successful if (!m_temp.Success) { break; } // increment count and update full match ++count; m.Concat(m_temp); } if (count == LowerBound) { while (count < UpperBound && !scanner.AtEnd) { m_temp = Parser.Parse(scanner); // stop if not successful if (!m_temp.Success) { break; } // increment count ++count; m.Concat(m_temp); } } else { m = ParserMatch.CreateFailureMatch(scanner, startOffset); } // restoring parser failed, rewind scanner if (!m.Success) { scanner.Seek(startOffset); } return(m); }
/// <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)); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { long offset = scanner.Offset; bool isAccepted = Accepts(scanner.Peek()); if (isAccepted && Negate || !isAccepted && !Negate) { return(ParserMatch.CreateFailureMatch(scanner)); } // match character scanner.Peek(); // if we arrive at this point, we have a match ParserMatch m = ParserMatch.CreateSuccessfulMatch(scanner, offset, 1); // updating offset scanner.Read(); // return match return(m); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(IScanner scanner) { long offset = scanner.Offset; foreach (Char c in MatchedString) { // if input consummed return null if (scanner.AtEnd || c != scanner.Peek()) { ParserMatch noMatch = ParserMatch.CreateFailureMatch(scanner, offset); scanner.Seek(offset); return(noMatch); } // read next characted scanner.Read(); } // if we arrive at this point, we have a match ParserMatch m = ParserMatch.CreateSuccessfulMatch(scanner, offset, MatchedString.Length); // return match return(m); }
/// <summary> /// Inner parse method /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(Scanners.IScanner scanner) { ParserMatch m = FirstParser.TryAccept(scanner); if (!m.Success) { return(m); } // doing difference ParserMatch d = SecondParser.TryAccept(scanner); if (d.Success) { if (d.Length >= m.Length) { return(ParserMatch.CreateFailureMatch(scanner, m.Offset, 0)); } } // ok FirstParser.Parse(scanner); return(m); }
/// <summary> /// Inner parse method /// match (first but not second) or (second but not first) /// </summary> /// <param name="scanner">scanner</param> /// <returns>the match</returns> protected override ParserMatch ParseMain(Scanners.IScanner scanner) { long startOffset = scanner.Offset; ParserMatch m1 = FirstParser.Parse(scanner); long offset1 = scanner.Offset; scanner.Seek(startOffset); ParserMatch m2 = SecondParser.Parse(scanner); if (m1.Success && !m2.Success) { scanner.Seek(offset1); return(m1); } if (m2.Success && !m1.Success) { return(m2); } scanner.Seek(startOffset); return(ParserMatch.CreateFailureMatch(scanner)); }