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