grammar.g4 new types fixes

This commit is contained in:
ProgramSnail 2023-08-12 14:44:05 +03:00
parent 8e0cea277e
commit c9bef5a02e
4 changed files with 195 additions and 106 deletions

View file

@ -133,7 +133,7 @@ test_k 'n 'k : Int Int -> Bool = {
(case
(name_expression
(name_expression
(type
(simple_type
(simple_type_identifier))
(simple_name_identifier)
(number_literal))
@ -257,13 +257,13 @@ test_k 'n 'k : Int Int -> Bool = {
(simple_name_identifier)
(argument_name_identifier)
(argument_name_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(block
(empty_lines)
@ -280,13 +280,13 @@ test_k 'n 'k : Int Int -> Bool = {
(return
(operator_expression
(name_expression
(type
(simple_type
(simple_type_identifier))
(simple_name_identifier)
(number_literal))
(operator)
(name_expression
(type
(simple_type
(simple_type_identifier))
(simple_name_identifier)
(argument_name_identifier)))))
@ -355,9 +355,9 @@ test_k 'n 'k : Int Int -> Bool = {
(function_definition
(simple_name_identifier)
(argument_name_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(block
(empty_lines)
@ -450,11 +450,11 @@ test_k 'n 'k : Int Int -> Bool = {
(simple_name_identifier)
(argument_name_identifier)
(argument_name_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(block
(empty_lines)

View file

@ -226,6 +226,12 @@ parse_number : Unit! = {
: useful when tuples returned
result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a, 'd := 'b, ());
// (A & B & C) same to Tuple[A B C]
tuple_argument_test 'x : (A & B & C) = do_something;
// ((A1 & A2) | B | C) same to Variant[Tuple[A1 A2] B C]
variant_argument_test 'x : ((A1 & A2) | B | C) = do_something;
--------------------------------------------------------------------------------
(source_file
@ -421,9 +427,9 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier)
(argument_name_identifier)
(annotation_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(match
(name_expression
@ -560,7 +566,7 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier))
(case
(constructor
(type
(simple_type
(simple_type_identifier))
(annotation_identifier)
(string_literal)
@ -627,7 +633,7 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier))
(case
(constructor
(type
(simple_type
(simple_type_identifier))
(annotation_identifier)
(unit_literal))))
@ -637,21 +643,21 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier))
(case
(constructor
(type
(simple_type
(simple_type_identifier))
(annotation_identifier)
(unit_literal))
(string_literal))
(case
(constructor
(type
(simple_type
(simple_type_identifier))
(annotation_identifier)
(unit_literal))
(string_literal))
(case
(constructor
(type
(simple_type
(simple_type_identifier))
(annotation_identifier)
(unit_literal))
@ -736,39 +742,35 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(empty_lines)
(type_definition
(simple_type_identifier)
(variant_type
(tuple_type
(annotation_identifier)
(type
(simple_type_identifier))
(annotation_identifier)
(type
(simple_type_identifier)))))
(tuple_type
(annotation_identifier)
(simple_type
(simple_type_identifier))
(annotation_identifier)
(simple_type
(simple_type_identifier))))
(empty_lines)
(type_definition
(simple_type_identifier)
(variant_type
(tuple_type
(annotation_identifier)
(type
(simple_type_identifier)))
(tuple_type
(annotation_identifier)
(type
(simple_type_identifier)))
(tuple_type
(annotation_identifier)
(type
(simple_type_identifier)))))
(annotation_identifier)
(simple_type
(simple_type_identifier))
(annotation_identifier)
(simple_type
(simple_type_identifier))
(annotation_identifier)
(simple_type
(simple_type_identifier))))
(empty_lines)
(function_definition
(definition_info
(info))
(simple_name_identifier)
(argument_name_identifier)
(type
(simple_type
(simple_type_identifier)
(type
(simple_type
(argument_type_identifier)))
(block
(empty_lines)
@ -839,9 +841,9 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(info))
(simple_name_identifier)
(argument_name_identifier)
(type
(simple_type
(simple_type_identifier)
(type
(simple_type
(argument_type_identifier)))
(block
(empty_lines)
@ -914,27 +916,28 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_type_identifier)
(argument_type_identifier)
(argument_type_identifier)
(variant_type
(tuple_type
(annotation_identifier)
(type
(simple_type_identifier))
(annotation_identifier)
(type
(simple_type_identifier))
(annotation_identifier)
(type
(tuple_type
(annotation_identifier)
(simple_type
(simple_type_identifier))
(annotation_identifier)
(simple_type
(simple_type_identifier))
(annotation_identifier)
(reference_type
(simple_type
(simple_type_identifier)
(type
(simple_type
(argument_type_identifier))
(type
(argument_type_identifier)))
(annotation_identifier)
(type
(simple_type
(argument_type_identifier))))
(annotation_identifier)
(reference_type
(simple_type
(simple_type_identifier)
(type
(simple_type
(argument_type_identifier))
(type
(simple_type
(argument_type_identifier))))))
(empty_lines)
(function_definition
@ -975,11 +978,11 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier))
(case
(name_expression
(type
(simple_type
(simple_type_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier)))
(simple_name_identifier))))
(empty_lines)
@ -988,9 +991,9 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier))
(case
(name_expression
(type
(simple_type
(simple_type_identifier)
(type
(simple_type
(simple_type_identifier)))
(simple_name_identifier))))
(empty_lines)))
@ -1076,9 +1079,9 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier)
(argument_name_identifier)
(argument_name_identifier)
(type
(simple_type
(argument_type_identifier))
(type
(simple_type
(argument_type_identifier))
(comma_expression
(comma_expression
@ -1132,11 +1135,11 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(definition_info
(info))
(simple_name_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(operator_expression
(operator_expression
@ -1154,7 +1157,7 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(info))
(simple_name_identifier)
(argument_name_identifier)
(type
(simple_type
(simple_type_identifier))
(match
(name_expression
@ -1169,14 +1172,14 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier)
(argument_name_identifier)
(argument_name_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(constructor
(type
(simple_type
(simple_type_identifier))
(annotation_identifier)
(argument_name_identifier)
@ -1189,14 +1192,14 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier)
(argument_name_identifier)
(argument_name_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier))
(constructor
(type
(simple_type
(simple_type_identifier))
(argument_name_identifier)
(argument_name_identifier)))
@ -1209,9 +1212,9 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(empty_lines)
(function_definition
(simple_name_identifier)
(type
(simple_type
(simple_type_identifier))
(type
(simple_type
(simple_type_identifier)))
(empty_lines)
(function_definition
@ -1238,8 +1241,9 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(definition_info
(info))
(simple_name_identifier)
(type
(simple_type_identifier))
(modified_type
(simple_type
(simple_type_identifier)))
(block
(empty_lines)
(match
@ -1247,7 +1251,7 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier))
(case
(name_expression
(type
(simple_type
(simple_type_identifier))
(simple_name_identifier))))
(empty_lines)
@ -1257,7 +1261,7 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(simple_name_identifier)))
(case
(name_expression
(type
(simple_type
(simple_type_identifier))
(simple_name_identifier)
(simple_name_identifier))))
@ -1325,4 +1329,37 @@ result_func! 'a 'b -> 'c -> 'd = ?? 'a == 0 => error "some error" !!=> ('c := 'a
(name_expression
(argument_name_identifier)))))
(unit_literal))))
(empty_lines)
(extra)
(empty_lines)
(function_definition
(simple_name_identifier)
(argument_name_identifier)
(tuple_type
(simple_type
(simple_type_identifier))
(simple_type
(simple_type_identifier))
(simple_type
(simple_type_identifier)))
(name_expression
(simple_name_identifier)))
(empty_lines)
(extra)
(empty_lines)
(function_definition
(simple_name_identifier)
(argument_name_identifier)
(variant_type
(tuple_type
(simple_type
(simple_type_identifier))
(simple_type
(simple_type_identifier)))
(simple_type
(simple_type_identifier))
(simple_type
(simple_type_identifier)))
(name_expression
(simple_name_identifier)))
(empty_lines))

View file

@ -3,6 +3,8 @@ grammar lang ;
// not always most recent grammar
// not checked
// TODO
source_file: (statement)+ EOF ;
statement: import
@ -21,7 +23,7 @@ constraint: '?' expression ;
function_definition: (definietion_info)? (ANNOTATION_INFO)* (constraint ';')*
('.')? (SIMPLE_NAME_IDENTIFIER | ('(' OPERATOR ')')) ('?' | '!')
(((ANNOTATION_IDENTIFIER)? (REFERENCE)? ARGUMENT_NAME_IDENTIFIER ('?' | '!')?)*)
(':' ((ANNOTATION_IDENTIFIER)? (REFERENCE)? type)+)?
(':' ((ANNOTATION_IDENTIFIER)? (REFERENCE)? scoped_type)+)?
(('=' ( /*prec 2*/ block | (super_expression ';'))) | ';')
;
@ -29,7 +31,7 @@ type_definition: (DEFINITION_INFO)? (ANNOTATION_INFO)* ('^')?
(SIMPLE_TYPE_IDENTIFIER | TYPECLASS_IDENTIFIER)
('[' (TYPECLASS_IDENTIFIER)+ ']')?
(ARGUMENT_TYPE_IDENTIFIER)*
('=' variant_type)?
('=' type)?
';'
;
@ -76,7 +78,7 @@ loop_control: 'break'
;
name_expression:
((type '.' SIMPLE_NAME_IDENTIFIER)
((scoped_type '.' SIMPLE_NAME_IDENTIFIER)
| (scoped_expression '.' SIMPLE_NAME_IDENTIFIER)
| name_identifier
| ('(' OPERATOR ')'))
@ -84,7 +86,7 @@ name_expression:
;
constructor: type (ANNOTATION_IDENTIFIER)? scoped_expression)+ ;
constructor: scoped_type (ANNOTATION_IDENTIFIER)? scoped_expression)+ ;
lambda: '\\' (ARGUMENT_NAME_IDENTIFIER)* '=>' expression ;
@ -121,13 +123,34 @@ not_name_scoped_expression: block
| '(' super_expression ')'
;
variant_type: ('|')? tuple_type ('|' tuple_type)* ;
variant_type: ('|')? annotated_type ('|' annotated_type)+ ;
tuple_type: ('&')? annotated_type ('&' _annotated_type)* ;
tuple_type: ('&')? annotated_type ('&' _annotated_type)+ ;
annotated_type: (ANNOTATION_IDENTIFIER)? type ;
annotated_type: (ANNOTATION_IDENTIFIER)? scoped_type ;
type: ('^')? type_identifier ('?')? ('[' (type)+ ']')? ;
array_type: '[[' type ']]' ;
reference_type: /* prec -1 */ '^' scoped_type ;
modified_type: scoped_type ('?' | '!') ;
simple_type: type_identifier ('[' (scoped_type)+ ']')? ;
type: simple_type
| reference_type
| modified_type
| array_type
| variant_type
| tuple_type
;
scoped_type: simple_type
| reference_type
| modified_type
| array_type
| '(' (variant_type | tuple_type) ')'
;
name_identifier: (ARGUMENT_NAME_IDENTIFIER | SIMPLE_NAME_IDENTIFIER) ;
type_identifier: (ARGUMENT_TYPE_IDENTIFIER | SIMPLE_TYPE_IDENTIFIER) ;
@ -152,7 +175,7 @@ EXTRA: EMPTY_LINES
EMPTY_LINES : ('\n')+ ;
LINE_COMMENT : '#!' [^\n]* -> skip ;
EXEC_COMMENT : '#!' [^\n]* -> skip ;
LINE_COMMENT : '//' [^\n]* -> skip ;
BLOCK_COMMENT : '\*' ([^*] |('\*' [^/]))* '*/' -> skip ;

View file

@ -56,7 +56,7 @@ module.exports = grammar({
repeat1(seq(
optional($.annotation_identifier),
optional($._reference),
$.type,
$._scoped_type,
)),
)),
choice(seq('=', choice(prec(2, choice($.block, $.array)), seq($._super_expression, ';'))), ';'),
@ -70,7 +70,7 @@ module.exports = grammar({
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
optional(seq('=', $._type)), // for datatypes only
';',
),
@ -152,7 +152,7 @@ module.exports = grammar({
name_expression: $ => seq(
choice(
seq($.type, '.', field('name', $.simple_name_identifier)),
seq($._scoped_type, '.', field('name', $.simple_name_identifier)),
seq($._scoped_expression, '.', field('name', $.simple_name_identifier)),
field('name', $._name_identifier),
seq('(', field('name', $.operator), ')'),
@ -161,7 +161,7 @@ module.exports = grammar({
),
constructor: $ => seq(
field('type', $.type),
field('type', $._scoped_type),
repeat1(seq(optional($.annotation_identifier), $._scoped_expression)),
),
@ -212,25 +212,54 @@ module.exports = grammar({
// --- type
// same to Variant[...]
variant_type: $ => seq(
optional('|'),
$.tuple_type,
repeat(seq('|', $.tuple_type)),
$._annotated_type,
repeat1(seq('|', $._annotated_type)),
),
// same to Tuple[...]
tuple_type: $ => seq(
optional('&'),
$._annotated_type,
repeat(seq('&', $._annotated_type)),
repeat1(seq('&', $._annotated_type)),
),
_annotated_type: $ => seq(optional($.annotation_identifier), $.type),
// add annotations to in all types ??
_annotated_type: $ => seq(optional($.annotation_identifier), $._scoped_type),
type: $ => seq(
optional('^'),
// same to Array[...]
array_type: $ => seq('[[', $._type, ']]'),
reference_type: $ => prec(-1, seq('^', $._scoped_type)),
// same to Optional[...] or Result[...]
modified_type: $ => seq($._scoped_type, $._optional_or_result),
simple_type: $ => seq(
field('name', $._type_identifier),
optional($._optional_or_result),
optional(seq('[', repeat1($.type), ']'))
optional(seq('[', repeat1($._scoped_type), ']'))
),
_type: $ => choice(
$.simple_type,
$.reference_type,
$.modified_type,
$.array_type,
$.variant_type,
$.tuple_type,
),
_scoped_type: $ => choice(
$.simple_type,
$.reference_type,
$.modified_type,
$.array_type,
seq('(', choice(
$.variant_type,
$.tuple_type,
), ')'),
),
// --- comments