Rune - Further Object abstraction work
[rune.git] / librune / lex.h
1 /*
2  * LEX.H
3  *
4  * (c)Copyright 1993-2014, Matthew Dillon, All Rights Reserved.  See the
5  * COPYRIGHT file at the base of the distribution.
6  */
7
8 struct Lex;
9
10 typedef RUNE_HEAD(lexlist, Lex) lexlist_t;
11
12 typedef struct Lex {
13     RUNE_ENTRY(Lex) l_Node;     /* saved Lex structures for error reporting */
14     char   *l_Path;
15     char   *l_Base;
16     int     l_Fd;
17     int     l_Bytes;
18     int     l_CacheLine;        /* cached line number */
19     int     l_CacheOff;         /* start of cache line */
20     int     l_RefCount;         /* LexRef count */
21     int     l_OpenCount;        /* open/close count */
22     int     l_DebugFileNo;      /* assigned during emit phase if debugging */
23 } Lex;
24
25 typedef struct {
26     const char *t_Ptr;
27     const char *t_End;
28     int     t_Len;
29     int     t_Type;
30     int     t_Line;
31     int     t_LinesInToken;
32     runeid_t t_Id;              /* if TOK_ID or TOKF_ID */
33     Lex    *t_Lex;
34 } token_t;
35
36 /*
37  * LexRef - save lexical position for error generation after lexical
38  * processing has completed.
39  *
40  * WARNING! Embedded in Declaration, fields must match classes/sys/runtime.d
41  */
42 typedef struct {
43     Lex    *lr_Lex;             /* pointer to (closed) Lex structure */
44     int     lr_Offset;          /* offset in file */
45     int     lr_Len;             /* length of token in file */
46     int     lr_Line;            /* line number */
47     int     lr_Reserved03;
48 } LexRef;
49
50 #define T_WS    0x01            /* white space CR LF FF SP TAB */
51 #define T_AL    0x02            /* alpha */
52 #define T_NU    0x04            /* numeric */
53 #define T_QU    0x08            /* single/backtick/double quote */
54 #define T_SP    0x10            /* single-character special */
55 #define T_CM    0x20            /* comment '#' */
56 #define T_MP    0x40            /* multi-character special or operator */
57 #define T_XP    0x80            /* possible comment (/) */
58
59 #define T_ALNU  (T_AL | T_NU)
60
61 #define TOK_DOLLAR      '$'
62 #define TOK_OPAREN      '('
63 #define TOK_CPAREN      ')'
64 #define TOK_COMMA       ','
65 #define TOK_DOT         '.'
66 #define TOK_COLON       ':'
67 #define TOK_SEMI        ';'
68 #define TOK_AT          '@'
69 #define TOK_OBRACKET    '['
70 #define TOK_CBRACKET    ']'
71 #define TOK_OBRACE      '{'
72 #define TOK_CBRACE      '}'
73 #define TOK_NOT         '!'     /* synthesized for exp, not actual lex token */
74
75 #define TOK_DSTRING     '"'
76 #define TOK_SSTRING     '\''
77 #define TOK_BSTRING     '`'
78 #define TOK_ANGLESTR    '<'     /* (specially flagged, nominally TOK_OPER) */
79
80 #define TOK_ASS         0x0100
81 #define TOK_ANDAND      0x0101
82 #define TOK_OROR        0x0102
83 #define TOK_OPER        0x0103
84 #define TOK_INTEGER     0x0104
85 #define TOK_FLOAT       0x0105
86 #define TOK_ID          (0x0106 | TOKF_ID)
87 #define TOK_EOF         (0x0107 | TOKF_EOF)
88 #define TOK_COMPOUND    0x0108  /* synthesized for exp (exp, exp, ...) */
89 #define TOK_UNUSED0109  0x0109
90 #define TOK_CALL        0x010A  /* synthesized for exp (exp, exp, ...) */
91 #define TOK_STRIND      0x010B  /* -> */
92 #define TOK_UNUSED010C  0x010C
93 #define TOK_PTRIND      0x010D  /* (synthesized) pointer indirect */
94 #define TOK_STRUCT_ID   0x010E  /* rhs side of '.' or '->' expression */
95 #define TOK_SEMGRP_ID   0x010F
96 #define TOK_UNUSED110   0x0110
97 #define TOK_DECL        0x0111  /* collapsed into declaration */
98 #define TOK_VOIDEXP     0x0112  /* like an integer, but represents void */
99 #define TOK_ADDR        0x0113  /* (synthesized) address of */
100 #define TOK_TYPE        0x0114  /* a type (in ex_Type) */
101 #define TOK_THREAD_SCHEDULE     0x0115  /* thread schedule */
102 #define TOK_CLASSID     (0x0116 | TOKF_ID)
103 #define TOK_UNUSED117   0x0117
104 #define TOK_UNUSED118   0x0118
105 #define TOK_SIZEOF      0x0119
106 #define TOK_ARYSIZE     0x011A
107 #define TOK_TYPEOF      0x011B
108 #define TOK_CONSTEXP    0x011C  /* resolved to cacheable constant */
109 #define TOK_INLINE_CALL 0x011D  /* call -> inlined call */
110 #define TOK_BRACKETED   0x011E  /* synthesized [ exp, exp, exp, ... ] */
111 #define TOK_THISARG     0x011F  /* synthesized 'this' argumenbt to method */
112
113 #define TOKF_ID         0x01000000
114 #define TOKF_ERROR      0x02000000
115 #define TOKF_EOF        0x04000000
116 #define TOKF_SCOPE_QUAL 0x08000000
117 #define TOKF_STOR_QUAL  0x10000000
118 #define TOKF_KEYWORD    0x20000000
119 #define TOKF_ERREOF     (TOKF_ERROR|TOKF_EOF)
120
121 #define TOKF_ERR_MASK   0x0000FFFF
122 #define TOKF_SCOPE_MASK 0x00000FFF
123 #define TOKF_STOR_MASK  0x00000FFF
124
125 #define TOK_ERR_NOERROR                 (TOKF_ERROR|0x00)       /* special */
126 #define TOK_ERR_UNKNOWN                 (TOKF_ERROR|0x01)       /* special */
127 #define TOK_ERR_UNTERMINATED_COMMENT    (TOKF_ERROR|0x02)
128 #define TOK_ERR_UNEXPECTED_CHAR         (TOKF_ERROR|0x03)
129 #define TOK_ERR_UNEXPECTED_TOKEN        (TOKF_ERROR|0x04)
130 #define TOK_ERR_BAD_NUMERIC_CONST       (TOKF_ERROR|0x05)
131 #define TOK_ERR_EMBEDDED_CRLF           (TOKF_ERROR|0x06)
132 #define TOK_ERR_ILLEGAL_ESCAPE          (TOKF_ERROR|0x07)
133 #define TOK_ERR_UNTERMINATED_STR        (TOKF_ERROR|0x08)
134 #define TOK_ERR_MULTIPLE_SCOPES         (TOKF_ERROR|0x09)
135 #define TOK_ERR_MISSING_OBRACE          (TOKF_ERROR|0x0A)
136 #define TOK_ERR_MISSING_CBRACE          (TOKF_ERROR|0x0B)
137 #define TOK_ERR_CASE_WITHOUT_SWITCH     (TOKF_ERROR|0x0C)
138 #define TOK_ERR_DEFAULT_WITHOUT_SWITCH  (TOKF_ERROR|0x0D)
139 #define TOK_ERR_ELSE_WITHOUT_IF         (TOKF_ERROR|0x0E)
140 #define TOK_ERR_DO_WITHOUT_WHILE        (TOKF_ERROR|0x0F)
141 #define TOK_ERR_BAD_BREAK_KEYWORD       (TOKF_ERROR|0x10)
142 #define TOK_ERR_MULTIPLE_DEFAULTS       (TOKF_ERROR|0x11)
143 #define TOK_ERR_SYNTAX                  (TOKF_ERROR|0x12)
144 #define TOK_ERR_EXPECTED_STRING         (TOKF_ERROR|0x13)
145 #define TOK_ERR_EXPECTED_ID             (TOKF_ERROR|0x14)
146 #define TOK_ERR_IMPORT_NOT_FOUND        (TOKF_ERROR|0x15)
147 #define TOK_ERR_DECL_NOT_PROCEDURE      (TOKF_ERROR|0x16)
148 #define TOK_ERR_EXPECTED_COMMA          (TOKF_ERROR|0x17)
149
150 #define TOK_ERR_EXP_SYNTAX              (TOKF_ERROR|0x18)
151 #define TOK_ERR_CLASS_ILLEGAL_TYPE      (TOKF_ERROR|0x19)
152 #define TOK_ERR_EXPECTED_CPAREN         (TOKF_ERROR|0x1A)
153 #define TOK_ERR_EXPECTED_TYPE           (TOKF_ERROR|0x1B)
154 #define TOK_ERR_MUTILPLE_IDS            (TOKF_ERROR|0x1C)
155 #define TOK_ERR_EXPECTED_DECLARATOR     (TOKF_ERROR|0x1D)
156 #define TOK_ERR_ILLEGAL_ID_CONTEXT      (TOKF_ERROR|0x1E)
157 #define TOK_ERR_IMPORT_FAILED           (TOKF_ERROR|0x1F)
158 #define TOK_ERR_EXPECTED_QUOTED_OPERATOR (TOKF_ERROR|0x20)
159 #define TOK_ERR_EXPECTED_SIMPLE_INTEGER (TOKF_ERROR|0x21)
160 #define TOK_ERR_BREAK_CONT_NOT_FOUND    (TOKF_ERROR|0x22)
161 #define TOK_ERR_DUPLICATE_ATTACH        (TOKF_ERROR|0x23)
162 #define TOK_ERR_UNRECOGNIZED_ATTACH     (TOKF_ERROR|0x24)
163 #define TOK_ERR_ILLEGAL_TYPEDEF         (TOKF_ERROR|0x25)
164 #define TOK_ERR_OPERATOR_NOT_PROCEDURE  (TOKF_ERROR|0x26)
165 #define TOK_ERR_EMPTY_PATH              (TOKF_ERROR|0x27)
166 #define TOK_ERR_NOT_SUPPORTED           (TOKF_ERROR|0x28)
167 #define TOK_ERR_BADLY_FORMED_SIZEOF     (TOKF_ERROR|0x29)
168 #define TOK_ERR_EXP_REMOVED             (TOKF_ERROR|0x2A)
169 #define TOK_ERR_EXP_SYNTAX_MAYTYPE      (TOKF_ERROR|0x2B)
170 #define TOK_ERR_PARSE_ID_NOT_CLASS      (TOKF_ERROR|0x2C)
171 #define TOK_ERR_PARSE_CLASS_NOT_FOUND   (TOKF_ERROR|0x2D)
172 #define TOK_ERR_NO_DOTTED_ID_HERE       (TOKF_ERROR|0x2E)
173 #define TOK_ERR_NESTED_CLASS            (TOKF_ERROR|0x2F)
174 #define TOK_ERR_DUPLICATE_ID            (TOKF_ERROR|0x30)
175 #define TOK_ERR_METHOD_REQUIRES_OBJ     (TOKF_ERROR|0x31)
176 #define TOK_ERR_SCOPE_NOT_VISIBLE       (TOKF_ERROR|0x32)
177 #define TOK_ERR_ID_NOT_FOUND            (TOKF_ERROR|0x33)
178 #define TOK_ERR_EXPECTED_CLASSID        (TOKF_ERROR|0x34)
179
180 #define TOK_ERR_INCOMPATIBLE_SUBCLASS   (TOKF_ERROR|0x35)
181 #define TOK_ERR_INCOMPATIBLE_BINDING    (TOKF_ERROR|0x36)
182 #define TOK_ERR_EXPECTED_INTEGRAL_TYPE  (TOKF_ERROR|0x37)
183 #define TOK_ERR_EXPECTED_INTEGRER_CONST (TOKF_ERROR|0x38)
184 #define TOK_ERR_ILLEGAL_UNLOCKED        (TOKF_ERROR|0x39)
185 #define TOK_ERR_ILLEGAL_ADDRLOCKED      (TOKF_ERROR|0x3A)
186 #define TOK_ERR_ILLEGAL_SUFFIX          (TOKF_ERROR|0x3B)
187 #define TOK_ERR_AUTOCAST_VALUE          (TOKF_ERROR|0x3C)
188 #define TOK_ERR_RESULT_SEQUENCING       (TOKF_ERROR|0x3D)
189 #define TOK_ERR_READONLY                (TOKF_ERROR|0x3E)
190 #define TOK_ERR_LIMITED_VOIDP_CAST      (TOKF_ERROR|0x3F)
191 #define TOK_ERR_SCOPE_MISMATCH          (TOKF_ERROR|0x40)
192 #define TOK_ERR_SCOPE_MISMATCH_THIS     (TOKF_ERROR|0x41)
193 #define TOK_ERR_ILLEGAL_LOCKING_REFINEMENT (TOKF_ERROR|0x42)
194 #define TOK_ERR_ILLEGAL_LOCKED          (TOKF_ERROR|0x43)
195 #define TOK_ERR_HASH_SYNTAX             (TOKF_ERROR|0x44)
196 #define TOK_ERR_CLASS_STRUCT_COMPAT     (TOKF_ERROR|0x45)
197 #define TOK_ERR_CLASS_STRUCT_EMBED      (TOKF_ERROR|0x46)
198 #define TOK_ERR_STMT_UNSUPPORTED        (TOKF_ERROR|0x47)
199 #define TOK_ERR_READONLY_ARG            (TOKF_ERROR|0x48)
200
201 #define TOK_ERR_STRUCT_CONTENT          (TOKF_ERROR|0x49)
202
203 #define LEX_ERR_STRINGS         \
204                 "",                                                     \
205                 "Unknown Error",                                        \
206                 "Unterminated Comment",                                 \
207                 "Unexpected Lexical Character",                         \
208                 "Unexpected Token",                                     \
209                 "Badly formed numerical constant",                      \
210                 "Embedded CR or LFs in strings are illegal",            \
211                 "Illegal escape in string",                             \
212 /* 0x08 */      "Unterminated string",                                  \
213                 "Multiple scopes specified",                            \
214                 "Expected open-brace",                                  \
215                 "Expected close-brace",                                 \
216                 "Case outside of switch",                               \
217                 "Default outside of switch",                            \
218                 "Else without if",                                      \
219                 "Do without while/until",                               \
220 /* 0x10 */      "Bad break/continue keyword",                           \
221                 "Multiple default: clauses were specified",             \
222                 "Syntax error",                                         \
223                 "Expected string constant",                             \
224                 "Expected identifier",                                  \
225                 "Import not found",                                     \
226                 "Illegal statement block, declaration is not a procedure",\
227                 "Expected comma",                                       \
228 /* 0x18 */      "Syntax error in expression",                           \
229                 "Illegal type for class declaration",                   \
230                 "Expected close parenthesis",                           \
231                 "Expected type",                                        \
232                 "Multiple identifiers were specified",                  \
233                 "Expected declarator",                                  \
234                 "Wrong kind of identifier was found for this context",  \
235                 "Error parsing imported file",                          \
236 /* 0x20 */      "Expected quoted operator",                             \
237                 "Expected simple integer",                              \
238                 "Break/continue point not found",                       \
239                 "Duplicate internal attach",                            \
240                 "Unrecognize internal attach id",                       \
241                 "Illegal typedef",                                      \
242                 "Operator/cast/method declaration is not a procedure!", \
243                 "Empty string not allowed",                             \
244 /* 0x28 */      "Not yet supported",                                    \
245                 "Badly formed sizeof(), typeof(), or arysize() operator",\
246                 "Expression removed from tree",                         \
247                 "Syntax error in expression.  If trying to make a "     \
248                  "declaration, do not forget the required scope or "    \
249                  "storage qualifier",                                   \
250                 "Can only enter declaration into a class",              \
251                 "Unable to find class to enter declaration into",       \
252                 "Dotted ids are not allowed here",                      \
253                 "You cannot nest classes or subclasses.  Perhaps "      \
254                  "you meant to use an interface here?",                 \
255 /* 0x30 */      "Duplicate identifier during parsing",                  \
256                 "A non-global method call must be called through an "   \
257                  "object context",                                      \
258                 "The id was found but is outside its visibility scope", \
259                 "Identifier could not be located",                      \
260                 "Expected type/class identifier",                       \
261                 "Subclass is not compatible with its superclass",       \
262                 "Subclass is incompatible for binding purposes",        \
263                 "Expected integral type",                               \
264 /* 0x38 */      "Expected resolvable integer constant",                 \
265                 "Rune only allows 'unlocked' or 'locked' scope if "     \
266                  "storage defaults to soft-locked",                     \
267                 "Cannot take the address of content-locked storage, "   \
268                  "you must make it 'unlocked'",                         \
269                 "Unrecognize suffix for constant",                      \
270                 "Autocast of constant would change its value",          \
271                 "Illegal placement of result; or use of argument "      \
272                  "or return value after result;",                       \
273                 "Storage is read-only and may not be modified",         \
274                 "Objects cast to void* may not contain lvalues, "       \
275                  "pointers, or references",                             \
276 /* 0x40 */      "Locking scope mismatch",                               \
277                 "Locking scope mismatch on method object ('this')",     \
278                 "Procedure refinement cannot change locking mode",      \
279                 "Locking scope illegal here",                           \
280                 "Hash (#) syntax error.  NOTE: # has multiple uses",    \
281                 "You cannot subclass a struct, use substruct",          \
282                 "You cannot directly embed a class, only a struct",     \
283                 "Unsuported/reserved/future Rune statement",            \
284 /* 0x48 */      "Argument storage is constant and may not be modified", \
285                 "Structs may not contain pointers or references, "      \
286                  "a class must be used",                                \
287                 NULL            /* terminate the macro, the NULL is not
288                                  * really used */