Skip to content

Commit b2a18fc

Browse files
committed
Include comment body in tokens
A boring change on its own, but should make it easier to flatten the trivia structure in the future.
1 parent 8becb81 commit b2a18fc

File tree

9 files changed

+50
-38
lines changed

9 files changed

+50
-38
lines changed

src/core/emit.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -291,10 +291,7 @@ let tprintf kind out fmt =
291291
Format.kfprintf (fun x -> Format.pp_close_stag x ()) out fmt
292292

293293
let trivial out = function
294-
| Node.LineComment x -> tprintf Comment out "--%s" x
295-
| BlockComment (n, x) ->
296-
let eq = String.make n '=' in
297-
tprintf Comment out "--[%s[%s]%s]" eq x eq
294+
| Node.LineComment x | BlockComment x -> tprintf Comment out "%s" x
298295
| Whitespace x -> Format.fprintf out "%s" x
299296

300297
let trivial_span out { Span.value; _ } = trivial out value

src/core/node.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
may be useful for understanding or recreating it. *)
88
type trivial =
99
| LineComment of string (** A short, comment which is terminated by a newline. *)
10-
| BlockComment of int * string (** A long comment, which may span multiple lines. *)
10+
| BlockComment of string (** A long comment, which may span multiple lines. *)
1111
| Whitespace of string (** Any whitespace, such as spaces, newlines or tabs. *)
1212
[@@deriving show]
1313

src/lint/lint_bracket_spacing.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ module ActualSpacing = struct
2727
let rec get t : Node.trivial Span.spanned list -> t = function
2828
| [] -> t
2929
| { value = LineComment _; _ } :: _ -> Newline
30-
| { value = BlockComment (_, s) | Whitespace s; _ } :: _ when String.contains s '\n' -> Newline
30+
| { value = BlockComment s | Whitespace s; _ } :: _ when String.contains s '\n' -> Newline
3131
| { value = BlockComment _; _ } :: xs -> get t xs
3232
| { value = Whitespace _; _ } :: xs -> get Space xs
3333

src/parser/lexer.mll

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,13 @@
2424
Buffer.add_char b char;
2525
b
2626

27-
let mk_long_comment eqs c = Trivial (BlockComment (eqs, c))
28-
let mk_long_string eqs c =
29-
let eqs = String.make eqs '=' in
30-
Token (String ("[" ^ eqs ^ "[" ^ c ^ "]" ^ eqs ^ "]"))
27+
let buffer_with' len str =
28+
let b = Buffer.create len in
29+
Buffer.add_string b str;
30+
b
31+
32+
let mk_long_comment c = Trivial (BlockComment c)
33+
let mk_long_string c = Token (String c)
3134
}
3235

