2009-04-24 23:35:41 +00:00
|
|
|
// Scintilla source code edit control
|
2009-08-23 02:24:48 +00:00
|
|
|
/** @file LexForth.cxx
|
|
|
|
** Lexer for FORTH
|
2009-04-24 23:35:41 +00:00
|
|
|
**/
|
2009-08-23 02:24:48 +00:00
|
|
|
// Copyright 1998-2003 by Neil Hodgson <neilh@scintilla.org>
|
2009-04-24 23:35:41 +00:00
|
|
|
// 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>
|
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"
|
|
|
|
#include "Scintilla.h"
|
|
|
|
#include "SciLexer.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "WordList.h"
|
|
|
|
#include "LexAccessor.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
#include "Accessor.h"
|
2009-08-23 02:24:48 +00:00
|
|
|
#include "StyleContext.h"
|
2010-08-21 23:59:56 +00:00
|
|
|
#include "CharacterSet.h"
|
|
|
|
#include "LexerModule.h"
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
using namespace Scintilla;
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
static inline bool IsAWordStart(int ch) {
|
|
|
|
return (ch < 0x80) && (isalnum(ch) || ch == '_' || ch == '.');
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
static inline bool IsANumChar(int ch) {
|
|
|
|
return (ch < 0x80) && (isxdigit(ch) || ch == '.' || ch == 'e' || ch == 'E' );
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
static inline bool IsASpaceChar(int ch) {
|
|
|
|
return (ch < 0x80) && isspace(ch);
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void ColouriseForthDoc(Sci_PositionU startPos, Sci_Position length, int initStyle, WordList *keywordLists[],
|
2009-08-23 02:24:48 +00:00
|
|
|
Accessor &styler) {
|
2009-04-24 23:35:41 +00:00
|
|
|
|
|
|
|
WordList &control = *keywordLists[0];
|
|
|
|
WordList &keyword = *keywordLists[1];
|
|
|
|
WordList &defword = *keywordLists[2];
|
|
|
|
WordList &preword1 = *keywordLists[3];
|
|
|
|
WordList &preword2 = *keywordLists[4];
|
|
|
|
WordList &strings = *keywordLists[5];
|
|
|
|
|
2009-08-23 02:24:48 +00:00
|
|
|
StyleContext sc(startPos, length, initStyle, styler);
|
|
|
|
|
|
|
|
for (; sc.More(); sc.Forward())
|
|
|
|
{
|
|
|
|
// Determine if the current state should terminate.
|
|
|
|
if (sc.state == SCE_FORTH_COMMENT) {
|
|
|
|
if (sc.atLineEnd) {
|
|
|
|
sc.SetState(SCE_FORTH_DEFAULT);
|
|
|
|
}
|
|
|
|
}else if (sc.state == SCE_FORTH_COMMENT_ML) {
|
|
|
|
if (sc.ch == ')') {
|
|
|
|
sc.ForwardSetState(SCE_FORTH_DEFAULT);
|
|
|
|
}
|
|
|
|
}else if (sc.state == SCE_FORTH_IDENTIFIER || sc.state == SCE_FORTH_NUMBER) {
|
|
|
|
// handle numbers here too, because what we thought was a number might
|
|
|
|
// turn out to be a keyword e.g. 2DUP
|
|
|
|
if (IsASpaceChar(sc.ch) ) {
|
|
|
|
char s[100];
|
|
|
|
sc.GetCurrentLowered(s, sizeof(s));
|
|
|
|
int newState = sc.state == SCE_FORTH_NUMBER ? SCE_FORTH_NUMBER : SCE_FORTH_DEFAULT;
|
|
|
|
if (control.InList(s)) {
|
|
|
|
sc.ChangeState(SCE_FORTH_CONTROL);
|
|
|
|
} else if (keyword.InList(s)) {
|
|
|
|
sc.ChangeState(SCE_FORTH_KEYWORD);
|
|
|
|
} else if (defword.InList(s)) {
|
|
|
|
sc.ChangeState(SCE_FORTH_DEFWORD);
|
|
|
|
} else if (preword1.InList(s)) {
|
|
|
|
sc.ChangeState(SCE_FORTH_PREWORD1);
|
|
|
|
} else if (preword2.InList(s)) {
|
|
|
|
sc.ChangeState(SCE_FORTH_PREWORD2);
|
|
|
|
} else if (strings.InList(s)) {
|
|
|
|
sc.ChangeState(SCE_FORTH_STRING);
|
|
|
|
newState = SCE_FORTH_STRING;
|
|
|
|
}
|
|
|
|
sc.SetState(newState);
|
|
|
|
}
|
|
|
|
if (sc.state == SCE_FORTH_NUMBER) {
|
|
|
|
if (IsASpaceChar(sc.ch)) {
|
|
|
|
sc.SetState(SCE_FORTH_DEFAULT);
|
|
|
|
} else if (!IsANumChar(sc.ch)) {
|
|
|
|
sc.ChangeState(SCE_FORTH_IDENTIFIER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}else if (sc.state == SCE_FORTH_STRING) {
|
|
|
|
if (sc.ch == '\"') {
|
|
|
|
sc.ForwardSetState(SCE_FORTH_DEFAULT);
|
|
|
|
}
|
|
|
|
}else if (sc.state == SCE_FORTH_LOCALE) {
|
|
|
|
if (sc.ch == '}') {
|
|
|
|
sc.ForwardSetState(SCE_FORTH_DEFAULT);
|
|
|
|
}
|
|
|
|
}else if (sc.state == SCE_FORTH_DEFWORD) {
|
|
|
|
if (IsASpaceChar(sc.ch)) {
|
|
|
|
sc.SetState(SCE_FORTH_DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Determine if a new state should be entered.
|
|
|
|
if (sc.state == SCE_FORTH_DEFAULT) {
|
|
|
|
if (sc.ch == '\\'){
|
|
|
|
sc.SetState(SCE_FORTH_COMMENT);
|
|
|
|
} else if (sc.ch == '(' &&
|
|
|
|
(sc.atLineStart || IsASpaceChar(sc.chPrev)) &&
|
|
|
|
(sc.atLineEnd || IsASpaceChar(sc.chNext))) {
|
|
|
|
sc.SetState(SCE_FORTH_COMMENT_ML);
|
2015-06-07 21:19:26 +00:00
|
|
|
} else if ( (sc.ch == '$' && (IsASCII(sc.chNext) && isxdigit(sc.chNext))) ) {
|
2009-08-23 02:24:48 +00:00
|
|
|
// number starting with $ is a hex number
|
|
|
|
sc.SetState(SCE_FORTH_NUMBER);
|
2015-06-07 21:19:26 +00:00
|
|
|
while(sc.More() && IsASCII(sc.chNext) && isxdigit(sc.chNext))
|
2009-08-23 02:24:48 +00:00
|
|
|
sc.Forward();
|
2015-06-07 21:19:26 +00:00
|
|
|
} else if ( (sc.ch == '%' && (IsASCII(sc.chNext) && (sc.chNext == '0' || sc.chNext == '1'))) ) {
|
2009-08-23 02:24:48 +00:00
|
|
|
// number starting with % is binary
|
|
|
|
sc.SetState(SCE_FORTH_NUMBER);
|
2015-06-07 21:19:26 +00:00
|
|
|
while(sc.More() && IsASCII(sc.chNext) && (sc.chNext == '0' || sc.chNext == '1'))
|
2009-08-23 02:24:48 +00:00
|
|
|
sc.Forward();
|
2015-06-07 21:19:26 +00:00
|
|
|
} else if ( IsASCII(sc.ch) &&
|
|
|
|
(isxdigit(sc.ch) || ((sc.ch == '.' || sc.ch == '-') && IsASCII(sc.chNext) && isxdigit(sc.chNext)) )
|
2009-08-23 02:24:48 +00:00
|
|
|
){
|
|
|
|
sc.SetState(SCE_FORTH_NUMBER);
|
|
|
|
} else if (IsAWordStart(sc.ch)) {
|
|
|
|
sc.SetState(SCE_FORTH_IDENTIFIER);
|
|
|
|
} else if (sc.ch == '{') {
|
|
|
|
sc.SetState(SCE_FORTH_LOCALE);
|
2015-06-07 21:19:26 +00:00
|
|
|
} else if (sc.ch == ':' && IsASCII(sc.chNext) && isspace(sc.chNext)) {
|
2009-08-23 02:24:48 +00:00
|
|
|
// highlight word definitions e.g. : GCD ( n n -- n ) ..... ;
|
|
|
|
// ^ ^^^
|
|
|
|
sc.SetState(SCE_FORTH_DEFWORD);
|
2015-06-07 21:19:26 +00:00
|
|
|
while(sc.More() && IsASCII(sc.chNext) && isspace(sc.chNext))
|
2009-08-23 02:24:48 +00:00
|
|
|
sc.Forward();
|
|
|
|
} else if (sc.ch == ';' &&
|
|
|
|
(sc.atLineStart || IsASpaceChar(sc.chPrev)) &&
|
|
|
|
(sc.atLineEnd || IsASpaceChar(sc.chNext)) ) {
|
|
|
|
// mark the ';' that ends a word
|
|
|
|
sc.SetState(SCE_FORTH_DEFWORD);
|
|
|
|
sc.ForwardSetState(SCE_FORTH_DEFAULT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
sc.Complete();
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
2019-05-04 18:14:48 +00:00
|
|
|
static void FoldForthDoc(Sci_PositionU, Sci_Position, int, WordList *[],
|
2009-08-23 02:24:48 +00:00
|
|
|
Accessor &) {
|
2009-04-24 23:35:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static const char * const forthWordLists[] = {
|
2009-08-23 02:24:48 +00:00
|
|
|
"control keywords",
|
|
|
|
"keywords",
|
|
|
|
"definition words",
|
|
|
|
"prewords with one argument",
|
|
|
|
"prewords with two arguments",
|
|
|
|
"string definition keywords",
|
|
|
|
0,
|
|
|
|
};
|
|
|
|
|
|
|
|
LexerModule lmForth(SCLEX_FORTH, ColouriseForthDoc, "forth", FoldForthDoc, forthWordLists);
|
|
|
|
|
2010-08-21 23:59:56 +00:00
|
|
|
|