Advertisement
Guest User

Untitled

a guest
Sep 17th, 2019
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. #_(
  4. "exec" "clojure" "-Sdeps" "{:deps {org.clojure/clojurescript {:mvn/version \"1.10.520\"}}}" "$0" "$@"
  5. )
  6.  
  7. ;; running js_parser.clj "function foo(x) { var y = x + 1; }" will print:
  8. ;; [{:type :function, :name "foo", :body [{:variable-statement [{:lvalue "y", :initializer {:type :binary-op, :left "x", :operator "+", :right "1"}}]}], :params ["x"]}]
  9.  
  10.  
  11. (import '[com.google.javascript.jscomp.parsing.parser
  12. Parser
  13. Parser$Config
  14. SourceFile
  15. IdentifierToken
  16. LiteralToken
  17. Token])
  18. (import '[com.google.javascript.jscomp.parsing.parser.trees
  19. FunctionDeclarationTree
  20. VariableStatementTree
  21. VariableDeclarationListTree
  22. VariableDeclarationTree
  23. IdentifierExpressionTree
  24. BlockTree
  25. FormalParameterListTree
  26. EmptyStatementTree
  27. LiteralExpressionTree
  28. BinaryOperatorTree])
  29.  
  30. (require '[clojure.pprint :refer [pprint]])
  31. (require '[clojure.datafy :refer [datafy]])
  32.  
  33. (defn inspect [x]
  34. (pprint (datafy (.getClass x))))
  35.  
  36. (defmulti ->clj class)
  37. (defmethod ->clj nil [x] x)
  38.  
  39. (defmethod ->clj com.google.common.collect.ImmutableList [x]
  40. (mapv ->clj x))
  41. (defmethod ->clj VariableStatementTree [x]
  42. {:variable-statement (->clj (.-declarations x))})
  43.  
  44. (defmethod ->clj VariableDeclarationListTree [x]
  45. (->clj (.-declarations x)))
  46.  
  47. (defmethod ->clj VariableDeclarationTree [x]
  48. {:lvalue (->clj (.-lvalue x))
  49. :initializer (->clj (.-initializer x))})
  50.  
  51. (defmethod ->clj IdentifierExpressionTree [x]
  52. (->clj (.-identifierToken x)))
  53.  
  54. (defmethod ->clj IdentifierToken [x]
  55. (.-value x))
  56.  
  57. (defmethod ->clj FunctionDeclarationTree [x]
  58. {:type :function
  59. :name (->clj (.-name x))
  60. :body (->clj (.-functionBody x))
  61. :params (->clj (.-formalParameterList x))})
  62.  
  63. (defmethod ->clj BlockTree [x]
  64. (->clj (.-statements x)))
  65.  
  66. (defmethod ->clj FormalParameterListTree [x]
  67. (->clj (.-parameters x)))
  68.  
  69. (defmethod ->clj EmptyStatementTree [x]
  70. nil)
  71.  
  72. (defmethod ->clj LiteralExpressionTree [x]
  73. (->clj (.-literalToken x)))
  74.  
  75. (defmethod ->clj LiteralToken [x]
  76. (.-value x))
  77.  
  78. (defmethod ->clj BinaryOperatorTree [x]
  79. {:type :binary-op
  80. :left (->clj (.-left x))
  81. :operator (->clj (.-operator x))
  82. :right (->clj (.-right x))})
  83.  
  84. (defmethod ->clj Token [x]
  85. (str x))
  86.  
  87. (def input (first *command-line-args*))
  88.  
  89. (def parsed
  90. (let [prog (.parseProgram (Parser. (Parser$Config.) nil (SourceFile. "" input)))]
  91. (->clj (.-sourceElements prog))))
  92.  
  93. (prn parsed)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement