File: SSyntax.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 (614 lines) | stat: -rw-r--r-- 15,057 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
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
/** 
 *  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/SSyntax.h"
#include "stoolkit/syntax/SHunspellPattern.h"
#include "stoolkit/syntax/SSyntaxMarker.h"
#include "stoolkit/SIO.h"
#include "stoolkit/SProperties.h"
#include "stoolkit/SUtil.h"


static SStringVector syntaxSearchPath;

SString
SSyntax::getLibraryLocation () {
  SStringVector path = getPath();
  SHunspellPattern::loadLibrary (path);
  return SString(SHunspellPattern::getLibraryLocation()); 
}

SSyntax::SSyntax (void) : parser ("")
{
  textData = 0;
  pattern = 0;
  listener = 0;
  syntaxListener = 0;
  syntaxState = 0;
}

SSyntax::~SSyntax ()
{
  if (syntaxState) delete syntaxState;
  if (pattern) delete pattern;
  clear ();
}

bool
SSyntax::isSupported (const SString& syn)
{
  SStringVector v(syn, ":", true);
  if (v.size() != 2) return false;
  if (v[0] == "test") return true;
  if (v[0] == "hunspell") return true;
  return false;
}

/**
 * @param ps - supported properties:
 *  none
 *  xml
 *  properties
 */
bool
SSyntax::setSyntax (const SString&  ps)
{
  parser = ps;
  if (pattern)
  {
    delete pattern;
    pattern = 0;
  }
  if (syntaxState)
  {
    delete syntaxState; 
    syntaxState = 0;
    if (syntaxListener) syntaxListener->syntaxChanged (
       SSyntaxListener::SD_PARSING_DONE);
  }
  clear ();
  if (ps == "")
  {
    return true;
  }
  SStringVector split (ps, ":", true);
  if (split.size() != 2)
  {
    parser = "";
    return false;
  }
  if (split[0] == "test") // update isSupported()
  {
    if (split[1] == "yuko")
    {
      pattern = new SPattern ();
      CHECK_NEW (pattern);
      lineGlobalChange ();
      return true;
    }
    else if (split[1] == "lines")
    {
      pattern = new SPattern ();
      CHECK_NEW (pattern);
      lineGlobalChange ();
      return true;
    }
  }
  else if (split[0] == "hunspell")
  {
    pattern = new SHunspellPattern (split[1], getPath());  
    CHECK_NEW (pattern);
    if (!pattern->isValid ())
    {
      delete pattern;
      pattern = 0;
      parser = "";
      return false;
    }
    lineGlobalChange ();
    return true;
  }
  parser = "";
  return false;
}

const SString&
SSyntax::getParser () const
{
  return parser;
}

void SSyntax::clear ()
{ 
  unsigned int i;
  for (i=0; i<syntaxLines.size(); i++)
  {
     delete syntaxLines[i];
  }
  syntaxLines.clear ();
  for (i=0; i<dataLines.size(); i++)
  {
     delete dataLines[i];
  }
  dataLines.clear ();
  clearIterator ();
  if (syntaxState)
  {
    delete syntaxState;
    syntaxState = 0;
    if (syntaxListener) syntaxListener->syntaxChanged (
       SSyntaxListener::SD_PARSING_DONE);
  }
}

void
SSyntax::clearIterator ()
{
  iteratorDataIndex = STextIndex (0,0);
  iteratorSyntaxIndex = STextIndex (0,0);
}

void
SSyntax::setTextData (const STextData* td)
{
  textData = td;
  clear ();
  lineGlobalChange ();
} 

SSyntax::SS_Tag
SSyntax::getTag (const STextIndex& index)
{
  if (parser.size()==0) return SD_NONE;
  if (index.line > syntaxLines.size()) return SD_NONE;
  if (index.index > syntaxLines[index.line]->size()) return SD_NONE;
  // Strip off control characters
  return (SS_Tag) (syntaxLines[index.line]->peek (index.index) & 0xff);
}

