File: SPattern.cpp

package info (click to toggle)
yudit 3.1.0-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 18,472 kB
  • sloc: cpp: 76,344; perl: 5,630; makefile: 989; ansic: 823; sh: 441
file content (186 lines) | stat: -rw-r--r-- 4,374 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/** 
 *  Yudit Unicode Editor Source File
 *
 *  GNU Copyright (C) 1997-2023  Gaspar Sinai <gaspar@yudit.org>  
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License, version 2,
 *  dated June 1991. See file COPYYING for details.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */
 
#include "stoolkit/syntax/SPattern.h"
#include "stoolkit/SCharClass.h"

/**
 * A sample pattern that can be extended. This pattern 
 * now highlights the following:
 *
 * 123... number 
 * Yuko   keyword
 * yuko   error
 * YUKO (kanji) define
 * characters none
 * other  control
 *
 * @author Gaspar Sinai
 * @version 2007-11-27
 */
SPattern::SPattern (void) :
ACT_NONE ("none"), 
ACT_ERROR ("error"), 
ACT_NUMBER ("number"), 
ACT_KEYWORD ("keyword"), 
ACT_VARIABLE ("variable"), 
ACT_DEFINE ("define"), 
ACT_CONTROL ("control"),
ACT_OTHER ("other")
{
  clear ();
}

SPattern::~SPattern ()
{
}

//
void
SPattern::clear ()
{
  action = ACT_NONE;
  matchBegin = SD_MATCH_EOD;
  matchEnd = 0;
  next = 0;
  current.clear ();
}

// This method will be overwritten
bool
SPattern::checkMatch ()
{
  return checkMatchTest();
}

void
SPattern::append (SS_UCS4 in)
{
  if (next == 0) matchEnd = 0;
  current.append (in);
  next++;
}

// We all ignore the last character in the buffer,
// that is next position, and it is non-inclusive
// note that current position is next - 1, and
// we dont look at current.
bool
SPattern::checkMatchTest ()
{
  // We are lucky, none of our staring chars are used in the other pattern.
  if (current.size() >= 3 &&
    current[current.size()-3] == 0x512A // YUU
      && current[current.size()-2] == 0x5B50) // KO
  {
    if (singleMatchTest (3)) return true;
    current.remove(0);
    current.remove(0);

    matchBegin = next-1-2;
    matchEnd = next-1;
    action = ACT_DEFINE;
    return true;
  }
  else if (current.size() >= 2
    && current[current.size()-2] == (SS_UCS4)'\n') // YUU
  {
    if (singleMatchTest (2)) return true;
    current.remove(0);
    matchBegin = next-1-1;
    matchEnd = next-1;
    action = ACT_CONTROL;
    return true;
  }
  else if (current.size() >= 5
    && current[current.size()-5] == (SS_UCS4)'y' 
    && current[current.size()-4] == (SS_UCS4)'u' 
    && current[current.size()-3] == (SS_UCS4)'k' 
    && current[current.size()-2] == (SS_UCS4)'o') 
  {
    if (singleMatchTest (5)) return true;
    current.remove(0);
    current.remove(0);
    current.remove(0);
    current.remove(0);
    matchBegin = next-1-4;
    matchEnd = next-1;
    action = ACT_ERROR;
    return true;
  }
  else if (current.size() >= 5
    && current[current.size()-5] == (SS_UCS4)'Y' 
    && current[current.size()-4] == (SS_UCS4)'u' 
    && current[current.size()-3] == (SS_UCS4)'k' 
    && current[current.size()-2] == (SS_UCS4)'o') 
  {
    if (singleMatchTest (5)) return true;
    current.remove(0);
    current.remove(0);
    current.remove(0);
    current.remove(0);
    matchBegin = next-1-4;
    matchEnd = next-1;
    action = ACT_KEYWORD;
    return true;
  }
  bool isEOD = current.size() > 0 &&  current[current.size()-1] == SD_MATCH_EOD;
  return singleMatchTest (isEOD ? 1: 5);
}

// match current[0] and remove it.
bool
SPattern::singleMatchTest (unsigned int leaveSize)
{
  if (current.size() > leaveSize)
  {
    matchBegin = matchEnd;
    matchEnd++;
    action = ACT_NONE;
    SD_CharClass cc = getCharClass (current[0]);
    switch (cc)
    {
    // letter
    case SD_CC_Lu:
    case SD_CC_Ll:
    case SD_CC_Lt:
    case SD_CC_Lm:
    case SD_CC_Lo:
      current.remove (0);
      if (!singleMatchTest (leaveSize))
      {
        return false;
      }
      return true;
    case SD_CC_Nd:
    case SD_CC_Nl:
    case SD_CC_No:
      action = ACT_NUMBER;
      break;
    case SD_CC_So:
      action = ACT_OTHER;
      break;
    default:
      action = ACT_CONTROL;
    }
    current.remove (0);
    return true;
  }
  return false;
}