Skip to content

Commit

Permalink
Reserve set literal syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Jun 18, 2024
1 parent 6eeda47 commit 066c7c5
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion documentation/technical.md
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ The Basic Latin block is listed in full. Other blocks only where used.
| U+0020 | *SP* | Whitespace |
| U+0021 | ! | *Reserved for future use* |
| U+0022 | " | String literals |
| U+0023 | # | Sharp accidental |
| U+0023 | # | Sharp accidental, record literals |
| U+0024 | $ | Current scale |
| U+0025 | % | Unary inversion, binary division (loose binding) |
| U+0026 | & | MOS chroma up accidental |
Expand Down
6 changes: 6 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,11 @@ export type ArrayLiteral = {
elements: Argument[];
};

export type SetLiteral = {
type: 'SetLiteral';
elements: Argument[];
};

export type RecordLiteral = {
type: 'RecordLiteral';
properties: [string | null, Expression][];
Expand Down Expand Up @@ -466,6 +471,7 @@ export type Expression =
| Range
| ArrayComprehension
| ArrayLiteral
| SetLiteral
| RecordLiteral
| StringLiteral
| HarmonicSegment;
Expand Down
11 changes: 10 additions & 1 deletion src/grammars/sonic-weave.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,7 @@ Primary
/ TemplateArgument
/ ArrayLiteral
/ RecordLiteral
/ SetLiteral
/ StringLiteral
UnitStepRange
Expand Down Expand Up @@ -1647,7 +1648,15 @@ ArrayLiteral
return {
type: 'ArrayLiteral',
elements,
}
};
}
SetLiteral
= '#[' _ elements: ArgumentList _ ']' {
return {
type: 'SetLiteral',
elements,
};
}
RecordLiteral
Expand Down
22 changes: 22 additions & 0 deletions src/parser/__tests__/sonic-weave-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1205,6 +1205,28 @@ describe('SonicWeave Abstract Syntax Tree parser', () => {
expression: {type: 'ColorLiteral', value: 'rgba(255 50% 5 / .5)'},
});
});

it('parses set literals', () => {
const ast = parseSingle('#[1, 2]');
expect(ast).toEqual({
type: 'ExpressionStatement',
expression: {
type: 'SetLiteral',
elements: [
{
type: 'Argument',
spread: false,
expression: {type: 'IntegerLiteral', value: 1n},
},
{
type: 'Argument',
spread: false,
expression: {type: 'IntegerLiteral', value: 2n},
},
],
},
});
});
});

describe('Automatic semicolon insertion', () => {
Expand Down
3 changes: 3 additions & 0 deletions src/parser/expression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,9 @@ export class ExpressionVisitor {
return this.visitSquareSuperparticular(node);
case 'TemplateArgument':
return this.visitTemplateArgument(node);
case 'SetLiteral':
// This requires hashable Intervals and support in xen-dev-utils.
throw new Error('Set literals not implemented yet.');
}
node satisfies never;
}
Expand Down

0 comments on commit 066c7c5

Please sign in to comment.