-
Notifications
You must be signed in to change notification settings - Fork 6
/
pratt.gleam
139 lines (118 loc) · 3.01 KB
/
pratt.gleam
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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// IMPORTS ---------------------------------------------------------------------
import gleam/list
import nibble.{type Parser}
// TYPES -----------------------------------------------------------------------
pub opaque type Config(a, tok, ctx) {
Config(
one_of: List(fn(Config(a, tok, ctx)) -> Parser(a, tok, ctx)),
and_then_one_of: List(Operator(a, tok, ctx)),
spaces: Parser(Nil, tok, ctx),
)
}
pub opaque type Operator(a, tok, ctx) {
Operator(fn(Config(a, tok, ctx)) -> #(Int, fn(a) -> Parser(a, tok, ctx)))
}
//
pub fn expression(
one_of first: List(fn(Config(a, tok, ctx)) -> Parser(a, tok, ctx)),
and_then then: List(Operator(a, tok, ctx)),
dropping spaces: Parser(Nil, tok, ctx),
) -> Parser(a, tok, ctx) {
let config = Config(first, then, spaces)
sub_expression(config, 0)
}
pub fn sub_expression(
config: Config(a, tok, ctx),
precedence: Int,
) -> Parser(a, tok, ctx) {
let expr = {
use <- nibble.lazy
config.one_of
|> list.map(fn(p) { p(config) })
|> nibble.one_of
}
let go = fn(expr) {
use _ <- nibble.do(config.spaces)
nibble.one_of([
operation(expr, config, precedence)
|> nibble.map(nibble.Continue),
nibble.return(expr)
|> nibble.map(nibble.Break),
])
}
use _ <- nibble.do(config.spaces)
use e <- nibble.do(expr)
nibble.loop(e, go)
}
fn operation(
expr: a,
config: Config(a, tok, ctx),
current_precedence: Int,
) -> Parser(a, tok, ctx) {
config.and_then_one_of
|> list.filter_map(fn(operator) {
let Operator(op) = operator
case op(config) {
#(precedence, parser) if precedence > current_precedence ->
Ok(parser(expr))
_ -> Error(Nil)
}
})
|> nibble.one_of()
}
//
pub fn prefix(
precedence: Int,
operator: Parser(Nil, tok, ctx),
apply: fn(a) -> a,
) -> fn(Config(a, tok, ctx)) -> Parser(a, tok, ctx) {
fn(config) {
use _ <- nibble.do(operator)
use subexpr <- nibble.do(sub_expression(config, precedence))
nibble.return(apply(subexpr))
}
}
pub fn infix_left(
precedence: Int,
operator: Parser(Nil, tok, ctx),
apply: fn(a, a) -> a,
) -> Operator(a, tok, ctx) {
make_infix(#(precedence, precedence), operator, apply)
}
pub fn infix_right(
precedence: Int,
operator: Parser(Nil, tok, ctx),
apply: fn(a, a) -> a,
) -> Operator(a, tok, ctx) {
make_infix(#(precedence, precedence - 1), operator, apply)
}
pub fn postfix(
precedence: Int,
operator: Parser(Nil, tok, ctx),
apply: fn(a) -> a,
) -> Operator(a, tok, ctx) {
use _ <- Operator
#(
precedence,
fn(lhs) {
use _ <- nibble.do(operator)
nibble.return(apply(lhs))
},
)
}
fn make_infix(
precedence: #(Int, Int),
operator: Parser(Nil, tok, ctx),
apply: fn(a, a) -> a,
) -> Operator(a, tok, ctx) {
let #(left_precedence, right_precedence) = precedence
use config <- Operator
#(
left_precedence,
fn(lhs) {
use _ <- nibble.do(operator)
use subexpr <- nibble.do(sub_expression(config, right_precedence))
nibble.return(apply(lhs, subexpr))
},
)
}