Skip to content

Commit b481dc9

Browse files
gilthojonludlam
authored andcommitted
Lexer readability of Math Kind
Signed-off-by: Sacha Ayoun <[email protected]>
1 parent 05a04c2 commit b481dc9

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

src/lexer.mll

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,13 @@ let unescape_word : string -> string = fun s ->
2727
scan_word 0;
2828
Buffer.contents buffer
2929

30-
let math_constr inline x = if inline then `Math_span x else `Math_block x
30+
type math_kind =
31+
Inline | Block
32+
33+
let math_constr kind x =
34+
match kind with
35+
| Inline -> `Math_span x
36+
| Block -> `Math_block x
3137

3238
(* This is used for code and verbatim blocks. It can be done with a regular
3339
expression, but the regexp gets quite ugly, so a function is easier to
@@ -322,10 +328,10 @@ rule token input = parse
322328
{ emit input (`Begin_style `Subscript) }
323329

324330
| "{math" space_char
325-
{ math false (Buffer.create 1024) 0 (Lexing.lexeme_start lexbuf) input lexbuf }
331+
{ math Block (Buffer.create 1024) 0 (Lexing.lexeme_start lexbuf) input lexbuf }
326332

327333
| "{m" horizontal_space
328-
{ math true (Buffer.create 1024) 0 (Lexing.lexeme_start lexbuf) input lexbuf }
334+
{ math Inline (Buffer.create 1024) 0 (Lexing.lexeme_start lexbuf) input lexbuf }
329335

330336

331337
| "{!modules:" ([^ '}']* as modules) '}'
@@ -546,49 +552,46 @@ and code_span buffer nesting_level start_offset input = parse
546552
{ Buffer.add_char buffer c;
547553
code_span buffer nesting_level start_offset input lexbuf }
548554

549-
and math inline buffer nesting_level start_offset input = parse
555+
and math kind buffer nesting_level start_offset input = parse
550556
| '}'
551557
{ if nesting_level == 0 then
552-
emit input (math_constr inline (Buffer.contents buffer)) ~start_offset
558+
emit input (math_constr kind (Buffer.contents buffer)) ~start_offset
553559
else begin
554560
Buffer.add_char buffer '}';
555-
math inline buffer (nesting_level - 1) start_offset input lexbuf
561+
math kind buffer (nesting_level - 1) start_offset input lexbuf
556562
end
557563
}
558564
| '{'
559565
{ Buffer.add_char buffer '{';
560-
math inline buffer (nesting_level + 1) start_offset input lexbuf }
566+
math kind buffer (nesting_level + 1) start_offset input lexbuf }
561567
| ("\\{" | "\\}") as s
562568
{ Buffer.add_string buffer s;
563-
math inline buffer nesting_level start_offset input lexbuf }
569+
math kind buffer nesting_level start_offset input lexbuf }
564570
| (newline) as s
565571
{
566-
if inline then
567-
begin
568-
warning
569-
input
570-
(Parse_error.not_allowed
571-
~what:(Token.describe (`Blank_line "\n"))
572-
~in_what:(Token.describe (math_constr inline "")));
573-
Buffer.add_char buffer '\n';
574-
math inline buffer nesting_level start_offset input lexbuf
575-
end
576-
else
577-
begin
578-
Buffer.add_string buffer s;
579-
math inline buffer nesting_level start_offset input lexbuf
580-
end
572+
match kind with
573+
| Inline ->
574+
warning
575+
input
576+
(Parse_error.not_allowed
577+
~what:(Token.describe (`Blank_line "\n"))
578+
~in_what:(Token.describe (math_constr kind "")));
579+
Buffer.add_char buffer '\n';
580+
math kind buffer nesting_level start_offset input lexbuf
581+
| Block ->
582+
Buffer.add_string buffer s;
583+
math kind buffer nesting_level start_offset input lexbuf
581584
}
582585
| eof
583586
{ warning
584587
input
585588
(Parse_error.not_allowed
586589
~what:(Token.describe `End)
587-
~in_what:(Token.describe (math_constr inline "")));
588-
emit input (math_constr inline (Buffer.contents buffer)) ~start_offset }
590+
~in_what:(Token.describe (math_constr kind "")));
591+
emit input (math_constr kind (Buffer.contents buffer)) ~start_offset }
589592
| _ as c
590593
{ Buffer.add_char buffer c;
591-
math inline buffer nesting_level start_offset input lexbuf }
594+
math kind buffer nesting_level start_offset input lexbuf }
592595

593596
and verbatim buffer last_false_terminator start_offset input = parse
594597
| (space_char as c) "v}"

0 commit comments

Comments
 (0)