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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
|
/***************************************************************************
saparser.cpp - description
-------------------
begin : Wed Feb 11 2004
copyright : (C) 2004 Andras Mantia <amantia@kde.org>
***************************************************************************/
/***************************************************************************
* *
* 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. *
* *
***************************************************************************/
//qt includes
#include <tqtimer.h>
//kde includes
#include <kdebug.h>
#include <tdetexteditor/editinterface.h>
//own includes
#include "saparser.h"
#include "sagroupparser.h"
#include "document.h"
#include "dtds.h"
#include "node.h"
#include "parsercommon.h"
#include "qtag.h"
#include "quantacommon.h"
#include "resource.h"
//#define DEBUG_PARSER
SAParser::SAParser()
{
m_write = 0L;
m_baseNode = 0L;
m_currentNode = 0L;
m_quotesRx = TQRegExp("\"|'");
m_specialInsideXml = false;
m_parsingEnabled = true;
m_synchronous = true;
m_parseOneLineTimer = new TQTimer(this);
connect(m_parseOneLineTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotParseOneLine()));
m_parseInDetailTimer = new TQTimer(this);
connect(m_parseInDetailTimer, TQT_SIGNAL(timeout()), this, TQT_SLOT(slotParseNodeInDetail()));
}
SAParser::~SAParser()
{
}
void SAParser::init(Node *node, Document* write)
{
m_baseNode = node;
m_write = write;
m_dtd = write->defaultDTD();
}
bool SAParser::slotParseOneLine()
{
if ((!m_parsingEnabled || !baseNode) && !m_synchronous)
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "slotParseOneLine - interrupted" << endl;
#endif
return false;
}
if (s_line <= s_endLine)
{
s_contextFound = false;
switch (s_currentContext.type)
{
case Group:
case Text: {
int areaEndPos = -1;
int quotedStringPos = -1;
int commentPos = -1;
int groupKeywordPos = -1;
if (s_searchContent || (s_parentNode && s_parentNode->tag->dtd()->family == Xml))
{
//search for different s_contexts
if (s_searchContent) //search for quoted strings, comments, groups only in non-comment special areas
{
quotedStringPos = s_textLine.find(m_quotesRx, s_col); //quoted strings
s_searchedString = s_textLine.left(quotedStringPos);
commentPos = s_searchedString.find(s_dtd->commentsStartRx, s_col); //comments
s_searchedString = s_textLine.left(commentPos);
if (s_fullParse)
groupKeywordPos = s_searchedString.find(s_dtd->structRx, s_col); //groups, like { }
} else
s_searchedString = s_textLine;
int specialAreaPos = -1;
if (s_searchForSpecialAreas) //special area inside special area
{
s_searchedString = s_textLine.left(groupKeywordPos);
specialAreaPos = s_searchedString.find(s_dtd->specialAreaStartRx, s_col);
}
if (s_searchForAreaEnd) //the end of the special area
{
s_searchedString = s_textLine.left(specialAreaPos);
areaEndPos = s_searchedString.find(s_areaEndString, s_col);
} else
if (s_searchForForcedAreaEnd) //the end of the special area if a forcing string was specified
{
s_searchedString = s_textLine.left(specialAreaPos);
areaEndPos = s_searchedString.find(s_forcedAreaRx, s_col);
if (areaEndPos != -1)
s_areaEndString = s_forcedAreaRx.cap();
}
//check which s_context was found first
if (quotedStringPos != -1) //is it a quoted string?
{
if ( (quotedStringPos < commentPos || commentPos == -1) &&
(quotedStringPos < groupKeywordPos || groupKeywordPos == -1) &&
(quotedStringPos < specialAreaPos || specialAreaPos == -1) &&
(quotedStringPos < areaEndPos || areaEndPos == -1) )
{
s_context.type = QuotedString;
s_context.area.bCol = quotedStringPos;
s_context.startString = s_textLine.mid(quotedStringPos, 1);
s_contextFound = true;
}
}
if (!s_contextFound && commentPos != -1) //is it a comment?
{
if ( (commentPos < groupKeywordPos || groupKeywordPos == -1) &&
(commentPos < specialAreaPos || specialAreaPos == -1) &&
(commentPos < areaEndPos || areaEndPos == -1) )
{
s_context.type = Comment;
s_context.area.bCol = commentPos;
s_context.startString = s_dtd->commentsStartRx.cap();
s_contextFound = true;
}
}
if (!s_contextFound && groupKeywordPos != -1) //is it a group structure?
{
if ( (groupKeywordPos < specialAreaPos || specialAreaPos == -1) &&
(groupKeywordPos < areaEndPos || areaEndPos == -1) )
{
TQString foundText = s_dtd->structRx.cap();
if (foundText == s_dtd->structBeginStr)
{
s_context.type = Group;
s_context.area.bCol = groupKeywordPos;
s_context.startString = foundText;
//create a text node until the struct. beginning
s_currentContext.area.eLine = s_line;
s_currentContext.area.eCol = groupKeywordPos + foundText.length() - 1;
if (s_currentNode &&
(s_currentNode->tag->type == Tag::Text ||
s_currentNode->tag->type == Tag::Empty) )
ParserCommon::appendAreaToTextNode(m_write, s_currentContext.area, s_currentNode);
else
s_currentNode = ParserCommon::createTextNode(m_write, s_currentNode, s_line, s_currentContext.area.eCol + 1, s_currentContext.parentNode);
s_currentNode->tag->type = Tag::ScriptStructureBegin;
s_currentNode->tag->single = false;
s_currentNode->insideSpecial = true;
s_currentNode->specialInsideXml = m_specialInsideXml;
s_currentContext.lastNode = s_currentNode;
s_contextStack.push(s_currentContext);
s_currentContext.parentNode = s_currentNode;
s_col = s_context.area.bCol + s_context.startString.length();
s_currentContext.area.bLine = s_line;
s_currentContext.area.bCol = s_col;
s_currentContext.type = Group;
if (m_synchronous)
//slotParseOneLine();
return true;
else
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseOneLine from parseArea (opening group struct)." << endl;
#endif
m_parseOneLineTimer->start(0, true);
}
return true;
} else //it's a closing group structure element (like "}")
{
if (s_currentContext.type != Group)
{
s_col = groupKeywordPos + foundText.length();
if (m_synchronous)
//slotParseOneLine();
return true;
else
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseOneLine from parseArea (closing group struct)." << endl;
#endif
m_parseOneLineTimer->start(0, true);
}
return true;
}
s_currentContext.area.eLine = s_line;
s_currentContext.area.eCol = groupKeywordPos - 1;
//kdDebug(24000) << TQString("Group Struct s_context: %1, %2, %3, %4").arg( s_currentContext.bLine).arg(s_currentContext.bCol).arg(s_currentContext.eLine).arg(s_currentContext.eCol) << endl;
if (s_currentNode &&
(s_currentNode->tag->type == Tag::Text ||
s_currentNode->tag->type == Tag::Empty) )
ParserCommon::appendAreaToTextNode(m_write, s_currentContext.area, s_currentNode);
else
s_currentNode = ParserCommon::createTextNode(m_write, s_currentNode, s_line, groupKeywordPos, s_currentContext.parentNode);
if (s_currentNode)
{
s_currentNode->insideSpecial = true;
s_currentNode->specialInsideXml = m_specialInsideXml;
}
s_previousContext = s_contextStack.pop();
s_currentContext.parentNode = s_previousContext.parentNode;
s_currentContext.lastNode = s_previousContext.lastNode;
s_currentContext.type = s_previousContext.type;
s_currentContext.area.bLine = s_line;
s_currentContext.area.bCol = groupKeywordPos + foundText.length();
s_currentContext.area.eLine = s_currentContext.area.eCol = -1;
s_currentContext.startString = s_previousContext.startString;
s_col = s_currentContext.area.bCol;
Tag *tag = new Tag();
tag->name = foundText;
tag->setStr(foundText);
tag->setWrite(m_write);
tag->setTagPosition(s_line, groupKeywordPos, s_line, s_col - 1);
tag->setDtd(s_dtd);
tag->type = Tag::ScriptStructureEnd;
tag->single = true;
Node *node = new Node(s_currentContext.parentNode);
nodeNum++;
node->tag = tag;
node->insideSpecial = true;
node->specialInsideXml = m_specialInsideXml;
if (s_currentContext.parentNode && !s_currentContext.parentNode->child)
{
s_currentContext.parentNode->child = node;
}
else if (s_currentContext.lastNode)
{
node->prev = s_currentContext.lastNode;
s_currentContext.lastNode->next = node;
}
s_currentNode = node;
if (m_synchronous)
return true;
else
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseOneLine from parseArea (group structure)." << endl;
#endif
m_parseOneLineTimer->start(0, true);
}
return true;
}
}
}
if (!s_contextFound && specialAreaPos != -1) //is it a special area?
{
if (specialAreaPos < areaEndPos || areaEndPos == -1)
{
TQString foundText = s_dtd->specialAreaStartRx.cap();
s_currentContext.area.eLine = s_line;
s_currentContext.area.eCol = specialAreaPos - 1;
if (s_fullParse)
{
if ( s_currentNode &&
(s_currentNode->tag->type == Tag::Text ||
s_currentNode->tag->type == Tag::Empty) )
ParserCommon::appendAreaToTextNode(m_write, s_currentContext.area, s_currentNode);
else
s_currentNode = ParserCommon::createTextNode(m_write, s_currentNode, s_line, specialAreaPos, s_currentContext.parentNode);
if (s_currentNode)
{
s_currentNode->insideSpecial = true;
s_currentNode->specialInsideXml = m_specialInsideXml;
}
}
//create a toplevel node for the included special area
AreaStruct area(s_line, specialAreaPos, s_line, specialAreaPos + foundText.length() - 1);
Node *node = ParserCommon::createScriptTagNode(m_write, area, foundText, s_dtd, s_currentContext.parentNode, s_currentNode);
#ifdef DEBUG_PARSER
kdDebug(24001) << "Parsing a nested area." << endl;
#endif
AreaStruct area2(s_line, specialAreaPos, s_endLine, s_endCol);
SAParser *p = new SAParser();
p->init(m_baseNode, m_write);
s_currentNode = p->parseArea(area2, foundText, "", node, s_fullParse, true);
s_line = p->lastParsedLine();
s_col = p->lastParsedColumn();
delete p;
s_currentContext.area.bLine = s_line;
s_currentContext.area.bCol = s_col + 1;
s_textLine = ParserCommon::getLine(m_write, s_line, s_endLine, s_endCol);
s_col++;
if (m_synchronous)
{
return true;
}
else
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseOneLine from slotParseOneLine (nested area)." << endl;
#endif
m_parseOneLineTimer->start(0, true);
return true;
}
}
}
} else //when we look only for the area end string
if (s_searchForAreaEnd)
{
areaEndPos = s_textLine.find(s_areaEndString, s_col);
} else
if (s_searchForForcedAreaEnd)
{
areaEndPos = s_textLine.find(s_forcedAreaRx, s_col);
if (areaEndPos != -1)
s_areaEndString = s_forcedAreaRx.cap();
}
if (!s_contextFound && areaEndPos != -1) //did we find the end of the special area?
{
m_lastParsedLine = s_line;
m_lastParsedCol = areaEndPos + s_areaEndString.length() - 1;
s_currentContext.area.eLine = s_line;
s_currentContext.area.eCol = areaEndPos - 1;
//Always create a node between the opening and closing special area nodes.
//This fixes the "commnet loss" bug when editing in VPL and autocompletion
//for simple special areas like <? a ?>
if (s_fullParse || !s_parentNode->child)
{
if ( s_currentNode &&
(s_currentNode->tag->type == Tag::Text ||
s_currentNode->tag->type == Tag::Empty) )
ParserCommon::appendAreaToTextNode(m_write, s_currentContext.area, s_currentNode);
else
{
s_currentNode = ParserCommon::createTextNode(m_write, s_currentNode, s_line, areaEndPos, s_parentNode);
}
if (s_currentNode)
{
s_currentNode->insideSpecial = true;
s_currentNode->specialInsideXml = m_specialInsideXml;
}
}
//kdDebug(24000) << TQString("Special area %1 ends at %2, %3").arg(s_dtd->name).arg(s_line).arg(lastCol) << endl;
//create a closing node for the special area
Tag *tag = new Tag();
tag->setTagPosition(s_line, areaEndPos, s_line, m_lastParsedCol);
tag->parse(s_areaEndString, m_write);
tag->setDtd(s_dtd);
tag->type = Tag::XmlTagEnd;
tag->validXMLTag = false; //FIXME: this is more or less a workaround. We should introduce and handle Tag::ScriptTagEnd
tag->single = true;
//at this point s_parentNode = the opening node of the special area (eg. <?)
//and it should always exist
Node *node = new Node(s_parentNode->parent);
nodeNum++;
s_parentNode->next = node;
node->prev = s_parentNode;
node->tag = tag;
node->closesPrevious = true;
if (s_fullParse)
{
Node *g_node, *g_endNode;
g_node = s_parentNode->child;
/* g_endNode = s_currentNode;
if (g_node && g_node == g_endNode)
g_endNode = s_parentNode->next;*/
g_endNode = node;
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseForScriptGroup from slotParseOneLine." << endl;
#endif
// slotParseForScriptGroup();
if (!m_synchronous)
{
bool parsingLastNode = true;
Node *n = g_endNode;
while (n)
{
n = n->nextSibling();
if (n && n->insideSpecial)
{
parsingLastNode = false;
break;
}
}
SAGroupParser *groupParser = new SAGroupParser(this, write(), g_node, g_endNode, m_synchronous, parsingLastNode, true);
connect(groupParser, TQT_SIGNAL(rebuildStructureTree(bool)), TQT_SIGNAL(rebuildStructureTree(bool)));
connect(groupParser, TQT_SIGNAL(cleanGroups()), TQT_SIGNAL(cleanGroups()));
connect(groupParser, TQT_SIGNAL(parsingDone(SAGroupParser*)), TQT_SLOT(slotGroupParsingDone(SAGroupParser*)));
groupParser->slotParseForScriptGroup();
m_groupParsers.append(groupParser);
}
}
m_lastParsedNode = node;
s_useReturnVars = true;
if (!m_synchronous)
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling parsingDone from slotParseOneLine (area end found)." << endl;
#endif
m_lastParsedNode = parsingDone();
}
return false; //parsing finished
}
if (s_contextFound)
{
s_context.area.bLine = s_line;
s_context.area.eLine = s_context.area.eCol = -1;
s_context.parentNode = s_currentContext.parentNode;
s_currentContext.area.eLine = s_context.area.bLine;
s_currentContext.area.eCol = s_context.area.bCol - 1;
// s_currentContext.parentNode = s_parentNode;
s_contextStack.push(s_currentContext);
if (s_fullParse)
{
if (s_currentNode &&
(s_currentNode->tag->type == Tag::Text || s_currentNode->tag->type == Tag::Empty) )
{
ParserCommon::appendAreaToTextNode(m_write, s_currentContext.area, s_currentNode);
s_currentNode->insideSpecial = true;
s_currentNode->specialInsideXml = m_specialInsideXml;
} else
if (s_currentContext.area.bLine < s_currentContext.area.eLine ||
(s_currentContext.area.bLine == s_currentContext.area.eLine &&
s_currentContext.area.bCol < s_currentContext.area.eCol))
{
//create a tag from the s_currentContext
Tag *tag = new Tag(s_currentContext.area, m_write, s_dtd);
TQString tagStr = tag->tagStr();
tag->cleanStr = tagStr;
QuantaCommon::removeCommentsAndQuotes(tag->cleanStr, s_dtd);
if (tagStr.simplifyWhiteSpace().isEmpty())
{
tag->type = Tag::Empty;
} else
{
tag->type = Tag::Text;
}
tag->single = true;
//create a node with the above tag
Node *node = new Node(s_currentContext.parentNode);
nodeNum++;
node->tag = tag;
node->insideSpecial = true;
node->specialInsideXml = m_specialInsideXml;
if (s_currentContext.parentNode && !s_currentContext.parentNode->child)
{
s_currentContext.parentNode->child = node;
}
else if (s_currentNode)
{
node->prev = s_currentNode;
s_currentNode->next = node;
}
s_currentNode = node;
}
}
//kdDebug(24000) << TQString("%1 s_context: %2, %3, %4, %5").arg(s_currentContext.type).arg( s_currentContext.bLine).arg(s_currentContext.bCol).arg(s_currentContext.eLine).arg(s_currentContext.eCol) << endl;
s_currentContext = s_context;
s_col = s_context.area.bCol + s_context.startString.length();
} else
{
s_line++;
s_col = 0;
s_textLine = ParserCommon::getLine(m_write, s_line, s_endLine, s_endCol);
}
break;
}
case QuotedString:
{
int pos = -1;
int p = s_col;
int l = s_textLine.length();
while (p < l)
{
p = s_textLine.find(s_currentContext.startString, p);
if (p != -1)
{
if (p >= 0)
{
int i = p - 1;
int slahNum = 0;
while (i > 0 && s_textLine[i] == '\\')
{
slahNum++;
i--;
}
if (p == 0 || (slahNum % 2 == 0))
{
pos = p;
break;
}
}
} else
break;
p++;
}
if (pos != -1)
{
// if (pos != 0) pos++;
s_currentContext.area.eLine = s_line;
s_currentContext.area.eCol = pos;
//kdDebug(24000) << TQString("Quoted String s_context: %1, %2, %3, %4").arg( s_currentContext.bLine).arg(s_currentContext.bCol).arg(s_currentContext.eLine).arg(s_currentContext.eCol) << endl;
if (s_fullParse)
{
if ( s_currentNode &&
(s_currentNode->tag->type == Tag::Text ||
s_currentNode->tag->type == Tag::Empty) )
ParserCommon::appendAreaToTextNode(m_write, s_currentContext.area, s_currentNode);
else
s_currentNode = ParserCommon::createTextNode(m_write, 0L, s_line, pos, s_currentContext.parentNode);
s_currentNode->insideSpecial = true;
s_currentNode->specialInsideXml = m_specialInsideXml;
}
s_previousContext = s_contextStack.pop();
s_currentContext.parentNode = s_previousContext.parentNode;
s_currentContext.type = s_previousContext.type;
s_currentContext.area.bLine = s_line;
s_currentContext.area.bCol = pos + 1;
s_currentContext.area.eLine = s_currentContext.area.eCol = -1;
s_currentContext.startString = s_previousContext.startString;
s_col = pos + 1;
} else
{
s_line++;
s_col = 0;
s_textLine = ParserCommon::getLine(m_write, s_line, s_endLine, s_endCol);
}
break;
}
case Comment:
{
int pos = s_textLine.find(s_dtd->comments[s_currentContext.startString], s_col);
if (pos == -1 && s_dtd->comments[s_currentContext.startString] == "\n")
{
int pos2 = s_textLine.find(s_areaEndString, s_col);
if (pos2 != -1)
{
pos = pos2 - 1;
} else
{
pos = s_textLine.length();
}
}
if (pos != -1)
{
s_currentContext.area.eLine = s_line;
s_currentContext.area.eCol = pos + s_dtd->comments[s_currentContext.startString].length() - 1;
s_currentContext.type = s_previousContext.type;
//kdDebug(24000) << TQString("Comment s_context: %1, %2, %3, %4").arg( s_currentContext.bLine).arg(s_currentContext.bCol).arg(s_currentContext.eLine).arg(s_currentContext.eCol) << endl;
if (s_fullParse)
{
//create a tag with the comment
Tag *tag = new Tag(s_currentContext.area, m_write, s_dtd);
tag->type = Tag::Comment;
tag->single = true;
//create a node with the above tag
Node *node = new Node(s_currentContext.parentNode);
nodeNum++;
node->tag = tag;
node->insideSpecial = true;
node->specialInsideXml = m_specialInsideXml;
if (s_currentNode && s_currentNode != node->parent)
{
s_currentNode->next = node;
node->prev = s_currentNode;
} else
if (node->parent && !node->parent->child)
node->parent->child = node;
s_currentNode = node;
}
s_previousContext = s_contextStack.pop();
s_currentContext.parentNode = s_previousContext.parentNode;
s_currentContext.type = s_previousContext.type;
s_currentContext.area.bLine = s_line;
s_currentContext.area.bCol = s_currentContext.area.eCol + 1;
s_currentContext.area.eLine = s_currentContext.area.eCol = -1;
s_currentContext.startString = s_previousContext.startString;
s_col = s_currentContext.area.bCol;
} else
{
s_line++;
s_col = 0;
s_textLine = ParserCommon::getLine(m_write, s_line, s_endLine, s_endCol);
}
break;
}
default:
{
s_line++;
s_col = 0;
s_textLine = ParserCommon::getLine(m_write, s_line, s_endLine, s_endCol);
}
}
if (m_synchronous)
{
//slotParseOneLine();
}
else
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseOneLine from slotParseOneLine." << endl;
#endif
m_parseOneLineTimer->start(0, true);
}
} else
{
if (!m_synchronous)
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling parsingDone from slotParseOneLine." << endl;
#endif
parsingDone();
}
return false; //parsing finished
}
return true;
}
Node* SAParser::parseArea(const AreaStruct &specialArea,
const TQString &areaStartString,
const TQString &forcedAreaEndString,
Node *parentNode,
bool fullParse, bool synchronous)
{
m_synchronous = synchronous;
s_parentNode = parentNode;
s_fullParse = fullParse;
#ifdef DEBUG_PARSER
kdDebug(24001) << "parseArea full: " << s_fullParse << " synch: " << m_synchronous <<endl;
#endif
int s_startLine = specialArea.bLine;
int s_startCol = specialArea.bCol;
s_endLine = specialArea.eLine;
s_endCol = specialArea.eCol;
//kdDebug(24000) << TQString("Starting to parse at %1, %2 for %3").arg(s_startLine).arg(s_startCol).arg(areaStartString) << endl;
s_searchForAreaEnd = false;
s_searchForForcedAreaEnd = false;
s_dtd = 0L;
if (s_parentNode && !areaStartString.isEmpty())
{
const DTDStruct *parentDTD = m_dtd;
if (s_parentNode->parent)
parentDTD = s_parentNode->parent->tag->dtd();
s_dtd = DTDs::ref()->find(parentDTD->specialAreaNames[areaStartString]);
s_areaEndString = parentDTD->specialAreas[areaStartString];
s_searchForAreaEnd = true;
}
if (!forcedAreaEndString.isEmpty())
{
s_forcedAreaRx.setPattern(forcedAreaEndString);
s_forcedAreaRx.setCaseSensitive(m_dtd->caseSensitive);
s_searchForForcedAreaEnd = true;
s_searchForAreaEnd = false;
if (s_parentNode)
s_dtd = s_parentNode->tag->dtd();
}
s_searchContent = true;
if (s_parentNode && s_parentNode->tag->type == Tag::Comment)
s_searchContent = false;
if (!s_dtd)
{
if (s_parentNode)
s_dtd = s_parentNode->tag->dtd(); //fallback, usually when the special area is a comment
else
s_dtd = m_dtd; //fallback when there is no s_parentNode
}
m_write->addDTEP(s_dtd->name);
s_searchForSpecialAreas = (s_dtd->specialAreas.count() > 0);
if (s_parentNode && s_parentNode->tag->type == Tag::Comment)
s_searchForSpecialAreas = false;
s_col = s_startCol + areaStartString.length();
s_line = s_startLine;
s_textLine = m_write->text(s_startLine, 0, s_startLine, m_write->editIf->lineLength(s_startLine));
if (s_line == s_endLine)
{
if (s_endCol > 0)
s_textLine.truncate(s_endCol + 1);
else
s_textLine = "";
}
s_previousContext.type = Unknown;
s_currentContext.type = Text;
s_currentContext.area.bLine = s_line;
s_currentContext.area.bCol = s_col;
s_currentContext.area.eLine = s_currentContext.area.eCol = -1;
s_currentContext.parentNode = s_parentNode;
s_currentNode = s_parentNode;
m_lastParsedNode = 0L;
s_useReturnVars = false;
if (s_line <= s_endLine)
{
if (m_synchronous)
{
while (slotParseOneLine()); //actually this parses the whole area, as synchronous == true
if (s_useReturnVars) //this is true if the special area end was found
{
return m_lastParsedNode;
}
}
else
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseOneLine from parseArea." << endl;
#endif
m_parseOneLineTimer->start(0, true);
return 0L;
}
}
if (m_synchronous) //if the special area end was not found and we are in synchronous mode
{
s_next = 0L;
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling parsingDone from parseArea." << endl;
#endif
s_currentNode = parsingDone();
return s_currentNode;
}
return 0L;
}
Node *SAParser::parsingDone()
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "parsingDone. Use return values:" << s_useReturnVars << endl;
#endif
if (s_useReturnVars)
{
if (s_fullParse)
{
Node *n = m_lastParsedNode;
if (m_useNext)
{
// kdDebug(24000) << "m_lastParsedNode: " << m_lastParsedNode << " " << m_lastParsedNode->tag->tagStr() << endl;
n->next = s_next;
if (s_next)
{
s_next->prev = n;
}
n->prev = s_parentNode;
}
m_currentNode = n->nextSibling();
if (m_currentNode)
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseNodeInDetail from parsingDone (use return values)" << endl;
#endif
m_parseInDetailTimer->start(0, true);
return m_lastParsedNode;
}
else
{
m_parsingEnabled = true;
#ifdef DEBUG_PARSER
kdDebug(24001) << "Emitting rebuildStructureTree from parsingDone (use return values). Enable parsing." << endl;
#endif
emit rebuildStructureTree(false);
#ifdef DEBUG_PARSER
// kdDebug(24000) << "Calling cleanGroups from SAParser::parsingDone" << endl;
#endif
emit cleanGroups();
}
}
m_currentNode = 0L;
return m_lastParsedNode;
}
if (!s_currentNode)
{
s_currentNode = ParserCommon::createTextNode(m_write, s_parentNode, s_endLine, s_endCol, s_parentNode);
if (s_currentNode)
{
s_currentNode->insideSpecial = true;
s_currentNode->specialInsideXml = m_specialInsideXml;
}
}
else if (s_parentNode && !s_parentNode->next)
{
s_currentNode = ParserCommon::createTextNode(m_write, s_currentNode, s_endLine, s_endCol, s_parentNode);
s_currentNode->insideSpecial = true;
s_currentNode->specialInsideXml = m_specialInsideXml;
}
if (s_fullParse)
{
Node *n;
if (s_parentNode)
{
n = s_parentNode;//->child;
} else
{
n = s_currentNode;
while (n && n->prev)
n = n->prev;
s_currentNode = n;
}
Node *g_node = n;
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseForScriptGroup from parsingDone. Synch:" << m_synchronous << endl;
#endif
//parse for groups only when doing aynchronous detailed parsing
if (!m_synchronous)
{
SAGroupParser *groupParser = new SAGroupParser(this, write(), g_node, 0L, m_synchronous, true /*last node*/, true);
connect(groupParser, TQT_SIGNAL(rebuildStructureTree(bool)), TQT_SIGNAL(rebuildStructureTree(bool)));
connect(groupParser, TQT_SIGNAL(cleanGroups()), TQT_SIGNAL(cleanGroups()));
connect(groupParser, TQT_SIGNAL(parsingDone(SAGroupParser*)), TQT_SLOT(slotGroupParsingDone(SAGroupParser*)));
groupParser->slotParseForScriptGroup();
m_groupParsers.append(groupParser);
}
}
m_lastParsedLine = s_endLine;
m_lastParsedCol = s_endCol + 1;
if (s_fullParse && m_currentNode)
{
if (m_useNext && s_currentNode)
{
// kdDebug(24000) << "s_currentNode: " << s_currentNode << endl;
Node *n = s_currentNode;
n->next = s_next;
if (s_next)
s_next->prev = n;
}
m_currentNode = m_currentNode->nextSibling();
if (m_currentNode)
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseNodeInDetail from parsingDone." << endl;
#endif
m_parseInDetailTimer->start(0, true);
emit rebuildStructureTree(false);
}
else
{
m_parsingEnabled = true;
#ifdef DEBUG_PARSER
kdDebug(24001) << "Emitting detailedParsingDone from parsingDone. Enable parsing." << endl;
#endif
emit rebuildStructureTree(false);
}
}
m_currentNode = 0L;
return s_currentNode;
}
void SAParser::parseInDetail(bool synchronous)
{
// synchronous = true; //for testing. Uncomment to test the parser in synchronous mode
// return; //for testing. Uncomment to disable the detailed parser
#ifdef DEBUG_PARSER
kdDebug(24001) << "parseInDetail. Enabled: " << m_parsingEnabled << endl;
#endif
if (!m_parsingEnabled)
{
m_currentNode = m_baseNode;
m_parsingEnabled = true;
m_synchronous = synchronous;
if (m_currentNode)
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling slotParseNodeInDetail from parseInDetail." << endl;
#endif
slotParseNodeInDetail();
}
}
}
void SAParser::slotParseNodeInDetail()
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "slotParseNodeInDetail. Enabled: " << m_parsingEnabled << " Synch: " << m_synchronous << endl; //this is really heavy debug information, enable only when really needed
#endif
if (m_currentNode && m_parsingEnabled && baseNode)
{
if (m_currentNode->insideSpecial &&
m_currentNode->tag->type != Tag::Comment &&
m_currentNode->tag->type != Tag::Text &&
m_currentNode->tag->type != Tag::Empty)
{
Node::deleteNode(m_currentNode->child);
m_currentNode->child = 0L;
AreaStruct area(m_currentNode->tag->area());
s_next = 0L;
m_useNext = false;
//FIXME: Find out why can the tag become 0L
if (m_currentNode->next && m_currentNode->next->tag)
{
AreaStruct area2(m_currentNode->next->tag->area());
area.eLine = area2.eLine;
area.eCol = area2.eCol + 1;
s_next = m_currentNode->next->next;
if (m_currentNode->next->closesPrevious)
{
m_currentNode->next->removeAll = false;
Node *secondNext = m_currentNode->next->next;
if (secondNext)
secondNext->prev = m_currentNode;
Node::deleteNode(m_currentNode->next);
m_currentNode->next = secondNext;
m_useNext = true;
}
} else
{
area.eLine = m_write->editIf->numLines() - 1;
area.eCol = m_write->editIf->lineLength(area.eLine);
}
#ifdef DEBUG_PARSER
kdDebug(24001) << "Calling parseArea from slotParseNodeInDetail." << endl;
#endif
TQString areaStartString = m_currentNode->tag->tagStr();
if (m_currentNode->tag->dtd()->specialAreaNames[areaStartString].isEmpty())
{
AreaStruct area2(m_currentNode->tag->area());
area.bLine = area2.eLine;
area.bCol = area2.eCol + 1;
parseArea(area, "", "</"+m_currentNode->tag->name+"\\s*>", m_currentNode, true, m_synchronous);
}
else
parseArea(area, m_currentNode->tag->tagStr(), "", m_currentNode, true, m_synchronous);
} else
{
// Node *node = m_currentNode;
m_currentNode = m_currentNode->nextSibling();
if (m_currentNode)
{
#ifdef DEBUG_PARSER
// kdDebug(24001) << "Calling slotParseNodeInDetail from slotParseNodeInDetail." << endl; //this is really heavy debug information, enable only when really needed
#endif
m_parseInDetailTimer->start(0, true);
} else
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Emitting rebuildStructureTree from slotParseNodeInDetail." << endl;
#endif
emit rebuildStructureTree(false);
}
}
}
}
void SAParser::setParsingEnabled(bool enabled)
{
#ifdef DEBUG_PARSER
kdDebug(24001) << "Parsing enabled: " << enabled << endl;
#endif
m_parsingEnabled = enabled;
if (!enabled)
{
m_parseOneLineTimer->stop();
m_parseInDetailTimer->stop();
for (TQValueList<SAGroupParser*>::Iterator it = m_groupParsers.begin(); it != m_groupParsers.end(); ++it)
{
(*it)->m_parseForGroupTimer->stop();
delete (*it);
}
m_groupParsers.clear();
}
}
void SAParser::slotGroupParsingDone(SAGroupParser *groupParser)
{
m_groupParsers.remove(groupParser);
delete groupParser;
}
#include "saparser.moc"
|