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

Parser cleanup #42

Merged
merged 3 commits into from
Sep 12, 2024
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
563 changes: 282 additions & 281 deletions src/parser/__tests__/__snapshots__/parse-chars.test.ts.snap

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions src/parser/grammar.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { Expr } from "../syntax-objects/expr.js";
import { Identifier } from "../syntax-objects/identifier.js";

export const idIs = (id: Expr | undefined, value: string) =>
id?.isIdentifier() && id.is(value);

export const isTerminator = (char: string) =>
isWhitespace(char) ||
isBracket(char) ||
Expand Down
6 changes: 2 additions & 4 deletions src/parser/parse-chars.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ export const parseChars = (
const list = new List({
location: file.currentSourceLocation(),
parent: opts.parent,
value: !opts.nested ? ["ast", ","] : [],
});

while (file.hasCharacters) {
const token = lexer(file);

if (processWithReaderMacro(token, list.last(), file, opts, list)) {
if (processWithReaderMacro(token, list.last(), file, list)) {
continue;
}

Expand Down Expand Up @@ -61,15 +60,14 @@ export const parseChars = (
}

list.location!.endIndex = file.position;
return list;
return opts.nested ? list : new List(["ast", list]);
};

/** Returns true if token was matched with and processed by a macro */
const processWithReaderMacro = (
token: Token,
prev: Expr | undefined,
file: CharStream,
opts: ParseCharsOpts,
list: List
) => {
const readerMacro = getReaderMacroForToken(token, prev, file.next);
Expand Down
2 changes: 1 addition & 1 deletion src/parser/reader-macros/array-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export const arrayLiteralMacro: ReaderMacro = {
match: (t) => t.value === "[",
macro: (file, { reader }) => {
const items = reader(file, "]");
return items.insert("array").insert(",", 1);
return items.insert("array");
},
};
2 changes: 1 addition & 1 deletion src/parser/reader-macros/dictionary-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export const dictionaryLiteralMacro: ReaderMacro = {
match: (t) => t.value === "#{",
macro: (file, { reader }) => {
const items = reader(file, "}");
return items.insertFnCall("dict");
return items.insert("dict");
},
};
2 changes: 1 addition & 1 deletion src/parser/reader-macros/generics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const genericsMacro: ReaderMacro = {
},
macro: (file, { reader }) => {
const items = reader(file, ">");
return items.insert("generics").insert(",", 1);
return items.insert("generics");
},
};
4 changes: 2 additions & 2 deletions src/parser/reader-macros/html/html-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,6 @@ export class HTMLParser {
}
}

const dict = () => new List({}).insertFnCall("dict");
const dict = () => new List({}).insert("dict");
const dictItem = (key: string, value: Expr) => array().push(key, value);
const array = () => new List({}).insertFnCall("array");
const array = () => new List({}).insert("array");
2 changes: 1 addition & 1 deletion src/parser/reader-macros/object-literal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ export const objectLiteralMacro: ReaderMacro = {
match: (t) => t.value === "{",
macro: (dream, { reader }) => {
const items = reader(dream, "}");
return items.insert("object").insert(",", 1);
return items.insert("object");
},
};
6 changes: 3 additions & 3 deletions src/parser/syntax-macros/functional-notation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isOp } from "../grammar.js";
import { idIs, isOp } from "../grammar.js";
import { Expr, List } from "../../syntax-objects/index.js";

export const functionalNotation = (list: List): List => {
Expand All @@ -9,11 +9,11 @@ export const functionalNotation = (list: List): List => {
if (expr.isWhitespace()) return expr;

const nextExpr = array[index + 1];
if (nextExpr && nextExpr.isList() && !isOp(expr)) {
if (nextExpr && nextExpr.isList() && !(isOp(expr) || idIs(expr, ","))) {
return processFnCall(expr, nextExpr, array, index);
}

if (list.mayBeTuple && expr.isIdentifier() && expr.is(",")) {
if (list.mayBeTuple && idIs(expr, ",")) {
isTuple = true;
}

Expand Down
8 changes: 4 additions & 4 deletions src/parser/syntax-macros/interpret-whitespace.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isContinuationOp, isGreedyOp } from "../grammar.js";
import { idIs, isContinuationOp, isGreedyOp } from "../grammar.js";
import { Expr, List } from "../../syntax-objects/index.js";

export const interpretWhitespace = (list: List, indentLevel?: number): List => {
Expand Down Expand Up @@ -69,7 +69,7 @@ const elideParens = (list: Expr, startIndentLevel?: number): Expr => {
continue;
}

if (next?.isIdentifier() && next.is(",")) {
if (idIs(next, ",")) {
break;
}

Expand Down Expand Up @@ -133,7 +133,7 @@ const nextExprIndentLevel = (list: List, startIndex?: number) => {
continue;
}

if (expr?.isIdentifier() && expr.is(",")) {
if (idIs(expr, ",")) {
return 0;
}

Expand All @@ -156,7 +156,7 @@ const nextNonWhitespace = (list: List, startIndex?: number) => {
const consumeLeadingWhitespace = (list: List) => {
while (list.hasChildren) {
const next = list.first();
if (next?.isWhitespace() || (next?.isIdentifier() && next.is(","))) {
if (next?.isWhitespace() || idIs(next, ",")) {
list.consume();
continue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ exports[`regular macro expansion 1`] = `
[
"exports",
[
"std#726",
"std#730",
],
],
[
Expand Down Expand Up @@ -47,7 +47,7 @@ exports[`regular macro expansion 1`] = `
],
[
"regular-macro",
"\`#755",
"\`#759",
[
"parameters",
],
Expand All @@ -68,7 +68,7 @@ exports[`regular macro expansion 1`] = `
],
[
"regular-macro",
"let#807",
"let#811",
[
"parameters",
],
Expand Down Expand Up @@ -121,7 +121,7 @@ exports[`regular macro expansion 1`] = `
],
[
"regular-macro",
"fn#1458",
"fn#1462",
[
"parameters",
],
Expand Down
5 changes: 0 additions & 5 deletions src/syntax-objects/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,6 @@ export class List extends Syntax {
return this.toArray().findIndex(cb);
}

insertFnCall(name: string) {
this.insert(name, 0).insert(",", 1);
return this;
}

insert(expr: Expr | string, at = 0) {
const result = typeof expr === "string" ? Identifier.from(expr) : expr;
result.parent = this;
Expand Down