68 lines
1.7 KiB
JavaScript
68 lines
1.7 KiB
JavaScript
/**
|
||
* @file Laddertypes grammar for tree-sitter
|
||
* @author Michael Sippel <senvas@exobiont.de>
|
||
* @license GPLv3
|
||
*/
|
||
|
||
/// <reference types="tree-sitter-cli/dsl" />
|
||
// @ts-check
|
||
|
||
module.exports = grammar({
|
||
name: "laddertypes",
|
||
|
||
rules: {
|
||
// TODO: add the actual grammar rules
|
||
source_file: ($) => $.type,
|
||
|
||
type: ($) =>
|
||
choice(
|
||
$.comment,
|
||
$.typeid,
|
||
$.num,
|
||
$.char,
|
||
$.univ,
|
||
$.spec,
|
||
$.func,
|
||
$.morph,
|
||
$.ladder,
|
||
$.struct,
|
||
$.enum,
|
||
$.seq,
|
||
),
|
||
|
||
constraint: ($) =>
|
||
choice(
|
||
$.valueConstraint,
|
||
$.unconstrainedType,
|
||
$.subType,
|
||
$.traitType,
|
||
$.parType,
|
||
),
|
||
|
||
comment: (_) => seq("/*", /(.*)/, "*/"),
|
||
|
||
typevar: (_) => /([a-zA-Z$][0-9a-zA-Z_\-]*)/,
|
||
typeid: (_) => /([a-zA-Z$][0-9a-zA-Z_\-\.]*)/,
|
||
num: (_) => /\d+(\.\d+)?/,
|
||
char: (_) => seq("'", /(.)/, "'"),
|
||
|
||
univ: ($) =>
|
||
prec.left(seq("∀", "(", $.typevar, ":", $.constraint, ")", ".", $.type)),
|
||
spec: ($) => prec.left(seq("<", repeat($.type), ">")),
|
||
func: ($) => prec.right(seq($.type, "-->", $.type)),
|
||
morph: ($) => prec.right(seq($.type, "-morph->", $.type)),
|
||
ladder: ($) => prec.left(seq($.type, "~", $.type)),
|
||
struct: ($) => prec.left(1, seq("{", repeat($.struct_member), "}")),
|
||
enum: ($) => prec.left(2, seq("{", repeat($.enum_variant), "}")),
|
||
seq: ($) => seq("[", repeat($.type), "]"),
|
||
|
||
struct_member: ($) => seq($.typevar, ":", $.type, ";"),
|
||
enum_variant: ($) => seq("|", $.typevar, ":", $.type),
|
||
|
||
valueConstraint: ($) => "ℤ",
|
||
unconstrainedType: ($) => "Type",
|
||
subType: ($) => seq("<=", $.type),
|
||
traitType: ($) => seq("><", $.type),
|
||
parType: ($) => seq("||", $.type),
|
||
},
|
||
});
|