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 #ifndef SSyntax_h
21 #define SSyntax_h
22 
23 #include "stoolkit/STextData.h"
24 #include "stoolkit/SBinVector.h"
25 #include "stoolkit/SBinHashtable.h"
26 #include "stoolkit/SVector.h"
27 #include "stoolkit/SString.h"
28 #include "stoolkit/SStringVector.h"
29 #include "stoolkit/SLineTracker.h"
30 #include "stoolkit/SEvent.h"
31 #include "stoolkit/syntax/SMatcher.h"
32 #include "stoolkit/syntax/SSyntaxMarker.h"
33 
34 // This many characters are processed, or this many unit works
35 // are performed in each timer call.
36 #define SD_UNIT_WORK_COUNT 100
37 
38 class SSyntaxListener
39 {
40 public:
41   enum SS_EventType { SD_PARSING_STARTED, SD_PARSING_INSERT, SD_PARSING_DONE };
SSyntaxListener(void)42   SSyntaxListener (void) {}
~SSyntaxListener()43   virtual ~SSyntaxListener () {}
44   virtual void syntaxChanged (SS_EventType _evnt)=0;
45 };
46 
47 class SSyntaxState
48 {
49 public:
50   // Context will delete the objects in args when destructed
SSyntaxState(SMatcher * _matcher,SSyntaxMarker * _marker,STimer * _timer)51   SSyntaxState (SMatcher* _matcher, SSyntaxMarker* _marker, STimer* _timer)
52     : matcher (_matcher), marker (_marker), timer (_timer)
53     {
54     }
~SSyntaxState()55   ~SSyntaxState ()
56     { delete timer; delete marker; delete matcher; }
getCurrentIndex()57   STextIndex getCurrentIndex  ()
58     { return marker->getCurrentIndex (); }
59   SMatcher*       matcher;
60   SSyntaxMarker*  marker;
61   STimer*         timer;
62 };
63 
64 // SExentTarget is for crawling timeout
65 class SSyntax : public SLineTracker, public SEventTarget
66 {
67 public:
68   // color is a precious resource, we have only
69   // a limited number of them here.
70   // The number of charactrers that are cached are
71   // detemined by SAwt constuctor cacheSize=2000
72 
73   // We have a shadow vrsion of these in  SSyntaxMarker
74   enum SS_Tag { SD_NONE=0, SD_ERROR,
75        SD_NUMBER, SD_STRING, SD_COMMENT, SD_KEYWORD,
76        SD_VARIABLE, SD_DEFINE, SD_CONTROL, SD_OTHER, SD_MAX };
77   SSyntax (void);
78   ~SSyntax ();
79 
80   bool setSyntax (const SString&  ps);
81   const SString& getParser () const;
82 
83   void setTextData (const STextData* td);
84 
85   /* by lines raw index */
86   SS_Tag getTag (const STextIndex& index);
87   /* get text data index */
88   SS_Tag getTagByTDI (const STextIndex& index);
89 
90   virtual void lineRemoved (void* src, unsigned int index);
91   virtual void lineInserted (void* src, unsigned int index);
92   virtual void lineChanged (void* src, unsigned int index);
93 
94   // The patch for the syntax files.
95   static void setPath(const SStringVector &p);
96   static void guessPath(const SStringVector& files, const SString& property);
97   static void guessPath();
98   static const SStringVector& getPath();
99 
100   static bool isSupported (const SString& syn);
101 
102   // This will get called from time-to-time
103   // when this class crawls text and parses
104   // Only whole lines will changed.
105   void addTextDataListener (STextDataListener* _listener);
106   void addSyntaxListener (SSyntaxListener* _listener);
107 
108   static SString       getMissingFile (const SString& name);
109   static SString       getFolderFor (const SString& name);
110   static SStringVector getCategories ();
111   static SStringVector getAvaliableList (const SString& category);
112 
113 private:
114   STextDataListener*       listener;
115   SSyntaxListener*         syntaxListener;
116   const STextData*         textData;
117   SSyntaxState*            syntaxState;
118 
119   void clear ();
120   void clearIterator ();
121   void lineGlobalChange ();
122   void applyActions ();
123 
124   // do another iteration of syntax checking
125   virtual bool timeout (const SEventSource* s);
126 
127   // if ndx is a move back restart parsing.
128   // if parse is "" delete parsing
129   // ndx is our index, not datamodel index.
130   void updateSyntaxState (const STextIndex ndx);
131 
132   SString                 parser;
133   SPattern*               pattern;
134 
135   // These two are in sync. They contain the full expanded line
136   SSyntaxData             syntaxLines;
137   SUnicodeData            dataLines;
138   // for consecutive getTagByTDI
139   STextIndex              iteratorSyntaxIndex;  // syntaxLines, dataLines
140   STextIndex              iteratorDataIndex;    // textData
141 
142 };
143 
144 #endif /* SSyntax */
145