internal override FSTemplate <T> Append(FSTemplate <T> template) { if (Templates.Last() is FElse <T> ) { throw new InvalidOperationException("No operation can follow an Else"); } if (Templates.Last() is FTakeRest <T> && !(template is FElse <T>)) { throw new InvalidOperationException("No operation other than Else can follow a TakeRest"); } var copy = new List <FSTemplate <T> >(Templates); var asCombo = template as Combo <T>; if (asCombo != null) { copy.AddRange(asCombo.Templates); } else { copy.Add(template); } return(new Combo <T> { Templates = copy }); }
/// <summary> /// Backtracks until <paramref name="until"/> is encountered in the string. /// /// The current index will be placed after the found string. /// /// If <paramref name="until"/> is not found, the directive fails. /// </summary> public static FSTemplate <T> Back <T>(this FSTemplate <T> template, string until) where T : class { ValidateNeedle(until, "until"); return(template.Append(new FMoveBackUntil <T> { Until = until })); }
/// <summary> /// Every directive can have a single TakeRest directive. /// /// This takes the remainder of the input string, and places it into the field /// or property specified by <paramref name="member"/>. /// /// Nothing can follow a TakeRest directive except an Else. /// </summary> public static FSTemplate <T> TakeRest <T>(this FSTemplate <T> template, MemberInfo member, string format = null) where T : class { ValidateMember <T>(member, format); return(template.Append(new FTakeRest <T> { Into = member, Format = format })); }
/// <summary> /// Backtracks a certain number of characters in the string. /// /// If the current index into the string is moved out of bounds, /// the directive fails. /// </summary> public static FSTemplate <T> Back <T>(this FSTemplate <T> template, int n) where T : class { if (n <= 0) { throw new ArgumentException("Back expects a positive, non-zero, value; found [" + n + "]"); } return(template.Append(new FMoveN <T> { N = -1 * n })); }
internal virtual FSTemplate <T> Append(FSTemplate <T> template) { var copy = new List <FSTemplate <T> >(); copy.Add(this); var asCombo = template as Combo <T>; if (asCombo != null) { copy.AddRange(asCombo.Templates); } else { copy.Add(template); } return(new Combo <T> { Templates = copy }); }
internal override FSTemplate <T> Append(FSTemplate <T> template) { throw new InvalidOperationException("TakeRest cannot be followed by any operation"); }
/// <summary> /// Concatenate one series of directives with another. /// /// Will error if multiple TakeRest or Else directives /// are defined by the union of the two series. /// </summary> public static FSTemplate <T> Append <T>(this FSTemplate <T> left, FSTemplate <T> right) where T : class { return(left.Append(right)); }
/// <summary> /// Take a specific number of characters from the input string, parse them, and put /// them into the given property on T. /// /// Expects a positive, non-zero n. /// </summary> public static FSTemplate <T> Take <T>(this FSTemplate <T> template, int n, MemberInfo into, string format = null) where T : class { return(template.Append(Take <T>(n, into, format))); }
/// <summary> /// Take a specific number of characters from the input string, parse them, and put /// them into the given property on T. /// /// Expects a positive, non-zero n. /// </summary> public static FSTemplate <T> Take <T>(this FSTemplate <T> template, int n, string into, string format = null) where T : class { return(Take <T>(template, n, FindMember <T>(into), format)); }
/// <summary> /// Advances a certain number of characters in the string. /// /// If the current index into the string is moved out of bounds, /// the directive fails. /// </summary> public static FSTemplate <T> Skip <T>(this FSTemplate <T> template, int n) where T : class { return(template.Append(Skip <T>(n))); }
/// <summary> /// Every directive can have a single TakeRest directive. /// /// This takes the remainder of the input string, and places it into the field /// or property specified by <paramref name="member"/>. /// /// Nothing can follow a TakeRest directive except an Else. /// </summary> public static FSTemplate <T> TakeRest <T>(this FSTemplate <T> template, string member, string format = null) where T : class { return(TakeRest <T>(template, FindMember <T>(member), format)); }
/// <summary> /// Every set of directives can have a single Else directive. /// /// If any directive fails (expected string not found, moves before the start of the input string, or past /// the end) the Else directive will be invoked. /// /// The Else directive must be the last one to appear. /// </summary> public static FSTemplate <T> Else <T>(this FSTemplate <T> template, Action <string, T> call) where T : class { return(template.Append(new FElse <T> { Call = call })); }
/// <summary> /// Takes characters from the input string, and puts them in the property /// or field referenced by <paramref name="member"/> until <paramref name="needle"/> is encountered. /// /// <paramref name="needle"/> is not placed in <paramref name="member"/>. /// /// Subsequent directives begin after <paramref name="needle"/> /// /// If <paramref name="needle"/> is not found, any Else directive is run. /// </summary> public static FSTemplate <T> Take <T>(this FSTemplate <T> template, string until, MemberInfo member, string format = null) where T : class { return(template.Append(Take <T>(until, member, format))); }
/// <summary> /// Advance in the string until <paramref name="needle"/> is encountered. /// /// Subsequent directives begin after <paramref name="needle"/>. /// /// If <paramref name="needle"/> is not found, any Else directive is run. /// </summary> public static FSTemplate <T> Until <T>(this FSTemplate <T> template, string needle) where T : class { return(template.Append(Until <T>(needle))); }