ChordPro

actor ChordPro

The ChordPro file parser

Parse a ‘ChordPro’ file

  • Parse a ChordPro file

    Declaration

    Swift

    static func parse(id: UUID, text: String, transpose: Int, instrument: Instrument, fileURL: URL?) -> Song

    Parameters

    text

    The text of the file

    transpose

    The optional transpose of the song

    id

    The ID of the song

    instrument

    The instrument of the song

    fileURL

    The optional file url of the song

    Return Value

    A Song item

Process a directive

Process a section

Process a chord definition

Process a tab environment

Process a grid environment

Process a strum environment

Process a line

Process a chord

‘ChordPro’ directives

‘ChordPro’ section environments

Regex definitions

  • All the directives we know about

    Declaration

    Swift

    static let directives = ChordPro.Directive.allCases.map(\.rawValue)
  • The regex for a directive with an optional label

    /// ## Examples
    
    {title: The title of the song}
    {chorus}
    {start_of_verse}
    {start_of_verse: Last Verse}
    

    Note

    This needs an extension for ChoiceOf

    Declaration

    Swift

    static let directiveRegex = Regex {
        "{"
        Capture {
            ChoiceOf(directives)
        } transform: {
            Directive(rawValue: $0.lowercased()) ?? .none
        }
        Optionally {
            ":"
            TryCapture {
                OneOrMore {
                    CharacterClass(
                        .anyOf("}").inverted
                    )
                }
            } transform: {
                $0.trimmingCharacters(in: .whitespacesAndNewlines)
            }
        }
        "}"
        Optionally {
            CharacterClass(
                .anyOf("{").inverted
            )
        }
    }
  • The regex for a normal line with optionalchords and/or lyrics

    /// ## Example
    
    [A]I sing you a [G]song!!
    

    Declaration

    Swift

    static let lineRegex = Regex {
        /// The chord
        Optionally {
            chordRegex
        }
        /// The lyric
        Optionally {
            Capture {
                OneOrMore {
                    CharacterClass(
                        .anyOf("[]").inverted
                    )
                }
            }
        }
    }
  • Regex for brackets for chords and directives

    Declaration

    Swift

    static let bracketRegex = Regex {
        Capture {
            OneOrMore {
                CharacterClass(
                    .anyOf("[]{}")
                )
            }
        }
    }
  • Regex for a chord

    Declaration

    Swift

    static let chordRegex = Regex {
        Regex {
            "["
            Capture {
                OneOrMore {
                    CharacterClass(
                        .anyOf("[] ").inverted
                    )
                }
            }
            "]"
        }
    }