improvements for proper printing, etc.

This commit is contained in:
ProgramSnail 2023-07-31 22:06:30 +03:00
parent b71b732797
commit 8e0cea277e
4 changed files with 231 additions and 119 deletions

View file

@ -4,10 +4,7 @@ module.exports = grammar({
word: $ => $.identifier,
extras: $ => [
$._exec_comment,
$._line_comment,
$._block_comment,
/\s/
/\s/,
],
rules: {
@ -20,7 +17,7 @@ module.exports = grammar({
$.import,
$.function_definition,
$.type_definition,
$.typeclass_definition,
$.extra,
$.empty_lines,
),
@ -29,10 +26,10 @@ module.exports = grammar({
field('name', choice($.simple_name_identifier, $.placeholder)),
optional(seq('=', field('module', $.simple_name_identifier))),
optional(seq(':', repeat1(choice(
$.simple_type_identifier,
$.simple_name_identifier,
$.typeclass_identifier,
seq('(',$.operator, ')'),
$.simple_type_identifier,
$.typeclass_identifier,
)))),
';',
),
@ -45,38 +42,36 @@ module.exports = grammar({
optional($.definition_info),
repeat($.annotation_info),
repeat(seq($.constraint, ';')),
optional($._var_let),
optional('.'), // for methods
choice(field('name', $.simple_name_identifier), seq('(', field('name', $.operator), ')')),
optional($._optional_result),
optional($._optional_or_result),
repeat(seq(
optional($.annotation_identifier),
optional($._reference),
$.argument_name_identifier,
optional($._optional_result),
optional($._optional_or_result),
)),
optional(seq(
':',
repeat1(seq(
optional($.annotation_identifier),
optional($._reference),
$.type,
)),
)),
optional(seq(':', repeat1(seq(
optional($.annotation_identifier),
optional($._reference),
$.type,
)))),
choice(seq('=', choice(prec(2, choice($.block, $.array)), seq($._super_expression, ';'))), ';'),
),
// datatype or typeclass definition
type_definition: $ => seq(
optional($.definition_info),
repeat($.annotation_info),
repeat($.annotation_info), // for datatypes only
optional('^'),
field('name', $.simple_type_identifier),
optional(seq(repeat($.argument_type_identifier), '=', $.variant_type)),
choice(seq('{', repeat($.function_definition), '}'), ';')
),
typeclass_definition: $ => seq(
optional($.definition_info),
// no argumenmts => no annotation info
field('name', $.typeclass_identifier),
optional(seq(':', repeat1($.typeclass_identifier))),
choice(seq('{', repeat($.function_definition), '}'), ';'),
field('name', choice($.simple_type_identifier, $.typeclass_identifier)),
optional(seq('[', repeat($.typeclass_identifier), ']')), // parametric typeclasses ??
repeat($.argument_type_identifier), // for datatypes only
optional(seq('=', $.variant_type)), // for datatypes only
';',
),
// --- flow control
@ -130,7 +125,7 @@ module.exports = grammar({
// --- continers
block: $ => seq('{', repeat(choice(seq($._super_expression, ';'), $.empty_lines)), '}'),
block: $ => seq('{', repeat(choice(seq($._super_expression, ';'), $.extra, $.empty_lines)), '}'),
array: $ => seq('[[', repeat1($._scoped_expression), ']]'),
@ -139,7 +134,7 @@ module.exports = grammar({
return: $ => seq(choice('return', 'bring'), $._expression),
name_definition: $ => seq(
$._var_let,
choice(choice('%', 'let'), choice('$', 'var')),
choice($.simple_name_identifier, $.placeholder),
),
@ -151,7 +146,7 @@ module.exports = grammar({
reference_expression: $ => prec(-1, seq($._reference, $._scoped_expression)),
suffix_expression: $ => seq($._scoped_expression, $._optional_result),
suffix_expression: $ => seq($._scoped_expression, $._optional_or_result),
// --- other
@ -234,7 +229,7 @@ module.exports = grammar({
type: $ => seq(
optional('^'),
field('name', $._type_identifier),
optional($._optional_result),
optional($._optional_or_result),
optional(seq('[', repeat1($.type), ']'))
),
@ -245,7 +240,8 @@ module.exports = grammar({
info: $ => /[^\n]*/,
empty_lines: $ => /\n\n+/,
extra: $ => choice($._exec_comment, $._line_comment, $._block_comment),
empty_lines: $ => prec.left(repeat1('\n')),
_exec_comment: $ => token(seq('#!', /[^\n]*/)),
_line_comment: $ => token(seq('//', /[^\n]*/)),
@ -254,11 +250,7 @@ module.exports = grammar({
// --- tokens
_do: $ => choice('=>', 'do'),
_var_let: $ => choice(choice('%', 'let'), choice('$', 'var')),
_optional_result: $ => choice('?', '!'),
//_reference: $ => choice(choice('->', 'out'), choice('<-', 'in'), choice('<>', 'ref'), choice('|->', 'or_out'), choice('<-|', 'or_in')),
// _reference: $ => seq(optional(choice('->', 'out')), optional(choice('<>', 'ref')), optional(choice('--', 'const')), optional(choice('<-', 'in'))),
_optional_or_result: $ => choice('?', '!'),
_reference: $ => choice(choice('<-', '<>', '--', '->',
'<-|<>', '<-|--', '<>|->', '--|->', '<>|--', '<-|->',