Skip to content

Commit ab91a8b

Browse files
committed
Add atomWithAdditionalQuotationRequirements parser combinator
1 parent 2ed205e commit ab91a8b

File tree

2 files changed

+51
-25
lines changed

2 files changed

+51
-25
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#L32-L54) are
52+
[reserved character sequences](./src/language/parsing/atom.ts#L24-L46) are
5353
atoms:
5454

5555
```

src/language/parsing/atom.ts

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,38 +21,64 @@ export const isAtom = (value: unknown): value is Atom =>
2121

2222
export const unit = '' as const
2323

24+
const atomComponentsRequiringQuotation = [
25+
whitespace,
26+
literal('"'),
27+
literal('{'),
28+
literal('}'),
29+
literal('['),
30+
literal(']'),
31+
literal('('),
32+
literal(')'),
33+
literal('<'),
34+
literal('>'),
35+
literal('#'),
36+
literal('&'),
37+
literal('|'),
38+
literal('\\'),
39+
literal('='),
40+
literal(':'),
41+
literal(';'),
42+
literal(','),
43+
literal('//'),
44+
literal('/*'),
45+
literal('*/'),
46+
] as const
47+
2448
export const atomParser: Parser<Atom> = optionallySurroundedByParentheses(
2549
lazy(() => oneOf([unquotedAtomParser, quotedAtomParser])),
2650
)
2751

52+
export const atomWithAdditionalQuotationRequirements = (
53+
additionalQuoteRequiringComponent: Parser<unknown>,
54+
) =>
55+
optionallySurroundedByParentheses(
56+
lazy(() =>
57+
oneOf([
58+
map(
59+
oneOrMore(
60+
butNot(
61+
anySingleCharacter,
62+
oneOf([
63+
...atomComponentsRequiringQuotation,
64+
additionalQuoteRequiringComponent,
65+
]),
66+
'a character sequence requiring quotation',
67+
),
68+
),
69+
characters => characters.join(''),
70+
),
71+
quotedAtomParser,
72+
]),
73+
),
74+
)
75+
2876
export const unquotedAtomParser = map(
2977
oneOrMore(
3078
butNot(
3179
anySingleCharacter,
32-
oneOf([
33-
whitespace,
34-
literal('"'),
35-
literal('{'),
36-
literal('}'),
37-
literal('['),
38-
literal(']'),
39-
literal('('),
40-
literal(')'),
41-
literal('<'),
42-
literal('>'),
43-
literal('#'),
44-
literal('&'),
45-
literal('|'),
46-
literal('\\'),
47-
literal('='),
48-
literal(':'),
49-
literal(';'),
50-
literal(','),
51-
literal('//'),
52-
literal('/*'),
53-
literal('*/'),
54-
]),
55-
'a forbidden character sequence',
80+
oneOf(atomComponentsRequiringQuotation),
81+
'a character sequence requiring quotation',
5682
),
5783
),
5884
characters => characters.join(''),

0 commit comments

Comments
 (0)