2020-10-28 05:16:30 +00:00
|
|
|
#ifndef CLEX_H
|
|
|
|
#define CLEX_H
|
|
|
|
|
|
|
|
#include "cosmo.h"
|
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
typedef enum
|
|
|
|
{
|
2020-10-28 05:16:30 +00:00
|
|
|
// single character tokens
|
|
|
|
TOKEN_LEFT_PAREN,
|
|
|
|
TOKEN_RIGHT_PAREN,
|
|
|
|
TOKEN_LEFT_BRACE,
|
|
|
|
TOKEN_RIGHT_BRACE,
|
2020-11-06 00:43:21 +00:00
|
|
|
TOKEN_LEFT_BRACKET,
|
|
|
|
TOKEN_RIGHT_BRACKET,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_COMMA,
|
2020-12-10 02:32:42 +00:00
|
|
|
TOKEN_COLON,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_DOT,
|
|
|
|
TOKEN_DOT_DOT,
|
2020-12-27 04:01:22 +00:00
|
|
|
TOKEN_DOT_DOT_DOT,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_MINUS,
|
2020-11-19 20:41:21 +00:00
|
|
|
TOKEN_MINUS_MINUS,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_PLUS,
|
2020-11-19 20:41:21 +00:00
|
|
|
TOKEN_PLUS_PLUS,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_SLASH,
|
|
|
|
TOKEN_STAR,
|
2020-11-30 18:32:04 +00:00
|
|
|
TOKEN_POUND,
|
2021-01-01 06:47:15 +00:00
|
|
|
TOKEN_PERCENT,
|
2021-02-10 23:26:20 +00:00
|
|
|
TOKEN_CARROT,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_EOS, // end of statement
|
|
|
|
|
|
|
|
// equality operators
|
|
|
|
TOKEN_BANG,
|
|
|
|
TOKEN_BANG_EQUAL,
|
|
|
|
TOKEN_EQUAL,
|
|
|
|
TOKEN_EQUAL_EQUAL,
|
|
|
|
TOKEN_GREATER,
|
|
|
|
TOKEN_GREATER_EQUAL,
|
|
|
|
TOKEN_LESS,
|
|
|
|
TOKEN_LESS_EQUAL,
|
|
|
|
|
|
|
|
// literals
|
|
|
|
TOKEN_IDENTIFIER,
|
2020-11-28 01:34:54 +00:00
|
|
|
TOKEN_STRING, // token.start is heap allocated and separate from the source string!
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_NUMBER,
|
2021-01-19 01:42:15 +00:00
|
|
|
TOKEN_HEXNUMBER,
|
|
|
|
TOKEN_BINNUMBER,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_NIL,
|
|
|
|
TOKEN_TRUE,
|
|
|
|
TOKEN_FALSE,
|
|
|
|
|
|
|
|
// keywords & reserved words
|
|
|
|
TOKEN_AND,
|
2020-12-24 06:41:00 +00:00
|
|
|
TOKEN_BREAK,
|
|
|
|
TOKEN_CONTINUE,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_DO,
|
|
|
|
TOKEN_ELSE,
|
|
|
|
TOKEN_ELSEIF,
|
|
|
|
TOKEN_END,
|
|
|
|
TOKEN_FOR,
|
2023-02-09 21:58:25 +00:00
|
|
|
TOKEN_FUNC,
|
2020-12-05 23:58:56 +00:00
|
|
|
TOKEN_PROTO,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_IF,
|
2020-12-16 03:21:51 +00:00
|
|
|
TOKEN_IN,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_LOCAL,
|
|
|
|
TOKEN_NOT,
|
|
|
|
TOKEN_OR,
|
|
|
|
TOKEN_RETURN,
|
|
|
|
TOKEN_THEN,
|
2023-02-09 21:58:25 +00:00
|
|
|
TOKEN_LET,
|
2020-10-28 05:16:30 +00:00
|
|
|
TOKEN_WHILE,
|
|
|
|
|
|
|
|
TOKEN_ERROR,
|
|
|
|
TOKEN_EOF
|
|
|
|
} CTokenType;
|
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2020-10-28 05:16:30 +00:00
|
|
|
CTokenType type;
|
|
|
|
const char *word;
|
|
|
|
int len;
|
|
|
|
} CReservedWord;
|
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2020-10-28 05:16:30 +00:00
|
|
|
CTokenType type;
|
|
|
|
char *start;
|
|
|
|
int length;
|
|
|
|
int line;
|
|
|
|
} CToken;
|
|
|
|
|
2023-02-09 18:32:48 +00:00
|
|
|
typedef struct
|
|
|
|
{
|
2020-10-28 05:16:30 +00:00
|
|
|
char *currentChar;
|
|
|
|
char *startChar;
|
2023-02-09 18:32:48 +00:00
|
|
|
char *buffer; // if non-NULL & bufCount > 0, token->start & token->length will be set to buffer
|
2023-06-03 06:39:35 +00:00
|
|
|
// & bufCount respectively. used exclusively for string literals
|
2020-11-26 05:34:02 +00:00
|
|
|
size_t bufCount;
|
2023-02-09 18:32:48 +00:00
|
|
|
size_t bufCap;
|
|
|
|
int line; // current line
|
2020-10-28 05:16:30 +00:00
|
|
|
int lastLine; // line of the previous consumed token
|
|
|
|
bool isEnd;
|
|
|
|
CTokenType lastType;
|
2020-11-26 05:34:02 +00:00
|
|
|
CState *cstate;
|
2020-10-28 05:16:30 +00:00
|
|
|
} CLexState;
|
|
|
|
|
2023-08-29 02:13:00 +00:00
|
|
|
void cosmoL_initLexState(CState *cstate, CLexState *state, const char *source);
|
|
|
|
void cosmoL_cleanupLexState(CState *state, CLexState *lstate);
|
2020-10-28 05:16:30 +00:00
|
|
|
|
|
|
|
CToken cosmoL_scanToken(CLexState *state);
|
|
|
|
|
2021-01-02 05:06:24 +00:00
|
|
|
#endif
|