Skip to content

Commit

Permalink
Fix AST node produced by a single semicolon
Browse files Browse the repository at this point in the history
Add a script to manually inspect results of parsing a short string.

ref #187
  • Loading branch information
frostburn committed Mar 30, 2024
1 parent 9de28c3 commit fc24dd1
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 3 deletions.
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@
"bench": "vitest bench",
"doc": "typedoc src/index.ts . --name sonic-weave",
"preautodoc": "npm run precompile && tsc -p tsconfig-autodoc.json",
"autodoc": "node .temp/scripts/builtin-docs.js > BUILTIN.md"
"autodoc": "node .temp/scripts/builtin-docs.js > BUILTIN.md",
"preinspect-grammar": "npm run compile-sonic-weave-parser",
"inspect-grammar": "node scripts/inspect-printable-ascii.js"
},
"devDependencies": {
"@types/node": "20.8.2",
Expand Down
30 changes: 30 additions & 0 deletions scripts/inspect-printable-ascii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
const {parse} = require('../src/sonic-weave-ast');

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) {
let result = '*Empty program*';
try {
const ast = parse(source);
if (ast.body.length) {
result = ast.body[0].type;
if (ast.body.length > 1) {
result += '+';
}
if (result === 'ExpressionStatement') {
result += '->' + ast.body[0].expression.type;
}
}
} catch {
result = '*Rejects*';
}
console.log(JSON.stringify(source) + '\t' + result);
}
5 changes: 5 additions & 0 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ export type Identifiers = {
rest?: Identifier;
};

export type EmptyStatement = {
type: 'EmptyStatement';
};

export type AssignmentStatement = {
type: 'AssignmentStatement';
name: Identifier | Identifiers | ArrayAccess | ArraySlice;
Expand Down Expand Up @@ -171,6 +175,7 @@ export type ExpressionStatement = {
};

export type Statement =
| EmptyStatement
| AssignmentStatement
| VariableDeclaration
| ExpressionStatement
Expand Down
2 changes: 2 additions & 0 deletions src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ export class StatementVisitor {
return this.visitContinueStatement(node);
case 'ThrowStatement':
throw this.visitThrowStatement(node);
case 'EmptyStatement':
return;
}
node satisfies never;
}
Expand Down
7 changes: 5 additions & 2 deletions src/sonic-weave.pegjs
Original file line number Diff line number Diff line change
Expand Up @@ -459,8 +459,11 @@ CatchClause
TryFinalizer = FinallyToken _ @Statement

EmptyStatement
= _ ';'
/ __ SingleLineComment LineTerminatorSequence
= (_ ';' / __ SingleLineComment LineTerminatorSequence) {
return {
type: 'EmptyStatement',
};
}

ExpressionStatement
= !("{" / FunctionToken / FunctionAliasToken) expression: (LabeledCommaDecimal / Expression) EOS {
Expand Down

0 comments on commit fc24dd1

Please sign in to comment.