tree-sitter-laddertypes/grammar.js
2025-06-11 07:35:51 +02:00

68 lines
1.7 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/**
* @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),
},
});