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) 2008-2016  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 __Quad_RE_DEFINED__
22 #define __Quad_RE_DEFINED__
23 
24 #include "QuadFunction.hh"
25 #include "Value.hh"
26 
27 class Regexp;
28 
29 /// The class implementing ⎕RE
30 class Quad_RE : public QuadFunction
31 {
32 public:
33    /// Constructor.
Quad_RE()34    Quad_RE() : QuadFunction(TOK_Quad_RE)
35    {}
36 
37    static Quad_RE * fun;          ///< Built-in function.
38    static Quad_RE  _fun;          ///< Built-in function.
39 
40 protected:
41    /// the type of result
42    enum Result_type
43       {
44         RT_string    = 0,   ///< matched string
45         RT_partition = 1,   ///< dito.
46         RT_pos_len   = 2,   ///< position and length
47         RT_reduce    = 3,   ///< dito.
48       } result_type;    ///< the type of result
49 
50    /// overloaded Function::eval_AB().
eval_AB(Value_P A,Value_P B)51    Token eval_AB(Value_P A, Value_P B)
52       { return eval_AXB(A, Str0(LOC), B); }
53 
54    /// overloaded Function::eval_AXB().
55    Token eval_AXB(Value_P A, Value_P X, Value_P B);
56 
57    /// overloaded Function::eval_B().
eval_B(Value_P B)58    Token eval_B(Value_P B)
59       { VALENCE_ERROR; }
60 
61    /// overloaded Function::eval_XB().
eval_XB(Value_P X,Value_P B)62    Token eval_XB(Value_P X, Value_P B)
63       { VALENCE_ERROR; }
64 
65 #ifdef HAVE_LIBPCRE2_32
66 
67    /// libpcre flags
68    class Flags
69       {
70         public:
71            /// constructor
72            Flags(const UCS_string & flags_in);
73 
74            /// return the flags
get_compflags() const75            int get_compflags() const
76               { return flags; }
77 
78            /// return true if no match shall give an error
get_error_on_no_match() const79            bool get_error_on_no_match() const
80               { return error_on_no_match; }
81 
82            /// return global
get_global() const83            bool get_global() const
84               { return global; }
85 
86            /// return the result type
get_result_type() const87            Result_type get_result_type() const
88               { return result_type; }
89 
90         protected:
91            /// the flags (see pcre.h)
92            int flags;
93 
94            /// make no match an error
95            bool error_on_no_match;
96 
97            /// dito.
98            bool global;
99 
100            /// dito.
101            Result_type result_type;
102       };
103 
104    /// return a result whose format is given by flags
105    static Value_P regex_results(const Regexp & A, const Flags & X,
106                                 const UCS_string & B);
107 
108    /// return a result that can be used directly by ⊂ or /
109    static Value_P partition_result(const Regexp & A, const Flags & X,
110                                    const UCS_string & B);
111 
112    /// return a result that contains the matched strings
113    static Value_P string_result(const Regexp & A, const Flags & X,
114                                  const UCS_string & B, ShapeItem & B_offset);
115 
116    /// return a result that can be used by [] (position and length)
117    static Value_P index_result(const Regexp & regex, const Flags & X,
118                                 const UCS_string & B, ShapeItem & B_offset);
119 
120 # endif
121 };
122 
123 #endif
124