/
FregeReferenceResolver.java
137 lines (122 loc) · 4.24 KB
/
FregeReferenceResolver.java
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
package frege.imp.referenceResolvers;
import io.usethesource.impulse.parser.IParseController;
import io.usethesource.impulse.services.IReferenceResolver;
import frege.data.TreeMap.TTreeMap;
import frege.compiler.types.Tokens.IShow_Token;
import frege.compiler.types.Tokens.TToken;
import frege.compiler.enums.TokenID.TTokenID;
import frege.compiler.types.Global.TGlobal;
import frege.compiler.types.QNames;
import frege.compiler.types.QNames.TQName;
import frege.compiler.types.Symbols.TSymbolT;
import frege.ide.Utilities;
import frege.imp.parser.FregeParseController;
import frege.prelude.PreludeBase.TEither;
import frege.prelude.PreludeBase.TEither.DLeft;
import frege.prelude.PreludeBase.TEither.DRight;
import frege.prelude.PreludeBase.TMaybe;
import frege.prelude.PreludeBase.TMaybe.DJust;
import frege.run7.Thunk;
public class FregeReferenceResolver implements IReferenceResolver {
public static class Symbol {
public final TGlobal g;
public final TSymbolT<TGlobal> sym;
public Symbol(TGlobal g, TSymbolT<TGlobal> sym) { this.g = g; this.sym = sym; }
public String toString() {
String s = FregeParseController.funSTIO(
Utilities.symbolDocumentation(sym), g);
return s; // Data.INice_QName.nicer(TSymbol.M.name(sym), g);
}
}
public static class Namespace {
public final TGlobal g;
public final String ns;
public final String pack;
public Namespace(TGlobal g, String ns, String p) {
this.g = g;
this.ns = ns;
this.pack = p;
}
public String toString() {
String s = FregeParseController.funSTIO(
Utilities.packDocumentation(Thunk.lazy(pack)), g);
return s;
}
}
public FregeReferenceResolver() {
}
/**
* Get the text associated with the given node for use in a link
* from (or to) that node
*/
public String getLinkText(Object node) {
// TODO Replace the following with an implementation suitable to your language and reference types
return "unimplemented"; // node.toString();
}
/**
* Get the target for the given source node in the AST produced by the
* given Parse Controller.
*/
public Object getLinkTarget(Object node, IParseController controller) {
TGlobal g = null;
if (controller != null) {
Object o = controller.getCurrentAst();
if (o != null && o instanceof TGlobal) g = (TGlobal) o;
}
if (g != null && node != null && node instanceof TToken) {
TToken tok = (TToken) node;
System.err.println("getLinkTarget: " + IShow_Token.show(tok));
int tid = TToken.tokid(tok);
if (tid == TTokenID.CHAR && "_".equals(TToken.value(tok))) {
tid = TTokenID.VARID;
tok = TToken.upd$tokid(tok, TTokenID.VARID);
tok = TToken.upd$value(tok, "it");
}
// is this '-' ?
final boolean isMinus = tid == TTokenID.CHAR && "-".equals(TToken.value(tok));
if (tid != TTokenID.VARID
&& tid != TTokenID.CONID
&& tid != TTokenID.QUALIFIER
&& tid != TTokenID.SOMEOP
&& !isMinus
) return null;
TMaybe<TEither<Short, TQName>> mb = TGlobal.resolved(g, tok);
DJust<TEither<Short, TQName>> just = mb.asJust();
if (just == null) {
if (isMinus) {
TToken neg = TToken.upd$value(
TToken.upd$tokid(tok, TTokenID.VARID),
"negate");
mb = TGlobal.resolved(g, neg);
just = mb.asJust();
if (just == null) return null;
}
else return null;
}
final TEither<Short, TQName> lr = just.mem1.call();
final DRight<Short, TQName> right = lr.asRight();
if (right != null) {
// this is a QName
TQName q = right.mem1.call();
final TMaybe<TSymbolT<TGlobal>> mbsym = TGlobal.findit(g, q).call();
final DJust<TSymbolT<TGlobal>> jsym = mbsym.asJust();
if (jsym == null) return null; // not found?
final TSymbolT<TGlobal> sym = jsym.mem1.call();
System.err.println("getLinkTarget: " + QNames.IShow_QName.show(q));
return new Symbol(g, sym);
}
final DLeft<Short, TQName> left = lr.asLeft();
if (left != null) {
// this is a namespace
String ns = TToken.value(tok);
final TTreeMap<String, String> tree = TGlobal.namespaces(g);
final TMaybe<String> mbpack = TTreeMap.lookupS(tree, ns);
final DJust<String> jpack = mbpack.asJust();
if (jpack == null) return null;
String pack = jpack.mem1.call();
return new Namespace(g, ns, pack);
}
}
return null;
}
}