Skip to content

Commit 370f1cf

Browse files
committed
Alternate method of invalid light table syntax
1 parent ffa35ff commit 370f1cf

File tree

4 files changed

+34
-45
lines changed

4 files changed

+34
-45
lines changed

src/loc.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,11 @@ let span spans =
2222

2323
let nudge_start offset span =
2424
{ span with start = { span.start with column = span.start.column + offset } }
25+
26+
let spans_multiple_lines = function
27+
| {
28+
location =
29+
{ start = { line = start_line; _ }; end_ = { line = end_line; _ }; _ };
30+
_;
31+
} ->
32+
end_line > start_line

src/loc.mli

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,7 @@ val map : ('a -> 'b) -> 'a with_location -> 'b with_location
3939
val same : _ with_location -> 'b -> 'b with_location
4040
(** [same x y] retuns the value y wrapped in a {!with_location} whose
4141
location is that of [x] *)
42+
43+
val spans_multiple_lines : _ with_location -> bool
44+
(** [spans_multiple_lines x] checks to see whether [x] is located
45+
on a single line or whether it covers more than one. *)

src/syntax.ml

Lines changed: 21 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,6 @@ type token_that_always_begins_an_inline_element =
162162
let _check_subset : token_that_always_begins_an_inline_element -> Token.t =
163163
fun t -> (t :> Token.t)
164164

