mondo: fix combination of | and ,
This commit is contained in:
parent
6fd89ddee7
commit
bd93e44154
2 changed files with 21 additions and 18 deletions
|
|
@ -107,11 +107,7 @@ export class MondoParser {
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
// Token[] => Token[][] . returns empty list if type not found
|
// Token[] => Token[][] . returns empty list if type not found
|
||||||
split_children(children, split_type, sequence_type) {
|
split_children(children, split_type) {
|
||||||
if (sequence_type) {
|
|
||||||
// if given, the first child is ignored
|
|
||||||
children = children.slice(1);
|
|
||||||
}
|
|
||||||
const chunks = [];
|
const chunks = [];
|
||||||
while (true) {
|
while (true) {
|
||||||
let commaIndex = children.findIndex((child) => child.type === split_type);
|
let commaIndex = children.findIndex((child) => child.type === split_type);
|
||||||
|
|
@ -126,11 +122,10 @@ export class MondoParser {
|
||||||
chunks.push(children);
|
chunks.push(children);
|
||||||
return chunks;
|
return chunks;
|
||||||
}
|
}
|
||||||
desugar_split(children, split_type, sequence_type) {
|
desugar_split(children, split_type, next) {
|
||||||
// children is expected to contain square or angle as first item
|
const chunks = this.split_children(children, split_type);
|
||||||
const chunks = this.split_children(children, split_type, sequence_type);
|
|
||||||
if (!chunks.length) {
|
if (!chunks.length) {
|
||||||
return this.desugar_children(children);
|
return next(children);
|
||||||
}
|
}
|
||||||
// collect args of stack function
|
// collect args of stack function
|
||||||
const args = chunks.map((chunk) => {
|
const args = chunks.map((chunk) => {
|
||||||
|
|
@ -139,12 +134,7 @@ export class MondoParser {
|
||||||
return chunk[0];
|
return chunk[0];
|
||||||
} else {
|
} else {
|
||||||
// chunks of multiple args
|
// chunks of multiple args
|
||||||
if (sequence_type) {
|
chunk = next(chunk);
|
||||||
// if given, each chunk needs to be prefixed
|
|
||||||
// [a b, c d] => (stack (square a b) (square c d))
|
|
||||||
chunk = [{ type: 'plain', value: sequence_type }, ...chunk];
|
|
||||||
}
|
|
||||||
chunk = this.desugar_children(chunk);
|
|
||||||
return { type: 'list', children: chunk };
|
return { type: 'list', children: chunk };
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
@ -241,9 +231,19 @@ export class MondoParser {
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
desugar(children, type) {
|
desugar(children, type) {
|
||||||
// not really needed but more readable and might be extended in the future
|
// if type is given, the first element is expected to contain it as plain value
|
||||||
children = this.desugar_split(children, 'or', type);
|
// e.g. with (square a b, c), we want to split (a b, c) and ignore "square"
|
||||||
children = this.desugar_split(children, 'stack', type);
|
children = type ? children.slice(1) : children;
|
||||||
|
children = this.desugar_split(children, 'stack', (children) =>
|
||||||
|
this.desugar_split(children, 'or', (children) => {
|
||||||
|
// chunks of multiple args
|
||||||
|
if (type) {
|
||||||
|
// the type we've removed before splitting needs to be added back
|
||||||
|
children = [{ type: 'plain', value: type }, ...children];
|
||||||
|
}
|
||||||
|
return this.desugar_children(children);
|
||||||
|
}),
|
||||||
|
);
|
||||||
return children;
|
return children;
|
||||||
}
|
}
|
||||||
parse_list() {
|
parse_list() {
|
||||||
|
|
|
||||||
|
|
@ -90,6 +90,9 @@ describe('mondo sugar', () => {
|
||||||
|
|
||||||
it('should desugar . ()', () => expect(desguar('[jazz hh.(fast 2)]')).toEqual('(square jazz (fast hh 2))'));
|
it('should desugar . ()', () => expect(desguar('[jazz hh.(fast 2)]')).toEqual('(square jazz (fast hh 2))'));
|
||||||
|
|
||||||
|
it('should desugar , |', () => expect(desguar('[bd, hh | oh]')).toEqual('(stack bd (or hh oh))'));
|
||||||
|
it('should desugar , | of []', () =>
|
||||||
|
expect(desguar('[bd, hh | [oh rim]]')).toEqual('(stack bd (or hh (square oh rim)))'));
|
||||||
it('should desugar , square', () => expect(desguar('[bd, hh]')).toEqual('(stack bd hh)'));
|
it('should desugar , square', () => expect(desguar('[bd, hh]')).toEqual('(stack bd hh)'));
|
||||||
it('should desugar , square 2', () => expect(desguar('[bd, hh oh]')).toEqual('(stack bd (square hh oh))'));
|
it('should desugar , square 2', () => expect(desguar('[bd, hh oh]')).toEqual('(stack bd (square hh oh))'));
|
||||||
it('should desugar , square 3', () =>
|
it('should desugar , square 3', () =>
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue