Skip to content

Commit

Permalink
Refactor pattern matching to be static & stand alone (#82)
Browse files Browse the repository at this point in the history
* Refactor "Pattern.Match" into stand-alone "PatternMatcher"

* Move all branch patterns into one file

* Consolidate defaulting of collected leaf patterns list
  • Loading branch information
atifaziz authored Apr 5, 2021
1 parent 8c04bcc commit ed57edb
Show file tree
Hide file tree
Showing 14 changed files with 173 additions and 220 deletions.
11 changes: 0 additions & 11 deletions src/DocoptNet/Argument.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace DocoptNet
{
using System.Collections;
using System.Collections.Generic;

class Argument: LeafPattern
{
Expand All @@ -24,16 +23,6 @@ public Argument(string name, int value)
{
}

public override (int Index, LeafPattern Match) SingleMatch(IList<LeafPattern> left)
{
for (var i = 0; i < left.Count; i++)
{
if (left[i] is Argument arg)
return (i, new Argument(Name, arg.Value));
}
return default;
}

public override Node ToNode()
{
return new ArgumentNode(this.Name, (this.Value != null && this.Value.IsList) ? ValueType.List : ValueType.String);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ namespace DocoptNet
/// </summary>
class BranchPattern : Pattern
{

public BranchPattern(params Pattern[] children)
{
if (children == null) throw new ArgumentNullException(nameof(children));
Expand Down Expand Up @@ -38,4 +37,27 @@ public override string ToString()
return string.Format("{0}({1})", GetType().Name, string.Join(", ", Children.Select(c => c == null ? "None" : c.ToString())));
}
}

class Required : BranchPattern
{
public Required(params Pattern[] patterns) : base(patterns) { }
}

class Optional : BranchPattern
{
public Optional(params Pattern[] patterns) : base(patterns) { }
}

// Marker/placeholder for [options] shortcut.
class OptionsShortcut : Optional { }

class Either : BranchPattern
{
public Either(params Pattern[] patterns) : base(patterns) { }
}

class OneOrMore : BranchPattern
{
public OneOrMore(params Pattern[] patterns) : base(patterns) { }
}
}
16 changes: 0 additions & 16 deletions src/DocoptNet/Command.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
namespace DocoptNet
{
using System.Collections.Generic;

class Command : Argument
{
public Command(string name, ValueObject value = null) : base(name, value ?? new ValueObject(false))
{
}

public override (int Index, LeafPattern Match) SingleMatch(IList<LeafPattern> left)
{
for (var i = 0; i < left.Count; i++)
{
if (left[i] is Argument arg)
{
if (arg.Value.ToString() == Name)
return (i, new Command(Name, new ValueObject(true)));
break;
}
}
return default;
}

public override Node ToNode() { return new CommandNode(this.Name); }

public override string GenerateCode()
Expand Down
2 changes: 1 addition & 1 deletion src/DocoptNet/Docopt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ protected IDictionary<string, ValueObject> Apply(string doc, Tokens tokens,
optionsShortcut.Children = docOptions.Distinct().Except(patternOptions).ToList();
}
Extras(help, version, arguments, doc);
if (pattern.Fix().Match(arguments) is (true, { Count: 0 }, _) res)
if (pattern.Fix().Match(arguments, null) is (true, { Count: 0 }, _) res)
{
var dict = new Dictionary<string, ValueObject>();
foreach (var p in pattern.Flat().OfType<LeafPattern>())
Expand Down
28 changes: 0 additions & 28 deletions src/DocoptNet/Either.cs

This file was deleted.

40 changes: 0 additions & 40 deletions src/DocoptNet/LeafPattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,46 +38,6 @@ public override ICollection<Pattern> Flat(params Type[] types)
return new Pattern[] {};
}

public virtual (int Index, LeafPattern Match) SingleMatch(IList<LeafPattern> patterns)
{
return default;
}

public override MatchResult Match(IList<LeafPattern> left,
IEnumerable<LeafPattern> collected = null)
{
var coll = collected ?? new List<LeafPattern>();
var (index, match) = SingleMatch(left);
if (match == null)
{
return new MatchResult(false, left, coll);
}
var left_ = new List<LeafPattern>();
left_.AddRange(left.Take(index));
left_.AddRange(left.Skip(index + 1));
var sameName = coll.Where(a => a.Name == Name).ToList();
if (Value != null && (Value.IsList || Value.IsOfTypeInt))
{
var increment = new ValueObject(1);
if (!Value.IsOfTypeInt)
{
increment = match.Value.IsString ? new ValueObject(new [] {match.Value}) : match.Value;
}
if (sameName.Count == 0)
{
match.Value = increment;
var res = new List<LeafPattern>(coll) {match};
return new MatchResult(true, left_, res);
}
sameName[0].Value.Add(increment);
return new MatchResult(true, left_, coll);
}
var resColl = new List<LeafPattern>();
resColl.AddRange(coll);
resColl.Add(match);
return new MatchResult(true, left_, resColl);
}

public override string ToString()
{
return string.Format("{0}({1}, {2})", GetType().Name, Name, Value);
Expand Down
39 changes: 0 additions & 39 deletions src/DocoptNet/OneOrMore.cs

This file was deleted.

11 changes: 0 additions & 11 deletions src/DocoptNet/Option.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
namespace DocoptNet
{
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

class Option : LeafPattern
Expand Down Expand Up @@ -48,16 +47,6 @@ public override string GenerateCode()
return string.Format("public string {0} {{ get {{ return null == _args[\"{1}\"] ? {2} : _args[\"{1}\"].ToString(); }} }}", s, Name, defaultValue);
}

public override (int Index, LeafPattern Match) SingleMatch(IList<LeafPattern> left)
{
for (var i = 0; i < left.Count; i++)
{
if (left[i].Name == Name)
return (i, left[i]);
}
return default;
}

public override string ToString()
{
return string.Format("Option({0},{1},{2},{3})", ShortName, LongName, ArgCount, Value);
Expand Down
26 changes: 0 additions & 26 deletions src/DocoptNet/Optional.cs

This file was deleted.

12 changes: 0 additions & 12 deletions src/DocoptNet/OptionsShortcut.cs

This file was deleted.

6 changes: 0 additions & 6 deletions src/DocoptNet/Pattern.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,6 @@ public static Either Transform(Pattern pattern)
return new Either(result.Select(r => new Required(r.ToArray()) as Pattern).ToArray());
}

public virtual MatchResult Match(IList<LeafPattern> left,
IEnumerable<LeafPattern> collected = null)
{
return new MatchResult();
}

public abstract ICollection<Pattern> Flat(params Type[] types);

/// <summary>
Expand Down
Loading

0 comments on commit ed57edb

Please sign in to comment.