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

[#595] First class types #596

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
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
2 changes: 1 addition & 1 deletion common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const BIN = /_?[0-1]+/

// identifiers
export const IDENTIFIER = /_?[a-z][a-z0-9_]*(\?|!)?'*/
export const OPERATOR = /(==|[!@$%^&*|<>~*\\\-+/.])[!@$%^&*|<>~*\\\-+/.=?]*/
export const OPERATOR = /[!@$%^&*|<>~*\\\-+/.?=]+/
export const TYPE = /[A-Z][a-zA-Z0-9]*/

// declarations
Expand Down
11 changes: 4 additions & 7 deletions common/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,12 @@ export const declaration_member = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
seq(
field('as', alias($.identifier, $.identifier_pattern_name)),
field('as', alias($.identifier, $.identifier_pattern)),
optional(
seq(
'from',
field('name', alias($.js_identifier, $.identifier_pattern_name)),
),
seq('from', field('name', alias($.js_identifier, $.identifier_pattern))),
),
'::',
field('type', $._type),
':',
field('type', $._term),
)

export const js_identifier = () => token(seq(JS_ALPHA, repeat(JS_ALPHANUMERIC)))
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,
}
41 changes: 0 additions & 41 deletions common/enums.ts

This file was deleted.

62 changes: 29 additions & 33 deletions common/imports.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 { commaSep1 } from './util'

export const import_ = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
Expand All @@ -8,36 +8,32 @@ export const exported_import = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => seq('export', $._import_body)

export const _import_body_constructor =
(dialect: Dialect) =>
<RuleName extends string>($: GrammarSymbols<RuleName>) => {
switch (dialect) {
case Dialect.DTN:
return prec.left(
Prec.Pattern,
seq(
'{',
commaSep1(field('import', $.import_type)),
'}',
'from',
field('source', $.raw_string),
),
)
case Dialect.Tony:
return prec.left(
Prec.Pattern,
seq(
choice(
field('default', alias($.identifier, $.identifier_pattern_name)),
seq(
optional(
seq(
field(
'default',
alias($.identifier, $.identifier_pattern_name),
),
',',
),
export const _import_body_constructor = (dialect: Dialect) => <
RuleName extends string
>(
$: GrammarSymbols<RuleName>,
) => {
switch (dialect) {
case Dialect.DTN:
return prec.left(
seq(
'{',
commaSep1(field('import', $.import_type)),
'}',
'from',
field('source', $.raw_string),
),
)
case Dialect.Tony:
return prec.left(
seq(
choice(
field('default', alias($.identifier, $.identifier_pattern)),
seq(
optional(
seq(
field('default', alias($.identifier, $.identifier_pattern)),
',',
),
'{',
commaSep1(
Expand All @@ -58,9 +54,9 @@ export const import_identifier = <RuleName extends string>(
) =>
seq(
optional(
seq(field('name', alias($.identifier, $.identifier_pattern_name)), 'as'),
seq(field('name', alias($.identifier, $.identifier_pattern)), 'as'),
),
field('as', alias($.identifier, $.identifier_pattern_name)),
field('as', alias($.identifier, $.identifier_pattern)),
)

export const import_type = <RuleName extends string>(
Expand Down
10 changes: 3 additions & 7 deletions common/literals.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
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, $.decimal, $.integer, $.string, $.regex)

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

export const _decimal = () => {
export const decimal = () => {
const digits = repeat1(DIGITS)
const exponent = seq(EXP, digits)

Expand All @@ -17,7 +16,7 @@ export const _decimal = () => {
)
}

export const _integer = () =>
export const integer = () =>
token(
choice(
seq('0x', repeat1(HEX)),
Expand All @@ -27,9 +26,6 @@ export const _integer = () =>
),
)

export const number = <RuleName extends string>($: GrammarSymbols<RuleName>) =>
choice($._decimal, $._integer)

export const raw_string = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => buildString($)
Expand Down
72 changes: 45 additions & 27 deletions common/patterns.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Prec } from './precedences'
import {
buildIdentifierPattern,
buildBindingPattern,
buildList,
buildMember,
buildStruct,
buildTuple,
} from './util'
import { Prec } from './enums'

export const _pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec(
Prec.Pattern,
'base',
choice(
$.wildcard_pattern,
$.identifier_pattern,
$.binding_pattern,
$._assignable_pattern,
$._literal_pattern,
),
Expand All @@ -23,28 +23,41 @@ export const _pattern = <RuleName extends string>(
export const _assignable_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
choice(
alias($.root_identifier_pattern, $.identifier_pattern),
$.destructuring_pattern,
$.tagged_pattern,
$.pattern_group,
prec(
'base',
choice(
alias($.root_binding_pattern, $.binding_pattern),
$.destructuring_pattern,
$.pattern_group,
),
)

export const destructuring_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec(
Prec.Pattern,
'base',
seq(
optional(
seq(
field('alias', alias($.identifier, $.identifier_pattern_name)),
field(
'alias',
choice(
alias($.identifier, $.identifier_pattern),
alias($.type, $.type_pattern),
),
),
'@',
),
),
field(
'pattern',
choice($.struct_pattern, $.tuple_pattern, $.list_pattern),
choice(
$.struct_pattern,
$.tuple_pattern,
$.list_pattern,
$.tag_pattern,
),
),
),
)
Expand All @@ -53,51 +66,56 @@ export const struct_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
prec(
Prec.Pattern,
'base',
buildStruct(
$,
'member',
choice(
$.member_pattern,
alias($.identifier_pattern, $.shorthand_member_pattern),
alias($.binding_pattern, $.shorthand_member_pattern),
),
true,
),
)

export const member_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => buildMember($, $._term, $._pattern)
) => prec('base', buildMember($, $._term, $._pattern))

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

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

export const identifier_pattern = <RuleName extends string>(
export const binding_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => buildIdentifierPattern($, true)
) => prec('base', buildBindingPattern($, true))

export const root_identifier_pattern = <RuleName extends string>(
export const root_binding_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => buildIdentifierPattern($, false)
) => prec('base', choice(buildBindingPattern($, false)))

export const wildcard_pattern = () => '_'

export const tagged_pattern = <RuleName extends string>(
export const tag_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) =>
seq(
field('name', alias($._identifier_without_operators, $.identifier)),
field('pattern', $._pattern),
prec(
'application',
seq(
field('name', alias($._identifier_without_operators, $.identifier)),
field('pattern', $._pattern),
),
)

export const _literal_pattern = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => choice($.boolean, $.number, $.raw_string, $.regex)
) =>
prec('base', choice($.boolean, $.decimal, $.integer, $.raw_string, $.regex))

export const pattern_group = <RuleName extends string>(
$: GrammarSymbols<RuleName>,
) => prec(Prec.Pattern, seq('(', field('pattern', $._pattern), ')'))
) => prec('base', seq('(', field('pattern', $._pattern), ')'))
Loading