SSyntax::SS_Tag
SSyntax::getTagByTDI (const STextIndex& index)
{
  if (parser.size()==0) return SD_NONE;
  if (index.line > textData->size()) return SD_NONE;
  if (index.line > syntaxLines.size()) return SD_NONE;
  if (index.index > textData->size(index.line)) return SD_NONE;
  if (parser == "test:lines") return (SS_Tag)(index.line % (int) SD_MAX);

  // return to previous iterator
  if (index.line != iteratorDataIndex.line 
     || index.index < iteratorDataIndex.index)
  {
     iteratorDataIndex.index = 0;
     iteratorSyntaxIndex.index = 0;
  } 
  iteratorDataIndex.line = index.line;
  iteratorSyntaxIndex.line = index.line;

  const SGlyph* g0 = (iteratorDataIndex.index > 0)
   ? textData->peekGlyphAt (STextIndex(iteratorDataIndex.line, 
   iteratorDataIndex.index-1)) : 0;
  while (iteratorDataIndex.index < index.index)
  {
    const SGlyph* g = textData->peekGlyphAt (iteratorDataIndex);
    SV_UCS4 chars = g->getChars ();
    if (chars.size() == 0) break; // sanity
    SV_UCS4 emb = g->getEmbeddingMarks(g0);
    g0 = g;
    iteratorSyntaxIndex.index += (chars.size() + emb.size());
    iteratorDataIndex.index = iteratorDataIndex.index + 1;
  }
  // Strip off embedding control characters
  unsigned int i=0;
  unsigned int max = dataLines[index.line]->size();
  for (i=iteratorSyntaxIndex.index; i<max; i++)
  {
    SS_UCS4 c = dataLines[index.line]->peek (i);
    if (c!=SD_CD_LRO && c!=SD_CD_RLO && c!=SD_CD_LRE && c!=SD_CD_RLE
       && c!=SD_CD_PDF) // most likely we wont have SD_CD_PDF
    {
      break;
    }
  }
  if (i==max) return SD_ERROR;
  if (syntaxLines[index.line]->size()<=i) return SD_ERROR;
  return (SS_Tag) (syntaxLines[index.line]->peek (i) & 0xff);
}

void
SSyntax::lineRemoved (void* src, unsigned int index)
{
  if (parser.size()==0) return;
  if (textData->size() == 0)
  {
     clear ();
     return;
  }
  if (index >= dataLines.size () || index > textData->size())
  {
#if DEBUG_PARSER
     fprintf (stderr, 
        "ERROR: lineRemoved index=%u lines.size=%u textData.size=%u\n", 
         index, lines.size(), textData->size());
#endif
     lineGlobalChange();
     return;
  }
  delete dataLines[index];
  dataLines.remove (index);
  delete syntaxLines[index];
  syntaxLines.remove (index);
  clearIterator ();
  updateSyntaxState (STextIndex (index, 0));
}

void
SSyntax::lineInserted (void* src, unsigned int index)
{
  if (parser.size()==0) return;
  if (index > dataLines.size () || index >= textData->size())
  {
#if 0
     fprintf (stderr, 
        "ERROR: lineInserted index=%u lines.size=%u textData.size=%u\n", 
         index, dataLines.size(), textData->size());
#endif
     lineGlobalChange();
     return;
  }
  SV_UCS4* l = new SV_UCS4(textData->getChars (index));
  CHECK_NEW (l);
  dataLines.insert (index, l);

  SSyntaxRow* row = new SSyntaxRow();
  CHECK_NEW (row);
  for (unsigned int i=0; i<l->size(); i++)
  {
    row->append (0);
  }
  syntaxLines.insert (index, row);
  clearIterator ();
  updateSyntaxState (STextIndex (index, 0));
}

