-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparser.ts
More file actions
79 lines (66 loc) · 1.94 KB
/
Copy pathparser.ts
File metadata and controls
79 lines (66 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import Either from "./Either.ts";
import Maybe from "./Maybe.ts";
import ParserError from "./ParserError.ts";
import ParserLocation from "./ParserLocation.ts";
import ParserResult from "./ParserResult.ts";
export type Coordinate = {
x: number;
y: number;
};
// Parser is a type constructor
// has a type hole we fill
// What is a Functor?
// a unary type constructor that
// has a function `fmap` that satisfies the Functor laws
// - identity (if you give fmap id and an instance of f a, you get back the same instance of f a)
// id : x -> x
// fmap : Functor f => (a -> b) -> f a -> f b
// a1 a2 r
// fmap id : f a -> f a
// fmap id fa : f a
// fmap id fa = fa
// - composition
// TODO: left up to reader
export default class Parser<A> {
constructor(public readonly _run: (loc: ParserLocation) => ParserResult<A>) {}
// Methods
// root run method
run(s: string): ParserResult<A> {
return this._run(new ParserLocation(s));
}
attempt(): Parser<A> {
throw new Error("not implemented");
}
// Compound
static coordinate(): Parser<Coordinate> {
throw new Error("not implemented");
}
// Primitives
static string<S extends string = string>(str: S): Parser<S> {
return new Parser((loc: ParserLocation) =>
loc.remaining().startsWith(str)
? ParserResult.success(str, str.length)
: ParserResult.error(new ParserError(true))
);
}
optional(): Parser<Maybe<A>> {
// TODO: try to implement this
throw new Error("not implemented");
}
many(): Parser<A[]> {
throw new Error("not implemented");
}
manyAtLeast1(): Parser<[A, ...A[]]> {
throw new Error("not implemented");
}
listOfN(n: number): Parser<A[]> {
throw new Error("not implemented");
}
// Combinators
and<B>(pb: Parser<B>): Parser<[A, B]> {
throw new Error("not implemented");
}
or<B>(pb: Parser<B>): Parser<Either<A, B>> {
throw new Error("not implemented");
}
}