• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/include/ucode/lexer.h

  1 /*
  2  * Copyright (C) 2020-2021 Jo-Philipp Wich <jo@mein.io>
  3  *
  4  * Permission to use, copy, modify, and/or distribute this software for any
  5  * purpose with or without fee is hereby granted, provided that the above
  6  * copyright notice and this permission notice appear in all copies.
  7  *
  8  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 10  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 11  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 12  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 13  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 14  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 15  */
 16 
 17 #ifndef UCODE_LEXER_H
 18 #define UCODE_LEXER_H
 19 
 20 #include "source.h"
 21 #include "types.h"
 22 
 23 
 24 typedef enum {
 25         TK_LEXP = 1,
 26         TK_REXP,
 27         TK_LSTM,
 28         TK_RSTM,
 29         TK_IF,
 30         TK_ELSE,
 31         TK_COMMA,
 32         TK_ASSIGN,
 33         TK_ASADD,
 34         TK_ASSUB,
 35         TK_ASMUL,
 36         TK_ASDIV,
 37         TK_ASMOD,
 38         TK_ASLEFT,
 39         TK_ASRIGHT,
 40         TK_ASBAND,
 41         TK_ASBXOR,
 42         TK_ASBOR,
 43         TK_QMARK,
 44         TK_COLON,
 45         TK_OR,
 46         TK_AND,
 47         TK_BOR,
 48         TK_BXOR,
 49         TK_BAND,
 50         TK_EQS,
 51         TK_NES,
 52         TK_EQ,
 53         TK_NE,
 54         TK_LT,
 55         TK_LE,
 56         TK_GT,
 57         TK_GE,
 58         TK_IN,
 59         TK_LSHIFT,
 60         TK_RSHIFT,
 61         TK_ADD,
 62         TK_SUB,
 63         TK_MUL,
 64         TK_DIV,
 65         TK_MOD,
 66         TK_EXP,
 67         TK_NOT,
 68         TK_COMPL,
 69         TK_INC,
 70         TK_DEC,
 71         TK_DOT,
 72         TK_LBRACK,
 73         TK_RBRACK,
 74         TK_LPAREN,
 75         TK_RPAREN,
 76         TK_TEXT,
 77         TK_LBRACE,
 78         TK_RBRACE,
 79         TK_SCOL,
 80         TK_ENDIF,
 81         TK_ELIF,
 82         TK_WHILE,
 83         TK_ENDWHILE,
 84         TK_FOR,
 85         TK_ENDFOR,
 86         TK_FUNC,
 87         TK_LABEL,
 88         TK_ENDFUNC,
 89         TK_TRY,
 90         TK_CATCH,
 91         TK_SWITCH,
 92         TK_CASE,
 93         TK_DEFAULT,
 94         TK_ELLIP,
 95         TK_RETURN,
 96         TK_BREAK,
 97         TK_CONTINUE,
 98         TK_LOCAL,
 99         TK_ARROW,
100         TK_TRUE,
101         TK_FALSE,
102         TK_NUMBER,
103         TK_DOUBLE,
104         TK_STRING,
105         TK_REGEXP,
106         TK_NULL,
107         TK_THIS,
108         TK_DELETE,
109         TK_CONST,
110         TK_QLBRACK,
111         TK_QLPAREN,
112         TK_QDOT,
113         TK_ASEXP,
114         TK_ASAND,
115         TK_ASOR,
116         TK_ASNULLISH,
117         TK_NULLISH,
118         TK_PLACEH,
119         TK_TEMPLATE,
120         TK_IMPORT,
121         TK_EXPORT,
122         TK_FROM,
123         TK_AS,
124 
125         TK_EOF,
126         TK_ERROR
127 } uc_tokentype_t;
128 
129 typedef enum {
130         UC_LEX_IDENTIFY_BLOCK,
131         UC_LEX_BLOCK_EXPRESSION_EMIT_TAG,
132         UC_LEX_BLOCK_COMMENT,
133         UC_LEX_IDENTIFY_TOKEN,
134         UC_LEX_PLACEHOLDER_START,
135         UC_LEX_PLACEHOLDER_END,
136         UC_LEX_EOF
137 } uc_lex_state_t;
138 
139 typedef struct {
140         uc_tokentype_t type;
141         uc_value_t *uv;
142         size_t pos;
143 } uc_token_t;
144 
145 typedef struct {
146         uc_lex_state_t state;
147         uc_parse_config_t *config;
148         uc_source_t *source;
149         uint8_t no_regexp:1;
150         uint8_t no_keyword:1;
151         uc_token_t curr;
152         int lead_surrogate;
153         size_t lastoff;
154         enum {
155                 UNSPEC,
156                 PLUS,
157                 MINUS,
158                 NEWLINE
159         } modifier;
160         enum {
161                 NONE,
162                 EXPRESSION = '{',
163                 STATEMENTS = '%',
164                 COMMENT = '#'
165         } block;
166         struct {
167                 size_t count;
168                 size_t *entries;
169         } templates;
170         struct {
171                 size_t count;
172                 char *entries;
173         } buffer;
174         unsigned char *rbuf;
175         size_t rlen, rpos;
176 } uc_lexer_t;
177 
178 
179 __hidden void uc_lexer_init(uc_lexer_t *lex, uc_parse_config_t *config, uc_source_t *source);
180 __hidden void uc_lexer_free(uc_lexer_t *lex);
181 
182 __hidden uc_token_t *uc_lexer_next_token(uc_lexer_t *lex);
183 
184 __hidden bool uc_lexer_is_keyword(uc_value_t *label);
185 
186 __hidden bool utf8enc(char **out, int *rem, int code);
187 
188 __hidden const char *uc_tokenname(unsigned type);
189 
190 #endif /* UCODE_LEXER_H */
191 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt