Skip to content

Commit 9e98523

Browse files
committed
Make infix syntax a bit more flexible
Allow a newline either before or after the operator, just not both. Previously newlines after operators were forbidden.
1 parent f34e353 commit 9e98523

File tree

2 files changed

+50
-15
lines changed

2 files changed

+50
-15
lines changed

src/end-to-end.test.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,13 @@ testCases(endToEnd, code => code)('end-to-end tests', [
327327
+ 4`,
328328
either.makeRight('10'),
329329
],
330+
[
331+
`1 +
332+
2 +
333+
3 +
334+
4`,
335+
either.makeRight('10'),
336+
],
330337
[`{ f: _ => 1 + 1 }.f(whatever)`, either.makeRight('2')],
331338
[
332339
`{
@@ -380,6 +387,16 @@ testCases(endToEnd, code => code)('end-to-end tests', [
380387
)(0)`,
381388
either.makeRight('10'),
382389
],
390+
391+
[
392+
`(
393+
:+(1) >>
394+
:+(2) >>
395+
:+(3) >>
396+
:+(4)
397+
)(0)`,
398+
either.makeRight('10'),
399+
],
383400
[`a |> :atom.append(b) |> :atom.append(c)`, either.makeRight('abc')],
384401
[`a |> (:atom.append(b) >> :atom.append(c))`, either.makeRight('abc')],
385402
[`:|>(:>>(:atom.append(c))(:atom.append(b)))(a)`, either.makeRight('abc')],

src/language/parsing/expression.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -295,23 +295,32 @@ const compactExpression: Parser<Molecule | Atom> = oneOf([
295295

296296
const trailingInfixTokens = oneOrMore(
297297
map(
298-
sequence([
299-
trivia,
300-
infixOperator,
301-
// Allowing newlines here could lead to ambiguity. The following object could either
302-
// have three enumerated atom-valued properties, or a single enumerated property
303-
// whose value is the result of an infix expression:
298+
oneOf([
299+
// Allowing newlines both before and after operators could lead to
300+
// ambiguity between three enumerated object properties, or a single
301+
// enumerated property whose value is the result of an infix expression:
304302
// ```
305303
// {
306304
// 1
307305
// +
308306
// 1
309307
// }
310308
// ```
311-
// TODO: This could be made context-dependent, only forbidding newlines when between
312-
// curly braces. Currently this forbids the above formatting even within parentheses.
313-
triviaExceptNewlines,
314-
compactExpression,
309+
// TODO: This could be made context-dependent, only forbidding newlines
310+
// when between curly braces. Currently this forbids the above formatting
311+
// even within parentheses, where there would be no ambiguity.
312+
sequence([
313+
trivia,
314+
infixOperator,
315+
triviaExceptNewlines,
316+
compactExpression,
317+
]),
318+
sequence([
319+
triviaExceptNewlines,
320+
infixOperator,
321+
trivia,
322+
compactExpression,
323+
]),
315324
]),
316325
([_trivia1, operator, _trivia2, operand]) => [operator, operand] as const,
317326
),
@@ -425,11 +434,20 @@ const precededByColonThenAtom = map(
425434
trailingIndexesAndArguments,
426435
zeroOrMore(
427436
map(
428-
sequence([
429-
trivia,
430-
infixOperator,
431-
triviaExceptNewlines, // See note in `precededByAtomThenTrivia`.
432-
compactExpression,
437+
// See note in `trailingInfixTokens` about newlines.
438+
oneOf([
439+
sequence([
440+
trivia,
441+
infixOperator,
442+
triviaExceptNewlines,
443+
compactExpression,
444+
]),
445+
sequence([
446+
triviaExceptNewlines,
447+
infixOperator,
448+
trivia,
449+
compactExpression,
450+
]),
433451
]),
434452
([_trivia1, operator, _trivia2, operand]) =>
435453
[operator, operand] as const,

0 commit comments

Comments
 (0)