diff --git a/packages/parse/src/parse-errors.ts b/packages/parse/src/parse-errors.ts new file mode 100644 index 0000000..664a173 --- /dev/null +++ b/packages/parse/src/parse-errors.ts @@ -0,0 +1,37 @@ +import { toTokenType } from './utils/to-token-type'; + +export class UnexpectedTokenError extends Error { + constructor(type: string | null, position: number) { + super( + type + ? `Unexpected ${toTokenType(type)} at position ${position}.` + : `Unexpected token at position ${position}.` + ); + } +} + +export class NoPropertyValueError extends Error { + constructor(propertyName: string | null) { + super( + propertyName + ? `No property value specified for property '${propertyName}'.` + : 'No property value specified.' + ); + } +} + +export class NonMatchingArrayBracket extends Error { + constructor(position: number | null) { + super( + position + ? `No closing bracket for array started at position ${position}.` + : 'No closing bracket for array.' + ); + } +} + +export class NonMatchingObjectBrace extends Error { + constructor(position: number) { + super(`No closing brace for object started at position ${position}.`); + } +} diff --git a/packages/parse/src/parse.ts b/packages/parse/src/parse.ts index a140cc1..375e524 100644 --- a/packages/parse/src/parse.ts +++ b/packages/parse/src/parse.ts @@ -1,43 +1,12 @@ import { isString } from '@utilz/types'; import { isWhitespace } from './utils/is-whitespace'; -import { toTokenType } from './utils/to-token-type'; import { lexer } from './lexer'; - -class UnexpectedTokenError extends Error { - constructor(type: string | null, position: number) { - super( - type - ? `Unexpected ${toTokenType(type)} at position ${position}.` - : `Unexpected token at position ${position}.` - ); - } -} - -class NoPropertyValueError extends Error { - constructor(propertyName: string | null) { - super( - propertyName - ? `No property value specified for property '${propertyName}'.` - : 'No property value specified.' - ); - } -} - -class NonMatchingArrayBracket extends Error { - constructor(position: number | null) { - super( - position - ? `No closing bracket for array started at position ${position}.` - : 'No closing bracket for array.' - ); - } -} - -class NonMatchingObjectBrace extends Error { - constructor(position: number) { - super(`No closing brace for object started at position ${position}.`); - } -} +import { + NoPropertyValueError, + NonMatchingArrayBracket, + NonMatchingObjectBrace, + UnexpectedTokenError, +} from './parse-errors'; const objectFactory = (position: number) => { const result: Record = {};