Date: 02-16-2022
Return to Index
created by gbSnippets
'This is presented only as a reference. It is pulled directly from
'the Scintilla distribution.
'Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
// Scintilla Source Code edit Control
/** @file LexVB.cxx
** Lexer For Visual Basic AND VBScript.
**/
// Copyright 1998-2005 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#Include <stdlib.h>
#Include <String.h>
#Include <ctype.h>
#Include <stdio.h>
#Include <stdarg.h>
#Include "Platform.h"
#Include "PropSet.h"
#Include "Accessor.h"
#Include "StyleContext.h"
#Include "KeyWords.h"
#Include "Scintilla.h"
#Include "SciLexer.h"
#ifdef SCI_NAMESPACE
using namespace Scintilla;
#EndIf
// Internal State, highlighted as number
#define SCE_B_FILENUMBER SCE_B_DEFAULT+100
Static bool IsVBComment(Accessor &styler, Int Pos, Int Len) {
Return Len > 0 && styler[Pos] == '\'';
}
Static inline bool IsTypeCharacter(Int ch) {
Return ch == '%' || ch == '&' || ch == '@' || ch == '!' || ch == '#' || ch == '$';
}
// Extended to Accept accented characters
Static inline bool IsAWordChar(Int ch) {
Return ch >= 0x80 ||
(isalnum(ch) || ch == '.' || ch == '_');
}
Static inline bool IsAWordStart(Int ch) {
Return ch >= 0x80 ||
(isalpha(ch) || ch == '_');
}
Static inline bool IsANumberChar(Int ch) {
// Not exactly following number definition (several dots are seen as OK, etc.)
// but probably enough in most cases.
Return (ch < 0x80) &&
(isdigit(ch) || toupper(ch) == 'E' ||
ch == '.' || ch == '-' || ch == '+');
}
Static void ColouriseVBDoc(unsigned Int startPos, Int length, Int initStyle,
WordList *keywordlists[], Accessor &styler, bool vbScriptSyntax) {
WordList &keywords = *keywordlists[0];
WordList &keywords2 = *keywordlists[1];
WordList &keywords3 = *keywordlists[2];
WordList &keywords4 = *keywordlists[3];
styler.StartAt(startPos);
Int visibleChars = 0;
Int fileNbDigits = 0;
// Do Not leak onto Next Line
If (initStyle == SCE_B_STRINGEOL || initStyle == SCE_B_COMMENT || initStyle == SCE_B_PREPROCESSOR) {
initStyle = SCE_B_DEFAULT;
}
StyleContext sc(startPos, length, initStyle, styler);
For (; sc.More(); sc.Forward()) {
If (sc.State == SCE_B_OPERATOR) {
sc.SetState(SCE_B_DEFAULT);
} Else If (sc.State == SCE_B_IDENTIFIER) {
If (!IsAWordChar(sc.ch)) {
// In Basic (except VBScript), a variable Name Or a Function Name
// can End with a special character indicating the Type of the value
// held Or returned.
bool skipType = false;
If (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
sc.Forward(); // Skip it
skipType = true;
}
If (sc.ch == ']') {
sc.Forward();
}
char s[100];
sc.GetCurrentLowered(s, SizeOf(s));
If (skipType) {
s[strlen(s) - 1] = '\0';
}
If (strcmp(s, "rem") == 0) {
sc.ChangeState(SCE_B_COMMENT);
} Else {
If (keywords.InList(s)) {
sc.ChangeState(SCE_B_KEYWORD);
} Else If (keywords2.InList(s)) {
sc.ChangeState(SCE_B_KEYWORD2);
} Else If (keywords3.InList(s)) {
sc.ChangeState(SCE_B_KEYWORD3);
} Else If (keywords4.InList(s)) {
sc.ChangeState(SCE_B_KEYWORD4);
} // Else, it is really an identifier...
sc.SetState(SCE_B_DEFAULT);
}
}
} Else If (sc.State == SCE_B_NUMBER) {
// We stop the number definition On non-numerical non-dot non-eE non-sign char
// Also accepts A-F For hex. numbers
If (!IsANumberChar(sc.ch) && !(tolower(sc.ch) >= 'a' && tolower(sc.ch) <= 'f')) {
sc.SetState(SCE_B_DEFAULT);
}
} Else If (sc.State == SCE_B_STRING) {
// VB doubles quotes to preserve them, so just End this String
// State now as a following quote will start again
If (sc.ch == '\"') {
If (sc.chNext == '\"') {
sc.Forward();
} Else {
If (tolower(sc.chNext) == 'c') {
sc.Forward();
}
sc.ForwardSetState(SCE_B_DEFAULT);
}
} Else If (sc.atLineEnd) {
visibleChars = 0;
sc.ChangeState(SCE_B_STRINGEOL);
sc.ForwardSetState(SCE_B_DEFAULT);
}
} Else If (sc.State == SCE_B_COMMENT) {
If (sc.atLineEnd) {
visibleChars = 0;
sc.ForwardSetState(SCE_B_DEFAULT);
}
} Else If (sc.State == SCE_B_PREPROCESSOR) {
If (sc.atLineEnd) {
visibleChars = 0;
sc.ForwardSetState(SCE_B_DEFAULT);
}
} Else If (sc.State == SCE_B_FILENUMBER) {
If (IsADigit(sc.ch)) {
fileNbDigits++;
If (fileNbDigits > 3) {
sc.ChangeState(SCE_B_DATE);
}
} Else If (sc.ch == '\r' || sc.ch == '\n' || sc.ch == ',') {
// Regular uses: Close #1; Put #1, ...; Get #1, ... etc.
// Too bad If date is format #27, Oct, 2003# Or something like that...
// Use regular number State
sc.ChangeState(SCE_B_NUMBER);
sc.SetState(SCE_B_DEFAULT);
} Else If (sc.ch == '#') {
sc.ChangeState(SCE_B_DATE);
sc.ForwardSetState(SCE_B_DEFAULT);
} Else {
sc.ChangeState(SCE_B_DATE);
}
If (sc.State != SCE_B_FILENUMBER) {
fileNbDigits = 0;
}
} Else If (sc.State == SCE_B_DATE) {
If (sc.atLineEnd) {
visibleChars = 0;
sc.ChangeState(SCE_B_STRINGEOL);
sc.ForwardSetState(SCE_B_DEFAULT);
} Else If (sc.ch == '#') {
sc.ForwardSetState(SCE_B_DEFAULT);
}
}
If (sc.State == SCE_B_DEFAULT) {
If (sc.ch == '\'') {
sc.SetState(SCE_B_COMMENT);
} Else If (sc.ch == '\"') {
sc.SetState(SCE_B_STRING);
} Else If (sc.ch == '#' && visibleChars == 0) {
// Preprocessor commands are alone On their Line
sc.SetState(SCE_B_PREPROCESSOR);
} Else If (sc.ch == '#') {
// It can be a date literal, ending with #, Or a file number, From 1 to 511
// The date literal depends On the locale, so anything can go between 's.
// Can be #January 1, 1993# Or #1 Jan 93# Or #05/11/2003#, etc.
// So we Set the FILENUMBER State, AND Switch to DATE If it 't a file number
sc.SetState(SCE_B_FILENUMBER);
} Else If (sc.ch == '&' && tolower(sc.chNext) == 'h') {
// Hexadecimal number
sc.SetState(SCE_B_NUMBER);
sc.Forward();
} Else If (sc.ch == '&' && tolower(sc.chNext) == 'o') {
// Octal number
sc.SetState(SCE_B_NUMBER);
sc.Forward();
} Else If (IsADigit(sc.ch) || (sc.ch == '.' && IsADigit(sc.chNext))) {
sc.SetState(SCE_B_NUMBER);
} Else If (IsAWordStart(sc.ch) || (sc.ch == '[')) {
sc.SetState(SCE_B_IDENTIFIER);
} Else If (isoperator(static_cast<char>(sc.ch)) || (sc.ch == '\\')) { // Integer division
sc.SetState(SCE_B_OPERATOR);
}
}
If (sc.atLineEnd) {
visibleChars = 0;
}
If (!IsASpace(sc.ch)) {
visibleChars++;
}
}
If (sc.State == SCE_B_IDENTIFIER && !IsAWordChar(sc.ch)) {
// In Basic (except VBScript), a variable Name Or a Function Name
// can End with a special character indicating the Type of the value
// held Or returned.
bool skipType = false;
If (!vbScriptSyntax && IsTypeCharacter(sc.ch)) {
sc.Forward(); // Skip it
skipType = true;
}
If (sc.ch == ']') {
sc.Forward();
}
char s[100];
sc.GetCurrentLowered(s, SizeOf(s));
If (skipType) {
s[strlen(s) - 1] = '\0';
}
If (strcmp(s, "rem") == 0) {
sc.ChangeState(SCE_B_COMMENT);
} Else {
If (keywords.InList(s)) {
sc.ChangeState(SCE_B_KEYWORD);
} Else If (keywords2.InList(s)) {
sc.ChangeState(SCE_B_KEYWORD2);
} Else If (keywords3.InList(s)) {
sc.ChangeState(SCE_B_KEYWORD3);
} Else If (keywords4.InList(s)) {
sc.ChangeState(SCE_B_KEYWORD4);
} // Else, it is really an identifier...
sc.SetState(SCE_B_DEFAULT);
}
}
sc.Complete();
}
Static void FoldVBDoc(unsigned Int startPos, Int length, Int,
WordList *[], Accessor &styler) {
Int endPos = startPos + length;
// Backtrack to Previous Line in Case need to Fix its fold Status
Int lineCurrent = styler.GetLine(startPos);
If (startPos > 0) {
If (lineCurrent > 0) {
lineCurrent--;
startPos = styler.LineStart(lineCurrent);
}
}
Int spaceFlags = 0;
Int indentCurrent = styler.IndentAmount(lineCurrent, &spaceFlags, IsVBComment);
char chNext = styler[startPos];
For (Int i = startPos; i < endPos; i++) {
char ch = chNext;
chNext = styler.SafeGetCharAt(i + 1);
If ((ch == '\r' && chNext != '\n') || (ch == '\n') || (i == endPos)) {
Int lev = indentCurrent;
Int indentNext = styler.IndentAmount(lineCurrent + 1, &spaceFlags, IsVBComment);
If (!(indentCurrent & SC_FOLDLEVELWHITEFLAG)) {
// Only non whitespace Lines can be headers
If ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext & SC_FOLDLEVELNUMBERMASK)) {
lev |= SC_FOLDLEVELHEADERFLAG;
} Else If (indentNext & SC_FOLDLEVELWHITEFLAG) {
// Line after is blank so Check the Next - maybe should continue further?
Int spaceFlags2 = 0;
Int indentNext2 = styler.IndentAmount(lineCurrent + 2, &spaceFlags2, IsVBComment);
If ((indentCurrent & SC_FOLDLEVELNUMBERMASK) < (indentNext2 & SC_FOLDLEVELNUMBERMASK)) {
lev |= SC_FOLDLEVELHEADERFLAG;
}
}
}
indentCurrent = indentNext;
styler.SetLevel(lineCurrent, lev);
lineCurrent++;
}
}
}
Static void ColouriseVBNetDoc(unsigned Int startPos, Int length, Int initStyle,
WordList *keywordlists[], Accessor &styler) {
ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, false);
}
Static void ColouriseVBScriptDoc(unsigned Int startPos, Int length, Int initStyle,
WordList *keywordlists[], Accessor &styler) {
ColouriseVBDoc(startPos, length, initStyle, keywordlists, styler, true);
}
Static const char * const vbWordListDesc[] = {
"Keywords",
"user1",
"user2",
"user3",
0
};
LexerModule lmVB(SCLEX_VB, ColouriseVBNetDoc, "vb", FoldVBDoc, vbWordListDesc);
LexerModule lmVBScript(SCLEX_VBSCRIPT, ColouriseVBScriptDoc, "vbscript", FoldVBDoc, vbWordListDesc);
'gbs_00673
'Date: 03-10-2012
http://www.garybeene.com/sw/gbsnippets.htm