Skip to content

Commit

Permalink
added multiline support and updated default weight and mode editor in…
Browse files Browse the repository at this point in the history
…dicators
  • Loading branch information
dsoskey committed Aug 6, 2024
1 parent 299af14 commit 282fa9a
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
25 changes: 24 additions & 1 deletion src/api/mtgql-ep/parser.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { alias, parseQuerySet, replaceUse, venn } from './parser'
import { alias, parseQuerySet, replaceUse, unMultiline, venn } from './parser'
import { Alias } from './types'
import { weightAlgorithms } from '../queryRunnerCommon'

Expand All @@ -10,6 +10,29 @@ describe('alias', function() {
})
})

describe('unMultiline', function() {

it('should parse an alias real proper', function() {
const input = [
"",
"jerp\\",
"clerp",
"shamp",
"champ \\",
"jamp \\",
"buppy",
];

const result = unMultiline(input);
expect(result).toEqual([
"",
"jerp clerp",
"shamp",
"champ jamp buppy"
])
})
})

describe('venn', function() {
function newVenn(left: string, right: string) {
return `@venn(${left})(${right})`
Expand Down
35 changes: 30 additions & 5 deletions src/api/mtgql-ep/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import cloneDeep from 'lodash/cloneDeep'
const ALIAS_REGEXP = /^@(a|alias):/
export const VENN_REGEXP = /^@(v|venn)/
const DEFAULT_DOMAIN_REGEXP = /^@(dd|defaultDomain)\((.+)\)$/
export const DEFAULT_WEIGHT_REGEXP = /^@(dw|defaultWeight):/
export const DEFAULT_MODE_REGEXP = /^@(dm|defaultMode):/

export function alias(_line: string): Result<Alias, ParserError> {
const line = _line.trim();
Expand Down Expand Up @@ -183,7 +185,7 @@ export function parseQueryEnv(lines: string[]): Result<QueryEnvironment, CogErro
})
}
aliases[aliasOk.name] = aliasOk
} else if (/^@(dw|defaultWeight):/.test(trimmed)) {
} else if (DEFAULT_WEIGHT_REGEXP.test(trimmed)) {
const index = trimmed.indexOf(":");
const value = trimmed.substring(index + 1);
switch (value) {
Expand All @@ -203,7 +205,7 @@ export function parseQueryEnv(lines: string[]): Result<QueryEnvironment, CogErro
displayMessage: `unrecognized weight algorithm ${value}. choose zipf or uniform`
})
}
} else if (/^@(dm|defaultMode):/.test(trimmed)) {
} else if (DEFAULT_MODE_REGEXP.test(trimmed)) {
const index = trimmed.indexOf(":");
const value = trimmed.substring(index + 1);
switch (value) {
Expand Down Expand Up @@ -257,15 +259,35 @@ export function parseQueryEnv(lines: string[]): Result<QueryEnvironment, CogErro
.mapErr(e => ({ query: "", displayMessage: e.message }))
}

export function unMultiline(queries: string[]): string[] {

const unMultilined: string[] = [];
let currentQuery: string[] = [];
for (const query of queries) {
currentQuery.push(query.replace(/\s*\\\s*$/, ""));
const trimmed = query.trim();
if (!trimmed.endsWith("\\") && !trimmed.startsWith("#")) {
unMultilined.push(currentQuery.join(" "));
currentQuery = [];
}
}
if (currentQuery.length) {
unMultilined.push(currentQuery.join(" "));
}
return unMultilined
}

export function parseQuerySet(
queries: string[],
baseIndex: number
): Result<ParsedQuerySet, CogError> {
return parseQueryEnv(queries)
const unMultilined = unMultiline(queries);
return parseQueryEnv(unMultilined)
.andThen(queryEnv => {
let selectedQueries: string[] = []
let currentIndex = baseIndex
while (currentIndex < queries.length && queries[currentIndex].trim() !== "") {
// we need to get the index without collapsing multilines
const query = queries[currentIndex].trim()
if (ALIAS_REGEXP.test(query)) {
const name = query.substring(query.indexOf(":") + 1, query.indexOf("("))
Expand All @@ -291,7 +313,9 @@ export function parseQuerySet(
}
currentIndex++
}
console.debug(selectedQueries)
// once we've selected our queries we can collapse multilines as we don't need the submitted index anymore
selectedQueries = unMultiline(selectedQueries);
console.debug("selected queries:", selectedQueries);
if (selectedQueries.length === 0) {
return err({
query: queries[baseIndex],
Expand All @@ -315,7 +339,8 @@ export function parseQuerySet(
.mapErr((errors) => ({ query: base, displayMessage: errors.join("\n") }));
})
.mapErr((e: ParserError) => ({ query: base, displayMessage: `Error with venn query: ${e.message}\n\t${columnShower(base, e.offset)}`}))
} else if (queryEnv.defaultMode === 'allsub') {
}
if (queryEnv.defaultMode === 'allsub') {
base = ""
sub = selectedQueries
}
Expand Down
14 changes: 12 additions & 2 deletions src/ui/component/editor/multiQueryActionBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React from "react";
import { rankInfo } from "./infoLines";
import { useHighlightPrism } from "../../../api/local/syntaxHighlighting";
import { QuerySetButton } from "../querySetButton";
import { DEFAULT_MODE_REGEXP, DEFAULT_WEIGHT_REGEXP } from '../../../api/mtgql-ep/parser'

export const VENN_REGEXP = /^@(v|venn)\((.+)\)\((.+)\)$/;

Expand All @@ -13,19 +14,28 @@ export const multiQueryInfo =
}
const result = [];
let count = 0;
let isMultiline = false;
for (const line of queries) {
if (line.trim().length === 0) {
const trimmed = line.trim();
if (trimmed.length === 0) {
result.push(" ");
count = 0;
} else if (line.trimStart().startsWith("#")) {
} else if (trimmed.startsWith("#")) {
result.push(" ");
} else if (count === 0 && DEFAULT_WEIGHT_REGEXP.test(trimmed)) {
result.push("WGHT");
} else if (count === 0 && DEFAULT_MODE_REGEXP.test(trimmed)) {
result.push("MODE");
} else if (count === 0) {
result.push(VENN_REGEXP.test(line.trim()) ? "VENN" : "BASE");
count += 1;
} else if (isMultiline) {
result.push(" ");
} else {
result.push(renderSubquery(count));
count += 1;
}
isMultiline = !trimmed.startsWith("#") && trimmed.endsWith("\\");
}
return result;
};
Expand Down

0 comments on commit 282fa9a

Please sign in to comment.