ast.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.toAst = exports.AnnotationAstNode = exports.ObjAstNode = exports.ArrAstNode = exports.BinAstNode = exports.StrAstNode = exports.FloatAstNode = exports.NintAstNode = exports.UintAstNode = exports.BoolAstNode = exports.NullAstNode = void 0;
  4. const utf8_1 = require("@jsonjoy.com/util/lib/strings/utf8");
  5. class NullAstNode {
  6. constructor() {
  7. this.val = null;
  8. this.len = 1;
  9. }
  10. byteLength() {
  11. return 1;
  12. }
  13. }
  14. exports.NullAstNode = NullAstNode;
  15. class BoolAstNode {
  16. constructor(val) {
  17. this.val = val;
  18. this.len = 1;
  19. }
  20. byteLength() {
  21. return 1;
  22. }
  23. }
  24. exports.BoolAstNode = BoolAstNode;
  25. class UintAstNode {
  26. constructor(val) {
  27. this.val = val;
  28. if (!val)
  29. this.len = 0;
  30. else if (val <= 0xff)
  31. this.len = 1;
  32. else if (val <= 0xffff)
  33. this.len = 2;
  34. else if (val <= 0xffffff)
  35. this.len = 3;
  36. else if (val <= 0xffffffff)
  37. this.len = 4;
  38. else if (val <= 0xffffffffff)
  39. this.len = 5;
  40. else if (val <= 0xffffffffffff)
  41. this.len = 6;
  42. else
  43. this.len = 7;
  44. }
  45. byteLength() {
  46. return 1 + this.len;
  47. }
  48. }
  49. exports.UintAstNode = UintAstNode;
  50. class NintAstNode {
  51. constructor(val) {
  52. this.val = val;
  53. const uint = -val;
  54. if (!uint)
  55. this.len = 0;
  56. else if (uint <= 0xff)
  57. this.len = 1;
  58. else if (uint <= 0xffff)
  59. this.len = 2;
  60. else if (uint <= 0xffffff)
  61. this.len = 3;
  62. else if (uint <= 0xffffffff)
  63. this.len = 4;
  64. else if (uint <= 0xffffffffff)
  65. this.len = 5;
  66. else if (uint <= 0xffffffffffff)
  67. this.len = 6;
  68. else
  69. this.len = 7;
  70. }
  71. byteLength() {
  72. return 1 + this.len;
  73. }
  74. }
  75. exports.NintAstNode = NintAstNode;
  76. class FloatAstNode {
  77. constructor(val) {
  78. this.val = val;
  79. this.len = 8;
  80. }
  81. byteLength() {
  82. return 1 + this.len;
  83. }
  84. }
  85. exports.FloatAstNode = FloatAstNode;
  86. const vUintLen = (num) => {
  87. if (num <= 0b1111111)
  88. return 1;
  89. else if (num <= 16383)
  90. return 2;
  91. else if (num <= 2097151)
  92. return 3;
  93. else if (num <= 268435455)
  94. return 4;
  95. else if (num <= 34359738367)
  96. return 5;
  97. else
  98. return 6;
  99. };
  100. class StrAstNode {
  101. constructor(val) {
  102. this.val = val;
  103. this.len = (0, utf8_1.utf8Size)(val);
  104. }
  105. byteLength() {
  106. return this.len < 14 ? 1 + this.len : 1 + vUintLen(this.len) + this.len;
  107. }
  108. }
  109. exports.StrAstNode = StrAstNode;
  110. class BinAstNode {
  111. constructor(val) {
  112. this.val = val;
  113. this.len = val.length;
  114. }
  115. byteLength() {
  116. return this.len < 14 ? 1 + this.len : 1 + vUintLen(this.len) + this.len;
  117. }
  118. }
  119. exports.BinAstNode = BinAstNode;
  120. class ArrAstNode {
  121. constructor(val) {
  122. this.val = val;
  123. if (val === null) {
  124. this.len = 1;
  125. }
  126. else {
  127. if (!val.length)
  128. this.len = 0;
  129. else {
  130. let elementLength = 0;
  131. for (let i = 0; i < val.length; i++)
  132. elementLength += val[i].byteLength();
  133. this.len = elementLength;
  134. }
  135. }
  136. }
  137. byteLength() {
  138. return this.len < 14 ? 1 + this.len : 1 + vUintLen(this.len) + this.len;
  139. }
  140. }
  141. exports.ArrAstNode = ArrAstNode;
  142. class ObjAstNode {
  143. constructor(val) {
  144. this.val = val;
  145. if (val === null) {
  146. this.len = 1;
  147. }
  148. else {
  149. if (!val.size)
  150. this.len = 0;
  151. else {
  152. let len = 0;
  153. val.forEach((node, symbolId) => {
  154. len += vUintLen(symbolId) + node.byteLength();
  155. });
  156. this.len = len;
  157. }
  158. }
  159. }
  160. byteLength() {
  161. return this.len < 14 ? 1 + this.len : 1 + vUintLen(this.len) + this.len;
  162. }
  163. }
  164. exports.ObjAstNode = ObjAstNode;
  165. class AnnotationAstNode {
  166. constructor(val, annotations) {
  167. this.val = val;
  168. this.annotations = annotations;
  169. let len = 0;
  170. for (let i = 0; i < annotations.length; i++)
  171. len += vUintLen(annotations[i]);
  172. this.annotationLen = len;
  173. len += vUintLen(len);
  174. len += val.byteLength();
  175. this.len = len;
  176. }
  177. byteLength() {
  178. return this.len < 14 ? 1 + this.len : 1 + vUintLen(this.len) + this.len;
  179. }
  180. }
  181. exports.AnnotationAstNode = AnnotationAstNode;
  182. const isSafeInteger = Number.isSafeInteger;
  183. const toAst = (val, symbols) => {
  184. if (val === null)
  185. return new NullAstNode();
  186. if (val instanceof Array)
  187. return new ArrAstNode(val.map((el) => (0, exports.toAst)(el, symbols)));
  188. if (val instanceof Uint8Array)
  189. return new BinAstNode(val);
  190. switch (typeof val) {
  191. case 'boolean':
  192. return new BoolAstNode(val);
  193. case 'number': {
  194. if (isSafeInteger(val))
  195. return val >= 0 ? new UintAstNode(val) : new NintAstNode(val);
  196. else
  197. return new FloatAstNode(val);
  198. }
  199. case 'string':
  200. return new StrAstNode(val);
  201. case 'object': {
  202. const struct = new Map();
  203. for (const key in val) {
  204. const symbolId = symbols.add(key);
  205. struct.set(symbolId, (0, exports.toAst)(val[key], symbols));
  206. }
  207. return new ObjAstNode(struct);
  208. }
  209. }
  210. throw new Error('UNKNOWN_TYPE');
  211. };
  212. exports.toAst = toAst;
  213. //# sourceMappingURL=ast.js.map