Skip to content

Commit 05a04c2

Browse files
gilthojonludlam
authored andcommitted
use Math_block and Math_span instead of one Math constr + boolean
1 parent b7b6336 commit 05a04c2

File tree

5 files changed

+56
-59
lines changed

5 files changed

+56
-59
lines changed

src/ast.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ type inline_element =
2323
| `Reference of
2424
reference_kind * string with_location * inline_element with_location list
2525
| `Link of string * inline_element with_location list
26-
| `Math of bool * string ]
26+
| `Math_span of string ]
2727
(** Inline elements are equivalent to what would be found in a [span] in HTML.
2828
Mostly these are straightforward. The [`Reference] constructor takes a triple
2929
whose second element is the reference itself, and the third the replacement
@@ -41,7 +41,8 @@ type nestable_block_element =
4141
| `List of
4242
[ `Unordered | `Ordered ]
4343
* [ `Light | `Heavy ]
44-
* nestable_block_element with_location list list ]
44+
* nestable_block_element with_location list list
45+
| `Math_block of string ]
4546
(** Some block elements may be nested within lists or tags, but not all.
4647
The [`List] constructor has a parameter of type [\[`Light | `Heavy\]].
4748
This corresponds to the syntactic constructor used (see the

src/lexer.mll

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

30-
30+
let math_constr inline x = if inline then `Math_span x else `Math_block x
3131

3232
(* This is used for code and verbatim blocks. It can be done with a regular
3333
expression, but the regexp gets quite ugly, so a function is easier to
@@ -549,7 +549,7 @@ and code_span buffer nesting_level start_offset input = parse
549549
and math inline buffer nesting_level start_offset input = parse
550550
| '}'
551551
{ if nesting_level == 0 then
552-
emit input (`Math (inline, Buffer.contents buffer)) ~start_offset
552+
emit input (math_constr inline (Buffer.contents buffer)) ~start_offset
553553
else begin
554554
Buffer.add_char buffer '}';
555555
math inline buffer (nesting_level - 1) start_offset input lexbuf
@@ -569,7 +569,7 @@ and math inline buffer nesting_level start_offset input = parse
569569
input
570570
(Parse_error.not_allowed
571571
~what:(Token.describe (`Blank_line "\n"))
572-
~in_what:(Token.describe (`Math (inline, ""))));
572+
~in_what:(Token.describe (math_constr inline "")));
573573
Buffer.add_char buffer '\n';
574574
math inline buffer nesting_level start_offset input lexbuf
575575
end
@@ -584,8 +584,8 @@ and math inline buffer nesting_level start_offset input = parse
584584
input
585585
(Parse_error.not_allowed
586586
~what:(Token.describe `End)
587-
~in_what:(Token.describe (`Math (inline, ""))));
588-
emit input (`Math (inline, Buffer.contents buffer)) ~start_offset }
587+
~in_what:(Token.describe (math_constr inline "")));
588+
emit input (math_constr inline (Buffer.contents buffer)) ~start_offset }
589589
| _ as c
590590
{ Buffer.add_char buffer c;
591591
math inline buffer nesting_level start_offset input lexbuf }

src/syntax.ml

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type token_that_always_begins_an_inline_element =
5555
| `Begin_reference_with_replacement_text of string
5656
| `Simple_link of string
5757
| `Begin_link_with_replacement_text of string
58-
| `Math of bool * string ]
58+
| `Math_span of string ]
5959

6060
(* Check that the token constructors above actually are all in [Token.t]. *)
6161
let _check_subset : token_that_always_begins_an_inline_element -> Token.t =
@@ -100,15 +100,9 @@ let rec inline_element :
100100
| `Plus ->
101101
junk input;
102102
Loc.at location (`Word "+")
103-
| `Code_span c ->
103+
| (`Code_span _ | `Math_span _ | `Raw_markup _) as token ->
104104
junk input;
105-
Loc.at location (`Code_span c)
106-
| `Raw_markup (raw_markup_target, s) ->
107-
junk input;
108-
Loc.at location (`Raw_markup (raw_markup_target, s))
109-
| `Math _ as tk ->
110-
junk input;
111-
Loc.at location tk
105+
Loc.at location token
112106
| `Begin_style s as parent_markup ->
113107
junk input;
114108

