2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
|
|
|
/** @file LexPascal.cxx
|
|
|
|
** Lexer for Pascal.
|
|
|
|
** Written by Laurent le Tynevez
|
|
|
|
** Updated by Simon Steele <s.steele@pnotepad.org> September 2002
|
|
|
|
** Updated by Mathias Rauen <scite@madshi.net> May 2003 (Delphi adjustments)
|
2009-06-24 19:09:31 +00:00
|
|
|
** Completely rewritten by Marko Njezic <sf@maxempire.com> October 2008
|
2009-04-24 23:35:41 +00:00
|
|
|
**/
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
/*
|
|
|
|
|
|
|
|
A few words about features of the new completely rewritten LexPascal...
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
Generally speaking LexPascal tries to support all available Delphi features (up
|
2015-06-07 21:19:26 +00:00
|
|
|
to Delphi XE4 at this time).
|
2009-06-24 19:09:31 +00:00
|
|
|
|
|
|
|
~ HIGHLIGHTING:
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
If you enable "lexer.pascal.smart.highlighting" property, some keywords will
|
|
|
|
only be highlighted in appropriate context. As implemented those are keywords
|
|
|
|
related to property and DLL exports declarations (similar to how Delphi IDE
|
|
|
|
works).
|
2009-06-24 19:09:31 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
For example, keywords "read" and "write" will only be highlighted if they are in
|
2009-06-24 19:09:31 +00:00
|
|
|
property declaration:
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
property MyProperty: boolean read FMyProperty write FMyProperty;
|
2009-06-24 19:09:31 +00:00
|
|
|
|
|
|
|
~ FOLDING:
|
|
|
|
|
|
|
|
Folding is supported in the following cases:
|
|
|
|
|
|
|
|
- Folding of stream-like comments
|
|
|
|
- Folding of groups of consecutive line comments
|
2010-08-21 23:59:56 +00:00
|
|
|
- Folding of preprocessor blocks (the following preprocessor blocks are
|
|
|
|
supported: IF / IFEND; IFDEF, IFNDEF, IFOPT / ENDIF and REGION / ENDREGION
|
2009-06-24 19:09:31 +00:00
|
|
|
blocks), including nesting of preprocessor blocks up to 255 levels
|
2010-08-21 23:59:56 +00:00
|
|
|
- Folding of code blocks on appropriate keywords (the following code blocks are
|
|
|
|
supported: "begin, asm, record, try, case / end" blocks, class & object
|
2009-06-24 19:09:31 +00:00
|
|
|
declarations and interface declarations)
|
|
|
|
|
|
|
|
Remarks:
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
- Folding of code blocks tries to handle all special cases in which folding
|
2009-06-24 19:09:31 +00:00
|
|
|
should not occur. As implemented those are:
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
1. Structure "record case / end" (there's only one "end" statement and "case" is
|
2009-06-24 19:09:31 +00:00
|
|
|
ignored as fold point)
|
2010-08-21 23:59:56 +00:00
|
|
|
2. Forward class declarations ("type TMyClass = class;") and object method
|
|
|
|
declarations ("TNotifyEvent = procedure(Sender: TObject) of object;") are
|
2009-06-24 19:09:31 +00:00
|
|
|
ignored as fold points
|
2010-08-21 23:59:56 +00:00
|
|
|
3. Simplified complete class declarations ("type TMyClass = class(TObject);")
|
2009-06-24 19:09:31 +00:00
|
|
|
are ignored as fold points
|
2010-08-21 23:59:56 +00:00
|
|
|
4. Every other situation when class keyword doesn't actually start class
|
|
|
|
declaration ("class procedure", "class function", "class of", "class var",
|
2009-06-24 19:09:31 +00:00
|
|
|
"class property" and "class operator")
|
2013-08-28 00:44:27 +00:00
|
|
|
5. Forward (disp)interface declarations ("type IMyInterface = interface;") are
|
|
|
|
ignored as fold points
|
2009-06-24 19:09:31 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
- Folding of code blocks inside preprocessor blocks is disabled (any comments
|
|
|
|
inside them will be folded fine) because there is no guarantee that complete
|
|
|
|
code block will be contained inside folded preprocessor block in which case
|
|
|
|
folded code block could end prematurely at the end of preprocessor block if
|
|
|
|
there is no closing statement inside. This was done in order to properly process
|
2009-06-24 19:09:31 +00:00
|
|
|
document that may contain something like this:
|
|
|
|
|
|
|
|
type
|
|
|
|
{$IFDEF UNICODE}
|
|
|
|
TMyClass = class(UnicodeAncestor)
|
|
|
|
{$ELSE}
|
|
|
|
TMyClass = class(AnsiAncestor)
|
|
|
|
{$ENDIF}
|
|
|
|
private
|
|
|
|
...
|
|
|
|
public
|
|
|
|
...
|
|
|
|
published
|
|
|
|
...
|
|
|
|
end;
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
If class declarations were folded, then the second class declaration would end
|
|
|
|
at "$ENDIF" statement, first class statement would end at "end;" statement and
|
|
|
|
preprocessor "$IFDEF" block would go all the way to the end of document.
|
|
|
|
However, having in mind all this, if you want to enable folding of code blocks
|
|
|
|
inside preprocessor blocks, you can disable folding of preprocessor blocks by
|
|
|
|
changing "fold.preprocessor" property, in which case everything inside them
|
2009-06-24 19:09:31 +00:00
|
|
|
would be folded.
|
|
|
|
|
|
|
|
~ KEYWORDS:
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
The list of keywords that can be used in pascal.properties file (up to Delphi
|
2015-06-07 21:19:26 +00:00
|
|
|
XE4):
|
2009-06-24 19:09:31 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
- Keywords: absolute abstract and array as asm assembler automated begin case
|
2015-06-07 21:19:26 +00:00
|
|
|
cdecl class const constructor delayed deprecated destructor dispid dispinterface
|
|
|
|
div do downto dynamic else end except experimental export exports external far
|
|
|
|
file final finalization finally for forward function goto helper if
|
|
|
|
implementation in inherited initialization inline interface is label library
|
|
|
|
message mod near nil not object of on operator or out overload override packed
|
|
|
|
pascal platform private procedure program property protected public published
|
|
|
|
raise record reference register reintroduce repeat resourcestring safecall
|
|
|
|
sealed set shl shr static stdcall strict string then threadvar to try type unit
|
|
|
|
unsafe until uses var varargs virtual while winapi with xor
|
2009-06-24 19:09:31 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
- Keywords related to the "smart highlithing" feature: add default implements
|
2009-06-24 19:09:31 +00:00
|
|
|
index name nodefault read readonly remove stored write writeonly
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
- Keywords related to Delphi packages (in addition to all above): package
|
2009-06-24 19:09:31 +00:00
|
|
|
contains requires
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2009-04-24 23:35:41 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
2010-08-21 23:59:56 +00:00
|
|
|
#include <assert.h>
|
|
|
|
#include <ctype.h>
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "ILexer.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "Scintilla.h"
|
|
|
|
#include "SciLexer.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
|
|
|
|
#include "WordList.h"
|
|
|
|
#include "LexAccessor.h"
|
|
|
|
#include "Accessor.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "StyleContext.h"
|
2009-06-24 19:09:31 +00:00
|
|
|
#include "CharacterSet.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "LexerModule.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
using namespace Scintilla;
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void GetRangeLowered(Sci_PositionU start,
|
|
|
|
Sci_PositionU end,
|
2009-04-24 23:35:41 +00:00
|
|
|
Accessor &styler,
|
|
|
|
char *s,
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU len) {
|
|
|
|
Sci_PositionU i = 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
while ((i < end - start + 1) && (i < len-1)) {
|
|
|
|
s[i] = static_cast<char>(tolower(styler[start + i]));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
s[i] = '\0';
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void GetForwardRangeLowered(Sci_PositionU start,
|
2009-06-24 19:09:31 +00:00
|
|
|
CharacterSet &charSet,
|
|
|
|
Accessor &styler,
|
|
|
|
char *s,
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU len) {
|
|
|
|
Sci_PositionU i = 0;
|
2009-06-24 19:09:31 +00:00
|
|
|
while ((i < len-1) && charSet.Contains(styler.SafeGetCharAt(start + i))) {
|
|
|
|
s[i] = static_cast<char>(tolower(styler.SafeGetCharAt(start + i)));
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
s[i] = '\0';
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
enum {
|
2010-08-21 23:59:56 +00:00
|
|
|
stateInAsm = 0x1000,
|
|
|
|
stateInProperty = 0x2000,
|
|
|
|
stateInExport = 0x4000,
|
|
|
|
stateFoldInPreprocessor = 0x0100,
|
|
|
|
stateFoldInRecord = 0x0200,
|
|
|
|
stateFoldInPreprocessorLevelMask = 0x00FF,
|
2009-06-24 19:09:31 +00:00
|
|
|
stateFoldMaskAll = 0x0FFF
|
|
|
|
};
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
static void ClassifyPascalWord(WordList *keywordlists[], StyleContext &sc, int &curLineState, bool bSmartHighlighting) {
|
2009-04-24 23:35:41 +00:00
|
|
|
WordList& keywords = *keywordlists[0];
|
|
|
|
|
|
|
|
char s[100];
|
2009-06-24 19:09:31 +00:00
|
|
|
sc.GetCurrentLowered(s, sizeof(s));
|
|
|
|
if (keywords.InList(s)) {
|
|
|
|
if (curLineState & stateInAsm) {
|
|
|
|
if (strcmp(s, "end") == 0 && sc.GetRelative(-4) != '@') {
|
|
|
|
curLineState &= ~stateInAsm;
|
|
|
|
sc.ChangeState(SCE_PAS_WORD);
|
|
|
|
} else {
|
|
|
|
sc.ChangeState(SCE_PAS_ASM);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
bool ignoreKeyword = false;
|
|
|
|
if (strcmp(s, "asm") == 0) {
|
|
|
|
curLineState |= stateInAsm;
|
|
|
|
} else if (bSmartHighlighting) {
|
|
|
|
if (strcmp(s, "property") == 0) {
|
|
|
|
curLineState |= stateInProperty;
|
|
|
|
} else if (strcmp(s, "exports") == 0) {
|
|
|
|
curLineState |= stateInExport;
|
|
|
|
} else if (!(curLineState & (stateInProperty | stateInExport)) && strcmp(s, "index") == 0) {
|
|
|
|
ignoreKeyword = true;
|
|
|
|
} else if (!(curLineState & stateInExport) && strcmp(s, "name") == 0) {
|
|
|
|
ignoreKeyword = true;
|
2010-08-21 23:59:56 +00:00
|
|
|
} else if (!(curLineState & stateInProperty) &&
|
|
|
|
(strcmp(s, "read") == 0 || strcmp(s, "write") == 0 ||
|
|
|
|
strcmp(s, "default") == 0 || strcmp(s, "nodefault") == 0 ||
|
|
|
|
strcmp(s, "stored") == 0 || strcmp(s, "implements") == 0 ||
|
|
|
|
strcmp(s, "readonly") == 0 || strcmp(s, "writeonly") == 0 ||
|
2009-06-24 19:09:31 +00:00
|
|
|
strcmp(s, "add") == 0 || strcmp(s, "remove") == 0)) {
|
|
|
|
ignoreKeyword = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ignoreKeyword) {
|
|
|
|
sc.ChangeState(SCE_PAS_WORD);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (curLineState & stateInAsm) {
|
|
|
|
sc.ChangeState(SCE_PAS_ASM);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void ColourisePascalDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordlists[],
|
2009-06-24 19:09:31 +00:00
|
|
|
Accessor &styler) {
|
|
|
|
bool bSmartHighlighting = styler.GetPropertyInt("lexer.pascal.smart.highlighting", 1) != 0;
|
|
|
|
|
|
|
|
CharacterSet setWordStart(CharacterSet::setAlpha, "_", 0x80, true);
|
|
|
|
CharacterSet setWord(CharacterSet::setAlphaNum, "_", 0x80, true);
|
|
|
|
CharacterSet setNumber(CharacterSet::setDigits, ".-+eE");
|
|
|
|
CharacterSet setHexNumber(CharacterSet::setDigits, "abcdefABCDEF");
|
|
|
|
CharacterSet setOperator(CharacterSet::setNone, "#$&'()*+,-./:;<=>@[]^{}");
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position curLine = styler.GetLine(startPos);
|
2009-06-24 19:09:31 +00:00
|
|
|
int curLineState = curLine > 0 ? styler.GetLineState(curLine - 1) : 0;
|
|
|
|
|
|
|
|
StyleContext sc(startPos, length, initStyle, styler);
|
|
|
|
|
|
|
|
for (; sc.More(); sc.Forward()) {
|
|
|
|
if (sc.atLineEnd) {
|
|
|
|
// Update the line state, so it can be seen by next line
|
|
|
|
curLine = styler.GetLine(sc.currentPos);
|
|
|
|
styler.SetLineState(curLine, curLineState);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
// Determine if the current state should terminate.
|
|
|
|
switch (sc.state) {
|
|
|
|
case SCE_PAS_NUMBER:
|
|
|
|
if (!setNumber.Contains(sc.ch) || (sc.ch == '.' && sc.chNext == '.')) {
|
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
|
|
|
} else if (sc.ch == '-' || sc.ch == '+') {
|
|
|
|
if (sc.chPrev != 'E' && sc.chPrev != 'e') {
|
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
break;
|
|
|
|
case SCE_PAS_IDENTIFIER:
|
|
|
|
if (!setWord.Contains(sc.ch)) {
|
|
|
|
ClassifyPascalWord(keywordlists, sc, curLineState, bSmartHighlighting);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
break;
|
|
|
|
case SCE_PAS_HEXNUMBER:
|
|
|
|
if (!setHexNumber.Contains(sc.ch)) {
|
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
break;
|
|
|
|
case SCE_PAS_COMMENT:
|
|
|
|
case SCE_PAS_PREPROCESSOR:
|
|
|
|
if (sc.ch == '}') {
|
|
|
|
sc.ForwardSetState(SCE_PAS_DEFAULT);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
break;
|
|
|
|
case SCE_PAS_COMMENT2:
|
|
|
|
case SCE_PAS_PREPROCESSOR2:
|
|
|
|
if (sc.Match('*', ')')) {
|
|
|
|
sc.Forward();
|
|
|
|
sc.ForwardSetState(SCE_PAS_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PAS_COMMENTLINE:
|
|
|
|
if (sc.atLineStart) {
|
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PAS_STRING:
|
|
|
|
if (sc.atLineEnd) {
|
|
|
|
sc.ChangeState(SCE_PAS_STRINGEOL);
|
|
|
|
} else if (sc.ch == '\'' && sc.chNext == '\'') {
|
|
|
|
sc.Forward();
|
|
|
|
} else if (sc.ch == '\'') {
|
|
|
|
sc.ForwardSetState(SCE_PAS_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PAS_STRINGEOL:
|
|
|
|
if (sc.atLineStart) {
|
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PAS_CHARACTER:
|
|
|
|
if (!setHexNumber.Contains(sc.ch) && sc.ch != '$') {
|
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SCE_PAS_OPERATOR:
|
|
|
|
if (bSmartHighlighting && sc.chPrev == ';') {
|
|
|
|
curLineState &= ~(stateInProperty | stateInExport);
|
|
|
|
}
|
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
|
|
|
break;
|
|
|
|
case SCE_PAS_ASM:
|
|
|
|
sc.SetState(SCE_PAS_DEFAULT);
|
|
|
|
break;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
// Determine if a new state should be entered.
|
|
|
|
if (sc.state == SCE_PAS_DEFAULT) {
|
|
|
|
if (IsADigit(sc.ch) && !(curLineState & stateInAsm)) {
|
|
|
|
sc.SetState(SCE_PAS_NUMBER);
|
|
|
|
} else if (setWordStart.Contains(sc.ch)) {
|
|
|
|
sc.SetState(SCE_PAS_IDENTIFIER);
|
|
|
|
} else if (sc.ch == '$' && !(curLineState & stateInAsm)) {
|
|
|
|
sc.SetState(SCE_PAS_HEXNUMBER);
|
|
|
|
} else if (sc.Match('{', '$')) {
|
|
|
|
sc.SetState(SCE_PAS_PREPROCESSOR);
|
|
|
|
} else if (sc.ch == '{') {
|
|
|
|
sc.SetState(SCE_PAS_COMMENT);
|
|
|
|
} else if (sc.Match("(*$")) {
|
|
|
|
sc.SetState(SCE_PAS_PREPROCESSOR2);
|
|
|
|
} else if (sc.Match('(', '*')) {
|
|
|
|
sc.SetState(SCE_PAS_COMMENT2);
|
|
|
|
sc.Forward(); // Eat the * so it isn't used for the end of the comment
|
|
|
|
} else if (sc.Match('/', '/')) {
|
|
|
|
sc.SetState(SCE_PAS_COMMENTLINE);
|
|
|
|
} else if (sc.ch == '\'') {
|
|
|
|
sc.SetState(SCE_PAS_STRING);
|
|
|
|
} else if (sc.ch == '#') {
|
|
|
|
sc.SetState(SCE_PAS_CHARACTER);
|
|
|
|
} else if (setOperator.Contains(sc.ch) && !(curLineState & stateInAsm)) {
|
|
|
|
sc.SetState(SCE_PAS_OPERATOR);
|
|
|
|
} else if (curLineState & stateInAsm) {
|
|
|
|
sc.SetState(SCE_PAS_ASM);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
if (sc.state == SCE_PAS_IDENTIFIER && setWord.Contains(sc.chPrev)) {
|
|
|
|
ClassifyPascalWord(keywordlists, sc, curLineState, bSmartHighlighting);
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
sc.Complete();
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
static bool IsStreamCommentStyle(int style) {
|
|
|
|
return style == SCE_PAS_COMMENT || style == SCE_PAS_COMMENT2;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static bool IsCommentLine(Sci_Position line, Accessor &styler) {
|
|
|
|
Sci_Position pos = styler.LineStart(line);
|
|
|
|
Sci_Position eolPos = styler.LineStart(line + 1) - 1;
|
|
|
|
for (Sci_Position i = pos; i < eolPos; i++) {
|
2009-06-24 19:09:31 +00:00
|
|
|
char ch = styler[i];
|
|
|
|
char chNext = styler.SafeGetCharAt(i + 1);
|
|
|
|
int style = styler.StyleAt(i);
|
|
|
|
if (ch == '/' && chNext == '/' && style == SCE_PAS_COMMENTLINE) {
|
|
|
|
return true;
|
|
|
|
} else if (!IsASpaceOrTab(ch)) {
|
|
|
|
return false;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
return false;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
static unsigned int GetFoldInPreprocessorLevelFlag(int lineFoldStateCurrent) {
|
|
|
|
return lineFoldStateCurrent & stateFoldInPreprocessorLevelMask;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
static void SetFoldInPreprocessorLevelFlag(int &lineFoldStateCurrent, unsigned int nestLevel) {
|
|
|
|
lineFoldStateCurrent &= ~stateFoldInPreprocessorLevelMask;
|
|
|
|
lineFoldStateCurrent |= nestLevel & stateFoldInPreprocessorLevelMask;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
static void ClassifyPascalPreprocessorFoldPoint(int &levelCurrent, int &lineFoldStateCurrent,
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU startPos, Accessor &styler) {
|
2009-06-24 19:09:31 +00:00
|
|
|
CharacterSet setWord(CharacterSet::setAlpha);
|
|
|
|
|
|
|
|
char s[11]; // Size of the longest possible keyword + one additional character + null
|
|
|
|
GetForwardRangeLowered(startPos, setWord, styler, s, sizeof(s));
|
|
|
|
|
|
|
|
unsigned int nestLevel = GetFoldInPreprocessorLevelFlag(lineFoldStateCurrent);
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
if (strcmp(s, "if") == 0 ||
|
|
|
|
strcmp(s, "ifdef") == 0 ||
|
|
|
|
strcmp(s, "ifndef") == 0 ||
|
|
|
|
strcmp(s, "ifopt") == 0 ||
|
2009-06-24 19:09:31 +00:00
|
|
|
strcmp(s, "region") == 0) {
|
|
|
|
nestLevel++;
|
|
|
|
SetFoldInPreprocessorLevelFlag(lineFoldStateCurrent, nestLevel);
|
|
|
|
lineFoldStateCurrent |= stateFoldInPreprocessor;
|
|
|
|
levelCurrent++;
|
2010-08-21 23:59:56 +00:00
|
|
|
} else if (strcmp(s, "endif") == 0 ||
|
|
|
|
strcmp(s, "ifend") == 0 ||
|
2009-06-24 19:09:31 +00:00
|
|
|
strcmp(s, "endregion") == 0) {
|
|
|
|
nestLevel--;
|
|
|
|
SetFoldInPreprocessorLevelFlag(lineFoldStateCurrent, nestLevel);
|
|
|
|
if (nestLevel == 0) {
|
|
|
|
lineFoldStateCurrent &= ~stateFoldInPreprocessor;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
levelCurrent--;
|
|
|
|
if (levelCurrent < SC_FOLDLEVELBASE) {
|
|
|
|
levelCurrent = SC_FOLDLEVELBASE;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
}
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static Sci_PositionU SkipWhiteSpace(Sci_PositionU currentPos, Sci_PositionU endPos,
|
2009-06-24 19:09:31 +00:00
|
|
|
Accessor &styler, bool includeChars = false) {
|
|
|
|
CharacterSet setWord(CharacterSet::setAlphaNum, "_");
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU j = currentPos + 1;
|
2009-06-24 19:09:31 +00:00
|
|
|
char ch = styler.SafeGetCharAt(j);
|
2010-08-21 23:59:56 +00:00
|
|
|
while ((j < endPos) && (IsASpaceOrTab(ch) || ch == '\r' || ch == '\n' ||
|
2009-06-24 19:09:31 +00:00
|
|
|
IsStreamCommentStyle(styler.StyleAt(j)) || (includeChars && setWord.Contains(ch)))) {
|
|
|
|
j++;
|
|
|
|
ch = styler.SafeGetCharAt(j);
|
|
|
|
}
|
|
|
|
return j;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
static void ClassifyPascalWordFoldPoint(int &levelCurrent, int &lineFoldStateCurrent,
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position startPos, Sci_PositionU endPos,
|
|
|
|
Sci_PositionU lastStart, Sci_PositionU currentPos, Accessor &styler) {
|
2009-06-24 19:09:31 +00:00
|
|
|
char s[100];
|
|
|
|
GetRangeLowered(lastStart, currentPos, styler, s, sizeof(s));
|
|
|
|
|
|
|
|
if (strcmp(s, "record") == 0) {
|
|
|
|
lineFoldStateCurrent |= stateFoldInRecord;
|
|
|
|
levelCurrent++;
|
2010-08-21 23:59:56 +00:00
|
|
|
} else if (strcmp(s, "begin") == 0 ||
|
|
|
|
strcmp(s, "asm") == 0 ||
|
|
|
|
strcmp(s, "try") == 0 ||
|
2009-06-24 19:09:31 +00:00
|
|
|
(strcmp(s, "case") == 0 && !(lineFoldStateCurrent & stateFoldInRecord))) {
|
|
|
|
levelCurrent++;
|
|
|
|
} else if (strcmp(s, "class") == 0 || strcmp(s, "object") == 0) {
|
|
|
|
// "class" & "object" keywords require special handling...
|
|
|
|
bool ignoreKeyword = false;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU j = SkipWhiteSpace(currentPos, endPos, styler);
|
2009-06-24 19:09:31 +00:00
|
|
|
if (j < endPos) {
|
|
|
|
CharacterSet setWordStart(CharacterSet::setAlpha, "_");
|
|
|
|
CharacterSet setWord(CharacterSet::setAlphaNum, "_");
|
|
|
|
|
|
|
|
if (styler.SafeGetCharAt(j) == ';') {
|
2010-08-21 23:59:56 +00:00
|
|
|
// Handle forward class declarations ("type TMyClass = class;")
|
2009-06-24 19:09:31 +00:00
|
|
|
// and object method declarations ("TNotifyEvent = procedure(Sender: TObject) of object;")
|
|
|
|
ignoreKeyword = true;
|
|
|
|
} else if (strcmp(s, "class") == 0) {
|
|
|
|
// "class" keyword has a few more special cases...
|
|
|
|
if (styler.SafeGetCharAt(j) == '(') {
|
2010-08-21 23:59:56 +00:00
|
|
|
// Handle simplified complete class declarations ("type TMyClass = class(TObject);")
|
2009-06-24 19:09:31 +00:00
|
|
|
j = SkipWhiteSpace(j, endPos, styler, true);
|
|
|
|
if (j < endPos && styler.SafeGetCharAt(j) == ')') {
|
|
|
|
j = SkipWhiteSpace(j, endPos, styler);
|
|
|
|
if (j < endPos && styler.SafeGetCharAt(j) == ';') {
|
|
|
|
ignoreKeyword = true;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
} else if (setWordStart.Contains(styler.SafeGetCharAt(j))) {
|
|
|
|
char s2[11]; // Size of the longest possible keyword + one additional character + null
|
|
|
|
GetForwardRangeLowered(j, setWord, styler, s2, sizeof(s2));
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
if (strcmp(s2, "procedure") == 0 ||
|
|
|
|
strcmp(s2, "function") == 0 ||
|
|
|
|
strcmp(s2, "of") == 0 ||
|
|
|
|
strcmp(s2, "var") == 0 ||
|
|
|
|
strcmp(s2, "property") == 0 ||
|
2009-06-24 19:09:31 +00:00
|
|
|
strcmp(s2, "operator") == 0) {
|
|
|
|
ignoreKeyword = true;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
if (!ignoreKeyword) {
|
|
|
|
levelCurrent++;
|
|
|
|
}
|
|
|
|
} else if (strcmp(s, "interface") == 0) {
|
|
|
|
// "interface" keyword requires special handling...
|
|
|
|
bool ignoreKeyword = true;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position j = lastStart - 1;
|
2009-06-24 19:09:31 +00:00
|
|
|
char ch = styler.SafeGetCharAt(j);
|
2010-08-21 23:59:56 +00:00
|
|
|
while ((j >= startPos) && (IsASpaceOrTab(ch) || ch == '\r' || ch == '\n' ||
|
2009-06-24 19:09:31 +00:00
|
|
|
IsStreamCommentStyle(styler.StyleAt(j)))) {
|
|
|
|
j--;
|
|
|
|
ch = styler.SafeGetCharAt(j);
|
|
|
|
}
|
|
|
|
if (j >= startPos && styler.SafeGetCharAt(j) == '=') {
|
|
|
|
ignoreKeyword = false;
|
|
|
|
}
|
2013-08-28 00:44:27 +00:00
|
|
|
if (!ignoreKeyword) {
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU k = SkipWhiteSpace(currentPos, endPos, styler);
|
2013-08-28 00:44:27 +00:00
|
|
|
if (k < endPos && styler.SafeGetCharAt(k) == ';') {
|
|
|
|
// Handle forward interface declarations ("type IMyInterface = interface;")
|
|
|
|
ignoreKeyword = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!ignoreKeyword) {
|
|
|
|
levelCurrent++;
|
|
|
|
}
|
|
|
|
} else if (strcmp(s, "dispinterface") == 0) {
|
|
|
|
// "dispinterface" keyword requires special handling...
|
|
|
|
bool ignoreKeyword = false;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU j = SkipWhiteSpace(currentPos, endPos, styler);
|
2013-08-28 00:44:27 +00:00
|
|
|
if (j < endPos && styler.SafeGetCharAt(j) == ';') {
|
|
|
|
// Handle forward dispinterface declarations ("type IMyInterface = dispinterface;")
|
|
|
|
ignoreKeyword = true;
|
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
if (!ignoreKeyword) {
|
|
|
|
levelCurrent++;
|
|
|
|
}
|
|
|
|
} else if (strcmp(s, "end") == 0) {
|
|
|
|
lineFoldStateCurrent &= ~stateFoldInRecord;
|
|
|
|
levelCurrent--;
|
|
|
|
if (levelCurrent < SC_FOLDLEVELBASE) {
|
|
|
|
levelCurrent = SC_FOLDLEVELBASE;
|
|
|
|
}
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void FoldPascalDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *[],
|
2009-06-24 19:09:31 +00:00
|
|
|
Accessor &styler) {
|
2009-04-24 23:35:41 +00:00
|
|
|
bool foldComment = styler.GetPropertyInt("fold.comment") != 0;
|
|
|
|
bool foldPreprocessor = styler.GetPropertyInt("fold.preprocessor") != 0;
|
|
|
|
bool foldCompact = styler.GetPropertyInt("fold.compact", 1) != 0;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_PositionU endPos = startPos + length;
|
2009-04-24 23:35:41 +00:00
|
|
|
int visibleChars = 0;
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lineCurrent = styler.GetLine(startPos);
|
2009-04-24 23:35:41 +00:00
|
|
|
int levelPrev = styler.LevelAt(lineCurrent) & SC_FOLDLEVELNUMBERMASK;
|
|
|
|
int levelCurrent = levelPrev;
|
2009-06-24 19:09:31 +00:00
|
|
|
int lineFoldStateCurrent = lineCurrent > 0 ? styler.GetLineState(lineCurrent - 1) & stateFoldMaskAll : 0;
|
2009-04-24 23:35:41 +00:00
|
|
|
char chNext = styler[startPos];
|
|
|
|
int styleNext = styler.StyleAt(startPos);
|
|
|
|
int style = initStyle;
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
Sci_Position lastStart = 0;
|
2009-06-24 19:09:31 +00:00
|
|
|
CharacterSet setWord(CharacterSet::setAlphaNum, "_", 0x80, true);
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
for (Sci_PositionU i = startPos; i < endPos; i++) {
|
2009-04-24 23:35:41 +00:00
|
|
|
char ch = chNext;
|
|
|
|
chNext = styler.SafeGetCharAt(i + 1);
|
|
|
|
int stylePrev = style;
|
|
|
|
style = styleNext;
|
|
|
|
styleNext = styler.StyleAt(i + 1);
|
|
|
|
bool atEOL = (ch == '\r' && chNext != '\n') || (ch == '\n');
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
if (foldComment && IsStreamCommentStyle(style)) {
|
|
|
|
if (!IsStreamCommentStyle(stylePrev)) {
|
|
|
|
levelCurrent++;
|
|
|
|
} else if (!IsStreamCommentStyle(styleNext) && !atEOL) {
|
|
|
|
// Comments don't end at end of line and the next character may be unstyled.
|
|
|
|
levelCurrent--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (foldComment && atEOL && IsCommentLine(lineCurrent, styler))
|
2009-04-24 23:35:41 +00:00
|
|
|
{
|
2009-06-24 19:09:31 +00:00
|
|
|
if (!IsCommentLine(lineCurrent - 1, styler)
|
|
|
|
&& IsCommentLine(lineCurrent + 1, styler))
|
|
|
|
levelCurrent++;
|
|
|
|
else if (IsCommentLine(lineCurrent - 1, styler)
|
|
|
|
&& !IsCommentLine(lineCurrent+1, styler))
|
|
|
|
levelCurrent--;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
if (foldPreprocessor) {
|
|
|
|
if (style == SCE_PAS_PREPROCESSOR && ch == '{' && chNext == '$') {
|
|
|
|
ClassifyPascalPreprocessorFoldPoint(levelCurrent, lineFoldStateCurrent, i + 2, styler);
|
2010-08-21 23:59:56 +00:00
|
|
|
} else if (style == SCE_PAS_PREPROCESSOR2 && ch == '(' && chNext == '*'
|
2009-06-24 19:09:31 +00:00
|
|
|
&& styler.SafeGetCharAt(i + 2) == '$') {
|
|
|
|
ClassifyPascalPreprocessorFoldPoint(levelCurrent, lineFoldStateCurrent, i + 3, styler);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
if (stylePrev != SCE_PAS_WORD && style == SCE_PAS_WORD)
|
|
|
|
{
|
|
|
|
// Store last word start point.
|
|
|
|
lastStart = i;
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
if (stylePrev == SCE_PAS_WORD && !(lineFoldStateCurrent & stateFoldInPreprocessor)) {
|
|
|
|
if(setWord.Contains(ch) && !setWord.Contains(chNext)) {
|
|
|
|
ClassifyPascalWordFoldPoint(levelCurrent, lineFoldStateCurrent, startPos, endPos, lastStart, i, styler);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
if (!IsASpace(ch))
|
|
|
|
visibleChars++;
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
if (atEOL) {
|
|
|
|
int lev = levelPrev;
|
|
|
|
if (visibleChars == 0 && foldCompact)
|
|
|
|
lev |= SC_FOLDLEVELWHITEFLAG;
|
|
|
|
if ((levelCurrent > levelPrev) && (visibleChars > 0))
|
|
|
|
lev |= SC_FOLDLEVELHEADERFLAG;
|
|
|
|
if (lev != styler.LevelAt(lineCurrent)) {
|
|
|
|
styler.SetLevel(lineCurrent, lev);
|
|
|
|
}
|
2009-06-24 19:09:31 +00:00
|
|
|
int newLineState = (styler.GetLineState(lineCurrent) & ~stateFoldMaskAll) | lineFoldStateCurrent;
|
|
|
|
styler.SetLineState(lineCurrent, newLineState);
|
2009-04-24 23:35:41 +00:00
|
|
|
lineCurrent++;
|
|
|
|
levelPrev = levelCurrent;
|
|
|
|
visibleChars = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-06-24 19:09:31 +00:00
|
|
|
// If we didn't reach the EOL in previous loop, store line level and whitespace information.
|
|
|
|
// The rest will be filled in later...
|
|
|
|
int lev = levelPrev;
|
|
|
|
if (visibleChars == 0 && foldCompact)
|
|
|
|
lev |= SC_FOLDLEVELWHITEFLAG;
|
|
|
|
styler.SetLevel(lineCurrent, lev);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const pascalWordListDesc[] = {
|
|
|
|
"Keywords",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
|
|
|
|
LexerModule lmPascal(SCLEX_PASCAL, ColourisePascalDoc, "pascal", FoldPascalDoc, pascalWordListDesc);
|