Skip to content
This repository has been archived by the owner on Jun 1, 2022. It is now read-only.

Commit

Permalink
[#591] Allow application of single arguments without parentheses (#592)
Browse files Browse the repository at this point in the history
* [#591] Allow application of single arguments…

…without parentheses

* remove parentheses from definitions of tags

* remove sequence node

* add test case
  • Loading branch information
jonhue committed Mar 25, 2021
1 parent 5766e80 commit ac889de
Show file tree
Hide file tree
Showing 9 changed files with 155 additions and 152 deletions.
16 changes: 9 additions & 7 deletions common/enums.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export enum Prec {
Group = -1,
SubtractionType = -1,
UnionType = 0,
Assignment = 1,
Expand All @@ -24,13 +25,14 @@ export enum Prec {
Exponentiation = 12,
Not = 13,
InfixApplication = 14,
PrefixApplication = 15,
Application = 16,
Term = 17,
Pattern = 17,
Access = 18,
Pipeline = 19,
ParametricTypeInstance = 20,
Application = 15,
Term = 16,
Pattern = 16,
Access = 17,
Pipeline = 18,
ParametricTypeInstance = 19,
Argument = 20,
Hole = 21,
}

export enum Dialect {
Expand Down
2 changes: 0 additions & 2 deletions common/patterns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ export const tagged_pattern = <RuleName extends string>(
) =>
seq(
field('name', alias($._identifier_without_operators, $.identifier)),
'(',
field('pattern', $._pattern),
')',
)

export const _literal_pattern = <RuleName extends string>(
Expand Down
36 changes: 16 additions & 20 deletions common/terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ export const _term = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
Prec.Term,
choice(
$.block,
$.sequence,
$.abstraction,
$.application,
$.prefix_application,
$.infix_application,
$._section,
$.access,
Expand Down Expand Up @@ -50,10 +48,6 @@ export const _term = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
export const block = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
buildBlock($, field('term', $._term))

export const sequence = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => seq('do', buildBlock($, field('term', $._term)))

export const export_ = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
seq('export', field('declaration', $.assignment))

Expand Down Expand Up @@ -95,7 +89,11 @@ export const instance = <RuleName extends string>(

export const argument = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => prec.left(choice(field('placeholder', '?'), field('value', $._element)))
) =>
prec.left(
Prec.Argument,
choice(field('placeholder', '?'), field('value', $._element)),
)

export const abstraction = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
Expand All @@ -112,19 +110,14 @@ export const abstraction = <RuleName extends string>(
export const application = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec(
prec.left(
Prec.Application,
seq(field('name', $._term), buildTuple($, $.argument, false, true)),
)

export const prefix_application = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.right(
Prec.PrefixApplication,
seq(
field('name', alias($._operator, $.identifier)),
field('value', $._term),
field('name', $._term),
choice(
prec(Prec.Argument, buildTuple($, $.argument, false, true)),
field('element', $._term),
),
),
)

Expand Down Expand Up @@ -483,7 +476,10 @@ export const type_hint = <RuleName extends string>(
)

export const hole = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
seq('?', field('name', alias($.identifier, $.identifier_pattern_name)))
prec(
Prec.Hole,
seq('?', field('name', alias($.identifier, $.identifier_pattern_name))),
)

export const _identifier_without_operators = () => IDENTIFIER

Expand All @@ -494,4 +490,4 @@ export const identifier = <RuleName extends string>(
) => choice($._operator, $._identifier_without_operators)

export const group = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
seq('(', field('term', $._term), ')')
prec(Prec.Group, seq('(', field('term', $._term), ')'))
4 changes: 1 addition & 3 deletions common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,7 @@ export const tagged_type = <RuleName extends string>(
'name',
alias($._identifier_without_operators, $.identifier_pattern_name),
),
'(',
field('type', $._type),
')',
),
)

Expand Down Expand Up @@ -236,7 +234,7 @@ export const refinement_type = <RuleName extends string>(

export const _predicate = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => choice($.application, $.infix_application, $.prefix_application)
) => choice($.application, $.infix_application)

export const type_group = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
Expand Down
16 changes: 8 additions & 8 deletions tony/corpus/literals.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ decimal
---

(program
term: (prefix_application
term: (application
name: (identifier)
value: (number))
term: (prefix_application
element: (number))
term: (application
name: (identifier)
value: (number))
element: (number))
term: (number)
term: (number)
term: (number)
Expand All @@ -48,9 +48,9 @@ decimal
term: (number)
term: (number)
term: (number)
term: (prefix_application
term: (application
name: (identifier)
value: (number)))
element: (number)))

==================
integer
Expand All @@ -68,9 +68,9 @@ integer
---

(program
term: (prefix_application
term: (application
name: (identifier)
value: (number))
element: (number))
term: (number)
term: (number)
term: (number)
Expand Down
21 changes: 11 additions & 10 deletions tony/corpus/patterns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ struct pattern
tagged struct pattern
============================================

tag({ b: c :: Type }) = a
tag({ b: c :: Type, d :: Type, ['e']: e :: Type, ...f :: Type = {} }) = a
tag { b: c :: Type } = a
tag { b: c :: Type, d :: Type, ['e']: e :: Type, ...f :: Type = {} } = a

---

Expand Down Expand Up @@ -137,7 +137,7 @@ tuple pattern
tagged tuple pattern
============================================

tag((a, b :: Type, ...c)) = a
tag (a, b :: Type, ...c) = a

---

Expand Down Expand Up @@ -202,8 +202,8 @@ list pattern
tagged list pattern
============================================

tag([b :: Type, c :: Type]) = a
tag([b :: Type, c :: Type, ...d :: Type = [1]]) = a
tag [b :: Type, c :: Type] = a
tag [b :: Type, c :: Type, ...d :: Type = [1]] = a

---

Expand Down Expand Up @@ -247,16 +247,17 @@ tag([b :: Type, c :: Type, ...d :: Type = [1]]) = a
tagged pattern
============================================

tag(b) = a
(tag b) = a

---

(program
term: (assignment
pattern: (tagged_pattern
name: (identifier)
pattern: (identifier_pattern
name: (identifier_pattern_name)))
pattern: (pattern_group
pattern: (tagged_pattern
name: (identifier)
pattern: (identifier_pattern
name: (identifier_pattern_name))))
value: (identifier)))

============================================
Expand Down
Loading

0 comments on commit ac889de

Please sign in to comment.