1 // Scintilla Lexer for EDIFACT
2 // Written by Iain Clarke, IMCSoft & Inobiz AB.
3 // EDIFACT documented here: https://www.unece.org/cefact/edifact/welcome.html
4 // and more readably here: https://en.wikipedia.org/wiki/EDIFACT
5 // This code is subject to the same license terms as the rest of the scintilla project:
6 // The License.txt file describes the conditions under which this software may be distributed.
7 //
8 
9 // Header order must match order in scripts/HeaderOrder.txt
10 #include <cstdlib>
11 #include <cassert>
12 #include <cstring>
13 #include <cctype>
14 
15 #include "ILexer.h"
16 #include "Scintilla.h"
17 #include "SciLexer.h"
18 
19 #include "LexAccessor.h"
20 #include "LexerModule.h"
21 #include "DefaultLexer.h"
22 
23 using namespace Scintilla;
24 
25 class LexerEDIFACT : public DefaultLexer
26 {
27 public:
28 	LexerEDIFACT();
~LexerEDIFACT()29 	virtual ~LexerEDIFACT() {} // virtual destructor, as we inherit from ILexer
30 
Factory()31 	static ILexer *Factory() {
32 		return new LexerEDIFACT;
33 	}
34 
Version() const35 	int SCI_METHOD Version() const override
36 	{
37 		return lvOriginal;
38 	}
Release()39 	void SCI_METHOD Release() override
40 	{
41 		delete this;
42 	}
43 
PropertyNames()44 	const char * SCI_METHOD PropertyNames() override
45 	{
46 		return "fold\nlexer.edifact.highlight.un.all";
47 	}
PropertyType(const char *)48 	int SCI_METHOD PropertyType(const char *) override
49 	{
50 		return SC_TYPE_BOOLEAN; // Only one property!
51 	}
DescribeProperty(const char * name)52 	const char * SCI_METHOD DescribeProperty(const char *name) override
53 	{
54 		if (!strcmp(name, "fold"))
55 			return "Whether to apply folding to document or not";
56 		if (!strcmp(name, "lexer.edifact.highlight.un.all"))
57 			return "Whether to apply UN* highlighting to all UN segments, or just to UNH";
58 		return NULL;
59 	}
60 
PropertySet(const char * key,const char * val)61 	Sci_Position SCI_METHOD PropertySet(const char *key, const char *val) override
62 	{
63 		if (!strcmp(key, "fold"))
64 		{
65 			m_bFold = strcmp(val, "0") ? true : false;
66 			return 0;
67 		}
68 		if (!strcmp(key, "lexer.edifact.highlight.un.all"))	// GetProperty
69 		{
70 			m_bHighlightAllUN = strcmp(val, "0") ? true : false;
71 			return 0;
72 		}
73 		return -1;
74 	}
DescribeWordListSets()75 	const char * SCI_METHOD DescribeWordListSets() override
76 	{
77 		return NULL;
78 	}
WordListSet(int,const char *)79 	Sci_Position SCI_METHOD WordListSet(int, const char *) override
80 	{
81 		return -1;
82 	}
83 	void SCI_METHOD Lex(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, IDocument *pAccess) override;
84 	void SCI_METHOD Fold(Sci_PositionU startPos, Sci_Position lengthDoc, int initStyle, IDocument *pAccess) override;
PrivateCall(int,void *)85 	void * SCI_METHOD PrivateCall(int, void *) override
86 	{
87 		return NULL;
88 	}
89 
90 protected:
91 	Sci_Position InitialiseFromUNA(IDocument *pAccess, Sci_PositionU MaxLength);
92 	Sci_Position FindPreviousEnd(IDocument *pAccess, Sci_Position startPos) const;
93 	Sci_Position ForwardPastWhitespace(IDocument *pAccess, Sci_Position startPos, Sci_Position MaxLength) const;
94 	int DetectSegmentHeader(char SegmentHeader[3]) const;
95 
96 	bool m_bFold;
97 
98 	// property lexer.edifact.highlight.un.all
99 	//	Set to 0 to highlight only UNA segments, or 1 to highlight all UNx segments.
100 	bool m_bHighlightAllUN;
101 
102 	char m_chComponent;
103 	char m_chData;
104 	char m_chDecimal;
105 	char m_chRelease;
106 	char m_chSegment;
107 };
108 
109 LexerModule lmEDIFACT(SCLEX_EDIFACT, LexerEDIFACT::Factory, "edifact");
110 
111 ///////////////////////////////////////////////////////////////////////////////
112 
113 
114 
115 ///////////////////////////////////////////////////////////////////////////////
116 
LexerEDIFACT()117 LexerEDIFACT::LexerEDIFACT()
118 {
119 	m_bFold = false;
120 	m_bHighlightAllUN = false;
121 	m_chComponent = ':';
122 	m_chData = '+';
123 	m_chDecimal = '.';
124 	m_chRelease = '?';
125 	m_chSegment = '\'';
126 }
127 
Lex(Sci_PositionU startPos,Sci_Position lengthDoc,int,IDocument * pAccess)128 void LexerEDIFACT::Lex(Sci_PositionU startPos, Sci_Position lengthDoc, int, IDocument *pAccess)
129 {
130 	Sci_PositionU posFinish = startPos + lengthDoc;
131 	InitialiseFromUNA(pAccess, posFinish);
132 
133 	// Look backwards for a ' or a document beginning
134 	Sci_PositionU posCurrent = FindPreviousEnd(pAccess, startPos);
135 	// And jump past the ' if this was not the beginning of the document
136 	if (posCurrent != 0)
137 		posCurrent++;
138 
139 	// Style buffer, so we're not issuing loads of notifications
140 	LexAccessor styler (pAccess);
141 	pAccess->StartStyling(posCurrent, '\377');
142 	styler.StartSegment(posCurrent);
143 	Sci_Position posSegmentStart = -1;
144 
145 	while ((posCurrent < posFinish) && (posSegmentStart == -1))
146 	{
147 		posCurrent = ForwardPastWhitespace(pAccess, posCurrent, posFinish);
148 		// Mark whitespace as default
149 		styler.ColourTo(posCurrent - 1, SCE_EDI_DEFAULT);
150 		if (posCurrent >= posFinish)
151 			break;
152 
153 		// Does is start with 3 charaters? ie, UNH
154 		char SegmentHeader[4] = { 0 };
155 		pAccess->GetCharRange(SegmentHeader, posCurrent, 3);
156 
157 		int SegmentStyle = DetectSegmentHeader(SegmentHeader);
158 		if (SegmentStyle == SCE_EDI_BADSEGMENT)
159 			break;
160 		if (SegmentStyle == SCE_EDI_UNA)
161 		{
162 			posCurrent += 9;
163 			styler.ColourTo(posCurrent - 1, SCE_EDI_UNA); // UNA
164 			continue;
165 		}
166 		posSegmentStart = posCurrent;
167 		posCurrent += 3;
168 
169 		styler.ColourTo(posCurrent - 1, SegmentStyle); // UNH etc
170 
171 		// Colour in the rest of the segment
172 		for (char c; posCurrent < posFinish; posCurrent++)
173 		{
174 			pAccess->GetCharRange(&c, posCurrent, 1);
175 
176 			if (c == m_chRelease) // ? escape character, check first, in case of ?'
177 				posCurrent++;
178 			else if (c == m_chSegment) // '
179 			{
180 				// Make sure the whole segment is on one line. styler won't let us go back in time, so we'll settle for marking the ' as bad.
181 				Sci_Position lineSegmentStart = pAccess->LineFromPosition(posSegmentStart);
182 				Sci_Position lineSegmentEnd = pAccess->LineFromPosition(posCurrent);
183 				if (lineSegmentStart == lineSegmentEnd)
184 					styler.ColourTo(posCurrent, SCE_EDI_SEGMENTEND);
185 				else
186 					styler.ColourTo(posCurrent, SCE_EDI_BADSEGMENT);
187 				posSegmentStart = -1;
188 				posCurrent++;
189 				break;
190 			}
191 			else if (c == m_chComponent) // :
192 				styler.ColourTo(posCurrent, SCE_EDI_SEP_COMPOSITE);
193 			else if (c == m_chData) // +
194 				styler.ColourTo(posCurrent, SCE_EDI_SEP_ELEMENT);
195 			else
196 				styler.ColourTo(posCurrent, SCE_EDI_DEFAULT);
197 		}
198 	}
199 	styler.Flush();
200 
201 	if (posSegmentStart == -1)
202 		return;
203 
204 	pAccess->StartStyling(posSegmentStart, -1);
205 	pAccess->SetStyleFor(posFinish - posSegmentStart, SCE_EDI_BADSEGMENT);
206 }
207 
Fold(Sci_PositionU startPos,Sci_Position lengthDoc,int,IDocument * pAccess)208 void LexerEDIFACT::Fold(Sci_PositionU startPos, Sci_Position lengthDoc, int, IDocument *pAccess)
209 {
210 	if (!m_bFold)
211 		return;
212 
213 	// Fold at UNx lines. ie, UNx segments = 0, other segments = 1.
214 	// There's no sub folding, so we can be quite simple.
215 	Sci_Position endPos = startPos + lengthDoc;
216 	char SegmentHeader[4] = { 0 };
217 
218 	int iIndentPrevious = 0;
219 	Sci_Position lineLast = pAccess->LineFromPosition(endPos);
220 
221 	for (Sci_Position lineCurrent = pAccess->LineFromPosition(startPos); lineCurrent <= lineLast; lineCurrent++)
222 	{
223 		Sci_Position posLineStart = pAccess->LineStart(lineCurrent);
224 		posLineStart = ForwardPastWhitespace(pAccess, posLineStart, endPos);
225 		Sci_Position lineDataStart = pAccess->LineFromPosition(posLineStart);
226 		// Fill in whitespace lines?
227 		for (; lineCurrent < lineDataStart; lineCurrent++)
228 			pAccess->SetLevel(lineCurrent, SC_FOLDLEVELBASE | SC_FOLDLEVELWHITEFLAG | iIndentPrevious);
229 		pAccess->GetCharRange(SegmentHeader, posLineStart, 3);
230 		//if (DetectSegmentHeader(SegmentHeader) == SCE_EDI_BADSEGMENT) // Abort if this is not a proper segment header
231 
232 		int level = 0;
233 		if (memcmp(SegmentHeader, "UNH", 3) == 0) // UNH starts blocks
234 			level = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
235 		// Check for UNA,B and Z. All others are inside messages
236 		else if (!memcmp(SegmentHeader, "UNA", 3) || !memcmp(SegmentHeader, "UNB", 3) || !memcmp(SegmentHeader, "UNZ", 3))
237 			level = SC_FOLDLEVELBASE;
238 		else
239 			level = SC_FOLDLEVELBASE | 1;
240 		pAccess->SetLevel(lineCurrent, level);
241 		iIndentPrevious = level & SC_FOLDLEVELNUMBERMASK;
242 	}
243 }
244 
InitialiseFromUNA(IDocument * pAccess,Sci_PositionU MaxLength)245 Sci_Position LexerEDIFACT::InitialiseFromUNA(IDocument *pAccess, Sci_PositionU MaxLength)
246 {
247 	MaxLength -= 9; // drop 9 chars, to give us room for UNA:+.? '
248 
249 	Sci_PositionU startPos = 0;
250 	startPos += ForwardPastWhitespace(pAccess, 0, MaxLength);
251 	if (startPos < MaxLength)
252 	{
253 		char bufUNA[9];
254 		pAccess->GetCharRange(bufUNA, startPos, 9);
255 
256 		// Check it's UNA segment
257 		if (!memcmp(bufUNA, "UNA", 3))
258 		{
259 			m_chComponent = bufUNA[3];
260 			m_chData = bufUNA[4];
261 			m_chDecimal = bufUNA[5];
262 			m_chRelease = bufUNA[6];
263 			// bufUNA [7] should be space - reserved.
264 			m_chSegment = bufUNA[8];
265 
266 			return 0; // success!
267 		}
268 	}
269 
270 	// We failed to find a UNA, so drop to defaults
271 	m_chComponent = ':';
272 	m_chData = '+';
273 	m_chDecimal = '.';
274 	m_chRelease = '?';
275 	m_chSegment = '\'';
276 
277 	return -1;
278 }
279 
ForwardPastWhitespace(IDocument * pAccess,Sci_Position startPos,Sci_Position MaxLength) const280 Sci_Position LexerEDIFACT::ForwardPastWhitespace(IDocument *pAccess, Sci_Position startPos, Sci_Position MaxLength) const
281 {
282 	char c;
283 
284 	while (startPos < MaxLength)
285 	{
286 		pAccess->GetCharRange(&c, startPos, 1);
287 		switch (c)
288 		{
289 		case '\t':
290 		case '\r':
291 		case '\n':
292 		case ' ':
293 			break;
294 		default:
295 			return startPos;
296 		}
297 
298 		startPos++;
299 	}
300 
301 	return MaxLength;
302 }
303 
DetectSegmentHeader(char SegmentHeader[3]) const304 int LexerEDIFACT::DetectSegmentHeader(char SegmentHeader[3]) const
305 {
306 	if (
307 		SegmentHeader[0] < 'A' || SegmentHeader[0] > 'Z' ||
308 		SegmentHeader[1] < 'A' || SegmentHeader[1] > 'Z' ||
309 		SegmentHeader[2] < 'A' || SegmentHeader[2] > 'Z')
310 		return SCE_EDI_BADSEGMENT;
311 
312 	if (!memcmp(SegmentHeader, "UNA", 3))
313 		return SCE_EDI_UNA;
314 
315 	if (m_bHighlightAllUN && !memcmp(SegmentHeader, "UN", 2))
316 		return SCE_EDI_UNH;
317 	else if (memcmp(SegmentHeader, "UNH", 3) == 0)
318 		return SCE_EDI_UNH;
319 
320 	return SCE_EDI_SEGMENTSTART;
321 }
322 
323 // Look backwards for a ' or a document beginning
FindPreviousEnd(IDocument * pAccess,Sci_Position startPos) const324 Sci_Position LexerEDIFACT::FindPreviousEnd(IDocument *pAccess, Sci_Position startPos) const
325 {
326 	for (char c; startPos > 0; startPos--)
327 	{
328 		pAccess->GetCharRange(&c, startPos, 1);
329 		if (c == m_chSegment)
330 			return startPos;
331 	}
332 	// We didn't find a ', so just go with the beginning
333 	return 0;
334 }
335 
336 
337