Skip to content

Commit

Permalink
Add parser.ts with some content
Browse files Browse the repository at this point in the history
  • Loading branch information
knutwannheden committed Sep 16, 2024
1 parent 5d06494 commit 3e5a43d
Show file tree
Hide file tree
Showing 4 changed files with 560 additions and 6 deletions.
1 change: 1 addition & 0 deletions openrewrite/src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './markers';
export * from './parser';
export * from './tree';
export * from './utils';
117 changes: 115 additions & 2 deletions openrewrite/src/core/markers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
import {random_id, UUID} from "./tree";
import {randomId, UUID} from "./tree";

export interface Marker {
get id(): UUID;

withId(id: UUID): Marker;
}

interface MarkerConstructor {
new (value?: any): Marker;
<T>(value?: T): Marker;
readonly prototype: Marker;
}

// pretend like we have a class called `Marker` which we can pass around
export declare var Marker: MarkerConstructor;


export class Markers {
public static readonly EMPTY: Markers = new Markers(random_id(), []);
public static readonly EMPTY: Markers = new Markers(randomId(), []);

private readonly _id: UUID;
private readonly _markers: Marker[] = [];
Expand Down Expand Up @@ -36,4 +46,107 @@ export class Markers {
public findFirst<T extends Marker>(markerType: new () => T): T | undefined {
return this._markers.find((marker) => marker instanceof markerType) as T || undefined;
}
}

export class ParseExceptionResult extends Marker {
private readonly _id: UUID;
private readonly _parserType: string;
private readonly _exceptionType: string;
private readonly _exceptionMessage: string;
private readonly _message: string | null;

constructor(
id: UUID,
parserType: string,
exceptionType: string,
exceptionMessage: string,
message: string | null
) {
super();
this._id = id;
this._parserType = parserType;
this._exceptionType = exceptionType;
this._exceptionMessage = exceptionMessage;
this._message = message;
}

static build(parser: Parser, exception: Error): ParseExceptionResult {
return new ParseExceptionResult(
randomId(),
parser.constructor.name,
exception.constructor.name,
exception.stack || exception.message || '',
null // Assuming message can be null
);
}

get id(): UUID {
return this._id;
}

withId(id: UUID): ParseExceptionResult {
return id === this._id ? this : new ParseExceptionResult(
id,
this._parserType,
this._exceptionType,
this._exceptionMessage,
this._message
);
}

get parserType(): string {
return this._parserType;
}

withParserType(parserType: string): ParseExceptionResult {
return parserType === this._parserType ? this : new ParseExceptionResult(
this._id,
parserType,
this._exceptionType,
this._exceptionMessage,
this._message
);
}

get exceptionType(): string {
return this._exceptionType;
}

withExceptionType(exceptionType: string): ParseExceptionResult {
return exceptionType === this._exceptionType ? this : new ParseExceptionResult(
this._id,
this._parserType,
exceptionType,
this._exceptionMessage,
this._message
);
}

get exceptionMessage(): string {
return this._exceptionMessage;
}

withExceptionMessage(exceptionMessage: string): ParseExceptionResult {
return exceptionMessage === this._exceptionMessage ? this : new ParseExceptionResult(
this._id,
this._parserType,
this._exceptionType,
exceptionMessage,
this._message
);
}

get message(): string {
return this._message;
}

withMessage(message: string): ParseExceptionResult {
return message === this._message ? this : new ParseExceptionResult(
this._id,
this._parserType,
this._exceptionType,
this._exceptionMessage,
message
);
}
}
Loading

0 comments on commit 3e5a43d

Please sign in to comment.