-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
parser.clj
248 lines (223 loc) · 11 KB
/
parser.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
(ns orchard.java.parser
"Source and docstring info for Java classes and members.
Parses `:doc`s using Markdown.
This ns is automatically discarded if `orchard.java.parser-next` can be loaded."
{:author "Jeff Valk"}
(:require
[clojure.java.io :as io]
[clojure.string :as string]
[orchard.java.parser-utils :refer [module-name parse-java parse-variable-element position source-path typesym]]
[orchard.misc :as misc])
(:import
(java.io StringReader)
(javax.lang.model.element Element ElementKind ExecutableElement TypeElement VariableElement)
(javax.swing.text.html HTML$Tag HTMLEditorKit$ParserCallback)
(javax.swing.text.html.parser ParserDelegator)
(jdk.javadoc.doclet DocletEnvironment)))
;;; ## JDK Compatibility
;;
;; This namespace requires JDK9+.
;;; ## Java Source Analysis
;;
;; Any metadata not available via reflection can be had from the source code; we
;; just need to parse it. In the case of docstrings, we actually need to parse
;; it twice -- first from Java source, and then from Javadoc HTML.
;;; ## Java Parsing
;;
;; The Java Compiler API provides in-process access to the Javadoc compiler.
;; Unlike the standard Java compiler which it extends, the Javadoc compiler
;; preserves docstrings (obviously), as well as source position and argument
;; names in its parse tree -- pieces we're after to augment reflection info.
;;
;; A few notes:
;;
;; 1. The compiler API `call` method is side-effect oriented; it returns only a
;; boolean indicating success. To use the result parse tree, we store this in
;; an atom.
;;
;; 2. The `result` atom must be scoped at the namespace level because a Doclet
;; is specified by passing a class name rather than an instance; hence, we
;; can't close over a local varaible in reify: `result` must be in scope when
;; the methods of a *new* instance of the Doclet class are called.
;;
;; 3. To compile an individual source that is defined as part of a module, the
;; compiler must be told to "patch" the module, and the source
;; `JavaFileobject`'s location must match the argument to the
;; "--patch-module" option.
;;
;; It's not clear how to make the "--patch-module" option work with a source
;; loaded from memory or a jar file; its syntax seems file system oriented.
;; Moreover, if the `StandardJavaFileManager` resolves a file, the
;; "--patch-module" option is matched, but if the exact same file is passed
;; as a proxy-ed `Simplejavafileobject` with an identical URI, the
;; compiler's internal `Enter` class doesn't see this as matcing the
;; "--patch-module" option. To accommodate this, the jar file entry is
;; written to a temp file and passed to the compiler from disk. Design-wise,
;; this is admittedly imperfect, but the performance cost is low and it works.
;;; ## Docstring Parsing
;;
;; Unlike source metadata (line, position, etc) that's available directly from
;; the compiler parse tree, docstrings are "some assembly required." Javadoc
;; comments use both `@tags` and HTML <tags> for semantics and formatting. The
;; latter could be passed through intact if our presentation layer could read
;; it, but we want a pure text representation, so we'll parse html to markdown.
;; This way it can either be rendered or displayed as text.
;; Use GFM extensions for multiline code blocks and tables.
(def markdown
"Syntax map from html tag to a tuple of tag type key, start, and end chars"
(let [char-map {:p ["\n\n"] :code ["`" "`"]
:br ["\n"] :code* ["\n\n```\n" "```\n\n"]
:em ["*" "*"] :table ["\n|--" "\n|--"]
:str ["**" "**"] :thead ["" "|--\n"]
:list ["\n"] :tr ["\n" "|"]
:li ["- "] :td ["|"]
:dd [": "] :th ["|"]}
tags {HTML$Tag/P :p HTML$Tag/TT :code
HTML$Tag/BR :br HTML$Tag/CODE :code
HTML$Tag/I :em HTML$Tag/VAR :code
HTML$Tag/EM :em HTML$Tag/KBD :code
HTML$Tag/B :str HTML$Tag/PRE :code*
HTML$Tag/STRONG :str HTML$Tag/BLOCKQUOTE :code*
HTML$Tag/UL :list HTML$Tag/TABLE :table
HTML$Tag/OL :list HTML$Tag/TR :tr
HTML$Tag/DL :list HTML$Tag/TD :td
HTML$Tag/LI :li HTML$Tag/TH :th
HTML$Tag/DT :li
HTML$Tag/DD :dd}]
(-> (reduce (fn [tags [tag k]]
(assoc tags tag (cons k (char-map k))))
{} tags)
(with-meta char-map))))
;; The HTML parser and DTD classes are in the `javax.swing` package, and have
;; internal references to the `sun.awt.AppContext` class. On Mac OS X, any use
;; of this class causes a stray GUI window to pop up. Setting the system
;; property below prevents this. We only set the property if it
;; hasn't already been explicitly set.
(when (nil? (System/getProperty "apple.awt.UIElement"))
(System/setProperty "apple.awt.UIElement" "true"))
;; We parse html and emit text in a single pass -- there's no need to build a
;; tree. The syntax map defines most of the output format, but a few stateful
;; rules are applied:
;;
;; 1. List items are indented to their nested depth.
;; 2. Nested elements with the same tag type key are coalesced (`<pre>` inside
;; of `<blockquote>` is common, for instance).
;; 3. A border row is inserted between `<th>` and `<td>` table rows. Since
;; `<thead>` and `<tbody>` are optional, we look for the th/td transition.
(defn parse-html
"Parse html to markdown text."
[html]
(let [sb (StringBuilder.)
sr (StringReader. html)
parser (ParserDelegator.)
stack (atom nil)
flags (atom #{})
handler (proxy [HTMLEditorKit$ParserCallback] []
(handleText [^chars chars _]
(.append sb (String. chars)))
(handleStartTag [tag _ _]
(let [[k start] (markdown tag)]
(when (and k (not= k (peek @stack)))
(swap! stack conj k)
;; Indent list items at the current depth.
(when (#{:li} k)
(let [depth (count (filter #{:list} @stack))]
(.append sb "\n")
(dotimes [_ (dec depth)]
(.append sb " "))))
;; Keep th/td state; emit border between th and td rows.
(when (#{:th} k) (swap! flags conj :th))
(when (and (#{:td} k) (@flags :th))
(.append sb (-> markdown meta :thead last)))
(when start (.append sb start)))))
(handleEndTag [tag _]
(let [[k _ end] (markdown tag)]
(when (and k (= k (peek @stack)))
(swap! stack pop)
(when (#{:table :td} k) (swap! flags disj :th))
(when end (.append sb end))))))]
(.parse parser sr handler false)
(-> (str sb)
(string/replace #"\n{3,}" "\n\n") ; normalize whitespace
(string/replace #" +```" "```"))))
(defn docstring
"Get parsed docstring text of `Element` e using source information in env"
[e ^DocletEnvironment env]
;;
;; NOTE This returns tags (e.g. @link) literally. To parse and resolve these,
;; we probably need to use: `(-> env .getDocTrees (.getDocCommentTree e))`.
;; That's an enhancement for another day.
{:doc (some-> env .getElementUtils (.getDocComment e) parse-html)})
;;; ## Java Parse Tree Traversal
;;
;; From the parse tree returned by the compiler, create a nested map structure
;; as produced by `orchard.java/reflect-info`: class members
;; are indexed first by name, then argument types.
(defprotocol Parsed
(parse-info* [o env]))
(defn parse-info
[o env]
(merge (parse-info* o env)
(docstring o env)
(position o env)))
(defn parse-executable-element [^ExecutableElement m env]
(let [argtypes (mapv #(-> ^VariableElement % .asType (typesym env)) (.getParameters m))]
{:name (if (= (.getKind m) ElementKind/CONSTRUCTOR)
(-> m .getEnclosingElement (typesym env)) ; class name
(-> m .getSimpleName str symbol)) ; method name
:type (-> m .getReturnType (typesym env))
:argtypes argtypes
:non-generic-argtypes (->> argtypes (mapv (comp symbol misc/normalize-subclass misc/remove-type-param str)))
:argnames (mapv #(-> ^VariableElement % .getSimpleName str symbol) (.getParameters m))}))
(extend-protocol Parsed
TypeElement ;; => class, interface, enum
(parse-info* [c env]
{:class (typesym c env)
:members (->> (.getEnclosedElements c)
(filter #(#{ElementKind/CONSTRUCTOR
ElementKind/METHOD
ElementKind/FIELD
ElementKind/ENUM_CONSTANT}
(.getKind ^Element %)))
(keep #(parse-info % env))
;; Index by name, argtypes. Args for fields are nil.
(group-by :name)
(reduce (fn [ret [n ms]]
(assoc ret n (zipmap (map :non-generic-argtypes ms) ms)))
{}))})
ExecutableElement ;; => method, constructor
(parse-info* [o env]
(parse-executable-element o env))
VariableElement ;; => field, enum constant
(parse-info* [o env]
(parse-variable-element o env)))
(def lock (Object.))
(defn source-info
"If the source for the Java class is available on the classpath, parse it
and return info to supplement reflection. Specifically, this includes source
file and position, docstring, and argument name info. Info returned has the
same structure as that of `orchard.java/reflect-info`."
[klass]
{:pre [(symbol? klass)]}
(locking lock ;; the jdk.javadoc.doclet classes aren't meant for concurrent modification/access.
(try
(when-let [path (source-path klass)]
(when-let [^DocletEnvironment root (parse-java path (module-name klass))]
(try
(let [path-resource (io/resource path)]
(assoc (->> (.getIncludedElements root)
(filter #(#{ElementKind/CLASS
ElementKind/INTERFACE
ElementKind/ENUM}
(.getKind ^Element %)))
(keep #(parse-info % root))
(filter #(= klass (:class %)))
(first))
;; relative path on the classpath
:file path
;; Legacy key. Please do not remove - we don't do breaking changes!
:path (.getPath path-resource)
;; Full URL, e.g. file:.. or jar:...
:resource-url path-resource))
(finally (.close (.getJavaFileManager root))))))
(catch Throwable _))))