use of catdata.fql.decl.FQLProgram in project fql by CategoricalData.
the class FqlCodeEditor method check.
public void check() {
String program = topArea.getText();
FQLProgram init;
try {
init = FQLParser.program(program);
} catch (ParserException e) {
int col = e.getLocation().column;
int line = e.getLocation().line;
topArea.requestFocusInWindow();
topArea.setCaretPosition(topArea.getDocument().getDefaultRootElement().getElement(line - 1).getStartOffset() + (col - 1));
// String s = e.getMessage();
// String t = s.substring(s.indexOf(" "));
// t.split("\\s+");
respArea.setText("Syntax error: " + e.getLocalizedMessage());
e.printStackTrace();
return;
} catch (RuntimeException e) {
respArea.setText("Error: " + e.getLocalizedMessage());
e.printStackTrace();
return;
}
String xxx = Driver.checkReport(init);
DateFormat format = DateFormat.getTimeInstance();
String time = format.format(new Date(System.currentTimeMillis()));
String foo = title;
JTextArea jta = new JTextArea(xxx);
jta.setWrapStyleWord(true);
jta.setLineWrap(true);
JScrollPane p = new JScrollPane(jta, ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
p.setPreferredSize(new Dimension(650, 300));
JOptionPane pane = new JOptionPane(p);
JDialog dialog = pane.createDialog(null, "Type Check " + foo + " - " + time);
dialog.setModal(false);
dialog.setResizable(true);
dialog.setVisible(true);
}
use of catdata.fql.decl.FQLProgram in project fql by CategoricalData.
the class FQLParser method program.
@SuppressWarnings({ "rawtypes", "unchecked" })
public static FQLProgram program(String s) {
List<NewDecl> ret = new LinkedList<>();
List decls = (List) program.parse(s);
for (Object d : decls) {
org.jparsec.functors.Pair pr = (org.jparsec.functors.Pair) d;
Object decl = pr.b;
String txt = pr.a.toString();
int idx = s.indexOf(txt);
if (idx < 0) {
throw new RuntimeException();
}
if (decl instanceof org.jparsec.functors.Pair) {
org.jparsec.functors.Pair p = (org.jparsec.functors.Pair) decl;
if (p.a.toString().equals("drop")) {
ret.add(NewDecl.dropDecl((List<String>) p.b));
continue;
}
}
Tuple3 t = (Tuple3) decl;
String kind = t.a.toString();
switch(kind) {
case "enum":
Tuple4 tte = (Tuple4) decl;
String name = (String) tte.b;
List<String> values = (List<String>) tte.d;
ret.add(NewDecl.typeDecl(name, values, idx));
break;
case "query":
Tuple4 tta = (Tuple4) decl;
name = (String) tta.b;
ret.add(NewDecl.queryDecl(name, idx, toQuery(tta.d)));
break;
case "QUERY":
tta = (Tuple4) decl;
name = (String) tta.b;
ret.add(NewDecl.fullQuery(name, toFullQuery(tta.d), idx));
break;
case "schema":
Tuple4 tt = (Tuple4) decl;
name = (String) tt.b;
ret.add(NewDecl.sigDecl(name, idx, toSchema(tt.d)));
break;
case "instance":
Tuple4 tt0 = (Tuple4) decl;
name = (String) t.b;
NewDecl toAdd = NewDecl.instDecl(name, idx, toInst(tt0.d));
ret.add(toAdd);
break;
case "mapping":
Tuple4 t0 = (Tuple4) decl;
name = (String) t.b;
ret.add(NewDecl.mapDecl(name, idx, toMapping(t0.d)));
break;
case "transform":
Tuple4 tx = (Tuple4) decl;
name = (String) tx.b;
ret.add(NewDecl.transDecl(name, idx, toTrans(tx.d)));
break;
default:
throw new RuntimeException("Unknown decl: " + kind);
}
}
return new FQLProgram(ret);
}
use of catdata.fql.decl.FQLProgram in project fql by CategoricalData.
the class FqlCodeEditor method vedit.
public void vedit() {
FQLProgram init = tryParse(topArea.getText());
if (init == null) {
respArea.setText(toDisplay);
return;
}
if (init.lines.isEmpty()) {
return;
}
String which = null;
int start = -1;
int offs = topArea.getCaretPosition();
int end = -1;
int i = 0;
int pos = 0;
for (String k : init.lines.keySet()) {
Integer v = init.lines.get(k);
if (v < offs && v > start) {
start = v;
which = k;
pos = i;
}
i++;
}
if (which == null) {
throw new RuntimeException();
}
int j = 0;
for (String k : init.lines.keySet()) {
if (j == pos + 1) {
end = init.lines.get(k);
break;
}
j++;
}
if (end == -1) {
end = topArea.getText().length();
}
InstExp ie = init.insts.get(which);
TransExp te = init.transforms.get(which);
if ((ie == null && te == null) || (ie != null && !(ie instanceof InstExp.Const)) || (te != null && !(te instanceof Const))) {
respArea.setText("Cannot visually edit " + which + ": only constant instances or transforms are visually editable.");
return;
}
try {
if (ie != null) {
InstExp.Const iec = (InstExp.Const) ie;
InstExp.Const n = new InstanceEditor(which, iec.sig.toSig(init), iec).show(Color.black);
if (n == null) {
return;
}
String newText = "instance " + which + " = " + n + " : " + n.sig + "\n\n";
topArea.replaceRange(newText, start, end);
} else {
Const iec = (Const) te;
if (iec == null) {
throw new RuntimeException("Anomaly: please report");
}
InstExp.Const s = (InstExp.Const) init.insts.get(iec.src);
InstExp.Const t = (InstExp.Const) init.insts.get(iec.dst);
Const n = new TransformEditor(which, init.insts.get(iec.src).type(init).toSig(init), iec, s, t).show(Color.black);
if (n == null) {
return;
}
String newText = "transform " + which + " = " + n + " : " + n.src + " -> " + n.dst + "\n\n";
topArea.replaceRange(newText, start, end);
}
} catch (FQLException fe) {
fe.printStackTrace();
respArea.setText(fe.getLocalizedMessage());
}
}
use of catdata.fql.decl.FQLProgram in project fql by CategoricalData.
the class FqlCodeEditor method format.
public void format() {
String input = topArea.getText();
FQLProgram p = tryParse(input);
if (p == null) {
respArea.setText(toDisplay);
return;
}
if (input.contains("//") || input.contains("/*")) {
int x = JOptionPane.showConfirmDialog(null, "Formatting will erase all comments - continue?", "Continue?", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE);
if (x != JOptionPane.YES_OPTION) {
return;
}
}
// order does not contain enums or drops
StringBuilder sb = new StringBuilder();
for (String k : p.enums.keySet()) {
Type t = p.enums.get(k);
if (!(t instanceof Type.Enum)) {
continue;
}
Type.Enum e = (Type.Enum) t;
sb.append("enum ").append(k).append(" = ").append(e.printFull());
sb.append("\n\n");
}
for (String k : p.order) {
Pair<String, Object> o = get(p, k);
sb.append(o.first).append(" ").append(k).append(" = ").append(o.second);
if (o.second instanceof InstExp.Const) {
InstExp.Const c = (InstExp.Const) o.second;
sb.append(" : ").append(c.sig);
} else if (o.second instanceof MapExp.Const) {
MapExp.Const c = (MapExp.Const) o.second;
sb.append(" : ").append(c.src).append(" -> ").append(c.dst);
} else if (o.second instanceof Const) {
Const c = (Const) o.second;
sb.append(" : ").append(c.src).append(" -> ").append(c.dst);
}
sb.append("\n\n");
}
if (!p.drop.isEmpty()) {
sb.append("drop ").append(PrettyPrinter.sep0(" ", p.drop)).append("\n\n");
}
topArea.setText(sb.toString().trim());
topArea.setCaretPosition(0);
}
Aggregations