@@ -734,10 +728,11 @@ let rec block_element_list :
734728
let block = Loc.at location block in
735729
let acc = block :: acc in
736730
consume_block_elements ~parsed_a_tag `After_text acc
737-
| { value = `Code_block (_, s) as token; location } as next_token ->
731+
| ( { value = `Code_block (_, { value = s; _ }) as token; location }
732+
| { value = `Math_block s as token; location } ) as next_token ->
738733
warn_if_after_tags next_token;
739734
warn_if_after_text next_token;
740-
if s.value = "" then
735+
if s = "" then
741736
Parse_error.should_not_be_empty ~what:(Token.describe token) location
742737
|> add_warning input;
743738

src/token.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ type t =
5353
string
5454
| `Code_span of string
5555
| `Raw_markup of string option * string
56-
| `Math of bool * string
57-
(* The boolean is true iff the math is a inline *)
56+
| `Math_span of string
57+
| `Math_block of string
5858
| `Begin_style of style
5959
| `Begin_paragraph_style of paragraph_style
6060
| (* Other inline element markup. *)
@@ -125,7 +125,8 @@ 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 "'{m ...}' (inline math)" else "'{math ...}' (math block)"
128+
| `Math_span _ -> "'{m ...}' (math span)"
129+
| `Math_block _ -> "'{math ...}' (math block)"
129130
| `Simple_reference _ -> "'{!...}' (cross-reference)"
130131
| `Begin_reference_with_replacement_text _ ->
131132
"'{{!...} ...}' (cross-reference)"

test/test.ml

Lines changed: 38 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,7 @@ module Ast_to_sexp = struct
4141
| `Code_span c -> List [ Atom "code_span"; Atom c ]
4242
| `Raw_markup (target, s) ->
4343
List [ Atom "raw_markup"; opt str target; Atom s ]
44-
| `Math (b, s) ->
45-
let b_descr = if b then Atom "inline" else Atom "block" in
46-
List [Atom "math"; b_descr; Atom s]
44+
| `Math_span s -> List [ Atom "math_span"; Atom s ]
4745
| `Styled (s, es) ->
4846
List [ style s; List (List.map (at.at (inline_element at)) es) ]
4947
| `Reference (kind, r, es) ->
@@ -64,6 +62,7 @@ module Ast_to_sexp = struct
6462
| `Paragraph es ->
6563
List
6664
[ Atom "paragraph"; List (List.map (at.at (inline_element at)) es) ]
65+
| `Math_block s -> List [ Atom "math_block"; Atom s ]
6766
| `Code_block (None, c) -> List [ Atom "code_block"; at.at str c ]
6867
| `Code_block (Some meta, c) ->
6968
List [ Atom "code_block"; code_block_meta at meta; at.at str c ]
@@ -5311,14 +5310,14 @@ let%expect_test _ =
53115310
let module Math = struct
53125311
let block =
53135312
test "{math \\sum_{i=0}^n x^i%}";
5314-
[%expect {|
5315-
((output
5316-
(((f.ml (1 0) (1 24))
5317-
(paragraph (((f.ml (1 0) (1 24)) (math block "\\sum_{i=0}^n x^i%")))))))
5313+
[%expect
5314+
{|
5315+
((output (((f.ml (1 0) (1 24)) (math_block "\\sum_{i=0}^n x^i%"))))
53185316
(warnings ())) |}]
5319-
5317+
53205318
let complex_block =
5321-
test {|{math
5319+
test
5320+
{|{math
53225321
\alpha(x)=\left\{
53235322
\begin{array}{ll} % beginning of the array
53245323
x \% 4\\ % some variable modulo 4
@@ -5327,54 +5326,55 @@ let%expect_test _ =
53275326
\end{array} % end of the array
53285327
\right.
53295328
}|};
5330-
[%expect {|
5329+
[%expect
5330+
{|
53315331
((output
53325332
(((f.ml (1 0) (9 7))
5333-
(paragraph
5334-
(((f.ml (1 0) (9 7))
5335-
(math block
5336-
" \\alpha(x)=\\left\\{\
5337-
\n \\begin{array}{ll} % beginning of the array\
5338-
\n x \\% 4\\\\ % some variable modulo 4\
5339-
\n \\frac{1}{1+e^{-kx}}\\\\ % something else\
5340-
\n \\frac{e^x-e^{-x}}{e^x+e^{-x}} % another action\
5341-
\n \\end{array} % end of the array\
5342-
\n \\right.\
5343-
\n ")))))))
5344-
(warnings ())) |}]
5345-
5333+
(math_block
5334+
" \\alpha(x)=\\left\\{\
5335+
\n \\begin{array}{ll} % beginning of the array\
5336+
\n x \\% 4\\\\ % some variable modulo 4\
5337+
\n \\frac{1}{1+e^{-kx}}\\\\ % something else\
5338+
\n \\frac{e^x-e^{-x}}{e^x+e^{-x}} % another action\
5339+
\n \\end{array} % end of the array\
5340+
\n \\right.\
5341+
\n "))))
5342+
(warnings ())) |}]
5343+
53465344
let inline =
53475345
test "{m x + 4}";
5348-
[%expect {|
5346+
[%expect
5347+
{|
53495348
((output
53505349
(((f.ml (1 0) (1 9))
5351-
(paragraph (((f.ml (1 0) (1 9)) (math inline "x + 4")))))))
5350+
(paragraph (((f.ml (1 0) (1 9)) (math_span "x + 4")))))))
53525351
(warnings ())) |}]
5353-
5352+
53545353
let inline_nested =
53555354
test "{m \\sub_{i=0}^n x^i}";
5356-
[%expect {|
5355+
[%expect
5356+
{|
53575357
((output
53585358
(((f.ml (1 0) (1 20))
5359-
(paragraph (((f.ml (1 0) (1 20)) (math inline "\\sub_{i=0}^n x^i")))))))
5359+
(paragraph (((f.ml (1 0) (1 20)) (math_span "\\sub_{i=0}^n x^i")))))))
53605360
(warnings ())) |}]
5361-
5361+
53625362
let inline_false_nesting =
53635363
test "{m \\{ \\mathbb{only_left}}";
5364-
[%expect {|
5364+
[%expect
5365+
{|
53655366
((output
53665367
(((f.ml (1 0) (1 25))
5367-
(paragraph
5368-
(((f.ml (1 0) (1 25)) (math inline "\\{ \\mathbb{only_left}")))))))
5368+
(paragraph (((f.ml (1 0) (1 25)) (math_span "\\{ \\mathbb{only_left}")))))))
53695369
(warnings ())) |}]
5370-
5370+
53715371
let inline_false_terminator =
5372-
test "{m \\mathbb{only_left}\\}}";
5373-
[%expect {|
5372+
test "{m \\mathbb{only_left}\\}}";
5373+
[%expect
5374+
{|
53745375
((output
53755376
(((f.ml (1 0) (1 24))
5376-
(paragraph
5377-
(((f.ml (1 0) (1 24)) (math inline "\\mathbb{only_left}\\}")))))))
5377+
(paragraph (((f.ml (1 0) (1 24)) (math_span "\\mathbb{only_left}\\}")))))))
53785378
(warnings ())) |}]
53795379
end in
5380-
()
5380+
()

0 commit comments

Comments
 (0)