Merge pull request #277 from susliko/rework-lambda-expressions

The Gordian Knot
text_sliders
eugene yokota 2023-06-08 10:50:11 +07:00 committed by GitHub
commit d24467932b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 103 additions and 34 deletions

@ -840,6 +840,8 @@ Value declarations (Scala 3 syntax)
================================================================================ ================================================================================
class A: class A:
// Comments that should not
// influence indentation
val b, c : Int val b, c : Int
val d : String val d : String
@ -849,6 +851,8 @@ class A:
(class_definition (class_definition
(identifier) (identifier)
(template_body (template_body
(comment)
(comment)
(val_declaration (val_declaration
(identifier) (identifier)
(identifier) (identifier)
@ -1454,6 +1458,10 @@ trait A {
def f: Int def f: Int
} }
trait A { self =>
def f: Int
}
class B { class B {
self: Something[A] => self: Something[A] =>
@ -1463,6 +1471,14 @@ class B {
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(compilation_unit (compilation_unit
(trait_definition
(identifier)
(template_body
(self_type
(identifier))
(function_declaration
(identifier)
(type_identifier))))
(trait_definition (trait_definition
(identifier) (identifier)
(template_body (template_body

@ -185,10 +185,11 @@ class C:
(indented_block (indented_block
(lambda_expression (lambda_expression
(identifier) (identifier)
(indented_block
(infix_expression (infix_expression
(identifier) (identifier)
(operator_identifier) (operator_identifier)
(integer_literal)))))) (integer_literal)))))))
(call_expression (call_expression
(identifier) (identifier)
(colon_argument (colon_argument
@ -217,8 +218,8 @@ class C:
(indented_cases (indented_cases
(case_clause (case_clause
(identifier) (identifier)
(identifier))))) (identifier))
(comment) (comment))))
(ascription_expression (ascription_expression
(identifier) (identifier)
(type_identifier)) (type_identifier))
@ -442,8 +443,8 @@ class C:
(if_expression (if_expression
(boolean_literal) (boolean_literal)
(indented_block (indented_block
(unit)) (unit)
(comment) (comment))
(indented_block (indented_block
(unit)))))))) (unit))))))))
@ -1054,8 +1055,14 @@ object O {
val l = a => a + 1 val l = a => a + 1
val b = (x: Int, y: Int) => { x * y } val b = (x: Int, y: Int) => { x * y }
val f = _ => 2 val f = _ => 2
(a, b, _) => a - b foo { i =>
foo { i => val x = 2 + i } val x = 2 + i
x
}
{ x =>
val y = 2 * x
y * y
}
} }
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
@ -1092,29 +1099,33 @@ object O {
(lambda_expression (lambda_expression
(wildcard) (wildcard)
(integer_literal))) (integer_literal)))
(call_expression
(identifier)
(block
(lambda_expression (lambda_expression
(bindings
(binding
(identifier))
(binding
(identifier))
(binding
(identifier)))
(infix_expression
(identifier) (identifier)
(indented_block
(val_definition
(identifier)
(infix_expression
(integer_literal)
(operator_identifier) (operator_identifier)
(identifier))) (identifier)))
(call_expression (identifier)))))
(identifier)
(block (block
(lambda_expression (lambda_expression
(identifier) (identifier)
(indented_block
(val_definition (val_definition
(identifier) (identifier)
(infix_expression (infix_expression
(integer_literal) (integer_literal)
(operator_identifier) (operator_identifier)
(identifier))))))))) (identifier)))
(infix_expression
(identifier)
(operator_identifier)
(identifier))))))))
================================================================================ ================================================================================
Unit expressions Unit expressions
@ -1648,6 +1659,7 @@ throws()
using() using()
-------------------------------------------------------------------------------- --------------------------------------------------------------------------------
(compilation_unit (compilation_unit
(call_expression (call_expression
(identifier) (identifier)

@ -369,16 +369,36 @@ module.exports = grammar({
/* /*
* TemplateBody ::= :<<< [SelfType] TemplateStat {semi TemplateStat} >>> * TemplateBody ::= :<<< [SelfType] TemplateStat {semi TemplateStat} >>>
*/ */
template_body: $ => template_body: $ => choice(
choice( prec.left(PREC.control, $._indented_template_body),
prec.left( prec.left(PREC.control, $._braced_template_body),
PREC.control,
seq(":", $._indent, optional($.self_type), $._block, $._outdent),
), ),
prec.left(
PREC.control, _indented_template_body: $ => seq(
seq("{", optional($.self_type), optional($._block), "}"), ':',
$._indent,
optional($.self_type),
$._block,
$._outdent,
),
_braced_template_body: $ => seq(
'{',
optional(choice(
$._braced_template_body1,
$._braced_template_body2,
)),
'}',
),
_braced_template_body1: $ => seq(optional($.self_type), $._block),
_braced_template_body2: $ => seq(
choice(
seq($._indent, optional($.self_type)),
seq(optional($.self_type), $._indent),
), ),
optional($._block),
$._outdent
), ),
/* /*
@ -1038,12 +1058,13 @@ module.exports = grammar({
$.call_expression, $.call_expression,
), ),
lambda_expression: $ => lambda_expression: $ =>
prec.right( prec.right(
seq( seq(
field("parameters", choice($.bindings, $._identifier, $.wildcard)), field("parameters", choice($.bindings, $._identifier, $.wildcard)),
"=>", "=>",
$._block, $._indentable_expression,
), ),
), ),

22
src/scanner.c vendored

@ -89,6 +89,18 @@ static bool scan_string_content(TSLexer *lexer, bool is_multiline, bool has_inte
} }
} }
static bool detect_comment_start(TSLexer *lexer) {
lexer->mark_end(lexer);
// Comments should not affect indentation
if (lexer->lookahead == '/') {
advance(lexer);
if (lexer->lookahead == '/' || lexer -> lookahead == '*') {
return true;
}
}
return false;
}
bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer, bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
const bool *valid_symbols) { const bool *valid_symbols) {
ScannerStack *stack = (ScannerStack *)payload; ScannerStack *stack = (ScannerStack *)payload;
@ -103,7 +115,8 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
( (
(prev != -1) && (prev != -1) &&
lexer->lookahead == ')' || lexer->lookahead == ')' ||
lexer->lookahead == ']' lexer->lookahead == ']' ||
lexer->lookahead == '}'
) || ( ) || (
stack->last_indentation_size != -1 && stack->last_indentation_size != -1 &&
prev != -1 && prev != -1 &&
@ -131,6 +144,9 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
newline_count > 0 && newline_count > 0 &&
(isEmptyStack(stack) || (isEmptyStack(stack) ||
indentation_size > peekStack(stack))) { indentation_size > peekStack(stack))) {
if (detect_comment_start(lexer)) {
return false;
}
pushStack(stack, indentation_size); pushStack(stack, indentation_size);
lexer->result_symbol = INDENT; lexer->result_symbol = INDENT;
LOG(" INDENT\n"); LOG(" INDENT\n");
@ -148,6 +164,10 @@ bool tree_sitter_scala_external_scanner_scan(void *payload, TSLexer *lexer,
LOG(" pop\n"); LOG(" pop\n");
LOG(" OUTDENT\n"); LOG(" OUTDENT\n");
lexer->result_symbol = OUTDENT; lexer->result_symbol = OUTDENT;
lexer->mark_end(lexer);
if (detect_comment_start(lexer)) {
return false;
}
stack->last_indentation_size = indentation_size; stack->last_indentation_size = indentation_size;
stack->last_newline_count = newline_count; stack->last_newline_count = newline_count;
if (lexer->eof(lexer)) { if (lexer->eof(lexer)) {