Skip to content


Subversion checkout URL

You can clone with
Download ZIP
Tree: 6c9a60bc77
Fetching contributors…

Cannot retrieve contributors at this time

42 lines (33 sloc) 1.27 KB
using System;
using System.Collections.Generic;
namespace Parsley.Primitives
internal class ZeroOrMoreParser<T> : Parser<IEnumerable<T>>
private readonly Parser<T> item;
public ZeroOrMoreParser(Parser<T> item)
this.item = item;
public Reply<IEnumerable<T>> Parse(TokenStream tokens)
var oldPosition = tokens.Position;
var reply = item.Parse(tokens);
var newPosition = reply.UnparsedTokens.Position;
var list = new List<T>();
while (reply.Success)
if (oldPosition == newPosition)
throw new Exception(string.Format("Parser encountered a potential infinite loop at position {0}.", newPosition));
oldPosition = newPosition;
reply = item.Parse(reply.UnparsedTokens);
newPosition = reply.UnparsedTokens.Position;
//The item parser finally failed.
if (oldPosition != newPosition)
return new Error<IEnumerable<T>>(reply.UnparsedTokens, reply.ErrorMessages);
return new Parsed<IEnumerable<T>>(list, reply.UnparsedTokens, reply.ErrorMessages);
Jump to Line
Something went wrong with that request. Please try again.