void 
SSyntax::lineChanged (void* src, unsigned int index)
{
  if (parser.size()==0) return;
  if (index > dataLines.size () || index >= textData->size())
  {
#if 0
     fprintf (stderr, 
        "ERROR: lineChanged index=%u lines.size=%u textData.size=%u\n", 
         index, dataLines.size(), textData->size());
#endif
     lineGlobalChange();
     return;
  }

  SV_UCS4* newdl = new SV_UCS4 (textData->getChars (index));
  CHECK_NEW (newdl);
  // check what changed.
  unsigned int floor = 0;
  unsigned int ceiling = 0;

  unsigned int lsize = newdl->size();
  unsigned int dsize = dataLines[index]->size();

  while (floor < lsize && floor < dsize)
  {
    if (newdl->peek  (floor) != dataLines[index]->peek (floor)) break;
    floor++;
  }
  while (ceiling < lsize && ceiling < dsize 
      && floor + ceiling < dsize && floor + ceiling <  lsize)
  {
    if (newdl->peek  (lsize-ceiling-1) 
       != dataLines[index]->peek (dsize-ceiling-1)) break;
    ceiling++;
  }
  SSyntaxRow* newsn = new SSyntaxRow();
  CHECK_NEW (newsn);
  // copy old data
  unsigned int i;
  // fprintf (stderr, "floor=%u ceiling=%u\n", floor, ceiling);
  // keep the syntax but remove the control
  for (i=0; i<floor; i++)
  {
    newsn->append (0xff & (unsigned int)syntaxLines[index]->peek (i));
  }
  for (i=0; i<ceiling; i++)
  {
    newsn->insert (floor, 0xff & (unsigned int)syntaxLines[index]->peek (dsize-i-1));
  }
  for (i=floor; i<lsize-ceiling; i++)
  {
    newsn->insert (i, 0);
  }
  if (newdl->size() != newsn->size())
  {
    fprintf (stderr, "Internal error SSyntax::lineChanged."); 
    lineGlobalChange();
    return;
  }

  delete dataLines[index];
  delete syntaxLines[index];
  dataLines.replace (index, newdl);
  syntaxLines.replace (index, newsn);
  clearIterator ();
  updateSyntaxState (STextIndex (index, 0));
}

void 
SSyntax::lineGlobalChange ()
{
  clear ();
  if (parser.size()==0) return;

  for (unsigned i=0; i<textData->size(); i++)
  {
    lineInserted (this, i);
  }
  updateSyntaxState (STextIndex(0,0));
}
void
SSyntax::updateSyntaxState (const STextIndex ndx)
{
  if (parser.size()==0)
  {
    if (syntaxState)
    {
      delete syntaxState;
      syntaxState = 0;
      if (syntaxListener) syntaxListener->syntaxChanged (
          SSyntaxListener::SD_PARSING_DONE);
    }
    return;
  }
  if (syntaxState)
  {
    STextIndex crawlIndex = syntaxState->getCurrentIndex ();
    // If the crawlIndex is less than our index, dont do anything,
    // this point is not checked yet.
    // the linesizes are cached as we crawl, so we should do this.
    if (crawlIndex.line < ndx.line)
    {
       return;
    }
    delete syntaxState;
    syntaxState = 0;
  }
  // create a new syntaxState
  SSyntaxMarker* marker = new SSyntaxMarker (syntaxLines, dataLines, ndx);
  CHECK_NEW (marker);
  SMatcher* matcher = new SMatcher (*pattern, *marker);
  CHECK_NEW (matcher);
  // effectively this is an idle timer.
  STimer* timer = STimer::newTimer(0, this);
  syntaxState = new SSyntaxState ( matcher, marker, timer);
  CHECK_NEW (syntaxState);
  if (syntaxListener) syntaxListener->syntaxChanged (
       SSyntaxListener::SD_PARSING_STARTED);
}

// do another iteration of syntax checking
// return false if finished, and cleanup syntaxState.
bool
SSyntax::timeout (const SEventSource* s)
{
  if (syntaxState == 0)
  {
    return false;// never
  }
  // 100 characters at a time
  unsigned int count = 0;
  unsigned int oldN = SD_MATCH_EOD;
  SS_UCS4 n = 0;
  while ((n=syntaxState->matcher->find (true)) != SD_MATCH_EOD)
  {
    if (n==SD_MATCH_AGAIN)
    {
      if (count < SD_UNIT_WORK_COUNT) continue;
      return true; // call timer again.
    }
    count++;
    if (oldN == n)
    {
      fprintf (stderr, "Detected infinite loop in matcher at %u.\n",
         oldN);
      n = SD_MATCH_EOD;
      break;
    }
    oldN = n;
    applyActions ();

    // set a sync marker to begin here
    STextIndex idx = syntaxState->marker->position2Index (n); 
    int syn = syntaxState->marker->getSyntaxAt (idx);
    syn = syn | SGC_BEGIN_MARK;
    syntaxState->marker->setSyntaxAt (idx, syn);
  }
  // end of file reached.
  applyActions ();
  if (n != SD_MATCH_EOD)
  {
    // set a sync marker to begin here
    STextIndex idx = syntaxState->marker->position2Index (n); 
    int syn = syntaxState->marker->getSyntaxAt (idx);
    syn = syn | SGC_BEGIN_MARK;
    syntaxState->marker->setSyntaxAt (idx, syn);
  }
  delete syntaxState;
  syntaxState = 0;
  if (syntaxListener) syntaxListener->syntaxChanged (
       SSyntaxListener::SD_PARSING_DONE);
//  fprintf (stderr, "SGC syntax finished.\n");
  return false;
}

