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

Commit

Permalink
Fix parsing and printing of externals inside ext/attr payload (#297)
Browse files Browse the repository at this point in the history
Fixes #260

Structure items inside an attribute payload don't need semicolons
  • Loading branch information
IwanKaramazow authored Feb 28, 2021
1 parent d6cd278 commit 539e28b
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/res_core.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6111,6 +6111,7 @@ and parseAttributeId ~startPos p =
and parsePayload p =
match p.Parser.token with
| Lparen when p.startPos.pos_cnum = p.prevEndPos.pos_cnum ->
Parser.leaveBreadcrumb p Grammar.AttributePayload;
Parser.next p;
begin match p.token with
| Colon ->
Expand All @@ -6127,6 +6128,7 @@ and parsePayload p =
Parsetree.PTyp (parseTypExpr p)
in
Parser.expect Rparen p;
Parser.eatBreadcrumb p;
payload
| Question ->
Parser.next p;
Expand All @@ -6139,6 +6141,7 @@ and parsePayload p =
None
in
Parser.expect Rparen p;
Parser.eatBreadcrumb p;
Parsetree.PPat (pattern, expr)
| _ ->
let items = parseDelimitedRegion
Expand All @@ -6148,6 +6151,7 @@ and parsePayload p =
p
in
Parser.expect Rparen p;
Parser.eatBreadcrumb p;
Parsetree.PStr items
end
| _ -> Parsetree.PStr []
Expand Down
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 =
| ListExpr
| JsFfiImport
| Pattern
| AttributePayload

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

let isSignatureItemStart = function
| Token.At
Expand Down Expand Up @@ -336,6 +338,7 @@ let isListElement grammar token =
| Primitive -> begin match token with Token.String _ -> true | _ -> false end
| JsxAttribute -> isJsxAttributeStart token
| JsFfiImport -> isJsFfiImportStart token
| AttributePayload -> token = Lparen
| _ -> false

let isListTerminator grammar token =
Expand All @@ -362,6 +365,7 @@ let isListTerminator grammar token =
| ConstructorDeclaration, token when token <> Bar -> true
| Primitive, Semicolon -> true
| Primitive, token when isStructureItemStart token -> true
| AttributePayload, Rparen -> true

| _ -> false

Expand Down
5 changes: 5 additions & 0 deletions tests/printer/other/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,11 @@ let x = 1
\\"A\\"
\\"B\\"
)
@@superPrivate(@module(\\"./logo.svg\\") external logo: string = \\"default\\")
@inlinePrivate(@module(\\"./logo.svg\\") external logo: string = \\"default\\")
let x = 1
"
`;
Expand Down
10 changes: 10 additions & 0 deletions tests/printer/other/attributes.res
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,13 @@ let x = 1
)

%%ext("A"; "B")

@@superPrivate(
@module("./logo.svg") external logo: string = "default"
)

@inlinePrivate(
@module("./logo.svg") external logo: string = "default"
)
let x = 1

2 changes: 2 additions & 0 deletions tests/printer/structure/__snapshots__/render.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,8 @@ exports[`extension.js 1`] = `
@attrStructureLvl4
@attrStructureLvl5
%%raw(\\"__eval__gc()\\")
%%private(@module(\\"./logo.svg\\") external logo: string = \\"default\\")
"
`;
Expand Down
4 changes: 4 additions & 0 deletions tests/printer/structure/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@

@attrStructureLvl @attrStructureLvl2 @attrStructureLvl3 @attrStructureLvl4 @attrStructureLvl5
%%raw("__eval__gc()")

%%private(
@module("./logo.svg") external logo: string = "default"
)

0 comments on commit 539e28b

Please sign in to comment.