aboutsummaryrefslogtreecommitdiff
path: root/src/scanners.re
blob: 71103f643e10855fd165bde5ce16f6be82607ae3 (plain)
  1. #include <stdlib.h>
  2. #define SCAN_DATA \
  3. const unsigned char *marker = NULL; \
  4. const unsigned char *start = p; \
  5. /*!re2c
  6. re2c:define:YYCTYPE = "unsigned char";
  7. re2c:define:YYCURSOR = p;
  8. re2c:define:YYMARKER = marker;
  9. re2c:define:YYCTXMARKER = marker;
  10. re2c:yyfill:enable = 0;
  11. wordchar = [^\x00-\x20];
  12. spacechar = [ \t\n];
  13. reg_char = [^\\()\x00-\x20];
  14. escaped_char = [\\][!"#$%&'()*+,./:;<=>?@[\\\]^_`{|}~-];
  15. tagname = [A-Za-z][A-Za-z0-9]*;
  16. blocktagname = 'article'|'header'|'aside'|'hgroup'|'iframe'|'blockquote'|'hr'|'body'|'li'|'map'|'button'|'object'|'canvas'|'ol'|'caption'|'output'|'col'|'p'|'colgroup'|'pre'|'dd'|'progress'|'div'|'section'|'dl'|'table'|'td'|'dt'|'tbody'|'embed'|'textarea'|'fieldset'|'tfoot'|'figcaption'|'th'|'figure'|'thead'|'footer'|'footer'|'tr'|'form'|'ul'|'h1'|'h2'|'h3'|'h4'|'h5'|'h6'|'video'|'script'|'style';
  17. attributename = [a-zA-Z_:][a-zA-Z0-9:._-]*;
  18. unquotedvalue = [^\"'=<>`\x00]+;
  19. singlequotedvalue = ['][^'\x00]*['];
  20. doublequotedvalue = [\"][^\"\x00]*[\"];
  21. attributevalue = unquotedvalue | singlequotedvalue | doublequotedvalue;
  22. attributevaluespec = spacechar* [=] spacechar* attributevalue;
  23. attribute = spacechar+ attributename attributevaluespec?;
  24. opentag = tagname attribute* spacechar* [/]? [>];
  25. closetag = [/] tagname spacechar* [>];
  26. htmlcomment = "!--" ([^-\x00]+ | [-][^-\x00]+)* "-->";
  27. processinginstruction = "?" ([^?>\x00]+ | [?][^>\x00])* "?>";
  28. declaration = "!" [A-Z]+ spacechar+ [^>\x00]* ">";
  29. cdata = "![CDATA[" ([^\]\x00]+ | "]" [^\]\x00] | "]]" [^>\x00])* "]]>";
  30. htmltag = opentag | closetag | htmlcomment | processinginstruction |
  31. declaration | cdata;
  32. in_parens_nosp = [(] (reg_char|escaped_char)* [)];
  33. in_double_quotes = ["] (escaped_char|[^"\x00])* ["];
  34. in_single_quotes = ['] (escaped_char|[^'\x00])* ['];
  35. in_parens = [(] (escaped_char|[^)\x00])* [)];
  36. scheme = 'coap'|'doi'|'javascript'|'aaa'|'aaas'|'about'|'acap'|'cap'|'cid'|'crid'|'data'|'dav'|'dict'|'dns'|'file'|'ftp'|'geo'|'go'|'gopher'|'h323'|'http'|'https'|'iax'|'icap'|'im'|'imap'|'info'|'ipp'|'iris'|'iris.beep'|'iris.xpc'|'iris.xpcs'|'iris.lwz'|'ldap'|'mailto'|'mid'|'msrp'|'msrps'|'mtqp'|'mupdate'|'news'|'nfs'|'ni'|'nih'|'nntp'|'opaquelocktoken'|'pop'|'pres'|'rtsp'|'service'|'session'|'shttp'|'sieve'|'sip'|'sips'|'sms'|'snmp'|'soap.beep'|'soap.beeps'|'tag'|'tel'|'telnet'|'tftp'|'thismessage'|'tn3270'|'tip'|'tv'|'urn'|'vemmi'|'ws'|'wss'|'xcon'|'xcon-userid'|'xmlrpc.beep'|'xmlrpc.beeps'|'xmpp'|'z39.50r'|'z39.50s'|'adiumxtra'|'afp'|'afs'|'aim'|'apt'|'attachment'|'aw'|'beshare'|'bitcoin'|'bolo'|'callto'|'chrome'|'chrome-extension'|'com-eventbrite-attendee'|'content'|'cvs'|'dlna-playsingle'|'dlna-playcontainer'|'dtn'|'dvb'|'ed2k'|'facetime'|'feed'|'finger'|'fish'|'gg'|'git'|'gizmoproject'|'gtalk'|'hcp'|'icon'|'ipn'|'irc'|'irc6'|'ircs'|'itms'|'jar'|'jms'|'keyparc'|'lastfm'|'ldaps'|'magnet'|'maps'|'market'|'message'|'mms'|'ms-help'|'msnim'|'mumble'|'mvn'|'notes'|'oid'|'palm'|'paparazzi'|'platform'|'proxy'|'psyc'|'query'|'res'|'resource'|'rmi'|'rsync'|'rtmp'|'secondlife'|'sftp'|'sgn'|'skype'|'smb'|'soldat'|'spotify'|'ssh'|'steam'|'svn'|'teamspeak'|'things'|'udp'|'unreal'|'ut2004'|'ventrilo'|'view-source'|'webcal'|'wtai'|'wyciwyg'|'xfire'|'xri'|'ymsgr';
  37. */
  38. // Try to match URI autolink after first <, returning number of chars matched.
  39. extern int _scan_autolink_uri(const unsigned char *p)
  40. {
  41. SCAN_DATA;
  42. /*!re2c
  43. scheme [:]([^\x00-\x20<>\\]|escaped_char)*[>] { return (p - start); }
  44. .? { return 0; }
  45. */
  46. }
  47. // Try to match email autolink after first <, returning num of chars matched.
  48. extern int _scan_autolink_email(const unsigned char *p)
  49. {
  50. SCAN_DATA;
  51. /*!re2c
  52. [a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+
  53. [@]
  54. [a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  55. ([.][a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*
  56. [>] { return (p - start); }
  57. .? { return 0; }
  58. */
  59. }
  60. // Try to match an HTML tag after first <, returning num of chars matched.
  61. extern int _scan_html_tag(const unsigned char *p)
  62. {
  63. SCAN_DATA;
  64. /*!re2c
  65. htmltag { return (p - start); }
  66. .? { return 0; }
  67. */
  68. }
  69. // Try to match an HTML block tag including first <,
  70. // returning num of chars matched.
  71. extern int _scan_html_block_tag(const unsigned char *p)
  72. {
  73. SCAN_DATA;
  74. /*!re2c
  75. [<] [/] blocktagname (spacechar | [>]) { return (p - start); }
  76. [<] blocktagname (spacechar | [/>]) { return (p - start); }
  77. [<] [!?] { return (p - start); }
  78. .? { return 0; }
  79. */
  80. }
  81. // Try to match a URL in a link or reference, return number of chars matched.
  82. // This may optionally be contained in <..>; otherwise
  83. // whitespace and unbalanced right parentheses aren't allowed.
  84. // Newlines aren't ever allowed.
  85. extern int _scan_link_url(const unsigned char *p)
  86. {
  87. SCAN_DATA;
  88. /*!re2c
  89. [ \n]* [<] ([^<>\n\\\x00] | escaped_char | [\\])* [>] { return (p - start); }
  90. [ \n]* (reg_char+ | escaped_char | in_parens_nosp)* { return (p - start); }
  91. .? { return 0; }
  92. */
  93. }
  94. // Try to match a link title (in single quotes, in double quotes, or
  95. // in parentheses), returning number of chars matched. Allow one
  96. // level of internal nesting (quotes within quotes).
  97. extern int _scan_link_title(const unsigned char *p)
  98. {
  99. SCAN_DATA;
  100. /*!re2c
  101. ["] (escaped_char|[^"\x00])* ["] { return (p - start); }
  102. ['] (escaped_char|[^'\x00])* ['] { return (p - start); }
  103. [(] (escaped_char|[^)\x00])* [)] { return (p - start); }
  104. .? { return 0; }
  105. */
  106. }
  107. // Match space characters, including newlines.
  108. extern int _scan_spacechars(const unsigned char *p)
  109. {
  110. SCAN_DATA;
  111. /*!re2c
  112. [ \t\n]* { return (p - start); }
  113. . { return 0; }
  114. */
  115. }
  116. // Match ATX header start.
  117. extern int _scan_atx_header_start(const unsigned char *p)
  118. {
  119. SCAN_DATA;
  120. /*!re2c
  121. [#]{1,6} ([ ]+|[\n]) { return (p - start); }
  122. .? { return 0; }
  123. */
  124. }
  125. // Match sexext header line. Return 1 for level-1 header,
  126. // 2 for level-2, 0 for no match.
  127. extern int _scan_setext_header_line(const unsigned char *p)
  128. {
  129. SCAN_DATA;
  130. /*!re2c
  131. [=]+ [ ]* [\n] { return 1; }
  132. [-]+ [ ]* [\n] { return 2; }
  133. .? { return 0; }
  134. */
  135. }
  136. // Scan a horizontal rule line: "...three or more hyphens, asterisks,
  137. // or underscores on a line by themselves. If you wish, you may use
  138. // spaces between the hyphens or asterisks."
  139. extern int _scan_hrule(const unsigned char *p)
  140. {
  141. SCAN_DATA;
  142. /*!re2c
  143. ([*][ ]*){3,} [ \t]* [\n] { return (p - start); }
  144. ([_][ ]*){3,} [ \t]* [\n] { return (p - start); }
  145. ([-][ ]*){3,} [ \t]* [\n] { return (p - start); }
  146. .? { return 0; }
  147. */
  148. }
  149. // Scan an opening code fence.
  150. extern int _scan_open_code_fence(const unsigned char *p)
  151. {
  152. SCAN_DATA;
  153. /*!re2c
  154. [`]{3,} / [^`\n\x00]*[\n] { return (p - start); }
  155. [~]{3,} / [^~\n\x00]*[\n] { return (p - start); }
  156. .? { return 0; }
  157. */
  158. }
  159. // Scan a closing code fence with length at least len.
  160. extern int _scan_close_code_fence(const unsigned char *p)
  161. {
  162. SCAN_DATA;
  163. /*!re2c
  164. ([`]{3,} | [~]{3,}) / spacechar* [\n] { return (p - start); }
  165. .? { return 0; }
  166. */
  167. }
  168. // Scans an entity.
  169. // Returns number of chars matched.
  170. extern int _scan_entity(const unsigned char *p)
  171. {
  172. SCAN_DATA;
  173. /*!re2c
  174. [&] ([#] ([Xx][A-Fa-f0-9]{1,8}|[0-9]{1,8}) |[A-Za-z][A-Za-z0-9]{1,31} ) [;]
  175. { return (p - start); }
  176. .? { return 0; }
  177. */
  178. }