void
SSyntax::applyActions ()
{
  syntaxState->marker->beginActionBlock ();
  syntaxState->matcher->applyActions (*syntaxState->marker);
  syntaxState->marker->endActionBlock ();
  STextIndex minModified = syntaxState->marker->minModified;
  STextIndex maxModified = syntaxState->marker->maxModified;
  // maxmodified is incluside.
  if (minModified <= maxModified)
  {
    // we are lazy, and set whole line modified instead of converting
    // our dataLine index to real textData index.
    maxModified.line++;
    maxModified.index=0;
    // reverse index.
    unsigned int lineCeiling = (maxModified.line >= textData->size()) 
       ?  0 : textData->size() -  maxModified.line;
    STextDataEvent evt (minModified);
    if (listener)
    {
       STextDataEvent evt (minModified);
       evt.setRemaining (STextIndex (lineCeiling, 0));
       evt.attribute = true;
       listener->textChanged (this, evt);
    }
  }
}

// It is a setter only
void
SSyntax::addTextDataListener (STextDataListener* _listener)
{
  listener = _listener;
}

void
SSyntax::addSyntaxListener (SSyntaxListener* _listener)
{
  syntaxListener = _listener;
}

void
SSyntax::setPath (const SStringVector& l)
{
  syntaxSearchPath = l;
}

const SStringVector&
SSyntax::getPath ()
{
  if (syntaxSearchPath.size() == 0) 
  {
     guessPath();
  }
  return syntaxSearchPath;
}

/**
 * search files for property in order and set the path to the 
 * property. Always add YUDIT_DATA/syntax
 */
void
SSyntax::guessPath ()
{
  syntaxSearchPath = getUserPath("yudit.syntaxpath", "syntax");
  //fprintf (stderr, "syntaxpath is %*.*s\n", SSARGS(syntaxSearchPath.join(",")));
}


// Get available syntax highlight categories
SStringVector
SSyntax::getCategories ()
{
  SStringVector ret;
//  ret.append ("test");
  ret.append ("hunspell");
  return SStringVector(ret);
}

// Get available syntax within a category. Please note that
// syntax itself should be unique across all categories.
SStringVector
SSyntax::getAvaliableList (const SString& category)
{
  SStringVector ret;
  if (category == "test")
  {
    ret.append ("lines");
    ret.append ("yuko");
    return SStringVector(ret);
  }
  SProperties   prop;
  unsigned int i;
  unsigned int j;
  if (category == "hunspell")
  {
    SStringVector p("*.dic");
    SStringVector spath = getPath();
    for (i=spath.size(); i>0; i--)
    {
      SDir dir (spath[i-1]);
      SStringVector f = dir.list (p);
      for (unsigned int j=0; j<f.size(); j++)
      {
        SString s = f[j];
        if (s.size() > 4) s.truncate (s.size()-4);
        prop.put (s, s);
      }
    }
  }
  for (i=0; i<prop.size(); i++)
  {
    for (j=0; j<prop.size(i); j++)
    {
       ret.append (*prop.get (i, j));
    }
  }
  ret.sort();
  return SStringVector(ret);
}

SString
SSyntax::getFolderFor (const SString& name)
{
  SStringVector v (name, ":", true);
  if (v.size () != 2) return (SString ("none"));
  if (v[0] == "simple") return  (SString ("built-in"));
  if (v[0] == "test") return  (SString ("built-in"));
  if (v[0] == "hunspell")
  {
    return SHunspellPattern::getFolderFor (v[1], getPath());
  }
  return SString ("");
}

SString
SSyntax::getMissingFile (const SString& name)
{
  SStringVector v (name, ":", true);
  if (v.size () != 2) return (SString (""));
  if (v[0] == "simple") return  (SString (""));
  if (v[0] == "test") return  (SString (""));
  if (v[0] == "hunspell")
  {
    return SHunspellPattern::getMissingFile (v[1], getPath());
  }
  return SString ("");
}