~ubuntu-branches/debian/jessie/fox1.6/jessie

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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/********************************************************************************
*                                                                               *
*                     H i g h l i g h t   E n g i n e                           *
*                                                                               *
*********************************************************************************
* Copyright (C) 2002,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
*********************************************************************************
* This program is free software; you can redistribute it and/or modify          *
* it under the terms of the GNU General Public License as published by          *
* the Free Software Foundation; either version 2 of the License, or             *
* (at your option) any later version.                                           *
*                                                                               *
* 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
*********************************************************************************
* $Id: Hilite.cpp,v 1.44 2006/01/22 18:01:10 fox Exp $                          *
********************************************************************************/
#include "fx.h"
#include <new>
#include "FXRex.h"
#include "FXArray.h"
#include "Hilite.h"


/*
  Notes:
  - Restart position: place in text which is default style, a few
    lines of context before the change.
  - Language mode: use wildcard on filename, or forced explicitly.
  - Either simple pattern, or begin/end pattern. Special stop pattern
    to prevent scanning indefinitely.  Patterns may have sub-patterns.
  - Simple pattern must be non-empty; begin/end patterns of a complex
    pattern may be zero-width assertions.
  - Capturing parenthesis are disabled, for speed reasons.
  - Sample text in FXSyntax is for displaying inside interactive
    style setup dialog; it is supposed to contain on instance of
    each pattern matched by the rule base
*/

/*******************************************************************************/


// Default style is all zeroes
const FXHiliteStyle FXSyntax::defaultStyle={
  FXRGBA(0,0,0,0),
  FXRGBA(0,0,0,0),
  FXRGBA(0,0,0,0),
  FXRGBA(0,0,0,0),
  FXRGBA(0,0,0,0),
  FXRGBA(0,0,0,0),
  FXRGBA(0,0,0,0),
  0
  };


FXIMPLEMENT(FXRule,FXObject,NULL,0)


// Fill textstyle with style, returns position of last change+1
static inline void fillstyle(FXchar* textstyle,FXchar style,FXint f,FXint t){
  while(f<t) textstyle[f++]=style;
  }


// Stylize text
FXbool FXRule::stylize(const FXchar*,FXchar*,FXint,FXint,FXint&,FXint&) const {
  return FALSE;
  }


// Stylize body, i.e. after begin pattern has been seen
FXbool FXRule::stylizeBody(const FXchar*,FXchar*,FXint,FXint,FXint&,FXint&) const {
  return FALSE;
  }


FXIMPLEMENT(FXSimpleRule,FXRule,NULL,0)

// Stylize simple expression
FXbool FXSimpleRule::stylize(const FXchar* text,FXchar *textstyle,FXint fm,FXint to,FXint& start,FXint& stop) const {
  if(pat.match(text,to,&start,&stop,REX_NOT_EMPTY|REX_FORWARD,1,fm,fm)){
    fillstyle(textstyle,style,start,stop);
    return TRUE;
    }
  return FALSE;
  }


FXIMPLEMENT(FXBracketRule,FXRule,NULL,0)

// Stylize complex recursive expression
FXbool FXBracketRule::stylizeBody(const FXchar* text,FXchar *textstyle,FXint fm,FXint to,FXint& start,FXint& stop) const {
  FXint head,tail,node;
  start=fm;
  while(fm<to){
    for(node=0; node<rules.no(); node++){
      if(rules[node]->stylize(text,textstyle,fm,to,head,tail)){
        fm=tail;
        goto nxt;
        }
      }
    if(end.match(text,to,&head,&stop,REX_FORWARD,1,fm,fm)){
      fillstyle(textstyle,style,head,stop);
      return TRUE;
      }
    textstyle[fm++]=style;
nxt:continue;
    }
  stop=fm;
  return TRUE;
  }


// Stylize complex recursive expression
FXbool FXBracketRule::stylize(const FXchar* text,FXchar *textstyle,FXint fm,FXint to,FXint& start,FXint& stop) const {
  FXint head,tail;
  if(beg.match(text,to,&start,&tail,REX_FORWARD,1,fm,fm)){
    fillstyle(textstyle,style,start,tail);
    FXBracketRule::stylizeBody(text,textstyle,tail,to,head,stop);
    return TRUE;
    }
  return FALSE;
  }


FXIMPLEMENT(FXSafeBracketRule,FXBracketRule,NULL,0)

