Skip to content
This repository was archived by the owner on Jun 1, 2022. It is now read-only.
Open
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
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), ')'))
80 changes: 80 additions & 0 deletions common/precedences.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
export enum Prec {
Access = 'Access',
And = 'And',
Application = 'Application',
Assignment = 'Assignment',
Biconditional = 'Biconditional',
CurriedOrIntersectionType = 'CurriedOrIntersectionType',
Equality = 'Equality',
Exponentiation = 'Exponentiation',
Identifier = 'Identifier',
Implication = 'Implication',
InfixApplication = 'InfixApplication',
LabeledType = 'LabeledType',
Mod = 'Mod',
NamedInfixApplication = 'NamedInfixApplication',
Not = 'Not',
OperatorInfixApplication = 'OperatorInfixApplication',
OptionalType = 'OptionalType',
Or = 'Or',
Order = 'Order',
PatternOrTerm = 'PatternOrTerm',
Pipeline = 'Pipeline',
PrefixApplication = 'PrefixApplication',
Product = 'Product',
SectionIdentifier = 'SectionIdentifier',
SubtractionType = 'SubtractionType',
Sum = 'Sum',
TaggedType = 'TaggedType',
TaggedValue = 'TaggedValue',
Type = 'Type',
TypeHint = 'TypeHint',
UnionType = 'UnionType',
}

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

const termPrecedences = [
Prec.PatternOrTerm,
Prec.Application,
Prec.PrefixApplication,
Prec.InfixApplication,
Prec.Assignment,
Prec.TypeHint,
Prec.TaggedValue,
Prec.SectionIdentifier,
Prec.Identifier,
]

const operatorPrecedences = [
Prec.Pipeline,
Prec.Access,
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,
]

export const precedences = () => [
typePrecedences,
termPrecedences,
operatorPrecedences,
]
Loading