forked from commandlineparser/commandline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSequence.cs
43 lines (40 loc) · 1.76 KB
/
Sequence.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Collections.Generic;
using System.Linq;
using CommandLine.Infrastructure;
using CSharpx;
namespace CommandLine.Core
{
static class Sequence
{
public static IEnumerable<Token> Partition(
IEnumerable<Token> tokens,
Func<string, Maybe<TypeDescriptor>> typeLookup)
{
return from tseq in tokens.Pairwise(
(f, s) =>
f.IsName() && s.IsValue()
? typeLookup(f.Text).MapValueOrDefault(info =>
info.TargetType == TargetType.Sequence
? new[] { f }.Concat(tokens.OfSequence(f, info))
: new Token[] { }, new Token[] { })
: new Token[] { })
from t in tseq
select t;
}
private static IEnumerable<Token> OfSequence(this IEnumerable<Token> tokens, Token nameToken, TypeDescriptor info)
{
var nameIndex = tokens.IndexOf(t => t.Equals(nameToken));
if (nameIndex >= 0)
{
return info.NextValue.MapValueOrDefault(
_ => info.MaxItems.MapValueOrDefault(
n => tokens.Skip(nameIndex + 1).Take(n),
tokens.Skip(nameIndex + 1).TakeWhile(v => v.IsValue() && !v.IsValueForced())),
tokens.Skip(nameIndex + 1).TakeWhile(v => v.IsValue() && !v.IsValueForced()));
}
return new Token[] { };
}
}
}