index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. module.exports = GlagolEslisp;
  2. function GlagolEslisp (options) {
  3. var eslisp = require('eslisp');
  4. return require('xtend')(require('glagol/formats/javascript'),
  5. { compile: compile
  6. , name: "Eslisp"
  7. , target: "javascript" });
  8. function compile (file) {
  9. var extraTransformMacros = options.extraTransformMacros;
  10. try {
  11. return eslisp(file.source,
  12. { transformMacros: [ glagolify ].concat(extraTransformMacros)});
  13. } catch (e) {
  14. if (e.node) e.message += "\n" + JSON.stringify(e.node);
  15. throw e;
  16. }
  17. }
  18. }
  19. function glagolify (token) {
  20. // eslisp global transform macro which
  21. // translates path-like syntax to regular
  22. // attribute access on the `_` object.
  23. if (token.type === "list") {
  24. token.values = token.values.map(glagolify);
  25. return token;
  26. }
  27. if (token.type === "atom" && token.value.indexOf('/') === 0 && token.value.length > 1) {
  28. return {
  29. type: 'list',
  30. location: token.location,
  31. values: [
  32. { type: 'atom', value: '.' },
  33. { type: 'atom', value: '$' },
  34. ].concat(token.value.split('/').slice(1).map(atomize))
  35. }
  36. }
  37. if (token.type === "atom" && token.value.indexOf('./') === 0) {
  38. return {
  39. type: 'list',
  40. location: token.location,
  41. values: [
  42. { type: 'atom', value: '.' },
  43. { type: 'atom', value: '_' },
  44. ].concat(token.value.split('/').slice(1).map(atomize))
  45. }
  46. }
  47. if (token.type === "atom" && token.value.indexOf('../') === 0) {
  48. return {
  49. type: 'list',
  50. location: token.location,
  51. values: [
  52. { type: 'atom', value: '.' },
  53. { type: 'atom', value: '__' },
  54. ].concat(token.value.split('/').slice(1).map(atomize))
  55. }
  56. }
  57. return token;
  58. function atomize (f) {
  59. return { type: 'atom', value: f === '..' ? '__' : f === '.' ? '_' : f }
  60. }
  61. }