3336
let white = [' ' '\t']
@@ -44,7 +47,7 @@ rule token l = parse
4447
| white+ as x { Trivial (Whitespace x) }
4548
| '\n' { new_line l; Trivial (Whitespace "\n") }
4649
| '\r' '\n' { new_line l; Trivial (Whitespace "\r\n") }
47-
| "--[" ('='* as x) '[' { long_string (Buffer.create 16) (String.length x) mk_long_comment l lexbuf }
50+
| ("--[" '='* '[') as x { long_string (buffer_with' 16 x) (String.length x - 4) mk_long_comment l lexbuf }
4851
(* We split line comments into two parts. Otherwise "--[^\n]*" would match "--[[foo]]". *)
4952
| "--" { line_comment lexbuf }
5053

@@ -108,7 +111,7 @@ rule token l = parse
108111

109112
| '\"' { string (buffer_with 17 '\"') '\"' lexbuf }
110113
| '\'' { string (buffer_with 17 '\'') '\'' lexbuf }
111-
| '[' ('='* as x) '[' { long_string (Buffer.create 16) (String.length x) mk_long_string l lexbuf }
114+
| ('[' '='* '[') as x { long_string (buffer_with' 16 x) (String.length x - 2) mk_long_string l lexbuf }
112115

113116
| eof { Token EoF }
114117

@@ -155,13 +158,14 @@ and string contents c = parse
155158

156159
and long_string buf eqs term l = parse
157160
| [^']' '\r' '\n']+ as x { Buffer.add_string buf x; long_string buf eqs term l lexbuf }
158-
| ']' '='* ']' as x { if String.length x == eqs + 2
159-
then term eqs (Buffer.contents buf)
160-
else (Buffer.add_string buf x; long_string buf eqs term l lexbuf) }
161+
| ']' '='* ']' as x { Buffer.add_string buf x;
162+
if String.length x == eqs + 2
163+
then term (Buffer.contents buf)
164+
else long_string buf eqs term l lexbuf }
161165
| ']' { Buffer.add_char buf ']'; long_string buf eqs term l lexbuf }
162166
| '\n' { Buffer.add_char buf '\n'; new_line l; long_string buf eqs term l lexbuf }
163167
| '\r' '\n' { Buffer.add_string buf "\r\n"; new_line l; long_string buf eqs term l lexbuf }
164168
| eof { unterminated_string ~eol:false lexbuf }
165169

166170
and line_comment = parse
167-
| [^'\r' '\n']* as x { Trivial (LineComment x) }
171+
| [^'\r' '\n']* as x { Trivial (LineComment ("--" ^ x)) }

src/semantics/doc_parser.ml

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -802,34 +802,45 @@ let rec extract_block line column lines = function
802802
(* Skip whitespace *)
803803
| { Span.value = Node.Whitespace _; _ } :: xs -> extract_block line column lines xs
804804
(* Comments aligned with this one on successive lines are included *)
805-
| { Span.value = Node.LineComment value; span } :: xs
805+
| { Span.value = Node.LineComment contents; span } :: xs
806806
when Span.start_line span = line + 1 && Span.start_col span = column ->
807807
let span = span |> Illuaminate.Lens.(Span.start_offset %= fun p -> p + 2) in
808+
let value = CCString.drop 2 contents in
808809
extract_block (line + 1) column ({ Span.span; value } :: lines) xs
809810
| xs -> (List.rev lines, (List.hd lines).span, xs)
810811

812+
let get_block_comment_start str =
813+
let rec worker str i =
814+
match str.[i] with
815+
| '=' -> worker str (i + 1)
816+
| '[' -> i + 1
817+
| _ -> failwith "Invalid block comment"
818+
in
819+
worker str 3
820+
811821
let extract node =
812822
(* Extract all comments before this token *)
813823
let rec extract_comments cs = function
814824
| [] -> cs
815-
| { Span.value = Node.BlockComment (n, c); span } :: xs when String.length c > 0 && c.[0] == '-'
816-
->
817-
let documented =
818-
split_lines (CCString.drop 1 c)
819-
(Illuaminate.Lens.(Span.start_offset %= fun p -> p + n + 5) span)
820-
|> Indent.drop_rest |> Lex.lex_of_lines |> Parse.comment span
821-
in
822-
extract_comments (documented :: cs) xs
823-
| { Span.value = Node.LineComment c; span } :: xs
824-
when String.length c > 0
825-
&& c.[0] == '-'
826-
&&
825+
| { Span.value = Node.BlockComment contents; span } :: xs ->
826+
let start = get_block_comment_start contents in
827+
if contents.[start] == '-' then
828+
let documented =
829+
split_lines
830+
(String.sub contents (start + 1) (String.length contents - start - (start - 1)))
831+
(Illuaminate.Lens.(Span.start_offset %= fun p -> p + start + 1) span)
832+
|> Indent.drop_rest |> Lex.lex_of_lines |> Parse.comment span
833+
in
834+
extract_comments (documented :: cs) xs
835+
else extract_comments cs xs
836+
| { Span.value = Node.LineComment contents; span } :: xs
837+
when String.starts_with ~prefix:"---" contents
827838
(* Skip comments which start with a line entirely composed of '-'. *)
828-
(String.length c = 1 || CCString.exists (fun x -> x <> '-') c) ->
839+
&& (String.length contents = 3 || CCString.exists (fun x -> x <> '-') contents) ->
829840
let lines, last, xs =
830841
extract_block (Span.start_line span) (Span.start_col span)
831842
[ { span = (span |> Illuaminate.Lens.(Span.start_offset %= fun p -> p + 3));
832-
value = CCString.drop 1 c
843+
value = CCString.drop 3 contents
833844
}
834845
]
835846
xs

test/data/parser/pass_multiline_string.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
{ Syntax.local_local =
44
Node.Node {
55
leading_trivia =
6-
[{ Span.value = (Node.BlockComment (0, " One line "));
6+
[{ Span.value = (Node.BlockComment "--[[ One line ]]");
77
span = pass_multiline_string.lua[1:1-1:16] };
88
{ Span.value = (Node.Whitespace "\n");
99
span = pass_multiline_string.lua[1:17-1:17] };
1010
{ Span.value = (Node.Whitespace "\n");
1111
span = pass_multiline_string.lua[2:1-2:1] };
12-
{ Span.value = (Node.BlockComment (0, "\nMulti-line\n"));
12+
{ Span.value = (Node.BlockComment "--[[\nMulti-line\n]]");
1313
span = pass_multiline_string.lua[3:1-5:2] };
1414
{ Span.value = (Node.Whitespace "\n");
1515
span = pass_multiline_string.lua[5:3-5:3] };

test/data/parser/pass_simple.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
trailing_trivia =
5050
[{ Span.value = (Node.Whitespace " ");
5151
span = pass_simple.lua[1:28-1:28] };
52-
{ Span.value = (Node.LineComment "- @local");
52+
{ Span.value = (Node.LineComment "--- @local");
5353
span = pass_simple.lua[1:29-1:38] };
5454
{ Span.value = (Node.Whitespace "\n");
5555
span = pass_simple.lua[1:39-1:39] }

test/lexer.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ let tests =
4343
[| Token (Ident "x");
4444
Trivial (Whitespace "\r\n");
4545
Token (Ident "y");
46-
Trivial (LineComment "foo");
46+
Trivial (LineComment "--foo");
4747
Trivial (Whitespace "\r\n");
4848
Token (String "[[x\r\ny]]");
4949
Token EoF

test/lint/fragment_linter.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ let schema =
1919
|> Schema.union (Schema.singleton Doc.Extract.Config.key)
2020

2121
let parse_schema : Node.trivial Span.spanned -> _ = function
22-
| { value = LineComment c | BlockComment (_, c); _ } ->
23-
c
24-
|> CCString.drop_while (fun c -> c == '-')
22+
| { value = LineComment contents | BlockComment contents; _ } ->
23+
contents
24+
|> CCString.drop_while (fun c -> c == '-' || c == '[')
2525
|> String.trim
2626
|> CCString.chop_prefix ~pre:"config:"
2727
|> Option.map @@ fun c ->

0 commit comments

Comments
 (0)