AtomicEditor.js 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. __atomic_acorn = require('acorn');
  2. __atomic_acorn_loose = require('acorn_loose');
  3. __atomic_beautify = require('beautify');
  4. function __atomic_parse_to_json(source) {
  5. var start = new Date().getTime();
  6. var comments = [];
  7. var ast = __atomic_acorn.parse( source, {
  8. // collect ranges for each node
  9. ranges: true,
  10. locations: true,
  11. onComment: comments,
  12. });
  13. var end = new Date().getTime();
  14. var time = end - start;
  15. // print('Acorn parse time: ' + time);
  16. ast["comments"] = comments;
  17. start = new Date().getTime();
  18. ast = JSON.stringify(ast);
  19. end = new Date().getTime();
  20. time = end - start;
  21. // print('JSON.stringify time: ' + time);
  22. return ast;
  23. }
  24. function __atomic_parse_to_json_loose(source) {
  25. var start = new Date().getTime();
  26. var comments = [];
  27. var ast = __atomic_acorn_loose.parse_dammit( source, {
  28. // collect ranges for each node
  29. ranges: true,
  30. locations: true,
  31. onComment: comments,
  32. });
  33. var end = new Date().getTime();
  34. var time = end - start;
  35. // print('Acorn parse time: ' + time);
  36. ast["comments"] = comments;
  37. start = new Date().getTime();
  38. ast = JSON.stringify(ast);
  39. end = new Date().getTime();
  40. time = end - start;
  41. // print('JSON.stringify time: ' + time);
  42. return ast;
  43. }
  44. function __atomic_parse_error_check(source) {
  45. try {
  46. __atomic_acorn.parse( source, {
  47. ranges: true,
  48. locations: true,
  49. });
  50. } catch(e) {
  51. //if (!(e instanceof __atomic_acorn.SyntaxError)) throw e;
  52. if (!(e instanceof SyntaxError))
  53. return false; // rethrow?
  54. return e;
  55. }
  56. return false;
  57. }
  58. // TODO: options
  59. function __atomic_js_beautify(source) {
  60. return __atomic_beautify.js_beautify(source);
  61. }