Skip to content

Commit

Permalink
[sheet] show query validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
ananthakumaran committed Jan 11, 2024
1 parent 7a4a48d commit 19dee84
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 33 deletions.
20 changes: 10 additions & 10 deletions src/lib/search_query_editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,20 +170,20 @@ class ConditionAST extends AST {
}

validate(): Diagnostic[] {
const diagnostics: Diagnostic[] = [];

const allowed: number[] =
allowedCombinations[this.property.childId.toString()][this.operator.value] || [];
if (!allowed.includes(this.value.value.id)) {
return [
{
from: this.node.from,
to: this.node.to,
severity: "error",
message: `${this.property.value} cannot be used with ${this.operator.value} and ${this.value.value.type}`
}
];
diagnostics.push({
from: this.node.from,
to: this.node.to,
severity: "error",
message: `${this.property.value} cannot be used with ${this.operator.value} and ${this.value.value.type}`
});
}

return [];
return diagnostics.concat(this.value.validate());
}

evaluate(): TransactionPredicate {
Expand Down Expand Up @@ -341,7 +341,7 @@ class ExpressionAST extends AST {
}
}

class QueryAST extends AST {
export class QueryAST extends AST {
readonly clauses: AST[];
constructor(node: SyntaxNode, state: EditorState) {
super(node);
Expand Down
50 changes: 30 additions & 20 deletions src/lib/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,39 @@ function lint(env: Environment) {
}
});

sheetEditorState.update((current) => {
if (!current.pendingEval) {
return current;
}
if (diagnostics.length == 0) {
sheetEditorState.update((current) => {
if (!current.pendingEval) {
return current;
}

const startTime = performance.now();
let results = current.results;
try {
const ast = buildAST(tree.topNode, editor.state);
const envCopy = env.clone();
results = ast.evaluate(envCopy);
latestIdentifiers = Object.keys(envCopy.scope);
} catch (e) {
// ignore
}
const endTime = performance.now();
const startTime = performance.now();
let results = current.results;
try {
const ast = buildAST(tree.topNode, editor.state);
diagnostics.push(...ast.validate());
if (diagnostics.length > 0) {
const endTime = performance.now();
return _.assign({}, current, {
pendingEval: false,
evalDuration: endTime - startTime
});
}
const envCopy = env.clone();
results = ast.evaluate(envCopy);
latestIdentifiers = Object.keys(envCopy.scope);
} catch (e) {
// ignore
}
const endTime = performance.now();

return _.assign({}, current, {
pendingEval: false,
evalDuration: endTime - startTime,
results
return _.assign({}, current, {
pendingEval: false,
evalDuration: endTime - startTime,
results
});
});
});
}

return diagnostics;
};
Expand Down
62 changes: 59 additions & 3 deletions src/lib/sheet/interpreter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ import * as Terms from "./parser.terms";
import type { EditorState } from "@codemirror/state";
import { BigNumber } from "bignumber.js";
import { asTransaction, formatCurrency, type Posting, type SheetLineResult } from "$lib/utils";
import { buildAST as buildSearchAST, type TransactionPredicate } from "$lib/search_query_editor";
import {
buildAST as buildSearchAST,
QueryAST,
type TransactionPredicate
} from "$lib/search_query_editor";
import { type Diagnostic } from "@codemirror/lint";

const STACK_LIMIT = 1000;

Expand Down Expand Up @@ -75,6 +80,8 @@ abstract class AST {
this.id = node.type.id;
}

abstract validate(): Diagnostic[];

abstract evaluate(env: Environment): any;
}

Expand All @@ -88,6 +95,10 @@ class NumberAST extends AST {
evaluate(): any {
return this.value;
}

validate(): Diagnostic[] {
return [];
}
}

class IdentifierAST extends AST {
Expand All @@ -103,6 +114,10 @@ class IdentifierAST extends AST {
}
return env.scope[this.name];
}

validate(): Diagnostic[] {
return [];
}
}

class UnaryExpressionAST extends AST {
Expand All @@ -129,6 +144,10 @@ class UnaryExpressionAST extends AST {
throw new Error("Unexpected operator");
}
}

validate(): Diagnostic[] {
return this.value.validate();
}
}

class BinaryExpressionAST extends AST {
Expand Down Expand Up @@ -181,6 +200,10 @@ class BinaryExpressionAST extends AST {
throw new Error("Unexpected operator");
}
}

validate(): Diagnostic[] {
return [...this.left.validate(), ...this.right.validate()];
}
}

class FunctionCallAST extends AST {
Expand All @@ -201,17 +224,26 @@ class FunctionCallAST extends AST {
}
return fun(env, ...this.arguments.map((arg) => arg.evaluate(env)));
}

validate(): Diagnostic[] {
return this.arguments.flatMap((arg) => arg.validate());
}
}

class PostingsAST extends AST {
readonly predicate: TransactionPredicate;
readonly value: QueryAST;
constructor(node: SyntaxNode, state: EditorState) {
super(node);
this.predicate = buildSearchAST(state, node.lastChild.firstChild.nextSibling).evaluate();
this.value = buildSearchAST(state, node.lastChild.firstChild.nextSibling);
}

evaluate(): Query {
return new Query(this.predicate);
return new Query(this.value.evaluate());
}

validate(): Diagnostic[] {
return this.value.validate();
}
}

Expand Down Expand Up @@ -267,6 +299,10 @@ class ExpressionAST extends AST {
evaluate(env: Environment): any {
return this.value.evaluate(env);
}

validate(): Diagnostic[] {
return this.value.validate();
}
}

class AssignmentAST extends AST {
Expand All @@ -282,6 +318,10 @@ class AssignmentAST extends AST {
env.scope[this.identifier] = this.value.evaluate(env);
return env.scope[this.identifier];
}

validate(): Diagnostic[] {
return this.value.validate();
}
}

class HeaderAST extends AST {
Expand All @@ -294,6 +334,10 @@ class HeaderAST extends AST {
evaluate(): any {
return this.text;
}

validate(): Diagnostic[] {
return [];
}
}

class FunctionDefinitionAST extends AST {
Expand All @@ -319,6 +363,10 @@ class FunctionDefinitionAST extends AST {
};
return null;
}

validate(): Diagnostic[] {
return this.body.validate();
}
}

class LineAST extends AST {
Expand Down Expand Up @@ -365,6 +413,10 @@ class LineAST extends AST {
throw new Error("Unexpected node type");
}
}

validate(): Diagnostic[] {
return this.value.validate();
}
}

class SheetAST extends AST {
Expand Down Expand Up @@ -401,6 +453,10 @@ class SheetAST extends AST {
}
return results;
}

validate(): Diagnostic[] {
return this.lines.flatMap((line) => line.validate());
}
}

function childrens(node: SyntaxNode): SyntaxNode[] {
Expand Down

0 comments on commit 19dee84

Please sign in to comment.