added api and keywords syntax

This commit is contained in:
BADIM-PC\Vadim 2017-12-20 23:14:41 +03:00
parent 354c0a4701
commit d5035caf47
1 changed files with 55 additions and 5 deletions

View File

@ -405,6 +405,8 @@ static bool isWord(char symbol) {return isLetter(symbol) || isNumber(symbol);}
#include <ctype.h> #include <ctype.h>
static inline bool islineend(char c) {return c == '\n' || c == '\0';} static inline bool islineend(char c) {return c == '\n' || c == '\0';}
static inline bool isalpha_(char c) {return isalpha(c) || c == '_';}
static inline bool isalnum_(char c) {return isalnum(c) || c == '_';}
static void parse(const char* start, u8* color) static void parse(const char* start, u8* color)
{ {
@ -413,6 +415,7 @@ static void parse(const char* start, u8* color)
const char* blockCommentStart = NULL; const char* blockCommentStart = NULL;
const char* blockStringStart = NULL; const char* blockStringStart = NULL;
const char* singleCommentStart = NULL; const char* singleCommentStart = NULL;
const char* wordStart = NULL;
const char* numberStart = NULL; const char* numberStart = NULL;
static const char BlockCommentStart[] = "--[["; static const char BlockCommentStart[] = "--[[";
@ -471,6 +474,44 @@ static void parse(const char* start, u8* color)
singleCommentStart = NULL; singleCommentStart = NULL;
continue; continue;
} }
else if(wordStart)
{
while(!islineend(*ptr) && isalnum_(*ptr)) ptr++;
bool keyword = false;
{
static const char* const Keywords [] =
{
"and", "break", "do", "else", "elseif",
"end", "false", "for", "function", "goto", "if",
"in", "local", "nil", "not", "or", "repeat",
"return", "then", "true", "until", "while"
};
for(s32 i = 0; i < COUNT_OF(Keywords); i++)
if(memcmp(wordStart, Keywords[i], strlen(Keywords[i])) == 0)
{
memset(color + (wordStart - start), getConfig()->theme.code.keyword, ptr - wordStart);
keyword = true;
break;
}
}
if(!keyword)
{
static const char* const Api[] = API_KEYWORDS;
for(s32 i = 0; i < COUNT_OF(Api); i++)
if(memcmp(wordStart, Api[i], strlen(Api[i])) == 0)
{
memset(color + (wordStart - start), getConfig()->theme.code.api, ptr - wordStart);
break;
}
}
wordStart = NULL;
continue;
}
else if(numberStart) else if(numberStart)
{ {
while(!islineend(*ptr)) while(!islineend(*ptr))
@ -511,7 +552,7 @@ static void parse(const char* start, u8* color)
if(c == '"' || c == '\'') if(c == '"' || c == '\'')
{ {
blockStringStart = ptr; blockStringStart = ptr;
ptr += 1; ptr++;
continue; continue;
} }
else else
@ -526,15 +567,24 @@ static void parse(const char* start, u8* color)
} }
else else
{ {
if(isdigit(c) || (c == '.' && isdigit(ptr[1]))) if(isalpha_(c))
{ {
numberStart = ptr; wordStart = ptr;
ptr += 1; ptr++;
continue; continue;
} }
else else
{ {
// other stuff if(isdigit(c) || (c == '.' && isdigit(ptr[1])))
{
numberStart = ptr;
ptr++;
continue;
}
else
{
// other stuff
}
} }
} }
} }