1 // 2 // gravity_token.h 3 // gravity 4 // 5 // Created by Marco Bambini on 31/08/14. 6 // Copyright (c) 2014 CreoLabs. All rights reserved. 7 // 8 9 #ifndef __GRAVITY_TOKEN__ 10 #define __GRAVITY_TOKEN__ 11 12 #include <stdint.h> 13 #include "debug_macros.h" 14 15 // ================ 16 // PREFIX OPERATORS 17 // ================ 18 // + Unary PLUS 19 // - Unary MINUS 20 // ! Logical NOT 21 // ~ Bitwise NOT 22 23 // ================ 24 // INFIX OPERATORS 25 // ================ 26 // << Bitwise left shift (160) 27 // >> Bitwise right shift (160) 28 // * Multiply (150) (associativity left) 29 // / Divide (150) (associativity left) 30 // % Remainder (150) (associativity left) 31 // & Bitwise AND (150) (associativity left) 32 // + Add (140) (associativity left) 33 // - Subtract (140) (associativity left) 34 // | Bitwise OR (140) (associativity left) 35 // ^ Bitwise XOR (140) (associativity left) 36 // ..< Half-open range (135) 37 // ... Closed range (135) 38 // is Type check (132) 39 // < Less than (130) 40 // <= Less than or equal (130) 41 // > Greater than (130) 42 // >= Greater than or equal (130) 43 // == Equal (130) 44 // != Not equal (130) 45 // === Identical (130) 46 // !== Not identical (130) 47 // ~= Pattern match (130) 48 // && Logical AND (120) (associativity left) 49 // || Logical OR (110) (associativity left) 50 // ?: Ternary conditional (100) (associativity right) 51 // = Assign (90) (associativity right) 52 // *= Multiply and assign (90) (associativity right) 53 // /= Divide and assign (90) (associativity right) 54 // %= Remainder and assign (90) (associativity right) 55 // += Add and assign (90) (associativity right) 56 // -= Subtract and assign (90) (associativity right) 57 // <<= Left bit shift and assign (90) (associativity right) 58 // >>= Right bit shift and assign (90) (associativity right) 59 // &= Bitwise AND and assign (90) (associativity right) 60 // ^= Bitwise XOR and assign (90) (associativity right) 61 // |= Bitwise OR and assign (90) (associativity right) 62 63 typedef enum { 64 // General (8) 65 TOK_EOF = 0, TOK_ERROR, TOK_COMMENT, TOK_STRING, TOK_NUMBER, TOK_IDENTIFIER, TOK_SPECIAL, TOK_MACRO, 66 67 // Keywords (36) 68 // remember to keep in sync functions token_keywords_indexes and token_name 69 TOK_KEY_FUNC, TOK_KEY_SUPER, TOK_KEY_DEFAULT, TOK_KEY_TRUE, TOK_KEY_FALSE, TOK_KEY_IF, 70 TOK_KEY_ELSE, TOK_KEY_SWITCH, TOK_KEY_BREAK, TOK_KEY_CONTINUE, TOK_KEY_RETURN, TOK_KEY_WHILE, 71 TOK_KEY_REPEAT, TOK_KEY_FOR, TOK_KEY_IN, TOK_KEY_ENUM, TOK_KEY_CLASS, TOK_KEY_STRUCT, TOK_KEY_PRIVATE, 72 TOK_KEY_FILE, TOK_KEY_INTERNAL, TOK_KEY_PUBLIC, TOK_KEY_STATIC, TOK_KEY_EXTERN, TOK_KEY_LAZY, TOK_KEY_CONST, 73 TOK_KEY_VAR, TOK_KEY_MODULE, TOK_KEY_IMPORT, TOK_KEY_CASE, TOK_KEY_EVENT, TOK_KEY_NULL, TOK_KEY_UNDEFINED, 74 TOK_KEY_ISA, TOK_KEY_CURRFUNC, TOK_KEY_CURRARGS, 75 76 // Operators (36) 77 TOK_OP_SHIFT_LEFT, TOK_OP_SHIFT_RIGHT, TOK_OP_MUL, TOK_OP_DIV, TOK_OP_REM, TOK_OP_BIT_AND, TOK_OP_ADD, TOK_OP_SUB, 78 TOK_OP_BIT_OR, TOK_OP_BIT_XOR, TOK_OP_BIT_NOT, TOK_OP_RANGE_EXCLUDED, TOK_OP_RANGE_INCLUDED, TOK_OP_LESS, TOK_OP_LESS_EQUAL, 79 TOK_OP_GREATER, TOK_OP_GREATER_EQUAL, TOK_OP_ISEQUAL, TOK_OP_ISNOTEQUAL, TOK_OP_ISIDENTICAL, TOK_OP_ISNOTIDENTICAL, 80 TOK_OP_PATTERN_MATCH, TOK_OP_AND, TOK_OP_OR, TOK_OP_TERNARY, TOK_OP_ASSIGN, TOK_OP_MUL_ASSIGN, TOK_OP_DIV_ASSIGN, 81 TOK_OP_REM_ASSIGN, TOK_OP_ADD_ASSIGN, TOK_OP_SUB_ASSIGN, TOK_OP_SHIFT_LEFT_ASSIGN, TOK_OP_SHIFT_RIGHT_ASSIGN, 82 TOK_OP_BIT_AND_ASSIGN, TOK_OP_BIT_OR_ASSIGN, TOK_OP_BIT_XOR_ASSIGN, TOK_OP_NOT, 83 84 // Punctuators (10) 85 TOK_OP_SEMICOLON, TOK_OP_OPEN_PARENTHESIS, TOK_OP_COLON, TOK_OP_COMMA, TOK_OP_DOT, TOK_OP_CLOSED_PARENTHESIS, 86 TOK_OP_OPEN_SQUAREBRACKET, TOK_OP_CLOSED_SQUAREBRACKET, TOK_OP_OPEN_CURLYBRACE, TOK_OP_CLOSED_CURLYBRACE, 87 88 // Mark end of tokens (1) 89 TOK_END 90 } gtoken_t; 91 92 typedef enum { 93 LITERAL_STRING, LITERAL_FLOAT, LITERAL_INT, LITERAL_BOOL, LITERAL_STRING_INTERPOLATED 94 } gliteral_t; 95 96 typedef enum { 97 BUILTIN_NONE, BUILTIN_LINE, BUILTIN_COLUMN, BUILTIN_FILE, BUILTIN_FUNC, BUILTIN_CLASS 98 } gbuiltin_t; 99 100 struct gtoken_s { 101 gtoken_t type; // enum based token type 102 uint32_t lineno; // token line number (1-based) 103 uint32_t colno; // token column number (0-based) at the end of the token 104 uint32_t position; // offset of the first character of the token 105 uint32_t bytes; // token length in bytes 106 uint32_t length; // token length (UTF-8) 107 uint32_t fileid; // token file id 108 gbuiltin_t builtin; // builtin special identifier flag 109 const char *value; // token value (not null terminated) 110 }; 111 typedef struct gtoken_s gtoken_s; 112 113 #define NO_TOKEN (gtoken_s){0,0,0,0,0,0,0,0,NULL} 114 #define UNDEF_TOKEN (gtoken_s){TOK_KEY_UNDEFINED,0,0,0,0,0,0,0,NULL} 115 #define TOKEN_BYTES(_tok) _tok.bytes 116 #define TOKEN_VALUE(_tok) _tok.value 117 118 gtoken_t token_keyword (const char *buffer, int32_t len); 119 void token_keywords_indexes (uint32_t *idx_start, uint32_t *idx_end); 120 const char *token_literal_name (gliteral_t value); 121 const char *token_name (gtoken_t token); 122 gtoken_t token_special_builtin(gtoken_s *token); 123 const char *token_string (gtoken_s token, uint32_t *len); 124 125 bool token_isassignment (gtoken_t token); 126 bool token_isaccess_specifier (gtoken_t token); 127 bool token_iscompound_statement (gtoken_t token); 128 bool token_isdeclaration_statement (gtoken_t token); 129 bool token_iseof (gtoken_t token); 130 bool token_isempty_statement (gtoken_t token); 131 bool token_iserror (gtoken_t token); 132 bool token_isexpression_statement (gtoken_t token); 133 bool token_isflow_statement (gtoken_t token); 134 bool token_isjump_statement (gtoken_t token); 135 bool token_islabel_statement (gtoken_t token); 136 bool token_isloop_statement (gtoken_t token); 137 bool token_isidentifier (gtoken_t token); 138 bool token_isimport_statement (gtoken_t token); 139 bool token_ismacro (gtoken_t token); 140 bool token_isoperator (gtoken_t token); 141 bool token_isprimary_expression (gtoken_t token); 142 bool token_isspecial_statement (gtoken_t token); 143 bool token_isstatement (gtoken_t token); 144 bool token_isstorage_specifier (gtoken_t token); 145 bool token_isvariable_assignment (gtoken_t token); 146 bool token_isvariable_declaration (gtoken_t token); 147 148 #endif 149