1 /***************************************************************************
2  *                      RegexElement.h  -  description
3  *                           -------------------
4  *  begin                : Fr Jan 22 2021
5  *  copyright            : (C) 2005-2021 by Andre Simon
6  *  email                : a.simon@mailbox.org
7  ***************************************************************************/
8 
9 
10 /*
11  This file is part of Highlight.
12 
13  Highlight is free software: you can redistribute it and/or modify
14  it under the terms of the GNU General Public License as published by
15  the Free Software Foundation, either version 3 of the License, or
16  (at your option) any later version.
17 
18  Highlight is distributed in the hope that it will be useful,
19  but WITHOUT ANY WARRANTY; without even the implied warranty of
20  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21  GNU General Public License for more details.
22 
23  You should have received a copy of the GNU General Public License
24  along with Highlight.  If not, see <http://www.gnu.org/licenses/>.
25  */
26 
27 #ifndef REGEXELEMENT_H
28 #define REGEXELEMENT_H
29 
30 #include <boost/xpressive/xpressive_dynamic.hpp>
31 
32 #include "enums.h"
33 
34 
35 /**
36  * @todo write docs
37  */
38 /**\brief Association of a regex with a state description
39  *
40  *  A RegexElement associates a regular expression with the state information
41  *  (opening and closing state, pattern, keyword class, keyword group id, language name)
42  */
43 
44 namespace highlight
45 {
46 
47 class RegexElement
48 {
49 public:
RegexElement()50     RegexElement()
51     :open ( STANDARD ), end ( STANDARD ), kwClass ( 0 ), capturingGroup ( -1 ),
52     langName(), instanceId(instanceCnt++),
53     priority(0), constraintLineNum (0)
54     {
55     }
56 
57     RegexElement ( State oState, State eState, const std::string&rePattern, unsigned int cID=0, int group=-1, const std::string& name="",
58                    unsigned int prio=0, unsigned int cLineNum=0,  const std::string &cFilename=""):
open(oState)59                    open ( oState ), end ( eState ), kwClass ( cID ), capturingGroup ( group ), langName(name),instanceId(instanceCnt++),
60                    priority(prio), constraintLineNum (cLineNum), constraintFilename (cFilename)
61     {
62         pattern=rePattern;
63         rex=boost::xpressive::sregex::compile(rePattern);
64     }
65 
~RegexElement()66     ~RegexElement()
67     {
68         instanceCnt--;
69     }
70 
71     State open, ///< opening state
72     end;  ///< closing state
73     boost::xpressive::sregex rex;
74     unsigned int kwClass;        ///< keyword class
75     int capturingGroup;          ///< capturing group ID
76     std::string langName;             ///< language name
77     std::string pattern;              ///< RE pattern
78     static int instanceCnt;
79     int instanceId;
80     unsigned int priority;          ///< if set and matched, no other other regular expression will be evaluated
81     unsigned int constraintLineNum; ///< restrict this regex to this source line number
82     std::string constraintFilename;      ///< restrict this regex to this source filename
83 };
84 
85 }
86 #endif // REGEXELEMENT_H
87