Skip to content

Commit

Permalink
Merge pull request #25 from hildjj/fix-23
Browse files Browse the repository at this point in the history
Fixes #23.  Add null check.
  • Loading branch information
hildjj authored Feb 29, 2024
2 parents bbbbba9 + b61ca19 commit c68f7ae
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 20 deletions.
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ tsconfig.json
vendor/*/rationale.md
eslint.config.js
.ncurc
.gitattributes
4 changes: 3 additions & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ const visit = peggy.compiler.visitor.build({
}
}

if (node.min.type === "constant" && !node.min.value) {
if (!node.min
|| ((node.min.type === "constant")
&& (node.min.value === 0))) {
return new ZeroOrMore(
visit(node.expression, context),
{ rep, min, max }
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
},
"devDependencies": {
"@peggyjs/eslint-config": "^3.2.4",
"@peggyjs/eslint-plugin": "2.0.2",
"@types/node": "^20.11.21",
"@peggyjs/eslint-plugin": "2.1.0",
"@types/node": "^20.11.24",
"@typescript-eslint/eslint-plugin": "^7.1.0",
"@typescript-eslint/parser": "^7.1.0",
"ava": "^6.1.1",
"ava": "^6.1.2",
"c8": "^9.1.0",
"chokidar-cli": "^3.0.0",
"eslint": "^8.57.0",
Expand Down
32 changes: 16 additions & 16 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ test("actions", t => {
test("wrapCount", t => {
t.is(wrapCount(null), null);
});

test("it handles single-value repeats", t => {
const diag = tracks({ text: "x = 'x'|2|" });
t.snapshot(diag.toStandalone());
});

143 changes: 143 additions & 0 deletions test/snapshots/index.test.js.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
# Snapshot report for `test/index.test.js`

The actual snapshot is saved in `index.test.js.snap`.

Generated by [AVA](https://avajs.dev).

## it handles single-value repeats

> Snapshot 1
`<svg class="railroad-diagram" width="256.5" height="153" viewBox="0 0 256.5 153" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">␊
<g transform="translate(.5 .5)">␊
<g class="Start">␊
<path d="M20 87v20m10 -20v20m-10 -10h20"></path>␊
</g>␊
<path d="M40 97h10"></path>␊
<g class="Group rule">␊
<path d="M50 97h0"></path>␊
<path d="M206.5 97h0"></path>␊
<rect x="50" y="36" width="156.5" height="97" rx="10" ry="10" class="group-box"></rect>␊
<g class="Choice">␊
<path d="M50 97h0"></path>␊
<path d="M206.5 97h0"></path>␊
<path d="M50 97a10 10 0 0 0 10 -10v-33a10 10 0 0 1 10 -10"></path>␊
<g class="Skip">␊
<path d="M70 44h116.5"></path>␊
</g>␊
<path d="M186.5 44a10 10 0 0 1 10 10v33a10 10 0 0 0 10 10"></path>␊
<path d="M50 97h20"></path>␊
<g class="Group repeat-box">␊
<path d="M70 97h0"></path>␊
<path d="M186.5 97h0"></path>␊
<rect x="70" y="68" width="116.5" height="57" rx="10" ry="10" class="group-box"></rect>␊
<g class="OneOrMore">␊
<path d="M70 97h46"></path>␊
<path d="M176.5 97h10"></path>␊
<g class="comment range">␊
<text x="92" y="89" class="comment">..</text>␊
</g>␊
<g class="comment">␊
<text x="107.5" y="89" class="comment">2</text>␊
</g>␊
<path d="M126 97a10 10 0 0 0 -10 10v0a10 10 0 0 0 10 10"></path>␊
<g class="Skip">␊
<path d="M126 117h40.5"></path>␊
</g>␊
<path d="M166.5 117a10 10 0 0 0 10 -10v0a10 10 0 0 0 -10 -10"></path>␊
<path d="M116 97h10"></path>␊
<path d="M166.5 97h10"></path>␊
<g class="terminal literal">␊
<path d="M126 97h-2.5"></path>␊
<path d="M169 97h-2.5"></path>␊
<rect x="123.5" y="86" width="45.5" height="22" rx="10" ry="10"></rect>␊
<text x="146.25" y="101">"x"</text>␊
</g>␊
</g>␊
<g class="comment">␊
<path d="M70 60h0"></path>␊
<path d="M122 60h0"></path>␊
<text x="96" y="65" class="comment">repeat</text>␊
</g>␊
</g>␊
<path d="M186.5 97h20"></path>␊
</g>␊
<g class="comment">␊
<path d="M50 28h0"></path>␊
<path d="M67 28h0"></path>␊
<text x="58.5" y="33" class="comment">x</text>␊
</g>␊
</g>␊
<path d="M206.5 97h10"></path>␊
<path class="End" d="M 216.5 97 h 20 m -10 -10 v 20 m 10 -20 v 20"></path>␊
</g>␊
<style>␊
svg {␊
background-color: hsl(30,20%,95%);␊
}␊
path {␊
stroke-width: 3;␊
stroke: black;␊
fill: rgba(0,0,0,0);␊
}␊
text {␊
font: bold 14px monospace;␊
text-anchor: middle;␊
white-space: pre;␊
}␊
text.diagram-text {␊
font-size: 12px;␊
}␊
text.diagram-arrow {␊
font-size: 16px;␊
}␊
text.label {␊
text-anchor: start;␊
}␊
text.comment {␊
font: italic 12px monospace;␊
}␊
g.non-terminal text {␊
/*font-style: italic;*/␊
}␊
rect {␊
stroke-width: 3;␊
stroke: black;␊
fill: hsl(120,100%,90%);␊
}␊
rect.group-box {␊
stroke: gray;␊
stroke-dasharray: 10 5;␊
fill: none;␊
}␊
path.diagram-text {␊
stroke-width: 3;␊
stroke: black;␊
fill: white;␊
cursor: help;␊
}␊
.decision path {␊
fill: #ccc;␊
}␊
g.diagram-text:hover path.diagram-text {␊
fill: #eee;␊
}␊
text.repeat-text {␊
font: italic 12px monospace;␊
text-anchor: end;␊
}␊
.repeat-box > rect.group-box {␊
stroke: gray;␊
stroke-width: 2;␊
stroke-dasharray: 1 5;␊
fill: none;␊
}␊
.delimiter > rect.group-box {␊
stroke: gray;␊
stroke-width: 2;␊
stroke-dasharray: 1 5;␊
fill: none;␊
}␊
</style>␊
</svg>␊
`
Binary file added test/snapshots/index.test.js.snap
Binary file not shown.

0 comments on commit c68f7ae

Please sign in to comment.