Skip to content

Commit 4152d27

Browse files
committed
Simplify atom parsers
1 parent d1b5334 commit 4152d27

File tree

2 files changed

+25
-26
lines changed

2 files changed

+25
-26
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ data representation implied by the fact that a value is an atom (e.g. the atom
4949
`2` may be an integer in memory).
5050

5151
Bare words not containing any
52-
[reserved character sequences](./src/language/parsing/atom.ts#L54-L76) are
52+
[reserved character sequences](./src/language/parsing/atom.ts#L32-L54) are
5353
atoms:
5454

5555
```

src/language/parsing/atom.ts

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,10 @@ export const isAtom = (value: unknown): value is Atom =>
2222
export const unit = '' as const
2323

2424
export const atomParser: Parser<Atom> = optionallySurroundedByParentheses(
25-
lazy(() => oneOf([quotedAtom, unquotedAtom])),
25+
lazy(() => oneOf([unquotedAtomParser, quotedAtomParser])),
2626
)
2727

28-
const quotedAtom = map(
29-
sequence([
30-
as(literal('"'), ''),
31-
map(
32-
zeroOrMore(
33-
oneOf([
34-
butNot(
35-
anySingleCharacter,
36-
oneOf([literal('"'), literal('\\')]),
37-
'`"` or `\\`',
38-
),
39-
as(literal('\\"'), '"'),
40-
as(literal('\\\\'), '\\'),
41-
]),
42-
),
43-
output => output.join(''),
44-
),
45-
as(literal('"'), ''),
46-
]),
47-
([_1, contents, _2]) => contents,
48-
)
49-
50-
const unquotedAtom = map(
28+
export const unquotedAtomParser = map(
5129
oneOrMore(
5230
butNot(
5331
anySingleCharacter,
@@ -80,4 +58,25 @@ const unquotedAtom = map(
8058
characters => characters.join(''),
8159
)
8260

83-
export { unquotedAtom as unquotedAtomParser }
61+
const quotedAtomParser = map(
62+
sequence([
63+
literal('"'),
64+
map(
65+
zeroOrMore(
66+
oneOf([
67+
// `"` and `\` need to be escaped
68+
butNot(
69+
anySingleCharacter,
70+
oneOf([literal('"'), literal('\\')]),
71+
'`"` or `\\`',
72+
),
73+
as(literal('\\"'), '"'),
74+
as(literal('\\\\'), '\\'),
75+
]),
76+
),
77+
output => output.join(''),
78+
),
79+
literal('"'),
80+
]),
81+
([_1, contents, _2]) => contents,
82+
)

0 commit comments

Comments
 (0)