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

Clean up end-of-file/printing newline logic #142

Merged
merged 3 commits into from
Sep 17, 2020
Merged
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
25 changes: 17 additions & 8 deletions src/res_diagnostics.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,24 @@ let make ~startPos ~endPos category = {
}

let printReport diagnostics src =
let rec print diagnostics src =
match diagnostics with
| [] -> ()
| d::rest ->
Res_diagnostics_printing_utils.Super_location.super_error_reporter
Format.err_formatter
~src
~startPos:d.startPos
~endPos:d.endPos
~msg:(explain d);
begin match rest with
| [] -> ()
| _ -> Format.fprintf Format.err_formatter "@."
end;
print rest src
in
Format.fprintf Format.err_formatter "@[<v>";
List.rev diagnostics |> List.iter (fun d ->
Res_diagnostics_printing_utils.Super_location.super_error_reporter
Format.err_formatter
~src
~startPos:d.startPos
~endPos:d.endPos
~msg:(explain d)
);
print (List.rev diagnostics) src;
Format.fprintf Format.err_formatter "@]@."

let unexpected token context =
Expand Down
4 changes: 0 additions & 4 deletions src/res_doc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,6 @@ let toString ~width doc =
end
in
process ~pos:0 [] [0, Flat, doc];

let len = MiniBuffer.length buffer in
if len > 0 && MiniBuffer.unsafe_get buffer (len - 1) != '\n' then
MiniBuffer.add_char buffer '\n';
MiniBuffer.contents buffer


Expand Down
7 changes: 1 addition & 6 deletions src/res_minibuffer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,6 @@ let create n =

let contents b = Bytes.sub_string b.buffer 0 b.position

let unsafe_get b ofs =
Bytes.unsafe_get b.buffer ofs

let length b = b.position

(* Can't be called directly, don't add to the interface *)
let resize_internal b more =
let len = b.length in
Expand Down Expand Up @@ -52,4 +47,4 @@ let flush_newline b =
position := !position - 1;
done;
b.position <- !position;
add_char b '\n'
add_char b '\n'
3 changes: 0 additions & 3 deletions src/res_minibuffer.mli
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
type t
val add_char : t -> char -> unit
val add_string : t -> string -> unit
val contents : t -> string
val create : int -> t
val flush_newline : t -> unit
val length : t -> int
val unsafe_get : t -> int -> char
4 changes: 2 additions & 2 deletions src/res_printer.ml
Original file line number Diff line number Diff line change
Expand Up @@ -5004,9 +5004,9 @@ let printImplementation ~width (s: Parsetree.structure) ~comments =
(* CommentTable.log cmtTbl; *)
let doc = printStructure s cmtTbl in
(* Doc.debug doc; *)
Doc.toString ~width doc
Doc.toString ~width doc ^ "\n"

