darklang / dark

Darklang main repo, including language, backend, and infra
https://darklang.com
Other
1.68k stars 91 forks source link

Tree-sitter grammar: support if/else expressions #5343

Closed OceanOak closed 7 months ago

OceanOak commented 7 months ago

Changelog:

Tree-sitter-darklang
- Update the grammar to support if/else statements
- Add an external scanner to handle indentation

5321

StachuDotNet commented 7 months ago

I wonder if we could write a function in our grammar.js that would abstract over this pattern

choice(
  seq(
    $.indent,
    repeat1(field("then_expression", $.expression)),
    $.dedent,
  ),
  field("then_expression", $.paren_expression),
),

like

let maybeWithIndentDedent (thing) = 
  choice(
    seq($.indent, thing, $.dedent),
    thing,
  ),
OceanOak commented 7 months ago

I wonder if we could write a function in our grammar.js that would abstract over this pattern

choice(
  seq(
    $.indent,
    repeat1(field("then_expression", $.expression)),
    $.dedent,
  ),
  field("then_expression", $.paren_expression),
),

like

let maybeWithIndentDedent (thing) = 
  choice(
    seq($.indent, thing, $.dedent),
    thing,
  ),

Some initial thoughts:

module.exports = grammar({ name: "darklang",

// I believe we can't declare that function here ...

so, the function would probably be more like 

let maybeWithIndentDedent = (indent, thing, dedent) => choice(seq(indent, thing, dedent), thing);



- Losing the field names would make it challenging to capture in the parser, no?

**Unrelated note:** 
we still need to wrap the 'then' expression in parentheses (only when writing an inline if/else expression) due to conflicts. planning to address this when fixing `apply`...