/
java.clj
78 lines (72 loc) · 3.13 KB
/
java.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
(ns clj-highlight.syntax.java
(:use clj-highlight.syntax.general))
(def java-keywords #{})
(defn str-matcher [token]
(fn [^String string idx]
(if (.startsWith string token idx)
token)))
(defn java-ident-token
[default-kind]
(fn java-ident-token* [string idx states syntax]
(if-let [token (re-find #"^[a-zA-Z_][a-zA-Z_0-9]*" (subs string idx))]
(let [token (str token)]
(condp = token
"import"
(let [states (conj states :include)]
[[:keyword token {:state states}] states])
"package"
(let [states (conj states :namespace)]
[[:keyword token {:state states}] states])
"class"
(let [states (conj states :class)]
[[:keyword token {:state states}] states])
"interface"
(let [states (conj states :class)]
[[:keyword token {:state states}] states])
token
[[default-kind token {:state states}] states])))))
(defn java-number-token [string idx states syntax]
(let [s (subs string idx)]
(if (re-find #"^[\d.]" s)
(if-let [token (re-find #"^\d+[fFdD]|\d*\.\d+(?:[eE][+-]?\d+)?[fFdD]?|\d+[eE][+-]?\d+[fFdD]?" s)]
[[:float token {:state states}] states]
(if-let [token (re-find #"^0[xX][0-9A-Fa-f]+" s)]
[[:hex token {:state states}] states]
(if-let [token (re-find #"^(?>0[0-7]+)(?![89.eEfF])" s)]
[[:oct token {:state states}] states]
(if-let [token (re-find #"^\d+[lL]?" s)]
[[:integer token {:state states}] states]
nil)))))))
(def java-sub-tokens
(list
(re-token #"[;{]" :opperator :pop)
(java-ident-token :ident)))
(def syntax
{:keywords java-keywords
:syntax-name "Java"
:sub-tokens [(re-token #"[;{]" :opperator :pop)
(java-ident-token :ident)]
:default-tokens [(re-token #"\s+|\n" :space)
(re-token #"\.(?!\d)|[,?:()\[\]{};]|--|\+\+|&&|\|\||\*\*=?|[-+*\/%^~&|<>=!]=?|<<<?=?|>>>?=?" :opperator)
java-number-token
(token (str-matcher "\"") :string :string1)
(token (str-matcher "'") :string :string2)
(re-token #"/\*(?s:.*?)\*/|//[^\n\\]*(?:\\.[^\n\\]*)*" :comment)
(re-token #"@[a-zA-Z_][A-Za-z_0-9]*" :annotation)]
:initial [(include :default-tokens)
(java-ident-token :ident)]
:include [(re-token #"[a-zA-Z_][A-Za-z_0-9]*+(?:\.[a-zA-Z_][A-Za-z_0-9]*+)*+(?:\.\*)?" :include)
(include :sub-tokens)
(include :default-tokens)]
:namespace [(re-token #"[a-zA-Z_][A-Za-z_0-9]*+(?:\.[a-zA-Z_][A-Za-z_0-9]*+)*+" :namespace)
(include :sub-tokens)
(include :default-tokens)]
:class [(re-token #"[a-zA-Z_][A-Za-z_0-9]*" :class)
(include :sub-tokens)
(include :default-tokens)]
:string1 [(re-token #"[^\"\\]+" :string)
(token (str-matcher "\"") :string :pop)
(re-token #"\\(?:[bfnrtv\n\"'\\]|x[a-fA-F0-9]{1,2}|[0-7]{1,3}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8})" :string)]
:string2 [(re-token #"[^'\\]+" :string)
(token (str-matcher "'") :string :pop)
(re-token #"\\(?:[bfnrtv\n\"'\\]|x[a-fA-F0-9]{1,2}|[0-7]{1,3}|u[a-fA-F0-9]{4}|U[a-fA-F0-9]{8})" :string)]})