🔍 Looking up ranges in structured data

Open
#162 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
ocaml
Domain
compilers

Research direction

The issue names Range.located, Range.view, and an LSP patch as starting points; read those APIs and the linked patch first. No repository file or test is identified. Done would be an agreed, tested way to use source ranges to look up syntax-tree nodes, including the relevant position-boundary cases.

Written by the indexing model from the issue text.

Description

When Asai reports an error, the diagnostics are attached to the string source like this:

 → error[(Reporter.Message.Tree_not_found foo-0006)]
 ꭍ ○ when expanding tree at address `foo-0004`
 ■ ./trees/foo-0004.tree
 1 | \import{foo-0006}
   ^ Could not find tree named `foo-0006'

I find myself in the position where it would be really useful if I could use the range to look up nodes in the syntax tree.

Nick Hu once sent a patch implementing an LSP server for Forester that contained the following function:


  let inside = fun (located : 'a Range.located) ->
    match located.loc with
    | Some loc -> begin
        match Range.view loc with
        | `Range (start, end_) ->
          let within (a, b) x = a <= x && x <= b in
          within (start.line_num, end_.line_num ) (pos.line + 1)
          && within ((start.offset - start.start_of_line), (end_.offset - end_.start_of_line - 1)) pos.character
        | _ -> false
      end
    | None -> false

I did not think through the arithmetic, but conceptually, this is the right way to do this, right? Or is there a more elegant way using more library functions?

Dominant language
OCaml
Stars
55
Forks
3
PR merge metrics
No merged PRs in 30d

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from RedPRL/asai

All issues in RedPRL/asai

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.