Skip to content

Commit

Permalink
match_query: use new type for parse errors
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Mar 16, 2024
1 parent 1767709 commit b57dcf4
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
10 changes: 5 additions & 5 deletions assets/js/query/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FieldMatcher, RangeEqualQualifier } from './types';
import { FieldMatcher, ParseError, RangeEqualQualifier } from './types';

type Year = number;
type Month = number;
Expand Down Expand Up @@ -55,7 +55,7 @@ function makeRelativeDateMatcher(dateVal: string, qual: RangeEqualQualifier): Fi
return makeMatcher(bottomDate, topDate, qual);
}

throw new Error(`Cannot parse date string: ${dateVal}`);
throw new ParseError(`Cannot parse date string: ${dateVal}`);
}

function makeAbsoluteDateMatcher(dateVal: string, qual: RangeEqualQualifier): FieldMatcher {
Expand Down Expand Up @@ -108,12 +108,12 @@ function makeAbsoluteDateMatcher(dateVal: string, qual: RangeEqualQualifier): Fi
localDateVal = localDateVal.substring(componentMatch[0].length);
}
else {
throw new Error(`Cannot parse date string: ${origDateVal}`);
throw new ParseError(`Cannot parse date string: ${origDateVal}`);
}
}

if (localDateVal.length > 0) {
throw new Error(`Cannot parse date string: ${origDateVal}`);
throw new ParseError(`Cannot parse date string: ${origDateVal}`);
}

// Apply the user-specified time zone offset. The JS Date constructor
Expand All @@ -136,7 +136,7 @@ export function makeDateMatcher(dateVal: string, qual: RangeEqualQualifier): Fie
try {
return makeAbsoluteDateMatcher(dateVal, qual);
}
catch (_) {
catch {
return makeRelativeDateMatcher(dateVal, qual);
}
}
4 changes: 2 additions & 2 deletions assets/js/query/lex.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { assertNotNull, assertNotUndefined } from '../utils/null';
import { AstMatcher, TokenList } from './types';
import { AstMatcher, ParseError, TokenList } from './types';

type TokenName = string;
type Token = [TokenName, RegExp];
Expand Down Expand Up @@ -181,7 +181,7 @@ export function generateLexArray(searchStr: string, parseTerm: ParseTerm): Token
pushTerm();

if (opQueue.indexOf('rparen') !== -1 || opQueue.indexOf('lparen') !== -1) {
throw new Error('Mismatched parentheses.');
throw new ParseError('Mismatched parentheses.');
}

// Concatenatte remaining operators to the token stack.
Expand Down
6 changes: 3 additions & 3 deletions assets/js/query/parse.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { matchAll, matchAny, matchNone, matchNot } from './boolean';
import { AstMatcher, TokenList } from './types';
import { AstMatcher, ParseError, TokenList } from './types';

export function parseTokens(lexicalArray: TokenList): AstMatcher {
const operandStack: AstMatcher[] = [];
Expand All @@ -16,7 +16,7 @@ export function parseTokens(lexicalArray: TokenList): AstMatcher {
const op1 = operandStack.pop();

if (typeof op1 === 'undefined' || typeof op2 === 'undefined') {
throw new Error('Missing operand.');
throw new ParseError('Missing operand.');
}

if (token === 'and_op') {
Expand All @@ -39,7 +39,7 @@ export function parseTokens(lexicalArray: TokenList): AstMatcher {
});

if (operandStack.length > 1) {
throw new Error('Missing operator.');
throw new ParseError('Missing operator.');
}

const op1 = operandStack.pop();
Expand Down
2 changes: 2 additions & 0 deletions assets/js/query/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export type FieldMatcher = (value: FieldValue, name: FieldName, documentId: numb

export type AstMatcher = (e: HTMLElement) => boolean;
export type TokenList = (string | AstMatcher)[];

export class ParseError extends Error {}

0 comments on commit b57dcf4

Please sign in to comment.