jsparse.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * This file is part of Espruino, a JavaScript interpreter for Microcontrollers
  3. *
  4. * Copyright (C) 2013 Gordon Williams <gw@pur3.co.uk>
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. *
  10. * ----------------------------------------------------------------------------
  11. * Recursive descent parser for code execution
  12. * ----------------------------------------------------------------------------
  13. */
  14. #ifndef JSPARSE_H_
  15. #define JSPARSE_H_
  16. #include "jsvar.h"
  17. #include "jslex.h"
  18. typedef struct {
  19. JsVar *root; ///< root of symbol table
  20. } JsParse;
  21. void jspInit(JsParse *parse);
  22. void jspKill(JsParse *parse);
  23. // jspSoft* - 'release' or 'claim' anything we are using, but ensure that it doesn't get freed
  24. void jspSoftInit(JsParse *parse); ///< used when recovering from or saving to flash
  25. void jspSoftKill(JsParse *parse); ///< used when recovering from or saving to flash
  26. bool jspIsCreatedObject(JsParse *parse, JsVar *v); ///< Is v likely to have been created by this parser?
  27. /** Returns true if the constructor function given is the same as that
  28. * of the object with the given name. */
  29. bool jspIsConstructor(JsVar *constructor, const char *constructorName);
  30. /// Create a new built-in object that jswrapper can use to check for built-in functions
  31. JsVar *jspNewBuiltin(const char *name);
  32. /** Create a new object of the given instance and add it to root with name 'name'.
  33. * If name!=0, added to root with name, and the name is returned
  34. * If name==0, not added to root and Object itself returned */
  35. JsVar *jspNewObject(JsParse *parse, const char *name, const char *instanceOf);
  36. /// if interrupting execution, this is set
  37. bool jspIsInterrupted();
  38. /// if interrupting execution, this is set
  39. void jspSetInterrupted(bool interrupt);
  40. /// Has there been an error during parsing
  41. bool jspHasError();
  42. bool jspAddNativeFunction(JsParse *parse, const char *funcDesc, JsCallback callbackPtr);
  43. JsVar *jspEvaluateVar(JsParse *parse, JsVar *str, JsVar *scope);
  44. JsVar *jspEvaluate(JsParse *parse, const char *str);
  45. bool jspExecuteFunction(JsParse *parse, JsVar *func, JsVar *parent, int argCount, JsVar **argPtr);
  46. /// Evaluate a JavaScript module and return its exports
  47. JsVar *jspEvaluateModule(JsParse *parse, JsVar *moduleContents);
  48. /** When parsing, this enum defines whether
  49. we are executing or not */
  50. typedef enum {
  51. EXEC_NO = 0,
  52. EXEC_YES = 1,
  53. EXEC_BREAK = 2,
  54. EXEC_CONTINUE = 4,
  55. EXEC_INTERRUPTED = 8, // true if execution has been interrupted
  56. EXEC_ERROR = 16,
  57. EXEC_ERROR_LINE_REPORTED = 32, // if an error has been reported, set this so we don't do it too much
  58. EXEC_FOR_INIT = 64, // when in for initialiser parsing - hack to avoid getting confused about multiple use for IN
  59. EXEC_IN_LOOP = 128, // when in a loop, set this - we can then block break/continue outside it
  60. EXEC_IN_SWITCH = 256, // when in a switch, set this - we can then block break outside it/loops
  61. EXEC_RUN_MASK = EXEC_YES|EXEC_BREAK|EXEC_CONTINUE|EXEC_INTERRUPTED,
  62. EXEC_ERROR_MASK = EXEC_INTERRUPTED|EXEC_ERROR,
  63. EXEC_SAVE_RESTORE_MASK = EXEC_YES|EXEC_IN_LOOP|EXEC_IN_SWITCH, // the things JSP_SAVE/RESTORE_EXECUTE should keep track of
  64. } JsExecFlags;
  65. /** This structure is used when parsing the JavaScript. It contains
  66. * everything that should be needed. */
  67. typedef struct {
  68. JsParse *parse;
  69. JsLex *lex;
  70. // TODO: could store scopes as JsVar array for speed
  71. JsVarRef scopes[JSPARSE_MAX_SCOPES];
  72. int scopeCount;
  73. /// Value of 'this' reserved word
  74. JsVar *thisVar;
  75. JsExecFlags execute;
  76. } JsExecInfo;
  77. /// flags for jspParseFunction
  78. typedef enum {
  79. JSP_NOSKIP_A = 1,
  80. JSP_NOSKIP_B = 2,
  81. JSP_NOSKIP_C = 4,
  82. JSP_NOSKIP_D = 8,
  83. JSP_NOSKIP_E = 16,
  84. JSP_NOSKIP_F = 32,
  85. JSP_NOSKIP_G = 64,
  86. JSP_NOSKIP_H = 128,
  87. } JspSkipFlags;
  88. /// parse function with max 4 arguments (can set arg to 0 to avoid parse). Usually first arg will be 0, but if we DON'T want to skip names on an arg stuff, we can say
  89. bool jspParseFunction(JspSkipFlags skipName, JsVar **a, JsVar **b, JsVar **c, JsVar **d);
  90. /// parse function with max 8 arguments (can set arg to 0 to avoid parse). Usually first arg will be 0, but if we DON'T want to skip names on an arg stuff, we can say
  91. bool jspParseFunction8(JspSkipFlags skipName, JsVar **a, JsVar **b, JsVar **c, JsVar **d, JsVar **e, JsVar **f, JsVar **g, JsVar **h);
  92. bool jspParseVariableName(); ///< parse single variable name
  93. bool jspParseEmptyFunction(); ///< parse function with no arguments
  94. JsVar *jspParseSingleFunction(); ///< parse function with a single argument, return its value (no names!)
  95. JsVar *jspParseFunctionAsArray(); ///< parse a function with any number of argument, and return an array of de-named aruments
  96. /** Handle a function call (assumes we've parsed the function name and we're
  97. * on the start bracket). 'thisArg' is the value of the 'this' variable when the
  98. * function is executed (it's usually the parent object)
  99. *
  100. * If !isParsing and arg0!=0, argument 0 is set to what is supplied (same with arg1)
  101. *
  102. * functionName is used only for error reporting - and can be 0
  103. */
  104. JsVar *jspeFunctionCall(JsVar *function, JsVar *functionName, JsVar *thisArg, bool isParsing, int argCount, JsVar **argPtr);
  105. #endif /* JSPARSE_H_ */