rune - Features and 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 typedef struct {
9         Node    l_Node;         /* saved Lex structures for error reporting */
10         char    *l_Path;
11         int     l_Fd;
12         char    *l_Base;
13         int     l_Bytes;
14         int     l_CacheLine;    /* cached line number */
15         int     l_CacheOff;     /* start of cache line */
16         int     l_RefCount;     /* LexRef count */
17         int     l_OpenCount;    /* open/close count */
18 } Lex;
19
20 typedef struct {
21         const char *t_Ptr;
22         const char *t_End;
23         int     t_Len;
24         int     t_Type;
25         Lex     *t_Lex;
26 } token_t;
27
28 /*
29  * LexRef - save lexical position for error generation after lexical
30  *          processing has completed.
31  */
32 typedef struct {
33         Lex     *lr_Lex;        /* pointer to (closed) Lex structure */
34         int     lr_Offset;      /* offset in file */
35         int     lr_Len;         /* length of token in file */
36 } LexRef;
37
38 #define T_WS    0x01    /* white space CR LF FF SP TAB */
39 #define T_AL    0x02    /* alpha */
40 #define T_NU    0x04    /* numeric */
41 #define T_QU    0x08    /* single/backtick/double quote */
42 #define T_SP    0x10    /* single-character special */
43 #define T_CM    0x20    /* comment '#' */
44 #define T_MP    0x40    /* multi-character special or operator */
45 #define T_XP    0x80    /* possible comment (/) */
46
47 #define T_ALNU  (T_AL | T_NU)
48
49 #define TOK_DOLLAR      '$'
50 #define TOK_OPAREN      '('
51 #define TOK_CPAREN      ')'
52 #define TOK_COMMA       ','
53 #define TOK_DOT         '.'
54 #define TOK_COLON       ':'
55 #define TOK_SEMI        ';'
56 #define TOK_AT          '@'
57 #define TOK_OBRACKET    '['
58 #define TOK_CBRACKET    ']'
59 #define TOK_OBRACE      '{'
60 #define TOK_CBRACE      '}'
61 #define TOK_NOT         '!'     /* synthesized for exp, not actual lex token */
62
63 #define TOK_DSTRING     '"'
64 #define TOK_SSTRING     '\''
65 #define TOK_BSTRING     '`'
66
67 #define TOK_ASS         0x0100
68 #define TOK_ANDAND      0x0101
69 #define TOK_OROR        0x0102
70 #define TOK_OPER        0x0103
71 #define TOK_INTEGER     0x0104
72 #define TOK_FLOAT       0x0105
73 #define TOK_ID          (0x0106 | TOKF_ID)
74 #define TOK_EOF         (0x0107 | TOKF_EOF)
75 #define TOK_COMPOUND    0x0108  /* synthesized for exp */
76 #define TOK_UNUSED0109  0x0109
77 #define TOK_CALL        0x010A  /* synthesized for exp */
78 #define TOK_STRIND      0x010B  /* -> */
79 #define TOK_UNUSED010C  0x010C
80 #define TOK_PTRIND      0x010D  /* (synthesized) pointer indirect */
81 #define TOK_STRUCT_ID   0x010E  /* rhs side of '.' or '->' expression */
82 #define TOK_SEMGRP_ID   0x010F
83 #define TOK_UNUSED110   0x0110
84 #define TOK_DECL        0x0111  /* collapsed into declaration */
85 #define TOK_VOIDEXP     0x0112  /* like an integer, but represents void */
86 #define TOK_ADDR        0x0113  /* (synthesized) address of */
87 #define TOK_TYPE        0x0114  /* a type (in ex_Type) */
88 #define TOK_THREAD_SCHEDULE     0x0115  /* thread schedule */
89 #define TOK_CLASSID     (0x0116 | TOKF_ID)
90 #define TOK_UNUSED117   0x0117
91 #define TOK_UNUSED118   0x0118
92 #define TOK_SIZEOF      0x0119
93 #define TOK_ARYSIZE     0x011A
94 #define TOK_TYPEOF      0x011B
95 #define TOK_CONSTEXP    0x011C  /* resolved to cacheable constant */
96
97 #define TOKF_ID         0x01000000
98 #define TOKF_ERROR      0x02000000
99 #define TOKF_EOF        0x04000000
100 #define TOKF_SCOPE_QUAL 0x08000000
101 #define TOKF_STOR_QUAL  0x10000000
102 #define TOKF_KEYWORD    0x20000000
103 #define TOKF_ERREOF     (TOKF_ERROR|TOKF_EOF)
104
105 #define TOKF_ERR_MASK   0x0000FFFF
106 #define TOKF_SCOPE_MASK 0x00000FFF
107 #define TOKF_STOR_MASK  0x00000FFF
108
109 #define TOK_ERR_NOERROR                 (TOKF_ERROR|0x00)       /* special */
110 #define TOK_ERR_UNKNOWN                 (TOKF_ERROR|0x01)       /* special */
111 #define TOK_ERR_UNTERMINATED_COMMENT    (TOKF_ERROR|0x02)
112 #define TOK_ERR_UNEXPECTED_CHAR         (TOKF_ERROR|0x03)
113 #define TOK_ERR_UNEXPECTED_TOKEN        (TOKF_ERROR|0x04)
114 #define TOK_ERR_BAD_NUMERIC_CONST       (TOKF_ERROR|0x05)
115 #define TOK_ERR_EMBEDDED_CRLF           (TOKF_ERROR|0x06)
116 #define TOK_ERR_ILLEGAL_ESCAPE          (TOKF_ERROR|0x07)
117 #define TOK_ERR_UNTERMINATED_STR        (TOKF_ERROR|0x08)
118 #define TOK_ERR_MULTIPLE_SCOPES         (TOKF_ERROR|0x09)
119 #define TOK_ERR_MISSING_OBRACE          (TOKF_ERROR|0x0A)
120 #define TOK_ERR_MISSING_CBRACE          (TOKF_ERROR|0x0B)
121 #define TOK_ERR_CASE_WITHOUT_SWITCH     (TOKF_ERROR|0x0C)
122 #define TOK_ERR_DEFAULT_WITHOUT_SWITCH  (TOKF_ERROR|0x0D)
123 #define TOK_ERR_ELSE_WITHOUT_IF         (TOKF_ERROR|0x0E)
124 #define TOK_ERR_DO_WITHOUT_WHILE        (TOKF_ERROR|0x0F)
125 #define TOK_ERR_BAD_BREAK_KEYWORD       (TOKF_ERROR|0x10)
126 #define TOK_ERR_MULTIPLE_DEFAULTS       (TOKF_ERROR|0x11)
127 #define TOK_ERR_SYNTAX                  (TOKF_ERROR|0x12)
128 #define TOK_ERR_EXPECTED_STRING         (TOKF_ERROR|0x13)
129 #define TOK_ERR_EXPECTED_ID             (TOKF_ERROR|0x14)
130 #define TOK_ERR_IMPORT_NOT_FOUND        (TOKF_ERROR|0x15)
131 #define TOK_ERR_DECL_NOT_PROCEDURE      (TOKF_ERROR|0x16)
132 #define TOK_ERR_EXPECTED_COMMA          (TOKF_ERROR|0x17)
133
134 #define TOK_ERR_EXP_SYNTAX              (TOKF_ERROR|0x18)
135 #define TOK_ERR_CLASS_ILLEGAL_TYPE      (TOKF_ERROR|0x19)
136 #define TOK_ERR_EXPECTED_CPAREN         (TOKF_ERROR|0x1A)
137 #define TOK_ERR_EXPECTED_TYPE           (TOKF_ERROR|0x1B)
138 #define TOK_ERR_MUTILPLE_IDS            (TOKF_ERROR|0x1C)
139 #define TOK_ERR_EXPECTED_DECLARATOR     (TOKF_ERROR|0x1D)
140 #define TOK_ERR_ILLEGAL_ID_CONTEXT      (TOKF_ERROR|0x1E)
141 #define TOK_ERR_IMPORT_FAILED           (TOKF_ERROR|0x1F)
142 #define TOK_ERR_EXPECTED_QUOTED_OPERATOR (TOKF_ERROR|0x20)
143 #define TOK_ERR_EXPECTED_SIMPLE_INTEGER (TOKF_ERROR|0x21)
144 #define TOK_ERR_BREAK_CONT_NOT_FOUND    (TOKF_ERROR|0x22)
145 #define TOK_ERR_DUPLICATE_ATTACH        (TOKF_ERROR|0x23)
146 #define TOK_ERR_UNRECOGNIZED_ATTACH     (TOKF_ERROR|0x24)
147 #define TOK_ERR_ILLEGAL_TYPEDEF         (TOKF_ERROR|0x25)
148 #define TOK_ERR_OPERATOR_NOT_PROCEDURE  (TOKF_ERROR|0x26)
149 #define TOK_ERR_EMPTY_PATH              (TOKF_ERROR|0x27)
150 #define TOK_ERR_NOT_SUPPORTED           (TOKF_ERROR|0x28)
151 #define TOK_ERR_BADLY_FORMED_SIZEOF     (TOKF_ERROR|0x29)
152 #define TOK_ERR_EXP_REMOVED             (TOKF_ERROR|0x2A)
153 #define TOK_ERR_EXP_SYNTAX_MAYTYPE      (TOKF_ERROR|0x2B)
154 #define TOK_ERR_PARSE_ID_NOT_CLASS      (TOKF_ERROR|0x2C)
155 #define TOK_ERR_PARSE_CLASS_NOT_FOUND   (TOKF_ERROR|0x2D)
156 #define TOK_ERR_NO_DOTTED_ID_HERE       (TOKF_ERROR|0x2E)
157 #define TOK_ERR_NESTED_CLASS            (TOKF_ERROR|0x2F)
158 #define TOK_ERR_DUPLICATE_ID            (TOKF_ERROR|0x30)
159 #define TOK_ERR_METHOD_REQUIRES_OBJ     (TOKF_ERROR|0x31)
160 #define TOK_ERR_SCOPE_NOT_VISIBLE       (TOKF_ERROR|0x32)
161 #define TOK_ERR_ID_NOT_FOUND            (TOKF_ERROR|0x33)
162 #define TOK_ERR_EXPECTED_CLASSID        (TOKF_ERROR|0x34)
163
164 #define TOK_ERR_INCOMPATIBLE_SUBCLASS   (TOKF_ERROR|0x35)
165 #define TOK_ERR_INCOMPATIBLE_BINDING    (TOKF_ERROR|0x36)
166 #define TOK_ERR_EXPECTED_INTEGRAL_TYPE  (TOKF_ERROR|0x37)
167 #define TOK_ERR_EXPECTED_INTEGRER_CONST (TOKF_ERROR|0x38)
168
169 #define LEX_ERR_STRINGS         \
170                 "",                                                     \
171                 "Unknown Error",                                        \
172                 "Unterminated Comment",                                 \
173                 "Unexpected Lexical Character",                         \
174                 "Unexpected Token",                                     \
175                 "Badly formed numerical constant",                      \
176                 "Embedded CR or LFs in strings are illegal",            \
177                 "Illegal escape in string",                             \
178 /* 0x08 */      "Unterminated string",                                  \
179                 "Multiple scopes specified",                            \
180                 "Expected open-brace",                                  \
181                 "Expected close-brace",                                 \
182                 "Case outside of switch",                               \
183                 "Default outside of switch",                            \
184                 "Else without if",                                      \
185                 "Do without while/until",                               \
186 /* 0x10 */      "Bad break/continue keyword",                           \
187                 "Multiple default: clauses were specified",             \
188                 "Syntax error",                                         \
189                 "Expected string constant",                             \
190                 "Expected identifier",                                  \
191                 "Import not found",                                     \
192                 "Illegal statement block, declaration is not a procedure",\
193                 "Expected comma",                                       \
194 /* 0x18 */      "Syntax error in expression",                           \
195                 "Illegal type for class declaration",                   \
196                 "Expected close parenthesis",                           \
197                 "Expected type",                                        \
198                 "Multiple identifiers were specified",                  \
199                 "Expected declarator",                                  \
200                 "Wrong kind of identifier was found for this context",  \
201                 "Error parsing imported file",                          \
202 /* 0x20 */      "Expected quoted operator",                             \
203                 "Expected simple integer",                              \
204                 "Break/continue point not found",                       \
205                 "Duplicate internal attach",                            \
206                 "Unrecognize internal attach id",                       \
207                 "Illegal typedef",                                      \
208                 "Operator/cast/method declaration is not a procedure!", \
209                 "Empty string not allowed",                             \
210 /* 0x28 */      "Not yet supported",                                    \
211                 "Badly formed sizeof(), typeof(), or arysize() operator",\
212                 "Expression removed from tree",                         \
213                 "Syntax error in expression.  If trying to make a "     \
214                  "declaration, do not forget the required scope or "    \
215                  "storage qualifier",                                   \
216                 "Can only enter declaration into a class",              \
217                 "Unable to find class to enter declaration into",       \
218                 "Dotted ids are not allowed here",                      \
219                 "You cannot nest classes or subclasses.  Perhaps "      \
220                  "you meant to use an interface here?",                 \
221 /* 0x30 */      "Duplicate identifier during parsing",                  \
222                 "A non-global method call must be called through an "   \
223                  "object context",                                      \
224                 "The id was found but is outside its visibility scope", \
225                 "Identifier could not be located",                      \
226                 "Expected type/class identifier",                       \
227                 "Subclass is not compatible with its superclass",       \
228                 "Subclass is incompatible for binding purposes",        \
229                 "Expected integral type",                               \
230                 "Expected resolvable integer constant",                 \
231                 NULL    /* terminate the macro, the NULL is not really used */
232
233 #define dassert_lexref(lexref, cond)                    \
234         do {                                            \
235                 if (!(cond)) LexPrintRef((lexref), 0);  \
236                 dassert(0);                             \
237         } while (0)