Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remarkSyntaxDiagram: fix rendering of (x y z)* #145

Merged
merged 2 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 99 additions & 22 deletions src/lib/__tests__/remarkSyntaxDiagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,54 @@ describe('deepEq', () => {
})
})

describe('isRTLCapable', () => {
it('treats leaf nodes as RTL capable', () => {
expect(isRTLCapable({ type: 'Terminal', text: 'a' })).toBeTruthy()
expect(isRTLCapable({ type: 'NonTerminal', text: 'a' })).toBeTruthy()
expect(isRTLCapable({ type: 'Skip' })).toBeTruthy()
})
it('treats sequences to be not RTL capable', () => {
expect(isRTLCapable({
type: 'Sequence',
items: [
{ type: 'Terminal', text: 'a' },
{ type: 'Terminal', text: 'b' },
],
})).toBeFalsy();
expect(isRTLCapable({
type: 'OptionalSequence',
items: [
{ type: 'Terminal', text: 'a' },
{ type: 'Terminal', text: 'b' },
],
})).toBeFalsy()
})
it('can see through choices', () => {
expect(isRTLCapable({
type: 'Choice',
normalIndex: 0,
options: [
{ type: 'Terminal', text: 'a' },
{ type: 'Terminal', text: 'b' },
],
})).toBeTruthy()
expect(isRTLCapable({
type: 'Choice',
normalIndex: 0,
options: [
{ type: 'Terminal', text: 'a' },
{
type: 'Sequence',
items: [
{ type: 'Terminal', text: 'a' },
{ type: 'Terminal', text: 'b' },
],
}
],
})).toBeFalsy()
})
})

describe('appendNodeToChoices', () => {
it('can recognize the `a | a? b` pattern', () => {
let opts = [
Expand Down Expand Up @@ -299,14 +347,17 @@ describe('appendNodeToSequence', () => {
{ type: 'Terminal', text: 'a' },
]
appendNodeToSequence(items, {
type: 'OneOrMore',
item: { type: 'Skip' },
repeat: {
type: 'Sequence',
items: [
{ type: 'Terminal', text: ',' },
{ type: 'Terminal', text: 'a' },
],
type: 'Optional',
item: {
type: 'OneOrMore',
item: {
type: 'Sequence',
items: [
{ type: 'Terminal', text: ',' },
{ type: 'Terminal', text: 'a' },
],
},
repeat: { type: 'Skip' },
},
})
expect(items).toEqual([
Expand All @@ -318,36 +369,62 @@ describe('appendNodeToSequence', () => {
},
])
})
it('just appends without special patterns', () => {
it('just appends when repeating part is not RTL capable', () => {
let items = [
{ type: 'Terminal', text: 'x' },
{ type: 'Terminal', text: 'a' },
]
appendNodeToSequence(items, {
type: 'OneOrMore',
item: { type: 'Skip' },
repeat: {
type: 'Sequence',
items: [
{ type: 'Terminal', text: ',' },
{ type: 'Terminal', text: 'b' },
],
const tail = {
type: 'Optional',
item: {
type: 'OneOrMore',
item: {
type: 'Sequence',
items: [
{
type: 'Sequence',
items: [
{ type: 'Terminal', text: 'AND' },
{ type: 'Terminal', text: 'THEN' },
],
},
{ type: 'Terminal', text: 'a' },
],
},
repeat: { type: 'Skip' },
},
})
}
appendNodeToSequence(items, tail)
expect(items).toEqual([
{ type: 'Terminal', text: 'x' },
{ type: 'Terminal', text: 'a' },
{
tail,
])
})
it('just appends without special patterns', () => {
let items = [
{ type: 'Terminal', text: 'x' },
{ type: 'Terminal', text: 'a' },
]
const tail = {
type: 'Optional',
item: {
type: 'OneOrMore',
item: { type: 'Skip' },
repeat: {
item: {
type: 'Sequence',
items: [
{ type: 'Terminal', text: ',' },
{ type: 'Terminal', text: 'b' },
],
},
repeat: { type: 'Skip' },
},
}
appendNodeToSequence(items, tail)
expect(items).toEqual([
{ type: 'Terminal', text: 'x' },
{ type: 'Terminal', text: 'a' },
tail,
])
})
})
Expand Down
52 changes: 42 additions & 10 deletions src/lib/remarkSyntaxDiagram.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ const ebnfParser = pegjs.generate(`
case '+':
return {type: 'OneOrMore', item: p, repeat: {type: 'Skip'}};
case '*':
return {type: 'OneOrMore', item: {type: 'Skip'}, repeat: p};
return options.context.isRTLCapable(p) ?
{type: 'OneOrMore', item: {type: 'Skip'}, repeat: p} :
{type: 'Optional', item: {type: 'OneOrMore', item: p, repeat: {type: 'Skip'}}};
default:
return p;
}
Expand Down Expand Up @@ -109,6 +111,34 @@ function deepEq(a, b) {
return aa.length === bb.length && aa.every((n, i) => deepEq(n, bb[i]))
}

/**
* Checks whether the component can be read from right to left. This also means
* the component is only one node wide.
*
* @param {RRComponent} a -
* the railroad component
* @returns {boolean} whether the component can be read from right to left
*/
function isRTLCapable(a) {
switch (a.type) {
case 'Skip':
case 'Terminal':
case 'NonTerminal':
return true;
case 'Optional':
return isRTLCapable(a.item);
case 'OneOrMore':
return isRTLCapable(a.item) && isRTLCapable(a.repeat);
case 'Choice':
return a.options.every(isRTLCapable);
case 'Sequence':
case 'OptionalSequence':
case 'Stack':
const length = a.items.length;
return length === 1 ? isRTLCapable(a.items[0]) : length === 0;
}
}

/**
* Appends an EBNF node to a Choice container.
*
Expand Down Expand Up @@ -147,7 +177,8 @@ function appendNodeToChoices(opts, node) {
* Appends an EBNF node to a Sequence container.
*
* This function will also try to optimize the pattern `a (b a)*` into the
* specialized railroad component `OneOrMore(item=a, repeat=b)`.
* specialized railroad component `OneOrMore(item=a, repeat=b)`, if the node
* `b` is capable to be read RTL (e.g. a single terminal, but not a sequence).
*
* @param {RRComponent[]} items -
* a list of existing items, the new node will be appended here
Expand All @@ -156,18 +187,19 @@ function appendNodeToChoices(opts, node) {
*/
function appendNodeToSequence(items, node) {
if (
node.type === 'OneOrMore' &&
node.item.type === 'Skip' &&
node.repeat.type === 'Sequence' &&
node.repeat.items.length === 2
node.type === 'Optional' &&
node.item.type === 'OneOrMore' &&
node.item.repeat.type === 'Skip' &&
node.item.item.type === 'Sequence' &&
node.item.item.items.length === 2
) {
const left = items[items.length - 1]
const right = node.repeat.items[1]
if (deepEq(left, right)) {
const [ repeat, right ] = node.item.item.items;
if (isRTLCapable(repeat) && deepEq(left, right)) {
items[items.length - 1] = {
type: 'OneOrMore',
item: left,
repeat: node.repeat.items[0],
repeat,
}
return
}
Expand Down Expand Up @@ -245,7 +277,7 @@ function remarkSyntaxDiagram(tree) {
try {
/** @type {{name: string, content: RRComponent}[]} */
const grammar = ebnfParser.parse(node.value, {
context: { appendNodeToChoices, appendNodeToSequence },
context: { appendNodeToChoices, appendNodeToSequence, isRTLCapable },
})
const diagrams = grammar
.map(({ name, content }) => {
Expand Down