Skip to content

Commit

Permalink
Disallow numeric separators at the end of numeric literals
Browse files Browse the repository at this point in the history
Extend manual grammar inspection to length = 2.

ref #187
  • Loading branch information
frostburn committed Mar 30, 2024
1 parent fc24dd1 commit 0b620df
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 13 deletions.
45 changes: 34 additions & 11 deletions scripts/inspect-printable-ascii.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
const {parse} = require('../src/sonic-weave-ast');
const readline = require('readline').createInterface({
input: process.stdin,
output: process.stdout,
});

console.log(
'Iterating over all printable ASCII character to see which are accepted by the parser.'
);

const ASCII = ['\t', '\n', '\r'];

for (let i = 32; i < 127; ++i) {
ASCII.push(String.fromCharCode(i));
}

for (const source of ASCII) {
function logResult(source) {
let result = '*Empty program*';
try {
const ast = parse(source);
Expand All @@ -28,3 +22,32 @@ for (const source of ASCII) {
}
console.log(JSON.stringify(source) + '\t' + result);
}

console.log(
'Iterating over all printable ASCII character to see which are accepted by the parser.'
);

const ASCII = ['\t', '\n', '\r'];

for (let i = 32; i < 127; ++i) {
ASCII.push(String.fromCharCode(i));
}

for (const source of ASCII) {
logResult(source);
}

let i = 0;

function nextPage() {
for (const b of ASCII) {
logResult(ASCII[i] + b);
}
if (++i < ASCII.length) {
readline.question('(next page)', nextPage);
} else {
readline.close();
}
}

readline.question('Confirm to continue with strings of length 2.', nextPage);
10 changes: 10 additions & 0 deletions src/__tests__/sonic-weave-ast.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -913,3 +913,13 @@ describe('Automatic semicolon insertion', () => {
expect(ast.body).toHaveLength(2);
});
});

describe('Negative tests', () => {
it('rejects numbers without digits', () => {
expect(() => parse('._')).toThrow();
});

it('rejects trailing underscores in numbers', () => {
expect(() => parse('7_')).toThrow();
});
});
14 changes: 12 additions & 2 deletions src/base.pegjs
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
UnderscoreDigits
= num: $([_0-9]*) { return num.replace(/_/g, ''); }
= num: $([_0-9]*) {
if (num.endsWith('_')) {
error('Numeric separators are not allowed at the end of numeric literals.');
}
return num.replace(/_/g, '');
}

NonEmptyUnderscoreDigits
= num: $([_0-9]+) { return num.replace(/_/g, ''); }
= num: $([0-9] [_0-9]*) {
if (num.endsWith('_')) {
error('Numeric separators are not allowed at the end of numeric literals.');
}
return num.replace(/_/g, '');
}

PositiveInteger
= num:([1-9] UnderscoreDigits) { return BigInt(num.join('')); }
Expand Down

0 comments on commit 0b620df

Please sign in to comment.