|
| 1 | +#include <stdint.h> |
| 2 | +#include <stdbool.h> |
| 3 | +#include <stddef.h> |
| 4 | +#include <limits.h> |
| 5 | +#include <string.h> |
| 6 | + |
| 7 | +extern unsigned char __heap_base; |
| 8 | + |
| 9 | +const uint16_t* source = (void*)&__heap_base; |
| 10 | +uint32_t parse_error; |
| 11 | + |
| 12 | +struct Slice { |
| 13 | + const uint16_t* start; |
| 14 | + const uint16_t* end; |
| 15 | + struct Slice* next; |
| 16 | +}; |
| 17 | +typedef struct Slice Slice; |
| 18 | + |
| 19 | +struct StarExportBinding { |
| 20 | + const uint16_t* specifier_start; |
| 21 | + const uint16_t* specifier_end; |
| 22 | + const uint16_t* id_start; |
| 23 | + const uint16_t* id_end; |
| 24 | +}; |
| 25 | +typedef struct StarExportBinding StarExportBinding; |
| 26 | + |
| 27 | +Slice* first_export = NULL; |
| 28 | +Slice* export_read_head = NULL; |
| 29 | +Slice* export_write_head = NULL; |
| 30 | +Slice* first_reexport = NULL; |
| 31 | +Slice* reexport_read_head = NULL; |
| 32 | +Slice* reexport_write_head = NULL; |
| 33 | +Slice* first_unsafe_getter = NULL; |
| 34 | +Slice* unsafe_getter_read_head = NULL; |
| 35 | +Slice* unsafe_getter_write_head = NULL; |
| 36 | +void* analysis_base; |
| 37 | +void* analysis_head; |
| 38 | + |
| 39 | +void bail (uint32_t err); |
| 40 | + |
| 41 | +// allocateSource |
| 42 | +const uint16_t* sa (uint32_t utf16Len) { |
| 43 | + const uint16_t* sourceEnd = source + utf16Len + 1; |
| 44 | + // ensure source is null terminated |
| 45 | + *(uint16_t*)(source + utf16Len) = '\0'; |
| 46 | + analysis_base = (void*)sourceEnd; |
| 47 | + analysis_head = analysis_base; |
| 48 | + first_export = NULL; |
| 49 | + export_write_head = NULL; |
| 50 | + export_read_head = NULL; |
| 51 | + first_reexport = NULL; |
| 52 | + reexport_write_head = NULL; |
| 53 | + reexport_read_head = NULL; |
| 54 | + first_unsafe_getter = NULL; |
| 55 | + unsafe_getter_write_head = NULL; |
| 56 | + unsafe_getter_read_head = NULL; |
| 57 | + return source; |
| 58 | +} |
| 59 | + |
| 60 | +// getErr |
| 61 | +uint32_t e () { |
| 62 | + return parse_error; |
| 63 | +} |
| 64 | + |
| 65 | +// getExportStart |
| 66 | +uint32_t es () { |
| 67 | + return export_read_head->start - source; |
| 68 | +} |
| 69 | +// getExportEnd |
| 70 | +uint32_t ee () { |
| 71 | + return export_read_head->end - source; |
| 72 | +} |
| 73 | +// getReexportStart |
| 74 | +uint32_t res () { |
| 75 | + return reexport_read_head->start - source; |
| 76 | +} |
| 77 | +// getReexportEnd |
| 78 | +uint32_t ree () { |
| 79 | + return reexport_read_head->end - source; |
| 80 | +} |
| 81 | +// getUnsafeGetterStart |
| 82 | +uint32_t us () { |
| 83 | + return unsafe_getter_read_head->start - source; |
| 84 | +} |
| 85 | +// getUnsafeGetterEnd |
| 86 | +uint32_t ue () { |
| 87 | + return unsafe_getter_read_head->end - source; |
| 88 | +} |
| 89 | +// readExport |
| 90 | +bool re () { |
| 91 | + if (export_read_head == NULL) |
| 92 | + export_read_head = first_export; |
| 93 | + else |
| 94 | + export_read_head = export_read_head->next; |
| 95 | + if (export_read_head == NULL) |
| 96 | + return false; |
| 97 | + return true; |
| 98 | +} |
| 99 | +// readReexport |
| 100 | +bool rre () { |
| 101 | + if (reexport_read_head == NULL) |
| 102 | + reexport_read_head = first_reexport; |
| 103 | + else |
| 104 | + reexport_read_head = reexport_read_head->next; |
| 105 | + if (reexport_read_head == NULL) |
| 106 | + return false; |
| 107 | + return true; |
| 108 | +} |
| 109 | +// readUnsafeGetter |
| 110 | +bool ru () { |
| 111 | + if (unsafe_getter_read_head == NULL) |
| 112 | + unsafe_getter_read_head = first_unsafe_getter; |
| 113 | + else |
| 114 | + unsafe_getter_read_head = unsafe_getter_read_head->next; |
| 115 | + if (unsafe_getter_read_head == NULL) |
| 116 | + return false; |
| 117 | + return true; |
| 118 | +} |
| 119 | + |
| 120 | +void _addExport (const uint16_t* start, const uint16_t* end) { |
| 121 | + Slice* export = (Slice*)(analysis_head); |
| 122 | + analysis_head = analysis_head + sizeof(Slice); |
| 123 | + if (export_write_head == NULL) |
| 124 | + first_export = export; |
| 125 | + else |
| 126 | + export_write_head->next = export; |
| 127 | + export_write_head = export; |
| 128 | + export->start = start; |
| 129 | + export->end = end; |
| 130 | + export->next = NULL; |
| 131 | +} |
| 132 | +void _addReexport (const uint16_t* start, const uint16_t* end) { |
| 133 | + Slice* reexport = (Slice*)(analysis_head); |
| 134 | + analysis_head = analysis_head + sizeof(Slice); |
| 135 | + if (reexport_write_head == NULL) |
| 136 | + first_reexport = reexport; |
| 137 | + else |
| 138 | + reexport_write_head->next = reexport; |
| 139 | + reexport_write_head = reexport; |
| 140 | + reexport->start = start; |
| 141 | + reexport->end = end; |
| 142 | + reexport->next = NULL; |
| 143 | +} |
| 144 | +void _addUnsafeGetter (const uint16_t* start, const uint16_t* end) { |
| 145 | + Slice* unsafe_getter = (Slice*)(analysis_head); |
| 146 | + analysis_head = analysis_head + sizeof(Slice); |
| 147 | + if (unsafe_getter_write_head == NULL) |
| 148 | + first_unsafe_getter = unsafe_getter; |
| 149 | + else |
| 150 | + unsafe_getter_write_head->next = unsafe_getter; |
| 151 | + unsafe_getter_write_head = unsafe_getter; |
| 152 | + unsafe_getter->start = start; |
| 153 | + unsafe_getter->end = end; |
| 154 | + unsafe_getter->next = NULL; |
| 155 | +} |
| 156 | +void _clearReexports () { |
| 157 | + reexport_write_head = NULL; |
| 158 | + first_reexport = NULL; |
| 159 | +} |
| 160 | +void (*addExport)(const uint16_t*, const uint16_t*) = &_addExport; |
| 161 | +void (*addReexport)(const uint16_t*, const uint16_t*) = &_addReexport; |
| 162 | +void (*addUnsafeGetter)(const uint16_t*, const uint16_t*) = &_addUnsafeGetter; |
| 163 | +void (*clearReexports)() = &_clearReexports; |
| 164 | +uint32_t parseCJS (uint16_t* source, uint32_t sourceLen, void (*addExport)(const uint16_t* start, const uint16_t* end), void (*addReexport)(const uint16_t* start, const uint16_t* end), void (*addUnsafeGetter)(const uint16_t*, const uint16_t*), void (*clearReexports)()); |
| 165 | + |
| 166 | +enum RequireType { |
| 167 | + Import, |
| 168 | + ExportAssign, |
| 169 | + ExportStar |
| 170 | +}; |
| 171 | + |
| 172 | +void tryBacktrackAddStarExportBinding (uint16_t* pos); |
| 173 | +bool tryParseRequire (enum RequireType requireType); |
| 174 | +void tryParseLiteralExports (); |
| 175 | +bool readExportsOrModuleDotExports (uint16_t ch); |
| 176 | +void tryParseModuleExportsDotAssign (); |
| 177 | +void tryParseExportsDotAssign (bool assign); |
| 178 | +void tryParseObjectDefineOrKeys (bool keys); |
| 179 | +bool identifier (uint16_t ch); |
| 180 | + |
| 181 | +void throwIfImportStatement (); |
| 182 | +void throwIfExportStatement (); |
| 183 | + |
| 184 | +void readImportString (const uint16_t* ss, uint16_t ch); |
| 185 | +uint16_t readExportAs (uint16_t* startPos, uint16_t* endPos); |
| 186 | + |
| 187 | +uint16_t commentWhitespace (); |
| 188 | +void stringLiteral (uint16_t quote); |
| 189 | +void regularExpression (); |
| 190 | +void templateString (); |
| 191 | +void blockComment (); |
| 192 | +void lineComment (); |
| 193 | + |
| 194 | +uint16_t readToWsOrPunctuator (uint16_t ch); |
| 195 | + |
| 196 | +uint32_t fullCharCode (uint16_t ch); |
| 197 | +uint32_t fullCharCodeAtLast (uint16_t* pos); |
| 198 | +bool isIdentifierStart (uint32_t code); |
| 199 | +bool isIdentifierChar (uint32_t code); |
| 200 | +int charCodeByteLen (uint32_t ch); |
| 201 | + |
| 202 | +bool isBr (uint16_t c); |
| 203 | +bool isBrOrWs (uint16_t c); |
| 204 | +bool isBrOrWsOrPunctuator (uint16_t c); |
| 205 | +bool isBrOrWsOrPunctuatorNotDot (uint16_t c); |
| 206 | + |
| 207 | +bool str_eq2 (uint16_t* pos, uint16_t c1, uint16_t c2); |
| 208 | +bool str_eq3 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3); |
| 209 | +bool str_eq4 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4); |
| 210 | +bool str_eq5 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5); |
| 211 | +bool str_eq6 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6); |
| 212 | +bool str_eq7 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7); |
| 213 | +bool str_eq8 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8); |
| 214 | +bool str_eq9 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9); |
| 215 | +bool str_eq10 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10); |
| 216 | +bool str_eq13 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13); |
| 217 | +bool str_eq18 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13, uint16_t c14, uint16_t c15, uint16_t c16, uint16_t c17, uint16_t c18); |
| 218 | +bool str_eq22 (uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7, uint16_t c8, uint16_t c9, uint16_t c10, uint16_t c11, uint16_t c12, uint16_t c13, uint16_t c14, uint16_t c15, uint16_t c16, uint16_t c17, uint16_t c18, uint16_t c19, uint16_t c20, uint16_t c21, uint16_t c22); |
| 219 | + |
| 220 | +bool readPrecedingKeyword2(uint16_t* pos, uint16_t c1, uint16_t c2); |
| 221 | +bool readPrecedingKeyword3(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3); |
| 222 | +bool readPrecedingKeyword4(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4); |
| 223 | +bool readPrecedingKeyword5(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5); |
| 224 | +bool readPrecedingKeyword6(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6); |
| 225 | +bool readPrecedingKeyword7(uint16_t* pos, uint16_t c1, uint16_t c2, uint16_t c3, uint16_t c4, uint16_t c5, uint16_t c6, uint16_t c7); |
| 226 | + |
| 227 | +bool keywordStart (uint16_t* pos); |
| 228 | +bool isExpressionKeyword (uint16_t* pos); |
| 229 | +bool isParenKeyword (uint16_t* pos); |
| 230 | +bool isPunctuator (uint16_t charCode); |
| 231 | +bool isExpressionPunctuator (uint16_t charCode); |
| 232 | +bool isExpressionTerminator (uint16_t* pos); |
| 233 | + |
| 234 | +void nextChar (uint16_t ch); |
| 235 | +void nextCharSurrogate (uint16_t ch); |
| 236 | +uint16_t readChar (); |
| 237 | + |
| 238 | +void syntaxError (uint32_t code); |
0 commit comments