+/* Parses a wide character regexp pattern into a syntax tree. This parser
+ handles both syntaxes (BRE and ERE), including the TRE extensions. */
+static reg_errcode_t
+tre_parse(tre_parse_ctx_t *ctx);
+
+
+/*
+ This parser is just a simple recursive descent parser for POSIX.2
+ regexps. The parser supports both the obsolete default syntax and
+ the "extended" syntax, and some nonstandard extensions.
+*/
+
+/* Characters with special meanings in regexp syntax. */
+#define CHAR_PIPE '|'
+#define CHAR_LPAREN '('
+#define CHAR_RPAREN ')'
+#define CHAR_LBRACE '{'
+#define CHAR_RBRACE '}'
+#define CHAR_LBRACKET '['
+#define CHAR_RBRACKET ']'
+#define CHAR_MINUS '-'
+#define CHAR_STAR '*'
+#define CHAR_QUESTIONMARK '?'
+#define CHAR_PLUS '+'
+#define CHAR_PERIOD '.'
+#define CHAR_COLON ':'
+#define CHAR_EQUAL '='
+#define CHAR_COMMA ','
+#define CHAR_CARET '^'
+#define CHAR_DOLLAR '$'
+#define CHAR_BACKSLASH '\\'
+#define CHAR_HASH '#'
+#define CHAR_TILDE '~'
+
+
+/* Some macros for expanding \w, \s, etc. */
+static const struct tre_macro_struct {
+ const char c;
+ const char *expansion;
+} tre_macros[] =
+ { {'t', "\t"}, {'n', "\n"}, {'r', "\r"},
+ {'f', "\f"}, {'a', "\a"}, {'e', "\033"},
+ {'w', "[[:alnum:]_]"}, {'W', "[^[:alnum:]_]"}, {'s', "[[:space:]]"},
+ {'S', "[^[:space:]]"}, {'d', "[[:digit:]]"}, {'D', "[^[:digit:]]"},
+ { 0, NULL }
+ };
+
+
+/* Expands a macro delimited by `regex' and `regex_end' to `buf', which
+ must have at least `len' items. Sets buf[0] to zero if the there
+ is no match in `tre_macros'. */
+static const char *
+tre_expand_macro(const char *regex)
+{
+ int i;
+
+ if (!*regex)
+ return 0;
+
+ for (i = 0; tre_macros[i].expansion && tre_macros[i].c != *regex; i++);
+ return tre_macros[i].expansion;
+}
+