You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

JSParser.h 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #ifndef JSPARSER_H
  2. #define JSPARSER_H
  3. #include <string>
  4. #include <vector>
  5. #include <map>
  6. // for gcc & shared_ptr
  7. #include <iostream>
  8. #include <memory>
  9. // Internal JS types: String, Number, Function, Array, Object
  10. // blocks/scopes?
  11. class js_internal_storage {
  12. public:
  13. // toBool
  14. // toString
  15. // toNumber
  16. // toFunction
  17. // toArray
  18. // toObject
  19. // toReference
  20. // toForward
  21. virtual ~js_internal_storage() = default;
  22. };
  23. // deprecated
  24. /*
  25. class js_scope {
  26. public:
  27. js_scope *parent;
  28. // what do we need children for?
  29. // a callstack only includes it's parents (in JS?)
  30. std::vector<js_scope> children;
  31. js_scope() {
  32. parent = nullptr;
  33. }
  34. std::map<std::string, std::string> variables;
  35. std::map<std::string, js_internal_storage *> data;
  36. // maybe a vector of resolve on execution assignments...
  37. // feel like we need an instruction pointer...
  38. // esp. for loops
  39. // but how we address tokens, by index?
  40. // could just be strings tbh
  41. //std::vector<std::string> forwardExpressions;
  42. std::vector<std::string> forwardCalls;
  43. };
  44. */
  45. // maybe singletons?
  46. class js_bool : public js_internal_storage {
  47. public:
  48. bool value;
  49. };
  50. class js_string : public js_internal_storage {
  51. public:
  52. std::string value;
  53. };
  54. class js_number : public js_internal_storage {
  55. public:
  56. signed long value;
  57. };
  58. class js_object : public js_internal_storage {
  59. public:
  60. std::map<std::string, js_internal_storage *> value;
  61. };
  62. class js_array : public js_internal_storage {
  63. public:
  64. std::vector<js_internal_storage> value;
  65. };
  66. // we probably should extend js_object
  67. class js_function : public js_internal_storage {
  68. public:
  69. std::vector<std::string> tokens;
  70. std::vector<std::string> parameters;
  71. js_function *parentScope = nullptr;
  72. js_object locals;
  73. //js_scope scope;
  74. };
  75. class js_reference : public js_internal_storage {
  76. public:
  77. js_internal_storage *ptr; // to a function
  78. };
  79. class js_forward : public js_internal_storage {
  80. public:
  81. std::string expression;
  82. };
  83. std::string typeOfStorage(js_internal_storage *storage);
  84. // should this be a member function?
  85. js_internal_storage *jsLocateKey(const js_function *scope, const std::string key);
  86. // left side deference
  87. js_internal_storage **getObjectKeyPointer(const std::string input, const js_function *scope);
  88. // right side deference
  89. js_internal_storage *dereferenceObject(const std::string input, const js_function *scope);
  90. js_function *makeFunctionFromString(const std::string body, const std::string prototype, js_function *parent);
  91. // FIXME: should be a member function
  92. void jsDisplayScope(const js_function *scope, size_t level);
  93. size_t findClosing(std::string token, size_t start, char open, char close);
  94. size_t locateFunctionNameEnd(std::string source, size_t start);
  95. size_t locateFunctionParamsEnd(std::string source, size_t start);
  96. // locate the end of single quoted expression
  97. // start after ', return at '
  98. size_t locateSingleQuoteEnd(const std::string source, const size_t start);
  99. // locate the end of double quoted expression
  100. // start after ", return at "
  101. size_t locateDoubleQuoteEnd(const std::string source, const size_t start);
  102. size_t locateRegexEnd(const std::string source, const size_t start);
  103. size_t getNextExpression(const std::string source, const size_t start);
  104. size_t parseFunctionBody(std::string source, size_t start);
  105. js_internal_storage *doFunctionCall(js_function *func, std::string params, js_function &scope);
  106. js_internal_storage *doExpression(js_function &rootScope, std::string token);
  107. bool doAssignment(js_function &rootScope, std::string token);
  108. void parseArray(js_function &rootScope, std::string token);
  109. js_internal_storage *parseExpression(js_function &rootScope, std::string token);
  110. void parseJSON(js_function &rootScope, std::string token);
  111. std::vector<std::string> jsGetTokens(const std::string &source, const size_t start);
  112. // belongs in js_function
  113. js_internal_storage *jsParseTokens(const std::vector<std::string> &tokens, js_function *scope);
  114. class JavaScript {
  115. public:
  116. JavaScript() {
  117. this->setUpRoot();
  118. }
  119. void setUpRoot() {
  120. // set up default window reference
  121. js_reference *window = new js_reference;
  122. window->ptr = &rootScope;
  123. rootScope.locals.value["window"] = window;
  124. }
  125. void clear() {
  126. tokens.clear();
  127. /*
  128. rootScope.parent = nullptr;
  129. rootScope.children.clear();
  130. rootScope.variables.clear();
  131. */
  132. delete rootScope.locals.value["window"];
  133. this->setUpRoot();
  134. rootScope.locals.value.clear();
  135. }
  136. // has to be called after the scopes are applied
  137. // actually maybe not with the forward system and parse/exec split
  138. void parse(const std::string);
  139. void append(const std::shared_ptr<JavaScript> &source);
  140. void applyScope(const std::shared_ptr<JavaScript> &source);
  141. void execute();
  142. // each token is one statement
  143. std::vector<std::string> tokens;
  144. // we're just settings the rootScope.variables
  145. std::vector<std::string> definitions; // all var declarations and their expressions
  146. std::vector<std::string> instructions; // then a list of all remaining expressions and function calls
  147. js_function rootScope;
  148. //js_scope rootScope;
  149. };
  150. #endif