1 /*
2     This file is part of GNU APL, a free implementation of the
3     ISO/IEC Standard 13751, "Programming Language APL, Extended"
4 
5     Copyright (C) 2017 Elias Mårtenson
6 
7     This program is free software: you can redistribute it and/or modify
8     it under the terms of the GNU General Public License as published by
9     the Free Software Foundation, either version 3 of the License, or
10     (at your option) any later version.
11 
12     This program is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15     GNU General Public License for more details.
16 
17     You should have received a copy of the GNU General Public License
18     along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef __Regexp_HH__DEFINED__
22 #define __Regexp_HH__DEFINED__
23 
24 #include "UCS_string.hh"
25 
26 #define PCRE2_CODE_UNIT_WIDTH 32
27 #include <pcre2.h>
28 
29 /// A Regular expression match
30 class RegexpMatch
31 {
32 public:
33    /// constructor
34    RegexpMatch(pcre2_code * code, const UCS_string & B, PCRE2_SIZE start);
35 
36    /// destructor
37    virtual ~RegexpMatch();
38 
39    /// return the number of offset pairs in a match
get_ovector_count() const40    uint32_t get_ovector_count() const
41        { return ovector_count; }
42 
43    /// return the offset pairs in a match
get_ovector() const44     const PCRE2_SIZE * get_ovector() const
45        { return ovector; }
46 
47    /// return \b true if there was a match
48     bool is_match() const;
49 
50    /// return the number of matches
51     int num_matches() const;
52 
53    /// return the matched string
54    UCS_string matched_string() const;
55 
56 protected:
57    /// the offset pairs
58    PCRE2_SIZE * ovector;
59 
60    /// the number of offset pairs
61    uint32_t ovector_count;
62 
63    /// the data related to a match
64    pcre2_match_data * match_data;
65 
66    /// the result of a match
67    int match_result;
68 
69    /// the right argument B of ⎕RE
70    const UCS_string & matched_B;
71 };
72 
73 /// Helper class for ⎕RE
74 class Regexp
75 {
76 public:
77     /// constructor
78     Regexp(const UCS_string & pattern, int flags);
79 
80     /// destructor
81     virtual ~Regexp();
82 
83    /// return a new match
84     const RegexpMatch * match(const UCS_string & match, PCRE2_SIZE size) const;
85 
86    /// return the number of matches
87     int expression_count() const;
88 
89    /// returned the compiled regular expression
get_code() const90     pcre2_code * get_code() const
91        { return code; }
92 
93    /// return a description for error code \b eec
94    static UCS_string pcre_error(int ec);
95 
96 protected:
97    /// compiled regular expression
98    pcre2_code * code;
99 };
100 
101 #endif // __Regexp_HH__DEFINED__
102