// Stylize complex recursive expression with termination pattern
FXbool FXSafeBracketRule::stylizeBody(const FXchar* text,FXchar *textstyle,FXint fm,FXint to,FXint& start,FXint& stop) const {
  FXint head,tail,node;
  start=fm;
  while(fm<to){
    for(node=0; node<rules.no(); node++){
      if(rules[node]->stylize(text,textstyle,fm,to,head,tail)){
        fm=tail;
        goto nxt;
        }
      }
    if(end.match(text,to,&head,&stop,REX_FORWARD,1,fm,fm)){
      fillstyle(textstyle,style,head,stop);
      return TRUE;
      }
    if(esc.match(text,to,&head,&stop,REX_FORWARD,1,fm,fm)){
      fillstyle(textstyle,style,head,stop);
      return TRUE;
      }
    textstyle[fm++]=style;
nxt:continue;
    }
  stop=fm;
  return TRUE;
  }


// Stylize complex recursive expression with termination pattern
FXbool FXSafeBracketRule::stylize(const FXchar* text,FXchar *textstyle,FXint fm,FXint to,FXint& start,FXint& stop) const {
  FXint head,tail;
  if(beg.match(text,to,&start,&tail,REX_FORWARD,1,fm,fm)){
    fillstyle(textstyle,style,start,tail);
    FXSafeBracketRule::stylizeBody(text,textstyle,tail,to,head,stop);
    return TRUE;
    }
  return FALSE;
  }


FXIMPLEMENT(FXMasterRule,FXRule,NULL,0)

// Stylize body
FXbool FXMasterRule::stylizeBody(const FXchar* text,FXchar *textstyle,FXint fm,FXint to,FXint& start,FXint& stop) const {
  FXint head,tail,node;
  start=fm;
  while(fm<to){
    for(node=0; node<rules.no(); node++){
      if(rules[node]->stylize(text,textstyle,fm,to,head,tail)){
        fm=tail;
        goto nxt;
        }
      }
    textstyle[fm++]=style;
nxt:continue;
    }
  stop=to;
  return TRUE;
  }


// Stylize text
FXbool FXMasterRule::stylize(const FXchar* text,FXchar *textstyle,FXint fm,FXint to,FXint& start,FXint& stop) const {
  return FXMasterRule::stylizeBody(text,textstyle,fm,to,start,stop);
  }


FXIMPLEMENT(FXSyntax,FXObject,NULL,0)


// Construct syntax object; needs at least one master rule
FXSyntax::FXSyntax(const FXString& lang):language(lang){
  rules.append(new FXMasterRule("Master",-1,0));
  delimiters=FXText::textDelimiters;
  contextLines=1;
  contextChars=1;
  }


// Match filename against wildcards
FXbool FXSyntax::matchFilename(const FXString& name) const {
  return FXPath::match(extensions,name);
  }


// Match contents against regular expression
FXbool FXSyntax::matchContents(const FXString& text) const {
  return FXRex(contents).match(text);
  }


// Append simple rule
FXint FXSyntax::append(const FXString& name,const FXString& rex,FXint parent){
  register FXint index=rules.no();
  FXASSERT(0<=parent && parent<rules.no());
  FXSimpleRule *rule=new FXSimpleRule(name,rex,parent,index);
  rules.append(rule);
  rules[parent]->rules.append(rule);
  return index;
  }


// Append bracket rule
FXint FXSyntax::append(const FXString& name,const FXString& brex,const FXString& erex,FXint parent){
  register FXint index=rules.no();
  FXASSERT(0<=parent && parent<rules.no());
  FXBracketRule *rule=new FXBracketRule(name,brex,erex,parent,index);
  rules.append(rule);
  rules[parent]->rules.append(rule);
  return index;
  }


// Append safe bracket rule
FXint FXSyntax::append(const FXString& name,const FXString& brex,const FXString& erex,const FXString& srex,FXint parent){
  register FXint index=rules.no();
  FXASSERT(0<=parent && parent<rules.no());
  FXSafeBracketRule *rule=new FXSafeBracketRule(name,brex,erex,srex,parent,index);
  rules.append(rule);
  rules[parent]->rules.append(rule);
  return index;
  }


// Return true if toplevel rule
FXbool FXSyntax::isRoot(FXint rule) const {
  FXASSERT(0<=rule && rule<rules.no());
  return rule==0 || rules[rule]->parent==0;
  }


// Return true if p is ancestor of c
FXbool FXSyntax::isAncestor(FXint p,FXint c) const {
  FXASSERT(0<=p && 0<=c);
  while(c>0){
    c=rules[c]->getParent();
    if(c==p) return TRUE;
    }
  return FALSE;
  }


// Clean up
FXSyntax::~FXSyntax(){
  for(int i=0; i<rules.no(); i++) delete rules[i];
  }