1 #include <assert.h>
2 #include <stddef.h>
3 
4 #include "lib/regex.h"
5 #include "lib/regex_impl.h"
6 #include "src/msg/location.h"
7 
8 
9 using namespace re2c;
10 using namespace re2c::libre2c;
11 
regexec(const regex_t * re,const char * string,size_t nmatch,regmatch_t pmatch[],int eflags)12 int regexec(const regex_t *re, const char *string, size_t nmatch,
13     regmatch_t pmatch[], int eflags)
14 {
15     const int cflags = re->flags;
16     if (!(cflags & REG_NFA)) {
17         // DFA-based algorithms
18         if (cflags & REG_REGLESS) {
19             // Registerless TDFA.
20             if (cflags & REG_LEFTMOST) {
21                 return regexec_dfa_regless<ldetctx_t>(re, string, nmatch, pmatch, eflags);
22             } else {
23                 return regexec_dfa_regless<pdetctx_t>(re, string, nmatch, pmatch, eflags);
24             }
25         } else {
26             // TDFA with registers and register operations on transitions.
27             if (cflags & REG_STADFA) {
28                 return regexec_dfa<true>(re, string, nmatch, pmatch, eflags);
29             } else {
30                 return regexec_dfa<false>(re, string, nmatch, pmatch, eflags);
31             }
32         }
33     } else {
34         // NFA-based algorithms
35         if (cflags & REG_LEFTMOST) {
36             // Leftmost greedy disambiguation
37             if (cflags & REG_TRIE) {
38                 return regexec_nfa_leftmost_trie(re, string, nmatch, pmatch, eflags);
39             } else {
40                 return regexec_nfa_leftmost(re, string, nmatch, pmatch, eflags);
41             }
42         } else {
43             // POSIX disambiguation
44             if (cflags & REG_BACKWARD) {
45                 return regexec_nfa_posix_backward(re, string, nmatch, pmatch, eflags);
46             } else if (cflags & REG_KUKLEWICZ) {
47                 return regexec_nfa_posix_kuklewicz(re, string, nmatch, pmatch, eflags);
48             } else if (cflags & REG_TRIE) {
49                 return regexec_nfa_posix_trie(re, string, nmatch, pmatch, eflags);
50             } else {
51                 return regexec_nfa_posix(re, string, nmatch, pmatch, eflags);
52             }
53         }
54     }
55 }
56 
regparse(const regex_t * re,const char * string,size_t nmatch)57 subhistory_t *regparse(const regex_t *re, const char *string, size_t nmatch)
58 {
59     const int cflags = re->flags;
60     assert(cflags & REG_SUBHIST);
61     if (!(cflags & REG_NFA)) {
62         // DFA-based algorithms
63         if (cflags & REG_REGLESS) {
64             // Registerless TDFA.
65             if (cflags & REG_LEFTMOST) {
66                 return regparse_dfa_regless<ldetctx_t>(re, string, nmatch);
67             } else {
68                 return regparse_dfa_regless<pdetctx_t>(re, string, nmatch);
69             }
70         } else {
71             // TDFA with registers and register operations on transitions.
72             if (cflags & REG_STADFA) {
73                 return regparse_dfa<true>(re, string, nmatch);
74             } else {
75                 return regparse_dfa<false>(re, string, nmatch);
76             }
77         }
78     } else {
79         // NFA-based algorithms (not implemented yet).
80         assert(false);
81         return NULL;
82     }
83 }
84 
regtstring(const regex_t * re,const char * string)85 const tstring_t *regtstring(const regex_t *re, const char *string)
86 {
87     const int cflags = re->flags;
88     assert(cflags & REG_TSTRING);
89     if (!(cflags & REG_NFA)) {
90         // DFA-based algorithms
91         if (cflags & REG_REGLESS) {
92             // Registerless TDFA.
93             if (cflags & REG_LEFTMOST) {
94                 return regtstring_dfa_regless<ldetctx_t>(re, string);
95             } else {
96                 return regtstring_dfa_regless<pdetctx_t>(re, string);
97             }
98         } else {
99             // TDFA with registers is not suited to tstring construction.
100             assert(false);
101             return NULL;
102         }
103     } else {
104         // NFA-based algorithms (not implemented yet).
105         assert(false);
106         return NULL;
107     }
108 }
109 
110