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

Commit

Permalink
[#521] Refactor precedences
Browse files Browse the repository at this point in the history
  • Loading branch information
jonhue committed Mar 14, 2021
1 parent 3529bcf commit 8d7753c
Show file tree
Hide file tree
Showing 10 changed files with 127 additions and 70 deletions.
4 changes: 4 additions & 0 deletions common/dialects.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export enum Dialect {
DTN,
Tony,
}
37 changes: 0 additions & 37 deletions common/enums.ts

This file was deleted.

7 changes: 4 additions & 3 deletions common/imports.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Dialect, Prec } from './enums'
import { Dialect } from './dialects'
import { Prec } from './precedences'
import { commaSep1 } from './util'

export const import_ = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
Expand All @@ -16,7 +17,7 @@ export const _import_body_constructor = (dialect: Dialect) => <
switch (dialect) {
case Dialect.DTN:
return prec.left(
Prec.Pattern,
Prec.PatternOrTerm,
seq(
'{',
commaSep1(field('import', $.import_type)),
Expand All @@ -27,7 +28,7 @@ export const _import_body_constructor = (dialect: Dialect) => <
)
case Dialect.Tony:
return prec.left(
Prec.Pattern,
Prec.PatternOrTerm,
seq(
choice(
field('default', alias($.identifier, $.identifier_pattern_name)),
Expand Down
3 changes: 1 addition & 2 deletions common/literals.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { BIN, DIGITS, EXP, HEX, OCT } from './constants'
import { Prec } from './enums'
import { buildString } from './util'

export const _literal = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => prec(Prec.Literal, choice($.boolean, $.number, $.string, $.regex))
) => choice($.boolean, $.number, $.string, $.regex)

export const boolean = () => choice('false', 'true')

Expand Down
16 changes: 8 additions & 8 deletions common/patterns.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { buildList, buildMember, buildStruct, buildTuple } from './util'
import { Prec } from './enums'
import { Prec } from './precedences'

export const _pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => prec(Prec.Pattern, choice($._assignable_pattern, $._literal_pattern))
) => prec(Prec.PatternOrTerm, choice($._assignable_pattern, $._literal_pattern))

export const _assignable_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
Expand All @@ -19,7 +19,7 @@ export const destructuring_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec(
Prec.Pattern,
Prec.PatternOrTerm,
seq(
optional(
seq(
Expand All @@ -38,7 +38,7 @@ export const struct_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec(
Prec.Pattern,
Prec.PatternOrTerm,
buildStruct(
$,
choice(
Expand All @@ -55,17 +55,17 @@ export const member_pattern = <RuleName extends string>(

export const tuple_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => prec(Prec.Pattern, buildTuple($, $._pattern, true))
) => prec(Prec.PatternOrTerm, buildTuple($, $._pattern, true))

export const list_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => prec(Prec.Pattern, buildList($, $._pattern, true))
) => prec(Prec.PatternOrTerm, buildList($, $._pattern, true))

export const identifier_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.right(
Prec.Pattern,
Prec.PatternOrTerm,
seq(
field('name', alias($.identifier, $.identifier_pattern_name)),
optional(seq('::', field('type', $._type))),
Expand All @@ -88,4 +88,4 @@ export const _literal_pattern = <RuleName extends string>(

export const pattern_group = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => prec(Prec.Pattern, seq('(', field('pattern', $._pattern), ')'))
) => prec(Prec.PatternOrTerm, seq('(', field('pattern', $._pattern), ')'))
76 changes: 76 additions & 0 deletions common/precedences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
export enum Prec {
SubtractionType = 'SubtractionType',
Type = 'Type',
UnionType = 'UnionType',
CurriedOrIntersectionType = 'CurriedOrIntersectionType',
LabeledType = 'LabeledType',
OptionalType = 'OptionalType',
TaggedType = 'TaggedType',

NamedInfixApplication = 'NamedInfixApplication',
OperatorInfixApplication = 'OperatorInfixApplication',
Biconditional = 'Biconditional',
Implication = 'Implication',
Or = 'Or',
And = 'And',
Equality = 'Equality',
Order = 'Order',
Mod = 'Mod',
Sum = 'Sum',
Product = 'Product',
Exponentiation = 'Exponentiation',
Not = 'Not',
Pipeline = 'Pipeline',

Identifier = 'Identifier',
SectionIdentifier = 'SectionIdentifier',
TypeHint = 'TypeHint',
TaggedValue = 'TaggedValue',
Assignment = 'Assignment',
PrefixApplication = 'PrefixApplication',
Application = 'Application',
PatternOrTerm = 'PatternOrTerm',
Access = 'Access',
}

const typePrecedences = [
Prec.Access,
Prec.TaggedType,
Prec.OptionalType,
Prec.LabeledType,
Prec.CurriedOrIntersectionType,
Prec.UnionType,
Prec.Type,
Prec.SubtractionType,
]

const termPrecedences = [
Prec.Pipeline,
Prec.Access,
Prec.PatternOrTerm,
Prec.Application,
Prec.PrefixApplication,
Prec.Not,
Prec.Exponentiation,
Prec.Product,
Prec.Sum,
Prec.Mod,
Prec.Order,
Prec.Equality,
Prec.And,
Prec.Or,
Prec.Implication,
Prec.Biconditional,
Prec.OperatorInfixApplication,
Prec.NamedInfixApplication,
Prec.Assignment,
Prec.TypeHint,
Prec.TaggedValue,
Prec.SectionIdentifier,
Prec.Identifier,
]

export const precedences = () => [
typePrecedences,
termPrecedences,
]
27 changes: 16 additions & 11 deletions common/terms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
buildTuple,
commaSep1,
} from './util'
import { Prec } from './enums'
import { Prec } from './precedences'

export const _term = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
choice(seq($._simple_term, $._newline), $._compound_term)
Expand All @@ -19,7 +19,7 @@ export const _simple_term = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.left(
Prec.Term,
Prec.PatternOrTerm,
choice(
alias($.simple_abstraction, $.abstraction),
$.application,
Expand Down Expand Up @@ -49,7 +49,7 @@ export const _compound_term = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.left(
Prec.Term,
Prec.PatternOrTerm,
choice(
alias($.compound_abstraction, $.abstraction),
alias($.compound_assignment, $.assignment),
Expand Down Expand Up @@ -175,6 +175,7 @@ export const simple_abstraction = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.left(
Prec.PatternOrTerm,
commaSep1(
field('branch', alias($.simple_abstraction_branch, $.abstraction_branch)),
),
Expand All @@ -188,6 +189,7 @@ export const compound_abstraction = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.left(
Prec.PatternOrTerm,
repeat1(
field(
'branch',
Expand Down Expand Up @@ -519,7 +521,7 @@ export const when = <RuleName extends string>($: GrammarSymbols<RuleName>) =>

export const struct = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
prec(
Prec.Term,
Prec.PatternOrTerm,
buildStruct(
$,
choice($.member, alias($.identifier, $.shorthand_member), $.spread),
Expand All @@ -530,10 +532,10 @@ export const member = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
buildMember($, $._simple_term, $._simple_term)

export const tuple = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
prec(Prec.Term, buildTuple($, $._element))
prec(Prec.PatternOrTerm, buildTuple($, $._element))

export const list = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
prec(Prec.Term, buildList($, $._element))
prec(Prec.PatternOrTerm, buildList($, $._element))

export const _element = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
Expand All @@ -545,10 +547,13 @@ export const spread = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
export const tagged_value = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
seq(
':',
field('name', alias($._identifier_without_operators, $.identifier)),
field('value', $._simple_term),
prec.right(
Prec.TaggedValue,
seq(
':',
field('name', alias($._identifier_without_operators, $.identifier)),
field('value', $._simple_term),
),
)

export const type_alias = <RuleName extends string>(
Expand Down Expand Up @@ -577,7 +582,7 @@ export const _operator = () => OPERATOR

export const identifier = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => choice($._operator, $._identifier_without_operators)
) => prec(Prec.Identifier, choice($._operator, $._identifier_without_operators))

export const group = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
seq('(', field('term', $._simple_term), ')')
11 changes: 6 additions & 5 deletions common/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Dialect, Prec } from './enums'
import { Dialect } from './dialects'
import {
buildGenericType,
buildStruct,
Expand All @@ -7,6 +7,7 @@ import {
commaSep1,
} from './util'
import { TYPE } from './constants'
import { Prec } from './precedences'

export const type_variable_declaration = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
Expand Down Expand Up @@ -50,7 +51,7 @@ export const _type_constructor = (dialect: Dialect) => <
$.refinement_type_declaration,
)

return choice(...choices)
return prec(Prec.Type, choice(...choices))
}

export const _term_type = <RuleName extends string>(
Expand Down Expand Up @@ -80,15 +81,15 @@ export const curried_type = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.right(
Prec.CurriedType,
Prec.CurriedOrIntersectionType,
seq(field('from', $._type), '->', field('to', $._type)),
)

export const intersection_type = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec.right(
Prec.IntersectionType,
Prec.CurriedOrIntersectionType,
seq(field('left', $._type), '&', field('right', $._type)),
)

Expand Down Expand Up @@ -198,7 +199,7 @@ export const optional_type = <RuleName extends string>(
export const tagged_type = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec(
prec.right(
Prec.TaggedType,
seq(
':',
Expand Down
5 changes: 3 additions & 2 deletions dtn/grammar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ import {
declaration_member,
js_identifier,
} from '../common/declarations'
import { Dialect } from '../common/enums'
import { Dialect } from '../common/dialects'
import { comment } from '../common/miscellaneous'
import { precedences } from '../common/precedences'

const dialect = Dialect.DTN

Expand All @@ -65,7 +66,7 @@ export = grammar({
extras: ($) => [$.comment, /\s+/],
word: ($) => $._identifier_without_operators,

precedences: () => [],
precedences,

rules: {
program: ($) =>
Expand Down
Loading

0 comments on commit 8d7753c

Please sign in to comment.