-
Notifications
You must be signed in to change notification settings - Fork 24
/
Gadt.java
107 lines (94 loc) · 2.56 KB
/
Gadt.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
/**
* (c) 2011, Alejandro Serrano
* Released under the terms of the EPL.
*/
package net.sf.eclipsefp.haskell.browser.items;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
/**
* Represents a GADT-like declaration, that is, a data or a newtype declaration.
*
* @author Alejandro Serrano
*/
public abstract class Gadt extends Declaration {
String[] context;
String[] vars;
String kind;
Constructor[] cons;
protected void setInfo(String[] context, String name, String[] vars,
String kind, Constructor[] cons) {
this.context = context;
this.setName(name);
this.vars = vars;
this.kind = kind;
this.cons = cons;
}
protected void setInfo(JSONObject o) throws JSONException {
this.setDoc(o);
ArrayList<Constructor> aCons = new ArrayList<Constructor>();
JSONArray jCons = o.getJSONArray("decls");
for (int i = 0; i < jCons.length(); i++) {
aCons.add(new Constructor(jCons.getJSONObject(i)));
}
JSONObject head = o.getJSONObject("head");
String oKind = o.optString("kind", "");
if (oKind.equals("null"))
oKind = "";
this.setInfo(Util.getStringArray(o.getJSONArray("context")),
head.getString("name"),
Util.getStringArray(head.getJSONArray("vars")),
oKind,
aCons.toArray(new Constructor[jCons.length()]));
}
public String[] getContext() {
return this.context;
}
public String[] getTypeVariables() {
return this.vars;
}
public String getKind() {
return this.kind;
}
public Constructor[] getConstructors() {
return this.cons;
}
public String getCompleteDefinition(String keyword) {
StringBuilder builder = new StringBuilder(keyword);
builder.append(' ');
if (this.getContext().length > 1) {
builder.append('(');
builder.append(this.getContext()[0]);
for (int i = 1; i < this.getContext().length; i++) {
builder.append(", ");
builder.append(this.getContext()[i]);
}
builder.append(") ");
} else if (this.getContext().length == 1) {
builder.append(this.getContext()[0]);
builder.append(' ');
}
if (this.getContext().length > 0)
builder.append("=> ");
builder.append(this.getName());
for (String tvar : this.getTypeVariables()) {
builder.append(' ');
builder.append(tvar);
}
if (this.getKind().length()>0) {
builder.append(" :: ");
builder.append(this.getKind());
}
return builder.toString();
}
@Override
public String getShownName() {
StringBuilder name = new StringBuilder(this.name);
for (String var : this.vars) {
name.append(' ');
name.append(var);
}
return name.toString();
}
}