summaryrefslogtreecommitdiff
path: root/support/highlight/src/core/languagedefinition.h
blob: 08caee3fc3c3d49db6941b41f1419726f4ab0999 (plain)
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
/***************************************************************************
                          languagedefinition.h  -  description
                             -------------------
    begin                : Wed Nov 28 2001
    copyright            : (C) 2001-2008 by Andre Simon
    email                : andre.simon1@gmx.de
 ***************************************************************************/


/*
This file is part of Highlight.

Highlight 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 3 of the License, or
(at your option) any later version.

Highlight 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 Highlight.  If not, see <http://www.gnu.org/licenses/>.
*/


#ifndef LANGUAGEDEFINITION_H
#define LANGUAGEDEFINITION_H

#include <string>
#include <map>
#include <iostream>
#include <fstream>
#include <iterator>
#include <sstream>

#include "configurationreader.h"
#include "platform_fs.h"
#include "enums.h"
#include "re/Pattern.h"
#include "re/Matcher.h"

namespace highlight
{

	class RegexElement;

	/** maps keywords and the corresponding class IDs*/
	typedef map <string, int> KeywordMap;

	/** maps embedded langiage names to exit delimiter regexes*/
	typedef map <string, string> EmbedLangDelimMap;

	/**\brief Contains specific data of the programming language being processed.

	   The load() method will only read a new language definition if the given
	   file path is not equal to the path of the current language definition.

	* @author Andre  Simon
	*/

	class LanguageDefinition
	{

		public:

			LanguageDefinition();

			~LanguageDefinition();

			/** \return Symbol string, containg all known symbols with the referencing state ids*/
			const string &getSymbolString() const { return symbolString; }

			/** \return Failed regilar expression */
			const string &getFailedRegex() const { return failedRegex; }

			/** \return Prefix of raw strings */
			unsigned char getRawStringPrefix() const { return rawStringPrefix; }

			/** \return Continuation Character */
			unsigned char getContinuationChar() const { return continuationChar; }

			/** \return true if syntax highlighting is enabled*/
			bool highlightingEnabled() const { return !disableHighlighting;}

			/** \return True if language is case sensitive */
			bool isIgnoreCase() const { return ignoreCase;}

			/** \param s String
			     \return class id of keyword, 0 if s is not a keyword */
			int isKeyword ( const string &s ) ;

			/** Load new language definition
			    \param langDefPath Path of language definition
			    \param clear Test if former data should be deleted
			    \return True if successfull  */
			bool load ( const string& langDefPath, bool clear=true );

			/** \return True if multi line comments may be nested */
			bool allowNestedMLComments() const { return allowNestedComments; }

			/** \return True if highlighting is disabled 
                                    TODO remove method */
			bool highlightingDisabled() const  { return disableHighlighting; }

			/** \return True if the next load() call would load a new language definition
			    \param  langDefPath Path to language definition  */
			bool needsReload ( const string &langDefPath ) const { return currentPath!=langDefPath; }

			/** \return True if current language may be reformatted (c, c++, c#, java) */
			bool enableReformatting() const { return reformatCode;}

			/** \return True if escape sequences are allowed outsde of strings */
			bool allowExtEscSeq() const { return allowExtEscape; }

			/** \return keywords*/
			const KeywordMap& getKeywords() const { return keywords; }

			/** \return keyword classes*/
			const vector<string>& getKeywordClasses() const { return keywordClasses;}

			/** \return regular expressions */
			const vector<RegexElement*>& getRegexElements() const {return regex;};

			/** \return description of the programming language */
			const string & getDescription () const {return langDesc;}

			/**  \param stateID state id
			     \return true,  if no closing delimiter exists (open and close delimiters are equal)
			 */
			bool delimiterIsDistinct ( int stateID )
			{
				return delimiterDistinct[stateID];
			}

			/**  Pairs of open/close tokens have a unique ID to test if two tokens act as delimiters
			     \param token delimiter token
			     \return token ID
			 */
			int getDelimiterPairID ( const string& token )
			{
				return delimiterPair[token];
			}

			string getDelimRegex(const string & lang){
				return exitDelimiters[lang];
			}

			/** initializes end delimiter regex to switch back to host language
				\param langPath path of embedded language definition
			*/
			void restoreLangEndDelim(const string&langPath);

			/** 
				\param lang language definition name  (no path, no ".lang" extension)
				\return absolute path based on the previously loaded definition
			*/
			string getNewPath(const string& lang);

			string getCurrentPath() { return currentPath;}

		private:

