RegexDefinitions

struct RegexDefinitions

Regex definitions to parse a chord

Regex to parse a chord name

  • The regex for a chord string

    It will parse the chord to find the root and optional quality

    /// ## Examples
    
    Am -> root: Am, quality: m
    Dsus4 -> root: D, quality: sus4
    

    Declaration

    Swift

    let chordRegex = Regex {
        /// The root
        rootRegex
        /// The optional quality
        Optionally {
            Capture {
                OneOrMore {
                    CharacterClass(
                        (.word),
                        (.digit),
                        .anyOf("#?")
                    )
                }
            } transform: { quality in
                /// Try to find the name of the quality
                for name in Chord.Quality.allCases where name.name.contains(String(quality)) {
                    return name
                }
                /// The quality is unknown
                return Chord.Quality.unknown
            }
        }
        Optionally {
            "/"
            rootRegex
        }
    }

Regex to parse a define

  • The regex for a chord definition

    Declaration

    Swift

    let defineRegex = Regex {
        /// Capture the name
        Capture {
            OneOrMore {
                CharacterClass(
                    .anyOf("#b/+?"),
                    (.word),
                    (.digit)
                )
            }
        } transform: { name in
            String(name)
        }
        /// Capture the base-fret
        Optionally {
            " base-fret "
            Capture {
                OneOrMore(.digit)
            } transform: { baseFret in
                Int(baseFret) ?? 0
            }
        }
        /// Capture the frets
        Optionally {
            " frets "
            Capture {
                OneOrMore {
                    CharacterClass(
                        .anyOf("x"),
                        (.digit),
                        (.whitespace)
                    )
                }
            } transform: { frets in
                String(frets).trimmingCharacters(in: .whitespacesAndNewlines)
            }
        }
        /// Capture the fingers
        Optionally {
            "fingers "
            Capture {
                OneOrMore {
                    CharacterClass(
                        (.digit),
                        (.whitespace)
                    )
                }
            } transform: { fingers in
                return String(fingers).trimmingCharacters(in: .whitespacesAndNewlines)
            }
        }
    }

Regex to parse the root of a chord

  • The regex to parse the root of a chord

    Declaration

    Swift

    static var rootRegex: Capture<(Substring, Chord.Root)>