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

allow step attributes to be valueless #99

Merged
merged 2 commits into from
Apr 6, 2023
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
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ecmarkdown",
"version": "8.0.0",
"version": "8.1.0",
"description": "A compiler for \"Ecmarkdown\" algorithm shorthand into HTML.",
"main": "dist/ecmarkdown.js",
"scripts": {
Expand Down
12 changes: 8 additions & 4 deletions src/tokenizer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import type { Unlocated, Token, AttrToken, Position } from './node-types';
const fieldOrSlotRegexp = /^\[\[[a-zA-Z0-9_]+\]\]/;
const tagRegexp = /^<[/!]?(\w[\w-]*)(\s+\w[\w-]*(\s*=\s*("[^"]*"|'[^']*'|[^><"'=`]+))?)*\s*>/;
const commentRegexp = /^<!--[\w\W]*?-->/;
const attrRegexp = /^\[ *[\w-]+ *= *"(?:[^"\\\x00-\x1F]|\\["\\/bfnrt]|\\u[a-fA-F]{4})*" *(?:, *[\w-]+ *= *"(?:[^"\\\x00-\x1F]|\\["\\/bfnrt]|\\u[a-fA-F]{4})*" *)*] /;
const attrRegexp = /^\[ *[\w-]+ *(?:= *"(?:[^"\\\x00-\x1F]|\\["\\/bfnrt]|\\u[a-fA-F]{4})*")? *(?:, *[\w-]+ *(?:= *"(?:[^"\\\x00-\x1F]|\\["\\/bfnrt]|\\u[a-fA-F]{4})*")? *)*] /;
const digitRegexp = /\d/;

const opaqueTags = new Set(['emu-grammar', 'emu-production', 'pre', 'code', 'script', 'style']);
Expand Down Expand Up @@ -158,13 +158,17 @@ export class Tokenizer {

// attribute tokens are only valid immediately after list tokens, so we let this be called by the parser.
tryScanListItemAttributes() {
const match = this.str.slice(this.pos).match(attrRegexp);
const rest = this.str.slice(this.pos);
const match = rest.match(attrRegexp);
if (!match) {
if (rest.startsWith('[')) {
this.raise('could not parse attributes for step', this.getLocation());
}
return [];
}

const parts = match[0].matchAll(
/([\w-]+) *= *("(?:[^"\\\x00-\x1F]|\\["\\/bfnrt]|\\u[a-fA-F]{4})*")/g
/([\w-]+) *(?:= *("(?:[^"\\\x00-\x1F]|\\["\\/bfnrt]|\\u[a-fA-F]{4})*"))?/g
);
const tokens = [];
let offset = 0;
Expand All @@ -178,7 +182,7 @@ export class Tokenizer {
const tok: Unlocated<AttrToken> = {
name: 'attr',
key,
value: JSON.parse(value),
value: value == null ? '' : JSON.parse(value), // the parse is guaranteed to succeed because the regex is quite restricted
};
this.pos += part.length;
this.locate(tok, tokStart);
Expand Down
1 change: 1 addition & 0 deletions test/cases/list-attrs.ecmarkdown
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
1. Item
1. Item
1. [attr="bar"] Item
1. [no-value, with-empty="", with-value="val"] Item
1. Item
1. Item
* Unordered item
Expand Down
1 change: 1 addition & 0 deletions test/cases/list-attrs.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<li>Item</li>
<li>Item<ol>
<li attr="bar">Item</li>
<li no-value with-empty with-value="val">Item</li>
<li>Item</li>
</ol>
</li>
Expand Down
9 changes: 9 additions & 0 deletions test/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,13 @@ ${M} * b
'Unexpected token parabreak; expected EOF'
);
});

it('bad step attributes', function () {
assertAlgError(
positioned`
1. ${M}[x y]
`,
'could not parse attributes for step'
);
});
});
8 changes: 5 additions & 3 deletions test/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ const { parseAlgorithm, parseFragment } = require('../');

describe('Parser', function () {
it('tracks positions in algorithms', function () {
const baseSource = ' 1. [id="thing"] a\n 2. b c\n <figure>text</figure>';
const baseSource = ' 1. [id="thing", other] a\n 2. b c\n <figure>text</figure>';
const assertNodeLocation = makeAssertLocation(baseSource);
const algorithm = parseAlgorithm(baseSource);
assertNodeLocation(algorithm, baseSource);
const list = algorithm.contents;
assertNodeLocation(list, ' 1. [id="thing"] a\n 2. b c\n <figure>text</figure>');
assertNodeLocation(list, ' 1. [id="thing", other] a\n 2. b c\n <figure>text</figure>');
const item0 = list.contents[0];
assertNodeLocation(item0, ' 1. [id="thing"] a\n');
assertNodeLocation(item0, ' 1. [id="thing", other] a\n');
assertNodeLocation(item0.attrs[0], 'id="thing"');
assertNodeLocation(item0.attrs[1], 'other');
assertNodeLocation(item0.contents[0], 'a');
const item1 = list.contents[1];
assertNodeLocation(item1, ' 2. b c\n <figure>text</figure>');
Expand Down