Source file commonmark.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
(*********************************************************************************)
(*                Higlo                                                          *)
(*                                                                               *)
(*    Copyright (C) 2014-2021 Institut National de Recherche en Informatique     *)
(*    et en Automatique. All rights reserved.                                    *)
(*                                                                               *)
(*    This program is free software; you can redistribute it and/or modify       *)
(*    it under the terms of the GNU Lesser General Public License version        *)
(*    3 as published by the Free Software Foundation.                            *)
(*                                                                               *)
(*    This program is distributed in the hope that it will be useful,            *)
(*    but WITHOUT ANY WARRANTY; without even the implied warranty of             *)
(*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the              *)
(*    GNU Library General Public License for more details.                       *)
(*                                                                               *)
(*    You should have received a copy of the GNU Lesser General Public           *)
(*    License along with this program; if not, write to the Free Software        *)
(*    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA                   *)
(*    02111-1307  USA                                                            *)
(*                                                                               *)
(*    Contact: Maxence.Guesdon@inria.fr                                          *)
(*                                                                               *)
(*                                                                               *)
(*********************************************************************************)

open Lang

let lexeme lb = Sedlexing.(Utf8.lexeme lb, lexeme_length lb);;
let sedlexeme = Sedlexing.Utf8.lexeme;;

let digit = [%sedlex.regexp? '0'..'9'|'_']
let hex = [%sedlex.regexp? digit | 'A'..'F' | 'a'..'f']
let integer = [%sedlex.regexp? Plus(digit)]
let decimal = [%sedlex.regexp? Star('0'..'9'), '.', Plus('0'..'9')]

let lf = [%sedlex.regexp? 0x000A]
let cr = [%sedlex.regexp? 0x000D]

let line_ending = [%sedlex.regexp? lf | (cr, Opt(lf))]

let space = [%sedlex.regexp? ' ']
let tab = [%sedlex.regexp? '\t']
let space_or_tab = [%sedlex.regexp? space | tab]
let star3_space = [%sedlex.regexp? Opt(space_or_tab),Opt(space_or_tab),Opt(space_or_tab)]

let thematic_break = [%sedlex.regexp? star3_space,
  (  (('_',Star(space_or_tab)),('_',Star(space_or_tab)),('_',Star(space_or_tab)),Star('_',Star(space_or_tab)))
   | (('-',Star(space_or_tab)),('-',Star(space_or_tab)),('-',Star(space_or_tab)),Star('-',Star(space_or_tab)))
   | (('*',Star(space_or_tab)),('*',Star(space_or_tab)),('*',Star(space_or_tab)),Star('*',Star(space_or_tab)))
  ), line_ending
]

let title = [%sedlex.regexp? star3_space,
  '#',Opt('#'),Opt('#'),Opt('#'),Opt('#'),Opt('#'), Plus(space_or_tab),
  Star(Compl(lf|cr|'#')),Opt(Plus(space_or_tab),Plus('#')),Opt(cr),lf]

let no_lf_cr = [%sedlex.regexp? 0x0000 .. 0x0009 | 0x000B | 0x000C | 0x000E .. 0xffff]

let setext_begin = [%sedlex.regexp? Compl(lf|cr|'>'|'+'|'-'|space_or_tab) ]
let setext_content = [%sedlex.regexp? Star(setext_begin | space_or_tab)]

let setext1 = [%sedlex.regexp?
  star3_space, setext_begin, setext_content, line_ending, star3_space, Plus('='),Star(space_or_tab),line_ending]
let setext2 = [%sedlex.regexp?
  star3_space, setext_begin, setext_content, line_ending, star3_space, Plus('-'),Star(space_or_tab),line_ending]

let indent = [%sedlex.regexp? space_or_tab, space_or_tab, space_or_tab, Plus(space_or_tab)]

let blank_line = [%sedlex.regexp? Star(space_or_tab), line_ending]
let code_line = [%sedlex.regexp? indent, Plus(no_lf_cr),line_ending]

let code_block = [%sedlex.regexp? Opt(blank_line), Plus(code_line), Opt(blank_line)]

let link_id = [%sedlex.regexp? '[',Plus(Compl(lf|cr|']')|"\\]"),']']
let link_url = [%sedlex.regexp? '(',Plus(Compl(lf|cr|')')|"\\)"),')']

let strong = [%sedlex.regexp?
  ( "**",Plus(Compl('*')),"**")
| ( "__",Plus(Compl('_')),"__")
]

let emph = [%sedlex.regexp?
  ( '*',Plus(Compl('*')),'*')
| ( '_',Plus(Compl('_')),'_')
]

let strikethrough = [%sedlex.regexp? ( "~~",Plus(Compl('~')),"~~")]


let rec fence_code_block lexbuf opening b len =
  match%sedlex lexbuf with
  | line_ending, star3_space,
    ( ("``",Plus('`')) | ("~~",Plus('~')) ) ->
      let str,n = lexeme lexbuf in
      let len = len + n in
      Buffer.add_string b str ;
      if Misc.is_prefix ~of_:opening (Misc.strip_string str) then
        [Symbol (1, (Buffer.contents b, len))]
      else
        fence_code_block lexbuf opening b len
  | eof -> [Symbol (1, (Buffer.contents b, len))]
  | any ->
      let (str, n) = lexeme lexbuf in
      Buffer.add_string b str;
      fence_code_block lexbuf opening b (len+n)
  | _ -> failwith "Invalid state"

and main lexbuf =
  match%sedlex lexbuf with
  | eof -> []
  | thematic_break -> [Symbol (4, lexeme lexbuf)]
  | title ->
      let (str, len) = lexeme lexbuf in
      let level =
        match Misc.split_string str [' '; '\t'] with
        | sharps :: _ -> String.length sharps
        | [] -> 0
      in
      [Title (level, (str, len))]
  | setext1 -> [Title (1, lexeme lexbuf)]
  | setext2 -> [Title (2, lexeme lexbuf)]
  | code_block -> [Symbol (1, lexeme lexbuf)]
  | star3_space, (("``",Plus('`')) | ("~~",Plus('~'))) ->
      let (opening, len) = lexeme lexbuf in
      let b = Buffer.create 256 in
      Buffer.add_string b opening ;
      fence_code_block lexbuf opening b len
  | link_id ->
      (
       let link_id = lexeme lexbuf in
       match%sedlex lexbuf with
       | link_url ->
           [ Id link_id ; Constant (lexeme lexbuf) ]
       | _ ->
           Sedlexing.rollback lexbuf;
           [ Id link_id ]
      )
  | strong -> [Symbol (5, lexeme lexbuf)]
  | emph -> [Symbol (6, lexeme lexbuf)]
  | strikethrough -> [Lcomment (lexeme lexbuf)]
  | any -> [Text (lexeme lexbuf)]
  | _ -> failwith "Invalid state"

let () = Lang.register_lang "commonmark" main;;