aboutsummaryrefslogtreecommitdiff
path: root/src/scanners.re
blob: 28aba9dc79ee2319d037daf00ca4793b99367df8 (plain)
  1. #include <stdlib.h>
  2. /*!re2c
  3. re2c:define:YYCTYPE = "unsigned char";
  4. re2c:define:YYCURSOR = p;
  5. re2c:define:YYMARKER = marker;
  6. re2c:define:YYCTXMARKER = marker;
  7. re2c:yyfill:enable = 0;
  8. wordchar = [^\x00-\x20];
  9. spacechar = [ \t\n];
  10. reg_char = [^\\()\x00-\x20];
  11. escaped_char = [\\][!"#$%&'()*+,./:;<=>?@[\\\]^_`{|}~-];
  12. tagname = [A-Za-z][A-Za-z0-9]*;
  13. 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';
  14. attributename = [a-zA-Z_:][a-zA-Z0-9:._-]*;
  15. unquotedvalue = [^\"'=<>`\x00]+;
  16. singlequotedvalue = ['][^'\x00]*['];
  17. doublequotedvalue = [\"][^\"\x00]*[\"];
  18. attributevalue = unquotedvalue | singlequotedvalue | doublequotedvalue;
  19. attributevaluespec = spacechar* [=] spacechar* attributevalue;
  20. attribute = spacechar+ attributename attributevaluespec?;
  21. opentag = tagname attribute* spacechar* [/]? [>];
  22. closetag = [/] tagname spacechar* [>];
  23. htmlcomment = "!--" ([^-\x00]+ | [-][^-\x00]+)* "-->";
  24. processinginstruction = "?" ([^?>\x00]+ | [?][^>\x00])* "?>";
  25. declaration = "!" [A-Z]+ spacechar+ [^>\x00]* ">";
  26. cdata = "![CDATA[" ([^\]\x00]+ | "]" [^\]\x00] | "]]" [^>\x00])* "]]>";
  27. htmltag = opentag | closetag | htmlcomment | processinginstruction |
  28. declaration | cdata;
  29. in_parens_nosp = [(] (reg_char|escaped_char)* [)];
  30. in_double_quotes = ["] (escaped_char|[^"\x00])* ["];
  31. in_single_quotes = ['] (escaped_char|[^'\x00])* ['];
  32. in_parens = [(] (escaped_char|[^)\x00])* [)];
  33. 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';
  34. */
  35. // Try to match URI autolink after first <, returning number of chars matched.
  36. extern int _scan_autolink_uri(const unsigned char *p)
  37. {
  38. const unsigned char *marker = NULL;
  39. const unsigned char *start = p;
  40. /*!re2c
  41. scheme [:]([^\x00-\x20<>\\]|escaped_char)*[>] { return (p - start); }
  42. .? { return 0; }
  43. */
  44. }
  45. // Try to match email autolink after first <, returning num of chars matched.
  46. extern int _scan_autolink_email(const unsigned char *p)
  47. {
  48. const unsigned char *marker = NULL;
  49. const unsigned char *start = p;
  50. /*!re2c
  51. [a-zA-Z0-9.!#$%&'*+/=?^_`{|}~-]+
  52. [@]
  53. [a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?
  54. ([.][a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*
  55. [>] { return (p - start); }
  56. .? { return 0; }
  57. */
  58. }
  59. // Try to match an HTML tag after first <, returning num of chars matched.
  60. extern int _scan_html_tag(const unsigned char *p)
  61. {
  62. const unsigned char *marker = NULL;
  63. const unsigned char *start = p;
  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. const unsigned char *marker = NULL;
  74. const unsigned char *start = p;
  75. /*!re2c
  76. [<] [/] blocktagname (spacechar | [>]) { return (p - start); }
  77. [<] blocktagname (spacechar | [/>]) { return (p - start); }
  78. [<] [!?] { return (p - start); }
  79. .? { return 0; }
  80. */
  81. }
  82. // Try to match a URL in a link or reference, return number of chars matched.
  83. // This may optionally be contained in <..>; otherwise
  84. // whitespace and unbalanced right parentheses aren't allowed.
  85. // Newlines aren't ever allowed.
  86. extern int _scan_link_url(const unsigned char *p)
  87. {
  88. const unsigned char *marker = NULL;
  89. const unsigned char *start = p;
  90. /*!re2c
  91. [ \n]* [<] ([^<>\n\\\x00] | escaped_char | [\\])* [>] { return (p - start); }
  92. [ \n]* (reg_char+ | escaped_char | in_parens_nosp)* { return (p - start); }
  93. .? { return 0; }
  94. */
  95. }
  96. // Try to match a link title (in single quotes, in double quotes, or
  97. // in parentheses), returning number of chars matched. Allow one
  98. // level of internal nesting (quotes within quotes).
  99. extern int _scan_link_title(const unsigned char *p)
  100. {
  101. const unsigned char *marker = NULL;
  102. const unsigned char *start = p;
  103. /*!re2c
  104. ["] (escaped_char|[^"\x00])* ["] { return (p - start); }
  105. ['] (escaped_char|[^'\x00])* ['] { return (p - start); }
  106. [(] (escaped_char|[^)\x00])* [)] { return (p - start); }
  107. .? { return 0; }
  108. */
  109. }
  110. // Match space characters, including newlines.
  111. extern int _scan_spacechars(const unsigned char *p)
  112. {
  113. const unsigned char *start = p; \
  114. /*!re2c
  115. [ \t\n]* { return (p - start); }
  116. . { return 0; }
  117. */
  118. }
  119. // Match ATX header start.
  120. extern int _scan_atx_header_start(const unsigned char *p)
  121. {
  122. const unsigned char *marker = NULL;
  123. const unsigned char *start = p;
  124. /*!re2c
  125. [#]{1,6} ([ ]+|[\n]) { return (p - start); }
  126. .? { return 0; }
  127. */
  128. }
  129. // Match sexext header line. Return 1 for level-1 header,
  130. // 2 for level-2, 0 for no match.
  131. extern int _scan_setext_header_line(const unsigned char *p)
  132. {
  133. const unsigned char *marker = NULL;
  134. /*!re2c
  135. [=]+ [ ]* [\n] { return 1; }
  136. [-]+ [ ]* [\n] { return 2; }
  137. .? { return 0; }
  138. */
  139. }
  140. // Scan a horizontal rule line: "...three or more hyphens, asterisks,
  141. // or underscores on a line by themselves. If you wish, you may use
  142. // spaces between the hyphens or asterisks."
  143. extern int _scan_hrule(const unsigned char *p)
  144. {
  145. const unsigned char *marker = NULL;
  146. const unsigned char *start = p;
  147. /*!re2c
  148. ([*][ ]*){3,} [ \t]* [\n] { return (p - start); }
  149. ([_][ ]*){3,} [ \t]* [\n] { return (p - start); }
  150. ([-][ ]*){3,} [ \t]* [\n] { return (p - start); }
  151. .? { return 0; }
  152. */
  153. }
  154. // Scan an opening code fence.
  155. extern int _scan_open_code_fence(const unsigned char *p)
  156. {
  157. const unsigned char *marker = NULL;
  158. const unsigned char *start = p;
  159. /*!re2c
  160. [`]{3,} / [^`\n\x00]*[\n] { return (p - start); }
  161. [~]{3,} / [^~\n\x00]*[\n] { return (p - start); }
  162. .? { return 0; }
  163. */
  164. }
  165. // Scan a closing code fence with length at least len.
  166. extern int _scan_close_code_fence(const unsigned char *p)
  167. {
  168. const unsigned char *marker = NULL;
  169. const unsigned char *start = p;
  170. /*!re2c
  171. ([`]{3,} | [~]{3,}) / spacechar* [\n] { return (p - start); }
  172. .? { return 0; }
  173. */
  174. }
  175. // Scans an entity.
  176. // Returns number of chars matched.
  177. extern int _scan_entity(const unsigned char *p)
  178. {
  179. const unsigned char *marker = NULL;
  180. const unsigned char *start = p;
  181. /*!re2c
  182. [&] ([#] ([Xx][A-Fa-f0-9]{1,8}|[0-9]{1,8}) |[A-Za-z][A-Za-z0-9]{1,31} ) [;]
  183. { return (p - start); }
  184. .? { return 0; }
  185. */
  186. }