cst_regex.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /*************************************************************************/
  2. /* */
  3. /* Language Technologies Institute */
  4. /* Carnegie Mellon University */
  5. /* Copyright (c) 1999 */
  6. /* All Rights Reserved. */
  7. /* */
  8. /* Permission is hereby granted, free of charge, to use and distribute */
  9. /* this software and its documentation without restriction, including */
  10. /* without limitation the rights to use, copy, modify, merge, publish, */
  11. /* distribute, sublicense, and/or sell copies of this work, and to */
  12. /* permit persons to whom this work is furnished to do so, subject to */
  13. /* the following conditions: */
  14. /* 1. The code must retain the above copyright notice, this list of */
  15. /* conditions and the following disclaimer. */
  16. /* 2. Any modifications must be clearly marked as such. */
  17. /* 3. Original authors' names are not deleted. */
  18. /* 4. The authors' names are not used to endorse or promote products */
  19. /* derived from this software without specific prior written */
  20. /* permission. */
  21. /* */
  22. /* CARNEGIE MELLON UNIVERSITY AND THE CONTRIBUTORS TO THIS WORK */
  23. /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
  24. /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
  25. /* SHALL CARNEGIE MELLON UNIVERSITY NOR THE CONTRIBUTORS BE LIABLE */
  26. /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
  27. /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
  28. /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
  29. /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
  30. /* THIS SOFTWARE. */
  31. /* */
  32. /*************************************************************************/
  33. /* Author: Alan W Black (awb@cs.cmu.edu) */
  34. /* Date: January 2000 */
  35. /*************************************************************************/
  36. /* */
  37. /* cst front-end to Henry Spencer's regex code */
  38. /* */
  39. /*************************************************************************/
  40. /* Includes portions or regexp.h, copyright follows: */
  41. /*
  42. * Copyright (c) 1986 by University of Toronto.
  43. * Copyright (c) 1989, 1993
  44. * The Regents of the University of California. All rights reserved.
  45. *
  46. * This code is derived from software contributed to Berkeley
  47. * by Henry Spencer.
  48. *
  49. * Redistribution and use in source and binary forms, with or without
  50. * modification, are permitted provided that the following conditions
  51. * are met:
  52. * 1. Redistributions of source code must retain the above copyright
  53. * notice, this list of conditions and the following disclaimer.
  54. * 2. Redistributions in binary form must reproduce the above copyright
  55. * notice, this list of conditions and the following disclaimer in the
  56. * documentation and/or other materials provided with the distribution.
  57. * 3. All advertising materials mentioning features or use of this software
  58. * must display the following acknowledgement:
  59. * This product includes software developed by the University of
  60. * California, Berkeley and its contributors.
  61. * 4. Neither the name of the University nor the names of its contributors
  62. * may be used to endorse or promote products derived from this software
  63. * without specific prior written permission.
  64. *
  65. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  66. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  67. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  68. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  69. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  70. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  71. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  72. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  73. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  74. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  75. * SUCH DAMAGE.
  76. *
  77. * @(#)regexp.h 8.1 (Berkeley) 6/2/93
  78. */
  79. #ifndef _CST_REGEX_H__
  80. #define _CST_REGEX_H__
  81. #include "cst_file.h"
  82. #include "cst_string.h"
  83. /*
  84. * The first byte of the regexp internal "program" is actually this magic
  85. * number; the start node begins in the second byte.
  86. */
  87. #define CST_REGMAGIC 0234
  88. typedef struct cst_regex_struct {
  89. char regstart; /* Internal use only. */
  90. char reganch; /* Internal use only. */
  91. char *regmust; /* Internal use only. */
  92. int regmlen; /* Internal use only. */
  93. int regsize;
  94. char *program;
  95. } cst_regex;
  96. #define CST_NSUBEXP 10
  97. typedef struct cst_regstate_struct {
  98. const char *startp[CST_NSUBEXP];
  99. const char *endp[CST_NSUBEXP];
  100. const char *input;
  101. const char *bol;
  102. } cst_regstate;
  103. cst_regex *new_cst_regex(const char *str);
  104. void delete_cst_regex(cst_regex *r);
  105. int cst_regex_match(const cst_regex *r, const char *str);
  106. cst_regstate *cst_regex_match_return(const cst_regex *r, const char *str);
  107. /* Internal functions from original HS code */
  108. cst_regex *hs_regcomp(const char *);
  109. cst_regstate *hs_regexec(const cst_regex *, const char *);
  110. void hs_regdelete(cst_regex *);
  111. /* Works similarly to snprintf(3), in that at most max characters are
  112. written to out, including the trailing NUL, and the return value is
  113. the number of characters written, *excluding* the trailing NUL.
  114. Also works similarly to wcstombs(3) in that passing NULL as out
  115. will count the number of characters that would be written without
  116. doing any actual conversion, and ignoring max. So, you could use
  117. it like this:
  118. rx = new_cst_regex("\\(.*\\)_\\(.*\\)");
  119. if ((rs = cst_regex_match_return(rx, "foo_bar")) != NULL) {
  120. size_t n;
  121. n = cst_regsub(rs, "\\1_\\2_quux", NULL, 0) + 1;
  122. out = cst_alloc(char, n);
  123. cst_regsub(rs, "\\1_\\2_quux", out, n);
  124. } */
  125. size_t cst_regsub(const cst_regstate *r, const char *in, char *out, size_t max);
  126. /* Initialize the regex engine and global regex constants */
  127. void cst_regex_init();
  128. /* Regexps used in text processing (these are latin-alphabet specific
  129. and to some extent US English-specific) */
  130. extern const cst_regex * const cst_rx_white;
  131. extern const cst_regex * const cst_rx_alpha;
  132. extern const cst_regex * const cst_rx_uppercase;
  133. extern const cst_regex * const cst_rx_lowercase;
  134. extern const cst_regex * const cst_rx_alphanum;
  135. extern const cst_regex * const cst_rx_identifier;
  136. extern const cst_regex * const cst_rx_int;
  137. extern const cst_regex * const cst_rx_double;
  138. extern const cst_regex * const cst_rx_commaint;
  139. extern const cst_regex * const cst_rx_digits;
  140. extern const cst_regex * const cst_rx_dotted_abbrev;
  141. /* Table of regexps used in CART trees (only one so far) */
  142. extern const cst_regex * const cst_regex_table[];
  143. #define CST_RX_dotted_abbrev_NUM 0
  144. #endif