165-
(* The different contexts in which the inline parser [inline_element] and
166-
[delimited_inline_parser] can be called. The inline parser's behavior depends
167-
somewhat on the context: new lines are forbidden in light tables. *)
168-
type inline_context = In_light_table | Outside_light_table
169-
170165
(* Consumes tokens that make up a single non-link inline element:
171166
172167
- a horizontal space ([`Space], significant in inline elements),
@@ -188,12 +183,8 @@ type inline_context = In_light_table | Outside_light_table
188183
189184
This function consumes exactly the tokens that make up the element. *)
190185
let rec inline_element :
191-
input ->
192-
Loc.span ->
193-
context:inline_context ->
194-
_ ->
195-
Ast.inline_element with_location =
196-
fun input location ~context next_token ->
186+
input -> Loc.span -> _ -> Ast.inline_element with_location =
187+
fun input location next_token ->
197188
match next_token with
198189
| `Space _ as token ->
199190
junk input;
@@ -226,8 +217,7 @@ let rec inline_element :
226217
in
227218
let content, brace_location =
228219
delimited_inline_element_list ~parent_markup
229-
~parent_markup_location:location ~requires_leading_whitespace ~context
230-
input
220+
~parent_markup_location:location ~requires_leading_whitespace input
231221
in
232222

233223
let location = Loc.span [ location; brace_location ] in
@@ -255,7 +245,7 @@ let rec inline_element :
255245
let content, brace_location =
256246
delimited_inline_element_list ~parent_markup
257247
~parent_markup_location:location ~requires_leading_whitespace:false
258-
~context input
248+
input
259249
in
260250

261251
let location = Loc.span [ location; brace_location ] in
@@ -293,7 +283,7 @@ let rec inline_element :
293283
let content, brace_location =
294284
delimited_inline_element_list ~parent_markup
295285
~parent_markup_location:location ~requires_leading_whitespace:false
296-
~context input
286+
input
297287
in
298288

299289
`Link (u, content) |> Loc.at (Loc.span [ location; brace_location ])
@@ -324,11 +314,9 @@ and delimited_inline_element_list :
324314
parent_markup:[< Token.t ] ->
325315
parent_markup_location:Loc.span ->
326316
requires_leading_whitespace:bool ->
327-
context:inline_context ->
328317
input ->
329318
Ast.inline_element with_location list * Loc.span =
330-
fun ~parent_markup ~parent_markup_location ~requires_leading_whitespace
331-
~context input ->
319+
fun ~parent_markup ~parent_markup_location ~requires_leading_whitespace input ->
332320
(* [~at_start_of_line] is used to interpret [`Minus] and [`Plus]. These are
333321
word tokens if not the first non-whitespace tokens on their line. Then,
334322
they are allowed in a non-link element list. *)
@@ -351,17 +339,10 @@ and delimited_inline_element_list :
351339
it is an internal space, and we want to add it to the non-link inline
352340
element list. *)
353341
| (`Space _ | #token_that_always_begins_an_inline_element) as token ->
354-
let acc =
355-
inline_element input next_token.location ~context token :: acc
356-
in
342+
let acc = inline_element input next_token.location token :: acc in
357343
consume_elements ~at_start_of_line:false acc
358-
| `Single_newline ws as blank ->
344+
| `Single_newline ws ->
359345
junk input;
360-
if context = In_light_table then
361-
Parse_error.not_allowed ~what:(Token.describe blank)
362-
~in_what:(Token.describe `Begin_table_light)
363-
next_token.location
364-
|> add_warning input;
365346
let element = Loc.same next_token (`Space ws) in
366347
consume_elements ~at_start_of_line:true (element :: acc)
367348
| `Blank_line ws as blank ->
@@ -374,9 +355,7 @@ and delimited_inline_element_list :
374355
let element = Loc.same next_token (`Space ws) in
375356
consume_elements ~at_start_of_line:true (element :: acc)
376357
| `Bar as token ->
377-
let acc =
378-
inline_element input next_token.location ~context token :: acc
379-
in
358+
let acc = inline_element input next_token.location token :: acc in
380359
consume_elements ~at_start_of_line:false acc
381360
| (`Minus | `Plus) as bullet ->
382361
(if at_start_of_line then
@@ -389,9 +368,7 @@ and delimited_inline_element_list :
389368
~suggestion next_token.location
390369
|> add_warning input);
391370

392-
let acc =
393-
inline_element input next_token.location ~context bullet :: acc
394-
in
371+
let acc = inline_element input next_token.location bullet :: acc in
395372
consume_elements ~at_start_of_line:false acc
396373
| other_token ->
397374
Parse_error.not_allowed
@@ -473,10 +450,7 @@ let paragraph : input -> Ast.nestable_block_element with_location =
473450
match next_token.value with
474451
| ( `Space _ | `Minus | `Plus | `Bar
475452
| #token_that_always_begins_an_inline_element ) as token ->
476-
let element =
477-
inline_element input next_token.location ~context:Outside_light_table
478-
token
479-
in
453+
let element = inline_element input next_token.location token in
480454
paragraph_line (element :: acc)
481455
| _ -> acc
482456
in
@@ -1023,7 +997,7 @@ let rec block_element_list :
1023997
let content, brace_location =
1024998
delimited_inline_element_list ~parent_markup:token
1025999
~parent_markup_location:location ~requires_leading_whitespace:true
1026-
~context:Outside_light_table input
1000+
input
10271001
in
10281002
let location = Loc.span [ location; brace_location ] in
10291003
let paragraph =
@@ -1063,8 +1037,7 @@ let rec block_element_list :
10631037
let content, brace_location =
10641038
delimited_inline_element_list ~parent_markup:token
10651039
~parent_markup_location:location
1066-
~requires_leading_whitespace:true ~context:Outside_light_table
1067-
input
1040+
~requires_leading_whitespace:true input
10681041
in
10691042
if content = [] then
10701043
Parse_error.should_not_be_empty ~what:(Token.describe token)
@@ -1081,7 +1054,7 @@ let rec block_element_list :
10811054
let content, brace_location =
10821055
delimited_inline_element_list ~parent_markup:token
10831056
~parent_markup_location:location ~requires_leading_whitespace:true
1084-
~context:Outside_light_table input
1057+
input
10851058
in
10861059
let location = Loc.span [ location; brace_location ] in
10871060

@@ -1307,9 +1280,13 @@ and light_table_row ~parent_markup ~last_loc input =
13071280
let acc_row = if new_line then [] else List.rev acc_cell :: acc_row in
13081281
consume_row acc_row [] ~new_line:false ~last_loc
13091282
| #token_that_always_begins_an_inline_element as token ->
1310-
let i =
1311-
inline_element input next_token.location ~context:In_light_table token
1312-
in
1283+
let i = inline_element input next_token.location token in
1284+
if Loc.spans_multiple_lines i then
1285+
Parse_error.not_allowed
1286+
~what:(Token.describe (`Single_newline ""))
1287+
~in_what:(Token.describe `Begin_table_light)
1288+
i.location
1289+
|> add_warning input;
13131290
consume_row acc_row (i :: acc_cell) ~new_line:false
13141291
~last_loc:next_token.location
13151292
| other_token ->

test/test_tables.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ let%expect_test _ =
546546
(paragraph (((f.ml (5 32) (5 37)) (code_span foo)))))))))))
547547
(align (default default))))))
548548
(warnings
549-
( "File \"f.ml\", line 4, character 18 to line 5, character 14:\
549+
( "File \"f.ml\", line 4, character 11 to line 5, character 23:\
550550
\nLine break is not allowed in '{t ...}' (table)."))) |}]
551551

552552
let no_space =

0 commit comments

Comments
 (0)