Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ranges to identifiers and code blocks in the ASTs #146

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from

Conversation

baronfel
Copy link
Collaborator

This PR changes the definitions of the types in the ASTs for FsLex and FsYacc. The intent would be to contain enough information to annotate identifiers in editor tooling to power experiences like code completion of identifiers, go-to-declaration, find usages, etc.

I've built this and started brief integrations into FsAutocomplete and initial results are promising! This would hopefully open to door to experiences like typechecking the TopCode/BottomCode, the individual rule productions, etc.

@baronfel baronfel force-pushed the add-ranges-to-AST branch from 541019b to cdf9e34 Compare July 11, 2021 18:48
@@ -47,82 +47,82 @@ type ArgParser() =
sbuf.ToString()


static member ParsePartial(cursor,argv,argSpecs:seq<ArgInfo>,?other,?usageText) =
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes were all to address some signature file/implementation argument name mismatch warnings.

@@ -6,8 +6,8 @@ open System.Collections.Generic
open System.Globalization
open FSharp.Text.Lexing

type Ident = string
type Code = string * Position
type Ident = string * Range
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the primary change is to add a Range to the various identifier/text structures, and from there flow the effects of that change up to things like error reporting.


type Rule = (Ident * Ident list * Clause list)
type Macro = Ident * Regexp
type Clause = { Matcher: Regexp; Code: Code }
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

adding types around these structures instead of tuples helps readability

@@ -26,9 +26,12 @@ spec:
{ TopCode=$1;Macros=$2;Rules=$4;BottomCode=$5 }
}

ident:
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the parser for fslex files itself had to be updated to adjust to the new AST shapes, supplying ranges where the types required it, or returning a record instead of a bare tuple.

@@ -15,30 +15,30 @@ open FSharp.Text.Lexing
let (|KeyValue|) (kvp:KeyValuePair<_,_>) = kvp.Key,kvp.Value


type Identifier = string
type Code = string * Position
type Identifier = string * Range
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similarly to fslex, fsyacc's own AST needed to be augmented with ranges for primitive productions like identifiers, code blocks, terminals, and nonterminals. This likewise flowed up through the remainder of the AST shapes.


let checkNonTerminal (name, range) =
if name <> "error" && not (nonTerminalNamesSet.Contains name) then
failwithf "%s(%d,%d): NonTerminal '%s' has no productions" range.startPos.FileName range.startPos.Line range.startPos.Column name
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is an example of the nicer errors we can get from these structures. before this is merged I definitely want to convert the entire .Core api surface area to return Results containing all collected errors, though. This won't be usable inside an IDE if it throws all over the place.

@baronfel
Copy link
Collaborator Author

something's different in the parsing of the ml-compatibility tests (the most recent commit failures should show this), but I'm not sure how concerned I should be. It might just be equality suffering due to the inclusion of the ranges on the identifiers?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant