Skip to content

Commit

Permalink
fix(ocamlc_loc): parse alerts
Browse files Browse the repository at this point in the history
correctly parse alerts and add a new severity kind

Signed-off-by: Rudi Grinberg <me@rgrinberg.com>

ps-id: 4ec7db6f-4026-4936-991c-fcfc4d535ba2
  • Loading branch information
rgrinberg committed Dec 12, 2022
1 parent 4dff505 commit 752c4c2
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
Unreleased
----------

- Fix the parsing of alerts. They will now show up in diagnostics correctly.
(#6678, @rginberg)

- Fix the compilation of modules generated at link time when
`implicit_transitive_deps` is enabled (#6642, @rgrinberg)

Expand Down
4 changes: 4 additions & 0 deletions otherlibs/ocamlc_loc/src/lexer.mli
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ type source =
type severity =
| Error of source option
| Warning of source
| Alert of
{ name : string
; source : string
}

type loc =
{ chars : (int * int) option
Expand Down
8 changes: 7 additions & 1 deletion otherlibs/ocamlc_loc/src/lexer.mll
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
type severity =
| Error of source option
| Warning of source
| Alert of { name : string ; source : string }

type loc =
{ chars : (int * int) option
Expand Down Expand Up @@ -45,6 +46,8 @@ let range = digits "-" digits

let any = _ *

let alert_name = ['a' - 'z'] ['A' - 'Z' 'a' - 'z' '0' - '9' '_']*

rule skip_excerpt = parse
| blank digits " | " [^ '\n']* "\n"?
{ `Continue }
Expand All @@ -66,9 +69,12 @@ and severity = parse
(blank any as rest)
{ Some (Error (Some (Code { code = int_of_string code ; name })), rest)
}
| "Alert " blank (alert_name as name) ":" (any as source)
{ Some (Alert { name ; source }, "")
}
| (("Error" | "Warning") as kind) " (alert " ([^ ')']+ as alert) "):"
(blank any as rest)
{ let alert = Alert alert in
{ let alert : source = Alert alert in
let res =
match kind with
| "Error" -> Error (Some alert)
Expand Down
5 changes: 5 additions & 0 deletions otherlibs/ocamlc_loc/src/ocamlc_loc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ let dyn_of_severity =
function
| Error w -> variant "Error" [ option dyn_of_source w ]
| Warning w -> variant "Warning" [ dyn_of_source w ]
| Alert { name; source } ->
variant "Alert"
[ record [ ("name", string name); ("source", string source) ] ]

let dyn_of_loc { path; lines; chars } =
let open Dyn in
Expand Down Expand Up @@ -86,6 +89,8 @@ end
let indent_of_severity = function
| Error _ -> String.length "Error: "
| Warning _ -> String.length "Warning: "
| Alert { name; source } ->
String.length "Alert :" + String.length name + String.length source + 1

let severity tokens =
match Tokens.peek tokens with
Expand Down
4 changes: 4 additions & 0 deletions otherlibs/ocamlc_loc/src/ocamlc_loc.mli
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ type loc =
type severity =
| Error of source option
| Warning of source
| Alert of
{ name : string
; source : string
}

type report =
{ loc : loc
Expand Down
18 changes: 16 additions & 2 deletions otherlibs/ocamlc_loc/test/ocamlc_loc_tests.ml
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,14 @@ File "foo.ml", line 8, characters 9-12:
Alert deprecated: A.f
foo
|};
[%expect {||}];
[%expect
{|
>> error 0
{ loc = { path = "foo.ml"; line = Single 8; chars = Some (9, 12) }
; message = "foo"
; related = []
; severity = Alert { name = "deprecated"; source = " A.f" }
} |}];
test_error
{|
File "foo.ml", line 8, characters 9-12:
Expand All @@ -409,4 +416,11 @@ File "foo.ml", line 8, characters 9-12:
Alert foobar: A.f
blah
|};
[%expect {||}]
[%expect
{|
>> error 0
{ loc = { path = "foo.ml"; line = Single 8; chars = Some (9, 12) }
; message = "blah"
; related = []
; severity = Alert { name = "foobar"; source = " A.f" }
} |}]

0 comments on commit 752c4c2

Please sign in to comment.