|
31 | 31 |
|
32 | 32 | // When code is sent to Espruino, search it for modules and add extra code required to load them
|
33 | 33 | Espruino.addProcessor("transformForEspruino", function(code, callback) {
|
34 |
| - if (!Espruino.Config.PRETOKENISE) return callback(code); |
| 34 | + if (Espruino.Config.PRETOKENISE == 0) return callback(code); // disabled? |
35 | 35 | if (Espruino.Config.SAVE_ON_SEND == 0) {
|
36 | 36 | console.log("pretokenise> Can't pretokenise code sent to REPL (RAM)");
|
37 | 37 | return callback(code);
|
|
40 | 40 | });
|
41 | 41 | // When code is sent to Espruino, search it for modules and add extra code required to load them
|
42 | 42 | Espruino.addProcessor("transformModuleForEspruino", function(module, callback) {
|
43 |
| - if (!Espruino.Config.PRETOKENISE || |
| 43 | + if (Espruino.Config.PRETOKENISE == 0 || // disabled? |
44 | 44 | Espruino.Config.MODULE_AS_FUNCTION) return callback(module);
|
45 | 45 | /* if MODULE_AS_FUNCTION is specified the module is uploaded inside a 'function'
|
46 | 46 | block, in which case it will be pretokenised anyway in a later step */
|
|
135 | 135 | if (Espruino.Config.PRETOKENISE==2) { // force all options always
|
136 | 136 | pretokeniseStrings = true;
|
137 | 137 | pretokeniseInts = true;
|
138 |
| - } else if (boardData && boardData.VERSION) { |
| 138 | + } else if (boardData && boardData.VERSION) { // Espruino.Config.PRETOKENISE is nonzero or we wouldn't be called |
139 | 139 | var v = parseFloat(boardData.VERSION.replace("v","0"));
|
140 | 140 | if (v >= 2020.48)
|
141 | 141 | pretokeniseStrings = true;
|
|
147 | 147 | let t = acorn.tokenizer(code, { ecmaVersion : 2020 });
|
148 | 148 | return { next : function() {
|
149 | 149 | let tk = t.getToken();
|
150 |
| - let tkStr = code.substring(tk.start, tk.end), tkValue = tk.value; |
| 150 | + let tkStr = code.substring(tk.start, tk.end), tkValue = tk.value, tkEnd = tk.end; |
151 | 151 | if (tk.type.label=="eof") return undefined;
|
152 | 152 | let tp = "?";
|
153 | 153 | if (tk.type.label=="`") { // template string
|
|
158 | 158 | if (tk2.type.label=="${")
|
159 | 159 | hasTemplate = true;
|
160 | 160 | } while (tk2.type.label!="`");
|
161 |
| - tkStr = code.substring(tk.start, tk2.end); |
| 161 | + tkEnd = tk2.end; |
| 162 | + tkStr = code.substring(tk.start, tkEnd); |
162 | 163 | tp = hasTemplate ? "TEMPLATEDSTRING" : "STRING"; // if we don't have any templates, treat as a normal string (https://github.com/espruino/Espruino/issues/2577)
|
163 | 164 | tkValue = hasTemplate ? tkStr : eval(tkStr); // don't evaluate if it has templates as it must be done at runtime!
|
164 |
| - } |
165 |
| - if (tk.type.label=="string") tp="STRING"; |
166 |
| - if (tk.type.label=="num") tp="NUMBER"; |
167 |
| - if (tk.type.keyword || tk.type.label=="name") tp="ID"; |
168 |
| - if (tp=="?" && tk.start+1==tk.end) tp="CHAR"; |
| 165 | + } else if (tk.type.label=="string") tp="STRING"; |
| 166 | + else if (tk.type.label=="num") tp="NUMBER"; |
| 167 | + else if (tk.type.keyword || tk.type.label=="name") tp="ID"; |
| 168 | + else if (tp=="?" && tk.start+1==tk.end) tp="CHAR"; |
169 | 169 | return {
|
170 | 170 | startIdx : tk.start,
|
171 |
| - endIdx : tk.end, |
| 171 | + endIdx : tkEnd, |
172 | 172 | str : tkStr,
|
173 | 173 | value : tkValue,
|
174 | 174 | type : tp
|
|
0 commit comments