Skip to content

Commit

Permalink
Cleanup regex definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Desbeers committed May 22, 2024
1 parent e921169 commit 5dcf488
Show file tree
Hide file tree
Showing 9 changed files with 84 additions and 113 deletions.
8 changes: 4 additions & 4 deletions Chord Provider.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@
D7A2FA692BE8F0C100869E24 /* ChordProEditor+Coordinator.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A2FA632BE8F0C100869E24 /* ChordProEditor+Coordinator.swift */; };
D7A2FA6A2BE8F0C100869E24 /* ChordProEditor+Connector.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A2FA642BE8F0C100869E24 /* ChordProEditor+Connector.swift */; };
D7A2FA6B2BE8F0C100869E24 /* ChordProEditor+Connector+Highlight.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A2FA652BE8F0C100869E24 /* ChordProEditor+Connector+Highlight.swift */; };
D7A2FA6C2BE8F0C100869E24 /* ChordProEditor+Regex.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A2FA662BE8F0C100869E24 /* ChordProEditor+Regex.swift */; };
D7A384CC2BEA1C510020221C /* ChordProEditor+Selection.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A384CB2BEA1C510020221C /* ChordProEditor+Selection.swift */; };
D7A384CE2BEA29D30020221C /* ChordProviderSettings+Editor.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A384CD2BEA29D30020221C /* ChordProviderSettings+Editor.swift */; };
D7A384D02BEA3DAA0020221C /* Color+extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7A384CF2BEA3DAA0020221C /* Color+extension.swift */; };
Expand All @@ -192,6 +191,8 @@
D7B319E52BD5833D006A173A /* PDFBuild+DocumentInfo.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7B319E22BD57DE7006A173A /* PDFBuild+DocumentInfo.swift */; };
D7C3D6212AC9E52600909639 /* ChordProviderSettings.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C3D6202AC9E52600909639 /* ChordProviderSettings.swift */; };
D7C3D6242AC9EA0400909639 /* AppState.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7C3D6232AC9EA0400909639 /* AppState.swift */; };
D7C725C52BFDE3B6006D9C74 /* RegexOutput+extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7EC28A22BF6982900F220A1 /* RegexOutput+extension.swift */; };
D7C725C62BFDE3B6006D9C74 /* RegexOutput+extension.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7EC28A22BF6982900F220A1 /* RegexOutput+extension.swift */; };
D7CA631F2AEA5EB800C6B4BB /* Song+Section+Line+strum.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7CA631E2AEA5EB800C6B4BB /* Song+Section+Line+strum.swift */; };
D7CA63202AEA5EB800C6B4BB /* Song+Section+Line+strum.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7CA631E2AEA5EB800C6B4BB /* Song+Section+Line+strum.swift */; };
D7CA63272AEB397A00C6B4BB /* Metronome.swift in Sources */ = {isa = PBXBuildFile; fileRef = D7CA63262AEB397A00C6B4BB /* Metronome.swift */; };
Expand Down Expand Up @@ -368,7 +369,6 @@
D7A2FA632BE8F0C100869E24 /* ChordProEditor+Coordinator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ChordProEditor+Coordinator.swift"; sourceTree = "<group>"; };
D7A2FA642BE8F0C100869E24 /* ChordProEditor+Connector.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ChordProEditor+Connector.swift"; sourceTree = "<group>"; };
D7A2FA652BE8F0C100869E24 /* ChordProEditor+Connector+Highlight.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ChordProEditor+Connector+Highlight.swift"; sourceTree = "<group>"; };
D7A2FA662BE8F0C100869E24 /* ChordProEditor+Regex.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ChordProEditor+Regex.swift"; sourceTree = "<group>"; };
D7A384CB2BEA1C510020221C /* ChordProEditor+Selection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChordProEditor+Selection.swift"; sourceTree = "<group>"; };
D7A384CD2BEA29D30020221C /* ChordProviderSettings+Editor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "ChordProviderSettings+Editor.swift"; sourceTree = "<group>"; };
D7A384CF2BEA3DAA0020221C /* Color+extension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Color+extension.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -605,7 +605,6 @@
D7A2FA642BE8F0C100869E24 /* ChordProEditor+Connector.swift */,
D7A2FA652BE8F0C100869E24 /* ChordProEditor+Connector+Highlight.swift */,
D7A2FA632BE8F0C100869E24 /* ChordProEditor+Coordinator.swift */,
D7A2FA662BE8F0C100869E24 /* ChordProEditor+Regex.swift */,
D7A384CB2BEA1C510020221C /* ChordProEditor+Selection.swift */,
D734B7BC2BEF5D2D0074087C /* ChordProEditor+Static.swift */,
D77979222BECFF4C00BE165A /* ChordProEditor+TextView.swift */,
Expand Down Expand Up @@ -1040,6 +1039,7 @@
D7117F942BA2297E00D17A52 /* ChordDiagramView.swift in Sources */,
D7117F922BA2292A00D17A52 /* Aliasses.swift in Sources */,
D70D10222BD66D79006703EF /* PDFBuild+ContentItem.swift in Sources */,
D7C725C62BFDE3B6006D9C74 /* RegexOutput+extension.swift in Sources */,
D7117F952BA229A600D17A52 /* ColumnsLayout.swift in Sources */,
D7B319E12BD564DE006A173A /* PDFBuild+TextblockSection.swift in Sources */,
D7117F8A2BA2281E00D17A52 /* Song+DisplayOptions.swift in Sources */,
Expand Down Expand Up @@ -1123,7 +1123,6 @@
D706FD912BCECC9F005DEDF6 /* PDFBuild+Divider.swift in Sources */,
D73A03E92AC85B6100D6CD0E /* Song+Render+ChordView.swift in Sources */,
D7F13FE82A6460B7002D875D /* AudioPlayerView.swift in Sources */,
D7A2FA6C2BE8F0C100869E24 /* ChordProEditor+Regex.swift in Sources */,
D7F13FD32A6460B7002D875D /* ChordPro.swift in Sources */,
D7A384CC2BEA1C510020221C /* ChordProEditor+Selection.swift in Sources */,
D70B72862BD50EC6003C89B4 /* PDFBuild+TabSection.swift in Sources */,
Expand Down Expand Up @@ -1232,6 +1231,7 @@
D7972BEC2BCEB5DF00752CF9 /* Logger+Extension.swift in Sources */,
D7F140152A64647D002D875D /* Song+Section+Line+Part.swift in Sources */,
D701F87B2A9CBA3D007DACF3 /* Aliasses.swift in Sources */,
D7C725C52BFDE3B6006D9C74 /* RegexOutput+extension.swift in Sources */,
D70D10212BD66D79006703EF /* PDFBuild+ContentItem.swift in Sources */,
D7B319E02BD564DE006A173A /* PDFBuild+TextblockSection.swift in Sources */,
D7F1400B2A646439002D875D /* PreviewProvider.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"location" : "https://github.com/Desbeers/SwiftlyChordUtilities",
"state" : {
"branch" : "main",
"revision" : "2acfa93058d1a4f94626fa4b2e7d586cbf48abfe"
"revision" : "fa3414ecebe24aec324ea63147433361b8ee4a76"
}
},
{
Expand Down
14 changes: 13 additions & 1 deletion Chord Provider/ChordProParser/ChordPro+Directive+extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,18 @@ extension ChordPro.Directive {

/// Array of ``ChordPro/Directive`` that can be edited by double click on it (macOS only)
static var editableDirectives: [ChordPro.Directive] {
metaDataDirectives + [.t, .st, .subtitle, st, .comment, .define]
metaDataDirectives + environmentDirectives + [
.sov,
.soc,
.sot,
.sog,
.sob,
.sos,
.t,
.st,
.subtitle,
.comment,
.define
]
}
}
62 changes: 42 additions & 20 deletions Chord Provider/ChordProParser/ChordPro+Regex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ extension ChordPro {

// MARK: Regex definitions

/// All the directives we know about
static let directives = ChordPro.Directive.allCases.map(\.rawValue)

/// The regex for a `directive` with an optional `label`
///
/// /// ## Examples
Expand All @@ -21,28 +24,31 @@ extension ChordPro {
/// {start_of_verse}
/// {start_of_verse: Last Verse}
///
/// - Note: This needs an extension for `ChoiceOf`
static let directiveRegex = Regex {
"{"
TryCapture {
OneOrMore {
CharacterClass(
.anyOf(":").inverted
)
}
Capture {
ChoiceOf(directives)
} transform: {
Directive(rawValue: $0.lowercased())
Directive(rawValue: $0.lowercased()) ?? .none
}
Optionally {
":"
TryCapture {
OneOrMore(.any)
OneOrMore {
CharacterClass(
.anyOf("}").inverted
)
}
} transform: {
$0.trimmingCharacters(in: .whitespacesAndNewlines)
}
}
"}"
Optionally {
OneOrMore(.any)
CharacterClass(
.anyOf("{").inverted
)
}
}

Expand All @@ -55,17 +61,7 @@ extension ChordPro {
static let lineRegex = Regex {
/// The chord
Optionally {
Regex {
"["
Capture {
OneOrMore {
CharacterClass(
.anyOf("[] ").inverted
)
}
}
"]"
}
chordRegex
}
/// The lyric
Optionally {
Expand All @@ -78,4 +74,30 @@ extension ChordPro {
}
}
}

/// Regex for brackets for chords and directives
static let bracketRegex = Regex {
Capture {
OneOrMore {
CharacterClass(
.anyOf("[]{}")
)
}
}
}

/// Regex for a chord
static let chordRegex = Regex {
Regex {
"["
Capture {
OneOrMore {
CharacterClass(
.anyOf("[] ").inverted
)
}
}
"]"
}
}
}
2 changes: 1 addition & 1 deletion Chord Provider/ChordProParser/ChordPro.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ actor ChordPro {
currentSection: inout Song.Section
) {

if let match = text.wholeMatch(of: directiveRegex) {
if let match = text.firstMatch(of: directiveRegex) {

let directive = match.1
let label = match.2
Expand Down
33 changes: 18 additions & 15 deletions Chord Provider/Editor/ChordProEditor+Connector+Highlight.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ extension ChordProEditor.Connector {
guard let textView
else { return }

let regex = ChordProEditor.Regexes()

let text = textView.string

let fullRange = NSRange(location: 0, length: textView.string.count)
Expand All @@ -34,7 +32,7 @@ extension ChordProEditor.Connector {
.font: baseFont
], range: currentParagraphRange)
/// Brackets
let brackets = text.ranges(of: regex.bracketRegex)
let brackets = text.ranges(of: ChordPro.bracketRegex)
for bracket in brackets {
let nsRange = NSRange(range: bracket, in: text)
if checkIntersection(nsRange) {
Expand All @@ -46,7 +44,7 @@ extension ChordProEditor.Connector {
}
}
/// Chords
let chords = text.ranges(of: regex.chordRegex)
let chords = text.ranges(of: ChordPro.chordRegex)
for chord in chords {
let nsRange = NSRange(range: chord, in: text, leadingOffset: 1, trailingOffset: 1)
if checkIntersection(nsRange) {
Expand All @@ -58,23 +56,28 @@ extension ChordProEditor.Connector {
}
}
/// Directives
let directives = text.matches(of: regex.directiveRegex)
let directives = text.matches(of: ChordPro.directiveRegex)
for directive in directives {
let nsRange = NSRange(range: directive.range, in: text, leadingOffset: 1, trailingOffset: 1)
var nsRange = NSRange(range: directive.range, in: text, leadingOffset: 1, trailingOffset: 1)
nsRange.length = directive.output.1.rawValue.count
if checkIntersection(nsRange) {
textView.attributedStorage?.addAttributes(
[
.foregroundColor: SWIFTColor(settings.directiveColor),
.definition: directive.output.1 ?? .none
.definition: directive.output.1
],
range: nsRange)
}
}
/// The definition of a directive
let definitions = text.ranges(of: regex.definitionRegex)
for definition in definitions {
let nsRange = NSRange(range: definition, in: text, leadingOffset: 1, trailingOffset: 1)
if checkIntersection(nsRange) {
range: nsRange
)
/// Highlight the optional definition
guard
let definition = directive.output.2,
let directiveRange = directive.output.0.description.range(of: definition)
else {
continue
}
let directiveNSRange = NSRange(range: directiveRange, in: directive.output.0.description)
nsRange.location += directiveNSRange.location - 1
nsRange.length = directiveNSRange.length
textView.attributedStorage?.addAttribute(
.foregroundColor,
value: SWIFTColor(settings.definitionColor),
Expand Down
66 changes: 0 additions & 66 deletions Chord Provider/Editor/ChordProEditor+Regex.swift

This file was deleted.

5 changes: 2 additions & 3 deletions Chord Provider/Views/EditorView/EditorView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,17 @@ extension EditorView {
connector.settings = appState.settings.editor
}
.onChange(of: connector.clickedFragment) {
let regex = ChordProEditor.Regexes()
guard
let directive = connector.currentDirective,
let fragment = connector.clickedFragment,
let paragraph = fragment.textElement as? NSTextParagraph,
let match = paragraph.attributedString.string.firstMatch(of: regex.definitionRegex)
let match = paragraph.attributedString.string.firstMatch(of: ChordPro.directiveRegex)

else {
return
}
directiveSettings.directive = directive
directiveSettings.definition = String(match.output.1).trimmingCharacters(in: .whitespacesAndNewlines)
directiveSettings.definition = String(match.output.2 ?? "").trimmingCharacters(in: .whitespacesAndNewlines)
directiveSettings.clickedFragment = fragment
switch directive {
case .define:
Expand Down
5 changes: 3 additions & 2 deletions Chord Provider/macOS/Credits.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@
<style>
body {
font-family: -apple-system;
text-align: center;
}
</style>
</head>

<body>
<p>This is a simple application to view and edit <strong>ChordPro</strong> files for your guitar songs.</p>
<p>It is released under the <strong>GPL3 licence</strong> and the project is on <a href="https://github.com/Desbeers/Chord-Provider">GitHub</a>.</p>
<p>A beautiful and <strong>real</strong> macOS application to view and edit <strong>ChordPro</strong> songs on your mac or export them to a PDF files.</p>
<p>The source code is released under the <strong>GPL3 licence</strong> and available on <a href="https://github.com/Desbeers/Chord-Provider">GitHub</a>.</p>
</body>
</html>

0 comments on commit 5dcf488

Please sign in to comment.