1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 #ifndef _SVTOOLS_SYNTAXHIGHLIGHT_HXX
24 #define _SVTOOLS_SYNTAXHIGHLIGHT_HXX
25 
26 #include <list>
27 
28 #include <vos/macros.hxx>
29 #include <vos/mutex.hxx>
30 
31 #include <vcl/svapp.hxx>
32 
33 #include <tools/stream.hxx>
34 #include <tools/shl.hxx>
35 
36 #include <svl/brdcst.hxx>
37 #include <svtools/svtdllapi.h>
38 
39 
40 // for the bsearch
41 #ifdef WNT
42 #define CDECL _cdecl
43 #endif
44 #if defined(UNX) || defined(OS2)
45 #define CDECL
46 #endif
47 #ifdef UNX
48 #include <sys/resource.h>
49 #endif
50 
51 #include <stdio.h>
52 
53 #include <tools/string.hxx>
54 #include <tools/gen.hxx>
55 
56 
57 // Token-Typen TT_...
58 enum TokenTypes
59 {
60 	TT_UNKNOWN,
61 	TT_IDENTIFIER,
62 	TT_WHITESPACE,
63 	TT_NUMBER,
64 	TT_STRING,
65 	TT_EOL,
66 	TT_COMMENT,
67 	TT_ERROR,
68 	TT_OPERATOR,
69 	TT_KEYWORDS,
70 	TT_PARAMETER
71 };
72 
73 struct HighlightPortion { sal_uInt16 nBegin; sal_uInt16 nEnd; TokenTypes tokenType; };
74 
75 
76 typedef std::vector<HighlightPortion> HighlightPortions;
77 
78 /////////////////////////////////////////////////////////////////////////
79 // Hilfsklasse zur Untersuchung von JavaScript-Modulen, zunaechst zum
80 // Heraussuchen der Funktionen, spaeter auch zum Syntax-Highlighting verwenden
81 
82 //	Flags fuer Zeichen-Eigenschaften
83 #define CHAR_START_IDENTIFIER	0x0001
84 #define CHAR_IN_IDENTIFIER		0x0002
85 #define CHAR_START_NUMBER		0x0004
86 #define CHAR_IN_NUMBER			0x0008
87 #define CHAR_IN_HEX_NUMBER		0x0010
88 #define CHAR_IN_OCT_NUMBER		0x0020
89 #define CHAR_START_STRING		0x0040
90 #define CHAR_OPERATOR			0x0080
91 #define CHAR_SPACE				0x0100
92 #define CHAR_EOL				0x0200
93 
94 #define CHAR_EOF				0x00
95 
96 
97 // Sprachmodus des HighLighters (spaeter eventuell feiner
98 // differenzieren mit Keyword-Liste, C-Kommentar-Flag)
99 enum HighlighterLanguage
100 {
101 	HIGHLIGHT_BASIC,
102 	HIGHLIGHT_SQL
103 };
104 
105 class SimpleTokenizer_Impl
106 {
107 	HighlighterLanguage aLanguage;
108 	// Zeichen-Info-Tabelle
109 	sal_uInt16 aCharTypeTab[256];
110 
111 	const sal_Unicode* mpStringBegin;
112 	const sal_Unicode* mpActualPos;
113 
114 	// Zeile und Spalte
115 	sal_uInt32 nLine;
116 	sal_uInt32 nCol;
117 
peekChar(void)118 	sal_Unicode peekChar( void )	{ return *mpActualPos; }
getChar(void)119 	sal_Unicode getChar( void )		{ nCol++; return *mpActualPos++; }
120 
121 	// Hilfsfunktion: Zeichen-Flag Testen
122 	sal_Bool testCharFlags( sal_Unicode c, sal_uInt16 nTestFlags );
123 
124 	// Neues Token holen, Leerstring == nix mehr da
125 	sal_Bool getNextToken( /*out*/TokenTypes& reType,
126 		/*out*/const sal_Unicode*& rpStartPos, /*out*/const sal_Unicode*& rpEndPos );
127 
128 	String getTokStr( /*out*/const sal_Unicode* pStartPos, /*out*/const sal_Unicode* pEndPos );
129 
130 #ifdef DBG_UTIL
131 	// TEST: Token ausgeben
132 	String getFullTokenStr( /*out*/TokenTypes eType,
133 		/*out*/const sal_Unicode* pStartPos, /*out*/const sal_Unicode* pEndPos );
134 #endif
135 
136 	const char** ppListKeyWords;
137 	sal_uInt16 nKeyWordCount;
138 
139 public:
140 	SimpleTokenizer_Impl( HighlighterLanguage aLang = HIGHLIGHT_BASIC );
141 	~SimpleTokenizer_Impl( void );
142 
143 	sal_uInt16 parseLine( sal_uInt32 nLine, const String* aSource );
144 	void getHighlightPortions( sal_uInt32 nParseLine, const String& rLine,
145 													/*out*/HighlightPortions& portions );
146 	void setKeyWords( const char** ppKeyWords, sal_uInt16 nCount );
147 };
148 
149 
150 //*** SyntaxHighlighter-Klasse ***
151 // Konzept: Der Highlighter wird ueber alle Aenderungen im Source
152 // informiert (notifyChange) und liefert dem Aufrufer jeweils die
153 // Information zurueck, welcher Zeilen-Bereich des Source-Codes
154 // aufgrund dieser Aenderung neu gehighlighted werden muss.
155 // Dazu merkt sich Highlighter intern fuer jede Zeile, ob dort
156 // C-Kommentare beginnen oder enden.
157 class SVT_DLLPUBLIC SyntaxHighlighter
158 {
159 	HighlighterLanguage eLanguage;
160 	SimpleTokenizer_Impl* m_pSimpleTokenizer;
161 	char* m_pKeyWords;
162 	sal_uInt16 m_nKeyWordCount;
163 
164 //	void initializeKeyWords( HighlighterLanguage eLanguage );
165 
166 public:
167 	SyntaxHighlighter( void );
168 	~SyntaxHighlighter( void );
169 
170 	// HighLighter (neu) initialisieren, die Zeilen-Tabelle wird
171 	// dabei komplett geloescht, d.h. im Abschluss wird von einem
172 	// leeren Source ausgegangen. In notifyChange() kann dann
173 	// nur Zeile 0 angegeben werden.
174 	void initialize( HighlighterLanguage eLanguage_ );
175 
176 	const Range notifyChange( sal_uInt32 nLine, sal_Int32 nLineCountDifference,
177 								const String* pChangedLines, sal_uInt32 nArrayLength);
178 
179 	void getHighlightPortions( sal_uInt32 nLine, const String& rLine,
180 											HighlightPortions& pPortions );
181 
GetLanguage()182 	HighlighterLanguage GetLanguage() { return eLanguage;}
183 };
184 #endif
185