let printInterface ~width (s: Parsetree.signature) ~comments =
let cmtTbl = CommentTable.make () in
CommentTable.walkSignature s cmtTbl comments;
Doc.toString ~width (printSignature s cmtTbl)
Doc.toString ~width (printSignature s cmtTbl) ^ "\n"
2 changes: 1 addition & 1 deletion tests/oprint/oprint.res.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,4 @@ type t21 = [#\"va r ia nt"]
type t22 = [#\"Variant ⛰"]
type \"let" = int
type \"type" = [#\"Point🗿"(\"let", float)]
type exoticUser = {\"let": string, \"type": float}
type exoticUser = {\"let": string, \"type": float}
18 changes: 15 additions & 3 deletions tests/parsing/errors/expressions/__snapshots__/parse.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ let x = ((let a = 1 in let b = 2 in fun pattern -> (\\"test\\" : int))
2) (pattern: int) => \\"test\\"



========================================================"
`;

Expand Down Expand Up @@ -108,6 +107,7 @@ let pipeline =

Did you forget a \`)\` here?


Syntax error!
parsing/errors/expressions/block.js:18:25-19:1
16 │
Expand All @@ -119,6 +119,7 @@ let pipeline =

Did you forget a \`)\` here?


Syntax error!
parsing/errors/expressions/block.js:22:11-23:1
20 │
Expand All @@ -130,6 +131,7 @@ let pipeline =

Looks like there might be an expression missing here


Syntax error!
parsing/errors/expressions/block.js:26:7-27:1
24 │ | Join(doc1, doc2) =>
Expand All @@ -141,6 +143,7 @@ let pipeline =

Did you forget a \`)\` here?


Syntax error!
parsing/errors/expressions/block.js:30:10-31:1
28 │
Expand Down Expand Up @@ -173,6 +176,7 @@ let () = ((let open Foo in let exception End in x ())[@ns.braces ])

consecutive statements on a line must be separated by ';' or a newline


Syntax error!
parsing/errors/expressions/consecutive.res:4:7
2 │
Expand All @@ -183,6 +187,7 @@ let () = ((let open Foo in let exception End in x ())[@ns.braces ])

consecutive expressions on a line must be separated by ';' or a newline


Syntax error!
parsing/errors/expressions/consecutive.res:8:16-27
6 │
Expand All @@ -193,6 +198,7 @@ let () = ((let open Foo in let exception End in x ())[@ns.braces ])

consecutive expressions on a line must be separated by ';' or a newline


Syntax error!
parsing/errors/expressions/consecutive.res:12:11-20
10 │
Expand Down Expand Up @@ -221,6 +227,7 @@ let f a b = ((())[@ns.braces ])

This let-binding misses an expression


Syntax error!
parsing/errors/expressions/emptyBlock.js:3:20
1 │ let x = {}
Expand Down Expand Up @@ -299,7 +306,6 @@ switch result {
}



========================================================"
`;

Expand Down Expand Up @@ -351,6 +357,7 @@ let x =

I'm not sure what to parse here when looking at \\"-\\".


Syntax error!
parsing/errors/expressions/jsx.js:2:20
1 │ let x = <di-v />
Expand All @@ -360,6 +367,7 @@ let x =

Did you forget a \`</\` here?


Syntax error!
parsing/errors/expressions/jsx.js:3:9-28
1 │ let x = <di-v />
Expand All @@ -370,6 +378,7 @@ let x =

Missing </Foo.Bar>


Syntax error!
parsing/errors/expressions/jsx.js:4:9-34
2 │ let x = <Unclosed >;
Expand All @@ -380,6 +389,7 @@ let x =

Missing </Foo.Bar.Baz>


Syntax error!
parsing/errors/expressions/jsx.js:5:9-27
3 │ let x = <Foo.Bar></Free.Will>;
Expand All @@ -390,6 +400,7 @@ let x =

Missing </Foo.bar>


Syntax error!
parsing/errors/expressions/jsx.js:6:17
4 │ let x = <Foo.Bar.Baz></Foo.Bar.Boo>
Expand Down Expand Up @@ -436,6 +447,7 @@ let record = { field = ([%rescript.exprhole ]) }

Did you forget a \`,\` here?


Syntax error!
parsing/errors/expressions/record.js:8:10-18
6 │
Expand All @@ -446,6 +458,7 @@ let record = { field = ([%rescript.exprhole ]) }

Did you forget a \`:\` here?


Syntax error!
parsing/errors/expressions/record.js:13:9-17:0
11 │
Expand Down Expand Up @@ -547,6 +560,5 @@ let x = (\\"hi\\" : string)
(\\"hi\\": string)



========================================================"
`;
5 changes: 5 additions & 0 deletions tests/parsing/errors/other/__snapshots__/parse.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ type nonrec 'extraInfo student =

Did you forget a \`,\` here?


Syntax error!
parsing/errors/other/regionMissingComma.res:8:11-9:11
6 │ type student<'extraInfo> = {
Expand Down Expand Up @@ -71,6 +72,7 @@ let x::y = myList

Arrays can't use the \`...\` spread currently. Please use \`concat\` or other Array helpers.


Syntax error!
parsing/errors/other/spread.js:2:6-8
1 │ let arr = [...x, ...y]
Expand All @@ -82,6 +84,7 @@ let x::y = myList
Explanation: such spread would create a subarray; out of performance concern, our pattern matching currently guarantees to never create new intermediate data.
Solution: if it's to validate the first few elements, use a \`when\` clause + Array size check + \`get\` checks on the current pattern. If it's to obtain a subarray, use \`Array.sub\` or \`Belt.Array.slice\`.


Syntax error!
parsing/errors/other/spread.js:4:21-23
2 │ let [...arr, _] = [1, 2, 3]
Expand All @@ -93,6 +96,7 @@ Solution: if it's to validate the first few elements, use a \`when\` clause + Ar
Records can only have one \`...\` spread, at the beginning.
Explanation: since records have a known, fixed shape, a spread like \`{a, ...b}\` wouldn't make sense, as \`b\` would override every field of \`a\` anyway.


Syntax error!
parsing/errors/other/spread.js:5:15-18
3 │
Expand All @@ -105,6 +109,7 @@ Explanation: since records have a known, fixed shape, a spread like \`{a, ...b}\
Explanation: you can't collect a subset of a record's field into its own record, since a record needs an explicit declaration and that subset wouldn't have one.
Solution: you need to pull out each field you want explicitly.


Syntax error!
parsing/errors/other/spread.js:8:13-22
6 │
Expand Down
3 changes: 3 additions & 0 deletions tests/parsing/errors/pattern/__snapshots__/parse.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ let 4 = for [%rescript.patternhole ] = 0 to 10 do Js.log \\"for\\" done

I was expecting a name for this let-binding. Example: \`let message = \\"hello\\"\`


Syntax error!
parsing/errors/pattern/missing.res:2:5
1 │ let = 2
Expand All @@ -24,6 +25,7 @@ let 4 = for [%rescript.patternhole ] = 0 to 10 do Js.log \\"for\\" done

I was expecting a name for this let-binding. Example: \`let message = \\"hello\\"\`


Syntax error!
parsing/errors/pattern/missing.res:4:5-6
2 │ let = 4
Expand All @@ -34,6 +36,7 @@ let 4 = for [%rescript.patternhole ] = 0 to 10 do Js.log \\"for\\" done

A for-loop has the following form: \`for i in 0 to 10\`. Did you forget to supply a name before \`in\`?


Syntax error!
parsing/errors/pattern/missing.res:9:3-4
7 │
Expand Down
2 changes: 2 additions & 0 deletions tests/parsing/errors/scanner/__snapshots__/parse.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ let x = \\"\\\\\\\\0AAA\\"

unknown escape sequence


Syntax error!
parsing/errors/scanner/escapeSequence.js:3:10-11
1 │ let x = \\"\\\\0\\"
Expand Down Expand Up @@ -42,6 +43,7 @@ let newX = x +. (newVelocity *. secondPerFrame)
Hmm, not sure what I should do here with this character.
If you're trying to deref an expression, use \`foo.contents\` instead.


Syntax error!
parsing/errors/scanner/oldDerefOp.js:2:46
1 │ let newVelocity = velocity +. a *. secondPerFrame^;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ val z2 : float[@@genType ]

consecutive specifications on a line must be separated by ';' or a newline


Syntax error!
parsing/errors/signature/consecutive.resi:3:15-21
1 │ let x: int let y: float
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ let (x : int1) = (int2 = 3 :> int3)

Did you forget a \`=\` here?


Syntax error!
parsing/errors/structure/letBinding.js:8:12-13
6 │ let x = {
Expand All @@ -132,6 +133,7 @@ let (x : int1) = (int2 = 3 :> int3)

Did you forget a \`=\` here?


Syntax error!
parsing/errors/structure/letBinding.js:14:19-26
12 │ let t = {
Expand All @@ -142,6 +144,7 @@ let (x : int1) = (int2 = 3 :> int3)

Did you forget a \`=\` here?


Syntax error!
parsing/errors/structure/letBinding.js:18:39-20:3
16 │ }
Expand All @@ -154,6 +157,7 @@ let (x : int1) = (int2 = 3 :> int3)

This let-binding misses an expression


Syntax error!
parsing/errors/structure/letBinding.js:20:10-23:3
18 │ let keyTable: Belt.Map.String.t<int> =
Expand All @@ -167,6 +171,7 @@ let (x : int1) = (int2 = 3 :> int3)

This let-binding misses an expression


Syntax error!
parsing/errors/structure/letBinding.js:23:11-13
21 │
Expand All @@ -177,6 +182,7 @@ let (x : int1) = (int2 = 3 :> int3)

Did you forget a \`=\` here?


Syntax error!
parsing/errors/structure/letBinding.js:26:6-8
24 │
Expand All @@ -187,6 +193,7 @@ let (x : int1) = (int2 = 3 :> int3)

Did you forget a \`=\` here?


Syntax error!
parsing/errors/structure/letBinding.js:29:13-15
27 │
Expand Down Expand Up @@ -214,6 +221,7 @@ let [%rescript.patternhole ] = 3

\`open\` is a reserved keyword. Keywords need to be escaped: \\\\\\"open\\"


Syntax error!
parsing/errors/structure/letBindingPatternKeyword.js:2:5-6
1 │ let open = 1
Expand Down
Loading