Skip to content

Commit

Permalink
Tidy up binary methods in parser, and add tokens to strings prior to …
Browse files Browse the repository at this point in the history
…AST visualisation
  • Loading branch information
jonathonherbert committed Oct 29, 2024
1 parent 62e994f commit f164387
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 17 deletions.
12 changes: 11 additions & 1 deletion prosemirror-client/src/cqlInput/editor/debug.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import { Node } from "prosemirror-model";
import { Mapping } from "prosemirror-transform";
import { Token } from "../../lang/token";
import {
QueryBinary,
QueryContent,
QueryField,
QueryGroup,
QueryList,
QueryStr,
} from "../../lang/ast";

// Debugging and visualisation utilities.

Expand Down Expand Up @@ -60,7 +68,9 @@ export const getDebugTokenHTML = (tokens: Token[]) => {
})
.join("")}
${
tokens[index + 1]?.start > token.end + 1 && tokens[index + 1]?.tokenType !== "EOF" && token.tokenType !== "EOF"
tokens[index + 1]?.start > token.end + 1 &&
tokens[index + 1]?.tokenType !== "EOF" &&
token.tokenType !== "EOF"
? `<div class="CqlDebug__queryBox"><div class="CqlDebug__queryIndex">${
token.end + 1
}</div></div>`
Expand Down
15 changes: 6 additions & 9 deletions prosemirror-client/src/lang/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,15 @@ export type QueryList = {
content: QueryBinary[];
};

export const createQueryList = (
content: QueryList["content"]
): QueryList => ({
export const createQueryList = (content: QueryList["content"]): QueryList => ({
type: "QueryList",
content,
});

export type QueryBinary = {
type: "QueryBinary";
left: QueryContent;
right?: [Token, QueryBinary

];
right?: [Token, QueryBinary];
};

export const createQueryBinary = (
Expand Down Expand Up @@ -50,11 +46,12 @@ export const createQueryGroup = (
content,
});

export type QueryStr = { type: "QueryStr"; searchExpr: string };
export type QueryStr = { type: "QueryStr"; searchExpr: string; token: Token };

export const createQueryStr = (searchExpr: string): QueryStr => ({
export const createQueryStr = (token: Token): QueryStr => ({
type: "QueryStr",
searchExpr,
searchExpr: token.literal ?? "",
token,
});

export type QueryField = { type: "QueryField"; key: Token; value?: Token };
Expand Down
5 changes: 4 additions & 1 deletion prosemirror-client/src/lang/parser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ describe("parser", () => {
expect(result).toEqual(
ok(
createQueryList([
createQueryBinary(createQueryContent(createQueryStr("a")), undefined),
createQueryBinary(
createQueryContent(createQueryStr(quotedStringToken("a"))),
undefined
),
createQueryBinary(
createQueryContent(
createQueryField(queryFieldKeyToken("", 2), undefined)
Expand Down
9 changes: 3 additions & 6 deletions prosemirror-client/src/lang/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export class Parser {
try {
const queries: QueryBinary[] = [];
while (this.peek().tokenType !== TokenType.EOF) {
queries.push(this.query());
queries.push(this.queryBinary());
}

return ok(createQueryList(queries));
Expand All @@ -50,16 +50,13 @@ export class Parser {
private startOfQueryField = [TokenType.CHIP_KEY];
private startOfQueryValue = [TokenType.CHIP_VALUE];

private query(): QueryBinary {
private queryBinary(): QueryBinary {
if (this.startOfQueryValue.some((i) => i === this.peek().tokenType))
throw new ParseError(
this.peek().start,
"I found an unexpected ':'. Did you numberend to search for a tag, section or similar, e.g. tag:news? If you would like to add a search phrase containing a ':' character, please surround it in double quotes."
);
else return this.queryBinary();
}

private queryBinary(): QueryBinary {
const left = this.queryContent();

switch (this.peek().tokenType) {
Expand Down Expand Up @@ -142,7 +139,7 @@ export class Parser {
private queryStr(): QueryStr {
const token = this.consume(TokenType.STRING, "Expected a string");

return createQueryStr(token.literal ?? "");
return createQueryStr(token);
}

private queryField(): QueryField {
Expand Down

0 comments on commit f164387

Please sign in to comment.