Skip to content

Commit

Permalink
Allow negating conditional directives
Browse files Browse the repository at this point in the history
Related to #983
  • Loading branch information
martijnversluis committed Dec 14, 2024
1 parent 4d8a310 commit 5f496f0
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/chord_sheet/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,8 @@ class Tag extends AstComponent {

selector: string | null = null;

isNegated = false;

/**
* The tag attributes. For example, section related tags can have a label:
* `{start_of_verse: label="Verse 1"}`
Expand Down
2 changes: 2 additions & 0 deletions src/chord_sheet_serializer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,11 @@ class ChordSheetSerializer {
chordDefinition,
attributes,
selector,
isNegated,
} = astComponent;
const tag = new Tag(name, value, { line, column, offset }, attributes);
tag.selector = selector || null;
tag.isNegated = isNegated || false;

if (chordDefinition) {
tag.chordDefinition = new ChordDefinition(
Expand Down
15 changes: 12 additions & 3 deletions src/parser/chord_pro/grammar.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,8 @@ ChordDefinition
value: text,
chordDefinition,
location: location().start,
selector,
selector: selector?.value,
isNegated: selector?.isNegated,
};
}

Expand All @@ -176,8 +177,16 @@ Tag
}

TagSelector
= "-" value:TagSelectorValue {
return value;
= "-" value:TagSelectorValue negator:TagSelectorNegator? {
return {
value,
isNegated: !!negator,
};
}

TagSelectorNegator
= "!" {
return true;
}

TagSelectorValue
Expand Down
5 changes: 3 additions & 2 deletions src/parser/chord_pro/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export function buildSection(startTag: SerializedTag, endTag: SerializedTag, con
export function buildTag(
name: string,
value: Partial<{ value: string | null, attributes: Record<string, string>}> | null,
selector: string | null,
selector: { value: string | null, isNegated: boolean } | null,
location: FileRange,
): SerializedTag {
return {
Expand All @@ -41,7 +41,8 @@ export function buildTag(
location: location.start,
value: value?.value || '',
attributes: value?.attributes || {},
selector,
selector: selector?.value,
isNegated: selector?.isNegated,
};
}

Expand Down
1 change: 1 addition & 0 deletions src/serialized_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export type SerializedTag = SerializedTraceInfo & {
chordDefinition?: SerializedChordDefinition,
attributes?: Record<string, string>,
selector?: string | null,
isNegated?: boolean,
};

export interface SerializedComment {
Expand Down
11 changes: 11 additions & 0 deletions test/parser/chord_pro_parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,17 @@ This part is [G]key
expect(tag).toBeTag('title', 'Guitar song', 'guitar');
});

it('allows negated conditional directives', () => {
const chordSheet = '{title-guitar!: Guitar song}';
const song = new ChordProParser().parse(chordSheet);

const tag = song.lines[0].items[0] as Tag;

expect(tag).toBeTag('title', 'Guitar song', 'guitar');

expect(tag.isNegated).toBe(true);
});

it('parses annotation', () => {
const chordSheet = '[*Full band!]Let it be';
const song = new ChordProParser().parse(chordSheet);
Expand Down

0 comments on commit 5f496f0

Please sign in to comment.