Skip to content

Commit 0a127cf

Browse files
committed
Allow whitespace in more places
1 parent 69d661e commit 0a127cf

File tree

4 files changed

+21
-4
lines changed

4 files changed

+21
-4
lines changed

src/end-to-end.test.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ testCases(endToEnd, code => code)('end-to-end tests', [
3232
['{a:A {@lookup {a}}}', either.makeRight({ a: 'A', 0: 'A' })],
3333
['{a:A :{a}}', either.makeRight({ a: 'A', 0: 'A' })],
3434
['{ a: (a => :a)(A) }', either.makeRight({ a: 'A' })],
35+
['{ a: ( a => :a )( A ) }', either.makeRight({ a: 'A' })],
3536
['(a => :a)(A)', either.makeRight('A')],
3637
[
3738
'{ a: a => :a, b: :a(A) }',
@@ -53,13 +54,13 @@ testCases(endToEnd, code => code)('end-to-end tests', [
5354
}),
5455
],
5556
['{ a: ({ A }) }', either.makeRight({ a: { 0: 'A' } })],
56-
['{ a: (A) }', either.makeRight({ a: 'A' })],
57+
['{ a: ( A ) }', either.makeRight({ a: 'A' })],
5758
['{ a: ("A A A") }', either.makeRight({ a: 'A A A' })],
5859
['{ ("a"): A }', either.makeRight({ a: 'A' })],
5960
['{ a: :(b), b: B }', either.makeRight({ a: 'B', b: 'B' })],
6061
['{ a: :("b"), b: B }', either.makeRight({ a: 'B', b: 'B' })],
6162
['{ (a: A) (b: B) }', either.makeRight({ a: 'A', b: 'B' })],
62-
['({ ((a): :(b)) ((b): B) })', either.makeRight({ a: 'B', b: 'B' })],
63+
['( { ((a): :(b)) ( ( b ): B ) } )', either.makeRight({ a: 'B', b: 'B' })],
6364
['{ (a: :(")")), (")": (B)) }', either.makeRight({ a: 'B', ')': 'B' })],
6465
[':match({ a: A })({ tag: a, value: {} })', either.makeRight('A')],
6566
[':{string concatenate}(a)(b)', either.makeRight('ba')],

src/language/parsing/molecule.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { parser, type Parser } from '../../parsing.js'
22
import { atomParser, type Atom } from './atom.js'
33
import { optionallySurroundedByParentheses } from './parentheses.js'
4+
import { whitespace } from './whitespace.js'
45

56
export type Molecule = { readonly [key: Atom]: Molecule | Atom }
67

@@ -48,7 +49,6 @@ const makeIncrementingIndexer = (): Indexer => {
4849
// Language-specific parsers follow.
4950

5051
const propertyDelimiter = parser.regularExpression(/[\s,]+/)
51-
const whitespace = parser.regularExpression(/\s+/)
5252

5353
const sugaredLookup: Parser<PartialMolecule> =
5454
optionallySurroundedByParentheses(

src/language/parsing/parentheses.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { parser, type Parser } from '../../parsing.js'
2+
import { whitespace } from './whitespace.js'
23

34
const optionallySurroundedBy = <Output>(
45
parser1: Parser<unknown>,
@@ -16,4 +17,16 @@ const optionallySurroundedBy = <Output>(
1617
export const optionallySurroundedByParentheses = <Output>(
1718
theParser: Parser<Output>,
1819
): Parser<Output> =>
19-
optionallySurroundedBy(parser.literal('('), theParser, parser.literal(')'))
20+
parser.oneOf([
21+
// This allows `theParser` to greedily consume whitespace.
22+
optionallySurroundedBy(
23+
parser.literal('('),
24+
theParser,
25+
parser.sequence([parser.zeroOrMore(whitespace), parser.literal(')')]),
26+
),
27+
optionallySurroundedBy(
28+
parser.sequence([parser.literal('('), parser.zeroOrMore(whitespace)]),
29+
theParser,
30+
parser.sequence([parser.zeroOrMore(whitespace), parser.literal(')')]),
31+
),
32+
])

src/language/parsing/whitespace.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { parser } from '../../parsing.js'
2+
3+
export const whitespace = parser.regularExpression(/\s+/)

0 commit comments

Comments
 (0)