1 /**
2 * Yudit Unicode Editor Source File
3 *
4 * GNU Copyright (C) 1997-2006 Gaspar Sinai <gaspar@yudit.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License, version 2,
8 * dated June 1991. See file COPYYING for details.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20 #include "stoolkit/syntax/SMatcher.h"
21
SMatcher(SPattern & pat,SMatcherIterator & it)22 SMatcher::SMatcher (SPattern& pat, SMatcherIterator& it)
23 : pattern(pat), iterator(it)
24 {
25 eod = false;
26 start = 0;
27 pattern.clear ();
28 }
29
30 // When a string macthes, it does not return immediatelly, until
31 // it reads again.
32 // if unitWork is true SD_MATCH_AGAIN can be returned after
33 // one character is read or one unit work is performed.
34 unsigned int
find(bool unitWork)35 SMatcher::find (bool unitWork)
36 {
37 int c;
38 if (pattern.checkMatch ())
39 {
40 return pattern.matchBegin;
41 }
42 if (eod)
43 {
44 return SD_MATCH_EOD;
45 }
46 while ((c = iterator.getNextCharacter ()) != -1)
47 {
48 // our next is current + 1
49 pattern.append ((SS_UCS4)c);
50 if (pattern.checkMatch ())
51 {
52 return pattern.matchBegin;
53 }
54 if (unitWork) return SD_MATCH_AGAIN;
55 }
56 if (!eod)
57 {
58 eod = true;
59 c = SD_MATCH_EOD;
60 pattern.append ((SS_UCS4)c);
61 }
62 if (pattern.checkMatch ())
63 {
64 return pattern.matchBegin;
65 }
66 pattern.action = pattern.ACT_NONE;
67 pattern.matchBegin = SD_MATCH_EOD;
68 return SD_MATCH_EOD;
69 }
70
71 void
applyActions(SMatcherAction & _action)72 SMatcher::applyActions (SMatcherAction& _action)
73 {
74 if (pattern.matchBegin == SD_MATCH_EOD)
75 {
76 // EOD is counted that is why upper limit is next-1
77 _action.applyAction (pattern.ACT_NONE, pattern.matchEnd, pattern.next-1);
78 return;
79 }
80 _action.applyAction (pattern.ACT_NONE, start, pattern.matchBegin);
81 _action.applyAction (pattern.action, pattern.matchBegin, pattern.matchEnd);
82 start = pattern.matchEnd;
83 }
84