Skip to content
This repository was archived by the owner on Jun 15, 2023. It is now read-only.

put parseTagName at toplevel #545

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4968,19 +4968,7 @@ and parsePolymorphicVariantType ~attrs p =
Parser.optional p Bar |> ignore;
let rowField = parseTagSpecFull p in
let rowFields = parseTagSpecFulls p in
let tagNames =
if p.token == GreaterThan
then begin
Parser.next p;
let rec loop p = match p.Parser.token with
| Rbracket -> []
| _ ->
let (ident, _loc) = parseHashIdent ~startPos:p.startPos p in
ident :: loop p
in
loop p
end
else [] in
let tagNames = parseTagNames p in
let variant =
let loc = mkLoc startPos p.prevEndPos in
Ast_helper.Typ.variant ~attrs ~loc (rowField :: rowFields) Closed (Some tagNames) in
Expand All @@ -4995,6 +4983,20 @@ and parsePolymorphicVariantType ~attrs p =
Parser.expect Rbracket p;
variant

and parseTagName p =
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These need to be at toplevel for the analysis to find them.
The restriction seems OK for this use case.

match p.Parser.token with
| Hash ->
let (ident, _loc) = parseHashIdent ~startPos:p.startPos p in
Some ident
| _ -> None

and parseTagNames p =
if p.Parser.token == GreaterThan then
(Parser.next p;
parseRegion p ~grammar:Grammar.TagNames ~f:parseTagName)
else
[]

and parseTagSpecFulls p =
match p.Parser.token with
| Rbracket ->
Expand Down Expand Up @@ -6449,4 +6451,4 @@ let parseSpecification p : Parsetree.signature =

(* module structure on the file level *)
let parseImplementation p : Parsetree.structure =
parseRegion p ~grammar:Grammar.Implementation ~f:parseStructureItemRegion
parseRegion p ~grammar:Grammar.Implementation ~f:parseStructureItemRegion
4 changes: 4 additions & 0 deletions src/res_grammar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ type t =
| JsFfiImport
| Pattern
| AttributePayload
| TagNames

let toString = function
| OpenDescription -> "an open description"
Expand Down Expand Up @@ -118,6 +119,7 @@ let toString = function
| Pattern -> "pattern"
| ExprFor -> "a for expression"
| AttributePayload -> "an attribute payload"
| TagNames -> "tag names"

let isSignatureItemStart = function
| Token.At
Expand Down Expand Up @@ -336,6 +338,7 @@ let isListElement grammar token =
| JsxAttribute -> isJsxAttributeStart token
| JsFfiImport -> isJsFfiImportStart token
| AttributePayload -> token = Lparen
| TagNames -> token = Hash
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is not currently used, as when returning None the current token won't be Hash, but it's good to have it here for later in case the parse tag function changes and does some more refined error recovery after #.

| _ -> false

let isListTerminator grammar token =
Expand All @@ -361,6 +364,7 @@ let isListTerminator grammar token =
| PackageConstraint, token when token <> And -> true
| ConstructorDeclaration, token when token <> Bar -> true
| AttributePayload, Rparen -> true
| TagNames, Rbracket -> true
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This actually needs to be specified in order for parseRegion to work.


| _ -> false

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

Syntax error!
tests/parsing/infiniteLoops/polymorphicVariantType.res:1:14

1 │ type x = [<y>

Did you forget a `]` here?

type nonrec x = [< y]
1 change: 1 addition & 0 deletions tests/parsing/infiniteLoops/polymorphicVariantType.res
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type x = [<y>