			static const string REGEX_IDENTIFIER;
			static const string REGEX_NUMBER;

			// string containing symbols and their IDs of the programming language
			string symbolString;

			// path to laoded language definition
			string currentPath;

			// Language description
			string langDesc;

			string failedRegex;

			KeywordMap keywords;

			vector <string> keywordClasses;

			vector <RegexElement*> regex;

			KeywordMap delimiterPrefixes;

			EmbedLangDelimMap exitDelimiters;

			// saves if delimiter pair consists of the same delimiter symbol
			map <int, bool> delimiterDistinct;

			map <string, int> delimiterPair;

			// keywords are not case sensitive if set
			bool ignoreCase,

			// highlighting is disabled
			disableHighlighting,

			// Escape sequences are allowed outrside of strings
			allowExtEscape,

			// allow nested multi line comment blocks
			allowNestedComments,

			// single line comments have to start in coloumn 1 if set
			fullLineComment,

			// code formatting is enabled if set
			reformatCode;

			// character which is prefix of raw string (c#)
			unsigned char rawStringPrefix,

			//character which continues curreent style on next line
			continuationChar;

			/* reset members */
			void reset();

			// add a symbol sequence to the symbolStream
			void addSimpleSymbol ( stringstream& symbolStream, State state,
			                       const string& paramValue );

			void addSymbol ( stringstream& symbolStream,
			                 State stateBegin,
			                 State stateEnd,
			                 bool isDelimiter,
			                 const string& paramValue,
			                 unsigned int classID=0 );

			// add a delimiter symbol sequence to the symbolStream
			void addDelimiterSymbol ( stringstream& symbolStream,
			                          State stateBegin, State stateEnd,
			                          const string& paramValue,
			                          unsigned int classID=0 );

			void addDelimiterRegex ( stringstream& symbolStream,
	       					 State stateBegin, State stateEnd,
	        				 const string& paramValue, const string& langName);



			//set flag if paramValue is defined
			void  getFlag ( string& paramValue, bool& flag );

			void getSymbol ( const string& paramValue, unsigned char& symbol );

			// generate a unique class ID of the class name
			unsigned int generateNewKWClass ( const string& newClassName );

			// add keywords to the given class
			void addKeywords ( const string &kwList,State stateBegin, State stateEnd, int classID );

			struct RegexDef extractRegex ( const string &paramValue );

			Pattern * reDefPattern;

	};


	/**\brief Association of a regex with a state description

	  A RegexElement associates a regular expression with the state information
	  (opening and closing state, pattern, keyword class, keyword group id, language name)
	*/
	class RegexElement
	{
		public:
			RegexElement() 
			:open ( STANDARD ), end ( STANDARD ), rePattern ( NULL ), kwClass ( 0 ),capturingGroup ( -1 ), langName()
			{
			}

			RegexElement ( State oState, State eState, Pattern *re, unsigned int cID=0, int group=-1, const string& name="" ) :
					open ( oState ), end ( eState ), rePattern ( re ), kwClass ( cID ), capturingGroup ( group ), langName(name)
			{
				// cerr << "new re element "<<  rePattern->getPattern() <<" open: "<<open<<" end "<<end<<"\n";
			}

			~RegexElement() { if ( rePattern ) delete rePattern; }

			State open, ///< opening state
			end;  ///< closing state
			Pattern *rePattern;          ///< regex pattern
			unsigned int kwClass;        ///< keyword class
			int capturingGroup;          ///< capturing group ID
			string langName;             ///< language name

		private:
			RegexElement (const RegexElement& rhs){
				// does not work because Pattern misses copy constructor
				/*open=rhs.open;
				end=rhs.end;
				kwClass=rhs.kwClass;
				capturingGroup=rhs.capturingGroup;
				Pattern *pOrig=rePattern;
				rePattern=new Pattern(*rhs.rePattern);
				delete pOrig;*/
			}
			RegexElement& operator=(const RegexElement& rhs){
				// does not work because Pattern misses copy constructor
				/*open=rhs.open;
				end=rhs.end;
				kwClass=rhs.kwClass;
				capturingGroup=rhs.capturingGroup;
				Pattern *pOrig=rePattern;
				rePattern=new Pattern(*rhs.rePattern);
				delete pOrig;
				*/
				return *this;
			}
	};

	/**\brief Association of a regex and its relevant capturing group
	*/
	struct RegexDef
	{
		RegexDef() :capturingGroup ( -1 ) {}
		string reString;     ///< regex string
		int capturingGroup;  ///< capturing group which should be recognized as token
	};

}
#endif