Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/end-to-end.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ testCases(endToEnd, code => code)('end-to-end tests', [
['{a:A {@lookup {a}}}', either.makeRight({ a: 'A', 0: 'A' })],
['{a:A :{a}}', either.makeRight({ a: 'A', 0: 'A' })],
['{ a: (a => :a)(A) }', either.makeRight({ a: 'A' })],
['{ a: ( a => :a )( A ) }', either.makeRight({ a: 'A' })],
['(a => :a)(A)', either.makeRight('A')],
[
'{ a: a => :a, b: :a(A) }',
Expand All @@ -53,13 +54,13 @@ testCases(endToEnd, code => code)('end-to-end tests', [
}),
],
['{ a: ({ A }) }', either.makeRight({ a: { 0: 'A' } })],
['{ a: (A) }', either.makeRight({ a: 'A' })],
['{ a: ( A ) }', either.makeRight({ a: 'A' })],
['{ a: ("A A A") }', either.makeRight({ a: 'A A A' })],
['{ ("a"): A }', either.makeRight({ a: 'A' })],
['{ a: :(b), b: B }', either.makeRight({ a: 'B', b: 'B' })],
['{ a: :("b"), b: B }', either.makeRight({ a: 'B', b: 'B' })],
['{ (a: A) (b: B) }', either.makeRight({ a: 'A', b: 'B' })],
['({ ((a): :(b)) ((b): B) })', either.makeRight({ a: 'B', b: 'B' })],
['( { ((a): :(b)) ( ( b ): B ) } )', either.makeRight({ a: 'B', b: 'B' })],
['{ (a: :(")")), (")": (B)) }', either.makeRight({ a: 'B', ')': 'B' })],
[':match({ a: A })({ tag: a, value: {} })', either.makeRight('A')],
[':{string concatenate}(a)(b)', either.makeRight('ba')],
Expand Down
2 changes: 1 addition & 1 deletion src/language/parsing/molecule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { parser, type Parser } from '../../parsing.js'
import { atomParser, type Atom } from './atom.js'
import { optionallySurroundedByParentheses } from './parentheses.js'
import { whitespace } from './whitespace.js'

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

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

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

const sugaredLookup: Parser<PartialMolecule> =
optionallySurroundedByParentheses(
Expand Down
15 changes: 14 additions & 1 deletion src/language/parsing/parentheses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { parser, type Parser } from '../../parsing.js'
import { whitespace } from './whitespace.js'

const optionallySurroundedBy = <Output>(
parser1: Parser<unknown>,
Expand All @@ -16,4 +17,16 @@ const optionallySurroundedBy = <Output>(
export const optionallySurroundedByParentheses = <Output>(
theParser: Parser<Output>,
): Parser<Output> =>
optionallySurroundedBy(parser.literal('('), theParser, parser.literal(')'))
parser.oneOf([
// This allows `theParser` to greedily consume whitespace.
optionallySurroundedBy(
parser.literal('('),
theParser,
parser.sequence([parser.zeroOrMore(whitespace), parser.literal(')')]),
),
optionallySurroundedBy(
parser.sequence([parser.literal('('), parser.zeroOrMore(whitespace)]),
theParser,
parser.sequence([parser.zeroOrMore(whitespace), parser.literal(')')]),
),
])
3 changes: 3 additions & 0 deletions src/language/parsing/whitespace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { parser } from '../../parsing.js'

export const whitespace = parser.regularExpression(/\s+/)
Loading