Skip to content

Commit aa1a9cf

Browse files
committed
Simplify molecule parsers more
1 parent 06edd28 commit aa1a9cf

File tree

1 file changed

+23
-22
lines changed

1 file changed

+23
-22
lines changed

src/language/parsing/molecule.ts

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,6 @@ export const moleculeParser: Parser<Molecule> = oneOf([
3232
// During parsing molecules and properties are represented as nested arrays (of key/value pairs).
3333
// The following utilities make it easier to work with such a structure.
3434

35-
const flat = <Output>(theParser: Parser<readonly Output[]>) =>
36-
map(theParser, output => output.flat())
37-
3835
const omit = (theParser: Parser<unknown>) => as(theParser, [])
3936

4037
const optional = <Output>(
@@ -60,7 +57,7 @@ const makeIncrementingIndexer = (): Indexer => {
6057
// Language-specific parsers follow.
6158

6259
const propertyDelimiter = oneOf([
63-
sequence([optional(omit(trivia)), literal(','), optional(omit(trivia))]),
60+
sequence([optional(trivia), literal(','), optional(trivia)]),
6461
trivia,
6562
])
6663

@@ -75,9 +72,9 @@ const sugaredFunction: Parser<Molecule> = optionallySurroundedByParentheses(
7572
map(
7673
sequence([
7774
atomParser,
78-
omit(trivia),
79-
omit(literal('=>')),
80-
omit(trivia),
75+
trivia,
76+
literal('=>'),
77+
trivia,
8178
lazy(() => propertyValue),
8279
]),
8380
([parameter, _trivia1, _arrow, _trivia2, body]) => ({
@@ -94,9 +91,9 @@ const sugaredApply: Parser<Molecule> = map(
9491
oneOrMore(
9592
sequence([
9693
literal('('),
97-
optional(omit(trivia)),
94+
optional(trivia),
9895
lazy(() => propertyValue),
99-
optional(omit(trivia)),
96+
optional(trivia),
10097
literal(')'),
10198
]),
10299
),
@@ -120,17 +117,13 @@ const propertyValue = oneOf([
120117
sugaredLookup,
121118
])
122119

123-
const namedProperty = flat(
124-
sequence([
125-
propertyKey,
126-
omit(literal(':')),
127-
optional(omit(trivia)),
128-
propertyValue,
129-
]),
120+
const namedProperty = map(
121+
sequence([propertyKey, literal(':'), optional(trivia), propertyValue]),
122+
([key, _colon, _trivia, value]) => [key, value] as const,
130123
)
131124

132125
const numberedProperty = (index: Indexer) =>
133-
map(propertyValue, value => [index(), value])
126+
map(propertyValue, value => [index(), value] as const)
134127

135128
const property = (index: Indexer) =>
136129
optionallySurroundedByParentheses(
@@ -139,18 +132,26 @@ const property = (index: Indexer) =>
139132

140133
const moleculeAsEntries = (index: Indexer) =>
141134
withoutOmittedOutputs(
142-
flat(
135+
map(
143136
sequence([
144-
omit(literal('{')),
137+
literal('{'),
145138
// Allow initial property not preceded by a delimiter (e.g. `{a b}`).
146139
map(optional(property(index)), property => [property]),
147140
zeroOrMore(
148-
flat(
149-
sequence([omit(propertyDelimiter), lazy(() => property(index))]),
141+
map(
142+
sequence([propertyDelimiter, property(index)]),
143+
([_delimiter, property]) => property,
150144
),
151145
),
152146
optional(omit(propertyDelimiter)),
153-
omit(literal('}')),
147+
literal('}'),
154148
]),
149+
([
150+
_openingBrace,
151+
optionalInitialProperty,
152+
remainingProperties,
153+
_delimiter,
154+
_closingBrace,
155+
]) => [...optionalInitialProperty, ...remainingProperties],
155156
),
156157
)

0 commit comments

Comments
 (0)