Skip to content

Commit

Permalink
multiple parantheses support
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-zhur committed Oct 9, 2024
1 parent c4f888e commit 563121c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ public void ParseChordType(string input, (List<(ChordTypeToken token, bool fromP
["ab()()", (List<(string, bool)>)[("ab()()", false)]],
["ab(cd)", (List<(string, bool)>)[("ab", false), ("cd", true)]],
["ab(c,d)", (List<(string, bool)>)[("ab", false), ("c", true), ("d", true)]],
["ab(c,d)(e)f(g)h", (List<(string, bool)>)[("ab", false), ("c", true), ("d", true), ("e", true), ("f", false), ("g", true), ("h", false)]],
["ab(c,d,)", (List<(string, bool)>)[("ab", false), ("c", true), ("d", true)]],
["ab(c,d,)", (List<(string, bool)>)[("ab", false), ("c", true), ("d", true)]],
["ab(c,,d)", (List<(string, bool)>)[("ab", false), ("c", true), ("d", true)]],
["(c,,d)", (List<(string, bool)>)[("c", true), ("d", true)]],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@ public enum ChordParseError
DuplicateAdditions,
EachExtensionTypeExpectedUnique,
MaxOneMajExtensionExpected,
EmptyBrackets,
}
26 changes: 12 additions & 14 deletions HarmonyDB.Theory/HarmonyDB.Theory.Chords/Parsers/ChordParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,24 +242,22 @@ internal static (List<(ChordTypeToken token, bool fromParentheses, MatchAmbiguit

internal static (List<(string fragment, bool fromParentheses)>? fragments, ChordParseError? error) TryUnwrapParentheses(string input, ChordTypeParsingOptions options)
{
var match = Regex.Match(input, "^([^\\(\\)]*)\\(([^\\(\\)]+)\\)([^\\(\\)]*)$");

List<(string input, bool fromParentheses)> inputs;
if (!match.Success)
var match1 = Regex.Match(input, @"^((?<notin>[^\(\)]*)|\((?<in>[^\(\)]+)\))*$");
if (!match1.Success)
{
inputs = [(input, false)];
}
else
{
inputs = match.Groups[2].Value.Split(',').Select(x => (x, true))
.Prepend((match.Groups[1].Value, false))
.Append((match.Groups[3].Value, false))
.ToList();
return ([(input, false)], null);
}

inputs = inputs.Where(x => x.input != string.Empty).ToList();
var inputs = match1.Groups["in"].Captures.Select(c => (c, fromParentheses: true))
.Concat(match1.Groups["notin"].Captures.Select(c => (c, fromParentheses: false)))
.Where(c => c.c.Length > 0)
.OrderBy(c => c.c.Index)
.Select(c => (c.c.Value, c.fromParentheses))
.SelectMany(c => !c.fromParentheses ? c.Once() : c.Value.Split(',').Select(x => (Value: x, fromParentheses: true)))
.Where(c => c.Value != string.Empty)
.ToList();

var trimmed = inputs.Select(x => x with { input = x.input.Trim() }).Where(x => x.input != string.Empty).ToList();
var trimmed = inputs.Select(x => x with { Value = x.Value.Trim() }).Where(x => x.Value != string.Empty).ToList();
if (!options.TrimWhitespaceFragments && (trimmed.Count != inputs.Count || trimmed.Zip(inputs).Any(x => x.First != x.Second)))
{
return new(null, ChordParseError.WhitespaceFragments);
Expand Down

0 comments on commit 563121c

Please sign in to comment.