Skip to content

Commit 06edd28

Browse files
committed
Simplify molecule parsers: ditch PartialMolecule
Since I wrote this the `sequence` combinator gained a fancier type signature, making it unnecessary.
1 parent 70fdf62 commit 06edd28

File tree

1 file changed

+24
-37
lines changed

1 file changed

+24
-37
lines changed

src/language/parsing/molecule.ts

Lines changed: 24 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -64,38 +64,31 @@ const propertyDelimiter = oneOf([
6464
trivia,
6565
])
6666

67-
const sugaredLookup: Parser<PartialMolecule> =
68-
optionallySurroundedByParentheses(
69-
map(
70-
sequence([literal(':'), oneOf([atomParser, moleculeParser])]),
71-
([_colon, query]) => ({ 0: '@lookup', query }),
72-
),
73-
)
67+
const sugaredLookup: Parser<Molecule> = optionallySurroundedByParentheses(
68+
map(
69+
sequence([literal(':'), oneOf([atomParser, moleculeParser])]),
70+
([_colon, query]) => ({ 0: '@lookup', query }),
71+
),
72+
)
7473

75-
const sugaredFunction: Parser<PartialMolecule> =
76-
optionallySurroundedByParentheses(
77-
map(
78-
flat(
79-
sequence([
80-
map(atomParser, output => [output]),
81-
omit(trivia),
82-
omit(literal('=>')),
83-
omit(trivia),
84-
map(
85-
lazy(() => propertyValue),
86-
output => [output],
87-
),
88-
]),
89-
),
90-
([parameter, body]) => ({
91-
0: '@function',
92-
parameter,
93-
body,
94-
}),
95-
),
96-
)
74+
const sugaredFunction: Parser<Molecule> = optionallySurroundedByParentheses(
75+
map(
76+
sequence([
77+
atomParser,
78+
omit(trivia),
79+
omit(literal('=>')),
80+
omit(trivia),
81+
lazy(() => propertyValue),
82+
]),
83+
([parameter, _trivia1, _arrow, _trivia2, body]) => ({
84+
0: '@function',
85+
parameter,
86+
body,
87+
}),
88+
),
89+
)
9790

98-
const sugaredApply: Parser<PartialMolecule> = map(
91+
const sugaredApply: Parser<Molecule> = map(
9992
sequence([
10093
oneOf([sugaredLookup, lazy(() => sugaredFunction)]),
10194
oneOrMore(
@@ -109,7 +102,7 @@ const sugaredApply: Parser<PartialMolecule> = map(
109102
),
110103
]),
111104
([f, multipleArguments]) =>
112-
multipleArguments.reduce<PartialMolecule>(
105+
multipleArguments.reduce<Molecule>(
113106
(expression, [_1, _2, argument, _3, _4]) => ({
114107
0: '@apply',
115108
function: expression,
@@ -161,9 +154,3 @@ const moleculeAsEntries = (index: Indexer) =>
161154
]),
162155
),
163156
)
164-
165-
// This is a lazy workaround for `sequence` returning an array rather than a tuple with
166-
// definitely-present elements.
167-
type PartialMolecule = {
168-
readonly [key: Atom]: PartialMolecule | Atom | undefined
169-
}

0 commit comments

Comments
 (0)