Skip to content

Commit b7b6336

Browse files
gilthojonludlam
authored andcommitted
use {math...} instead
Signed-off-by: Sacha Ayoun <[email protected]>
1 parent f31ee45 commit b7b6336

File tree

4 files changed

+52
-41
lines changed

4 files changed

+52
-41
lines changed

src/lexer.mll

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -321,8 +321,12 @@ rule token input = parse
321321
| "{_"
322322
{ emit input (`Begin_style `Subscript) }
323323

324+
| "{math" space_char
325+
{ math false (Buffer.create 1024) 0 (Lexing.lexeme_start lexbuf) input lexbuf }
326+
324327
| "{m" horizontal_space
325-
{ inline_math (Buffer.create 1024) 0 (Lexing.lexeme_start lexbuf) input lexbuf }
328+
{ math true (Buffer.create 1024) 0 (Lexing.lexeme_start lexbuf) input lexbuf }
329+
326330

327331
| "{!modules:" ([^ '}']* as modules) '}'
328332
{ emit input (`Modules modules) }
@@ -366,10 +370,7 @@ rule token input = parse
366370

367371
| "{%" ((raw_markup_target as target) ':')? (raw_markup as s)
368372
("%}" | eof as e)
369-
{ let token =
370-
match target with
371-
| Some "math" -> `Math (true, s)
372-
| _ -> `Raw_markup (target, s)
373+
{ let token = `Raw_markup (target, s)
373374
in
374375
if e <> "%}" then
375376
warning
@@ -545,40 +546,49 @@ and code_span buffer nesting_level start_offset input = parse
545546
{ Buffer.add_char buffer c;
546547
code_span buffer nesting_level start_offset input lexbuf }
547548

548-
and inline_math buffer nesting_level start_offset input = parse
549+
and math inline buffer nesting_level start_offset input = parse
549550
| '}'
550551
{ if nesting_level == 0 then
551-
emit input (`Math (false, Buffer.contents buffer)) ~start_offset
552+
emit input (`Math (inline, Buffer.contents buffer)) ~start_offset
552553
else begin
553554
Buffer.add_char buffer '}';
554-
inline_math buffer (nesting_level - 1) start_offset input lexbuf
555+
math inline buffer (nesting_level - 1) start_offset input lexbuf
555556
end
556557
}
557558
| '{'
558559
{ Buffer.add_char buffer '{';
559-
inline_math buffer (nesting_level + 1) start_offset input lexbuf }
560+
math inline buffer (nesting_level + 1) start_offset input lexbuf }
560561
| ("\\{" | "\\}") as s
561562
{ Buffer.add_string buffer s;
562-
inline_math buffer nesting_level start_offset input lexbuf }
563-
| newline newline
564-
{ warning
565-
input
566-
(Parse_error.not_allowed
567-
~what:(Token.describe (`Blank_line "\n\n"))
568-
~in_what:(Token.describe (`Math (false, ""))));
569-
Buffer.add_char buffer '\n';
570-
inline_math buffer nesting_level start_offset input lexbuf }
571-
563+
math inline buffer nesting_level start_offset input lexbuf }
564+
| (newline) as s
565+
{
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 (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
581+
}
572582
| eof
573583
{ warning
574584
input
575585
(Parse_error.not_allowed
576586
~what:(Token.describe `End)
577-
~in_what:(Token.describe (`Math (false, ""))));
578-
emit input (`Math (false, Buffer.contents buffer)) ~start_offset }
587+
~in_what:(Token.describe (`Math (inline, ""))));
588+
emit input (`Math (inline, Buffer.contents buffer)) ~start_offset }
579589
| _ as c
580590
{ Buffer.add_char buffer c;
581-
inline_math buffer nesting_level start_offset input lexbuf }
591+
math inline buffer nesting_level start_offset input lexbuf }
582592

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

src/syntax.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ let rec inline_element :
106106
| `Raw_markup (raw_markup_target, s) ->
107107
junk input;
108108
Loc.at location (`Raw_markup (raw_markup_target, s))
109-
| `Math s ->
109+
| `Math _ as tk ->
110110
junk input;
111-
Loc.at location (`Math s)
111+
Loc.at location tk
112112
| `Begin_style s as parent_markup ->
113113
junk input;
114114

src/token.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ type t =
5454
| `Code_span of string
5555
| `Raw_markup of string option * string
5656
| `Math of bool * string
57-
(* The boolean is true iff the math is a block *)
57+
(* The boolean is true iff the math is a inline *)
5858
| `Begin_style of style
5959
| `Begin_paragraph_style of paragraph_style
6060
| (* Other inline element markup. *)
@@ -125,7 +125,7 @@ let describe : [< t | `Comment ] -> string = function
125125
| `Begin_style `Emphasis -> "'{e ...}' (emphasized text)"
126126
| `Begin_style `Superscript -> "'{^...}' (superscript)"
127127
| `Begin_style `Subscript -> "'{_...}' (subscript)"
128-
| `Math (b, _) -> if b then "'{%math ...%}' (math block)" else "'{m ...}' (inline math)"
128+
| `Math (b, _) -> if b then "'{m ...}' (inline math)" else "'{math ...}' (math block)"
129129
| `Simple_reference _ -> "'{!...}' (cross-reference)"
130130
| `Begin_reference_with_replacement_text _ ->
131131
"'{{!...} ...}' (cross-reference)"

test/test.ml

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ module Ast_to_sexp = struct
4242
| `Raw_markup (target, s) ->
4343
List [ Atom "raw_markup"; opt str target; Atom s ]
4444
| `Math (b, s) ->
45-
let b_descr = if b then Atom "block" else Atom "inline" in
45+
let b_descr = if b then Atom "inline" else Atom "block" in
4646
List [Atom "math"; b_descr; Atom s]
4747
| `Styled (s, es) ->
4848
List [ style s; List (List.map (at.at (inline_element at)) es) ]
@@ -5310,42 +5310,43 @@ let%expect_test _ =
53105310
let%expect_test _ =
53115311
let module Math = struct
53125312
let block =
5313-
test "{%math:\\sum_{i=0}^n x^i%}";
5313+
test "{math \\sum_{i=0}^n x^i%}";
53145314
[%expect {|
53155315
((output
5316-
(((f.ml (1 0) (1 25))
5317-
(paragraph (((f.ml (1 0) (1 25)) (math block "\\sum_{i=0}^n x^i")))))))
5316+
(((f.ml (1 0) (1 24))
5317+
(paragraph (((f.ml (1 0) (1 24)) (math block "\\sum_{i=0}^n x^i%")))))))
53185318
(warnings ())) |}]
53195319

53205320
let complex_block =
5321-
test {|{%math
5321+
test {|{math
53225322
\alpha(x)=\left\{
53235323
\begin{array}{ll} % beginning of the array
53245324
x \% 4\\ % some variable modulo 4
53255325
\frac{1}{1+e^{-kx}}\\ % something else
53265326
\frac{e^x-e^{-x}}{e^x+e^{-x}} % another action
53275327
\end{array} % end of the array
53285328
\right.
5329-
%}|}
5330-
5331-
let inline =
5332-
test "{m x + 4}";
5329+
}|};
53335330
[%expect {|
53345331
((output
5335-
(((f.ml (1 0) (9 8))
5332+
(((f.ml (1 0) (9 7))
53365333
(paragraph
5337-
(((f.ml (1 0) (9 8))
5338-
(raw_markup ()
5339-
"math\
5340-
\n \\alpha(x)=\\left\\{\
5334+
(((f.ml (1 0) (9 7))
5335+
(math block
5336+
" \\alpha(x)=\\left\\{\
53415337
\n \\begin{array}{ll} % beginning of the array\
53425338
\n x \\% 4\\\\ % some variable modulo 4\
53435339
\n \\frac{1}{1+e^{-kx}}\\\\ % something else\
53445340
\n \\frac{e^x-e^{-x}}{e^x+e^{-x}} % another action\
53455341
\n \\end{array} % end of the array\
53465342
\n \\right.\
53475343
\n ")))))))
5348-
(warnings ()))((output
5344+
(warnings ())) |}]
5345+
5346+
let inline =
5347+
test "{m x + 4}";
5348+
[%expect {|
5349+
((output
53495350
(((f.ml (1 0) (1 9))
53505351
(paragraph (((f.ml (1 0) (1 9)) (math inline "x + 4")))))))
53515352
(warnings ())) |}]

0 commit comments

Comments
 (0)