File: LexMPT.cxx

package info (click to toggle)
scite 4.4.5-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,928 kB
  • sloc: cpp: 145,539; ansic: 19,277; python: 6,120; makefile: 806; perl: 200; sh: 184; sql: 160; objc: 18; ruby: 6; tcl: 6; php: 4
file content (191 lines) | stat: -rw-r--r-- 5,257 bytes parent folder | download | duplicates (7)
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
// Scintilla source code edit control
/** @file LexMPT.cxx
 ** Lexer for MPT specific files. Based on LexOthers.cxx
 ** LOT = the text log file created by the MPT application while running a test program
 ** Other MPT specific files to be added later.
 **/
// Copyright 2003 by Marius Gheorghe <mgheorghe@cabletest.com>
// The License.txt file describes the conditions under which this software may be distributed.

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>

#include <string>

#include "ILexer.h"
#include "Scintilla.h"
#include "SciLexer.h"

#include "WordList.h"
#include "LexAccessor.h"
#include "Accessor.h"
#include "StyleContext.h"
#include "CharacterSet.h"
#include "LexerModule.h"

using namespace Scintilla;

static int GetLotLineState(std::string &line) {
	if (line.length()) {
		// Most of the time the first non-blank character in line determines that line's type
		// Now finds the first non-blank character
		unsigned i; // Declares counter here to make it persistent after the for loop
		for (i = 0; i < line.length(); ++i) {
			if (!(IsASCII(line[i]) && isspace(line[i])))
				break;
		}

		// Checks if it was a blank line
		if (i == line.length())
			return SCE_LOT_DEFAULT;

		switch (line[i]) {
		case '*': // Fail measurement
			return SCE_LOT_FAIL;

		case '+': // Header
		case '|': // Header
			return SCE_LOT_HEADER;

		case ':': // Set test limits
			return SCE_LOT_SET;

		case '-': // Section break
			return SCE_LOT_BREAK;

		default:  // Any other line
			// Checks for message at the end of lot file
			if (line.find("PASSED") != std::string::npos) {
				return SCE_LOT_PASS;
			}
			else if (line.find("FAILED") != std::string::npos) {
				return SCE_LOT_FAIL;
			}
			else if (line.find("ABORTED") != std::string::npos) {
				return SCE_LOT_ABORT;
			}
			else {
				return i ? SCE_LOT_PASS : SCE_LOT_DEFAULT;
			}
		}
	}
	else {
		return SCE_LOT_DEFAULT;
	}
}

static void ColourizeLotDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
	styler.StartAt(startPos);
	styler.StartSegment(startPos);
	bool atLineStart = true;// Arms the 'at line start' flag
	char chNext = styler.SafeGetCharAt(startPos);
	std::string line("");
	line.reserve(256);	// Lot lines are less than 256 chars long most of the time. This should avoid reallocations

	// Styles LOT document
	Sci_PositionU i;			// Declared here because it's used after the for loop
	for (i = startPos; i < startPos + length; ++i) {
		char ch = chNext;
		chNext = styler.SafeGetCharAt(i + 1);
		line += ch;
		atLineStart = false;

		// LOT files are only used on the Win32 platform, thus EOL == CR+LF
		// Searches for the end of line
		if (ch == '\r' && chNext == '\n') {
			line += chNext; // Gets the '\n'
			++i; // Advances past the '\n'
			chNext = styler.SafeGetCharAt(i + 1); // Gets character of next line
			styler.ColourTo(i, GetLotLineState(line));
			line = "";
			atLineStart = true; // Arms flag for next line
		}
	}

	// Last line may not have a line ending
	if (!atLineStart) {
		styler.ColourTo(i - 1, GetLotLineState(line));
	}
}

// Folds an MPT LOT file: the blocks that can be folded are:
// sections (headed by a set line)
// passes (contiguous pass results within a section)
// fails (contiguous fail results within a section)
static void FoldLotDoc(Sci_PositionU startPos, Sci_Position length, int, WordList *[], Accessor &styler) {
	bool foldCompact = styler.GetPropertyInt("fold.compact", 0) != 0;
	Sci_PositionU endPos = startPos + length;
	int visibleChars = 0;
	Sci_Position lineCurrent = styler.GetLine(startPos);

	char chNext = styler.SafeGetCharAt(startPos);
	int style = SCE_LOT_DEFAULT;
	int styleNext = styler.StyleAt(startPos);
	int lev = SC_FOLDLEVELBASE;

	// Gets style of previous line if not at the beginning of the document
	if (startPos > 1)
		style = styler.StyleAt(startPos - 2);

	for (Sci_PositionU i = startPos; i < endPos; i++) {
		char ch = chNext;
		chNext = styler.SafeGetCharAt(i + 1);

		if (ch == '\r' && chNext == '\n') {
			// TO DO:
			// Should really get the state of the previous line from the styler
			int stylePrev = style;
			style = styleNext;
			styleNext = styler.StyleAt(i + 2);

			switch (style) {
/*
			case SCE_LOT_SET:
				lev = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
				break;
*/
			case SCE_LOT_FAIL:
/*
				if (stylePrev != SCE_LOT_FAIL)
					lev = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
				else
					lev = SC_FOLDLEVELBASE + 1;
*/
				lev = SC_FOLDLEVELBASE;
				break;

			default:
				if (lineCurrent == 0 || stylePrev == SCE_LOT_FAIL)
					lev = SC_FOLDLEVELBASE | SC_FOLDLEVELHEADERFLAG;
				else
					lev = SC_FOLDLEVELBASE + 1;

				if (visibleChars == 0 && foldCompact)
					lev |= SC_FOLDLEVELWHITEFLAG;
				break;
			}

			if (lev != styler.LevelAt(lineCurrent))
				styler.SetLevel(lineCurrent, lev);

			lineCurrent++;
			visibleChars = 0;
		}

		if (!isspacechar(ch))
			visibleChars++;
	}

	int flagsNext = styler.LevelAt(lineCurrent) & ~SC_FOLDLEVELNUMBERMASK;
	styler.SetLevel(lineCurrent, lev | flagsNext);
}

static const char * const emptyWordListDesc[] = {
	0
};

LexerModule lmLot(SCLEX_LOT, ColourizeLotDoc, "lot", FoldLotDoc, emptyWordListDesc);