Commit 15f3f474 authored by Sebastian Pretscher's avatar Sebastian Pretscher

Reduce Table View

parent 75ea76b2
......@@ -25,9 +25,14 @@ import org.eclipse.zest.core.viewers.IZoomableWorkbenchPart;
import org.eclipse.zest.core.viewers.ZoomContributionViewItem;
import de.in.tum.www2.cup.ConflictManager;
import de.in.tum.www2.cup.CupContext;
import de.in.tum.www2.cup.LALRResult;
import de.in.tum.www2.cup.internal.parse_action;
import de.in.tum.www2.cup.internal.parse_action_row;
import de.in.tum.www2.cup.internal.parse_action_table;
import de.in.tum.www2.cup.internal.parse_reduce_row;
import de.in.tum.www2.cup.internal.parse_reduce_table;
import de.in.tum.www2.cup.internal.reduce_action;
import de.tum.in.www2.cupplugin.controller.Controller;
import de.tum.in.www2.cupplugin.controller.Controller.JobsToDo;
import de.tum.in.www2.cupplugin.controller.IRegisterForControllerChanges;
......@@ -42,10 +47,9 @@ import de.tum.in.www2.cupplugin.views.CupOverviewView;
import de.tum.in.www2.cupplugin.views.CupReduceGraphView;
import de.tum.in.www2.cupplugin.views.CupReduceTableView;
public class MultiPageEditor extends MultiPageEditorPart
implements IRegisterForControllerChanges,
ICupParserLaLrChangeObserver,
IResourceChangeListener {
public class MultiPageEditor extends MultiPageEditorPart implements
IRegisterForControllerChanges, ICupParserLaLrChangeObserver,
IResourceChangeListener {
private CupTextEditor editor;
private ZoomContributionViewItem graphReduceZoomContribution;
......@@ -133,7 +137,8 @@ public class MultiPageEditor extends MultiPageEditorPart
void createOverviewPage(Jumper jumper) {
Composite composite = new Composite(getContainer(), SWT.NONE);
IDocument doc = editor.getDocument();
overviewView = new CupOverviewView(composite, jumper, doc, editor.getTitle(),editor);
overviewView = new CupOverviewView(composite, jumper, doc,
editor.getTitle(), editor);
addPage(OVERVIEW_PAGE_INDEX, composite);
setPageText(OVERVIEW_PAGE_INDEX, "Overview");
}
......@@ -156,7 +161,7 @@ public class MultiPageEditor extends MultiPageEditorPart
void createReduceTablePage(Jumper jumper) {
Composite composite = new Composite(getContainer(), SWT.NONE);
IDocument doc = editor.getDocument();
reduceTableView = new CupReduceTableView(composite, jumper, doc);
reduceTableView = new CupReduceTableView(composite, jumper, editor);
addPage(REDUCE_TABLE_PAGE_INDEX, composite);
setPageText(REDUCE_TABLE_PAGE_INDEX, "Reduce Table");
}
......@@ -175,7 +180,8 @@ public class MultiPageEditor extends MultiPageEditorPart
// only register after editor has been created.
Controller.getInstance(getEditor()).registerObserver(this);
Model.getInstanceForDocument(getEditor().getDocument()).registerModelObserver(this);
Model.getInstanceForDocument(getEditor().getDocument())
.registerModelObserver(this);
createOverviewPage(jumper);
createConflictsPage(jumper);
......@@ -212,7 +218,7 @@ public class MultiPageEditor extends MultiPageEditorPart
void jumpTo(int index) {
setActivePage(index);
}
public void gotoMarker(IMarker marker) {
setActivePage(EDITOR_PAGE_INDEX);
IDE.gotoMarker(getEditor(EDITOR_PAGE_INDEX), marker);
......@@ -245,7 +251,8 @@ public class MultiPageEditor extends MultiPageEditorPart
break;
case GRAPH_PAGE_INDEX:
graphReduceView.becameHidden();
removeObjectsWithMenuFromToolbar(graphReduceZoomContribution, graphReduceMenu);
removeObjectsWithMenuFromToolbar(graphReduceZoomContribution,
graphReduceMenu);
graphReduceZoomContribution = null;
graphReduceMenu = null;
break;
......@@ -253,7 +260,7 @@ public class MultiPageEditor extends MultiPageEditorPart
actionTableView.becameHidden();
break;
case REDUCE_TABLE_PAGE_INDEX:
reduceTableView.becameHidden();
reduceTableView.becameHidden();
break;
case CONFLICT_GRAPH_PAGE_INDEX:
conflictGraphView.becameHidden();
......@@ -328,19 +335,21 @@ public class MultiPageEditor extends MultiPageEditorPart
*/
IMenuManager manager = getEditorSite().getActionBars().getMenuManager();
graphReduceMenu = new MenuManager("Graph &Menu");
manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS, graphReduceMenu);
manager.prependToGroup(IWorkbenchActionConstants.MB_ADDITIONS,
graphReduceMenu);
graphReduceMenu.add(graphReduceZoomContribution);
getEditorSite().getActionBars().updateActionBars();
}
private void removeObjectsWithMenuFromToolbar(ContributionItem contItem, IMenuManager menu) {
private void removeObjectsWithMenuFromToolbar(ContributionItem contItem,
IMenuManager menu) {
if (contItem == null) {
return;
}
IMenuManager manager = getEditorSite().getActionBars().getMenuManager();
manager.remove(menu);
menu.remove(contItem);
//manager.remove(contItem);
// manager.remove(contItem);
}
@Override
......@@ -355,30 +364,46 @@ public class MultiPageEditor extends MultiPageEditorPart
if (status.hasFailed()) {
setCounter(CONFLICTS_PAGE_INDEX, -1);
setCounter(GRAPH_PAGE_INDEX, -1);
// setCounter(ACTION_TABLE_PAGE_INDEX, -1);
// setCounter(REDUCE_TABLE_PAGE_INDEX, -1);
setCounter(ACTION_TABLE_PAGE_INDEX, -1);
// setCounter(REDUCE_TABLE_PAGE_INDEX, -1);
}
}
}
@Override
public void modelChanged(Model model) {
LALRResult result = model.getLaLrResult();
CupContext ctx = model.getLaLrContext();
if (result == null)
return;
ConflictManager cm = result.getContext().getConflictManager();
setCounter(CONFLICTS_PAGE_INDEX, cm.getConflicts().size());
parse_action_table actionTable = result.getActionTable();
setCounter(GRAPH_PAGE_INDEX, actionTable.num_states());
/* parse_action_table actionTable = result.getActionTable();
if (actionTable != null)
setCounter(ACTION_TABLE_PAGE_INDEX, actionTable.num_states());
if (actionTable != null) {
setCounter(GRAPH_PAGE_INDEX, actionTable.num_states());
int counter = 0;
for (int row = 0; row < actionTable.num_states(); row++) {
for (int col = 0; col < parse_action_row.size(ctx); col++) {
if (actionTable.under_state[row].under_term[col].kind() != parse_action.ERROR) {
counter++;
}
}
}
setCounter(ACTION_TABLE_PAGE_INDEX, counter);
}
parse_reduce_table reduceTable = result.getReduceTable();
if (reduceTable != null)
setCounter(REDUCE_TABLE_PAGE_INDEX, reduceTable.num_states());
*/
if(reduceTable!=null) {
int counter = 0;
for (int row = 0; row < reduceTable.num_states(); row++) {
for (int col = 0; col < parse_reduce_row.size(ctx); col++) {
if (reduceTable.under_state[row].under_non_term[col] != null) {
counter++;
}
}
}
setCounter(REDUCE_TABLE_PAGE_INDEX, counter);
}
}
}
package de.tum.in.www2.cupplugin.views;
import java.util.EnumSet;
import java.util.LinkedList;
import java.util.List;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;
import de.in.tum.www2.cup.CupContext;
import de.in.tum.www2.cup.LALRResult;
import de.in.tum.www2.cup.internal.lalr_state;
import de.in.tum.www2.cup.internal.non_terminal;
import de.in.tum.www2.cup.internal.parse_reduce_row;
import de.in.tum.www2.cup.internal.parse_reduce_table;
import de.tum.in.www2.cupplugin.controller.Controller.JobsToDo;
import de.tum.in.www2.cupplugin.controller.Controller;
import de.tum.in.www2.cupplugin.controller.IRegisterForControllerChanges;
import de.tum.in.www2.cupplugin.controller.JobStatus;
import de.tum.in.www2.cupplugin.editors.CupTextEditor;
import de.tum.in.www2.cupplugin.editors.ICupEditorPageVisibility;
import de.tum.in.www2.cupplugin.editors.Jumper;
import de.tum.in.www2.cupplugin.model.ICupParserLaLrChangeObserver;
import de.tum.in.www2.cupplugin.model.Model;
public class CupReduceTableView extends FailableView implements
ICupEditorPageVisibility, ICupParserLaLrChangeObserver,
IRegisterForControllerChanges {
public class CupReduceTableView extends FailableView {
private CupTextEditor editor;
private IDocument doc;
private TreeViewer treeViewer;
private boolean isVisible = false;
private List<ReduceNode> stateList;
public CupReduceTableView(Composite realParent, Jumper jumper, IDocument doc) {
public CupReduceTableView(Composite realParent, Jumper jumper,
CupTextEditor editor) {
super(realParent);
Composite parent = getMain();
this.editor = editor;
this.doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
Controller.getInstance(editor).registerObserver(this);
Model.getInstanceForDocument(doc).registerModelObserver(this);
this.stateList = new LinkedList<ReduceNode>();
parent.setLayout(new FillLayout());
this.treeViewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.BORDER);
treeViewer.getTree().setHeaderVisible(true);
treeViewer.setContentProvider(new ReduceNodeContentProvider());
treeViewer.setLabelProvider(new ReduceNodeLabelProvider());
TreeColumn actionNodeColumn = new TreeColumn(treeViewer.getTree(),
SWT.LEFT);
treeViewer.getTree().setLinesVisible(true);
actionNodeColumn.setAlignment(SWT.LEFT);
actionNodeColumn.setText("Reduces Nodes");
actionNodeColumn.setWidth(160);
TreeColumn actionNodeDoColumn = new TreeColumn(treeViewer.getTree(),
SWT.LEFT);
actionNodeDoColumn.setAlignment(SWT.RIGHT);
actionNodeDoColumn.setText("Target");
actionNodeDoColumn.setWidth(160);
// the viewer field is an already configured TreeViewer
Tree tree = (Tree) treeViewer.getControl();
tree.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
if (e.item != null) {
TreeItem item = (TreeItem) e.item;
if (item.getItemCount() > 0) {
item.setExpanded(!item.getExpanded());
// update the viewer
treeViewer.refresh();
}
}
}
});
treeViewer.setInput(stateList);
}
public void dispose() {
System.out.println("ConflictTableView disposed!");
}
@Override
public void willBecomeVisible() {
System.out.println("CupReduceTableView will become visible.");
isVisible = true;
if (!Controller.getInstance(editor).requestJobRun()) {
modelChanged(Model.getInstanceForDocument(doc));
}
}
@Override
public void becameHidden() {
System.out.println("CupReduceTableView became hidden.");
isVisible = false;
}
@Override
public EnumSet<JobsToDo> getRequiredJobs() {
EnumSet<JobsToDo> requestedJobs = EnumSet.noneOf(JobsToDo.class);
if (isVisible) {
requestedJobs = EnumSet.of(JobsToDo.buildTable);
}
return requestedJobs;
}
@Override
public void jobStatusChanged(JobStatus status) {
// TODO Auto-generated method stub
}
@Override
public void modelChanged(Model model) {
if (!isVisible) {
return;
}
stateList.clear();
LALRResult lalrResult = model.getLaLrResult();
CupContext context = model.getLaLrContext();
parse_reduce_table table = lalrResult.getReduceTable();
// System.out.println(table);
for (int row = 0; row < table.num_states(); row++) {
ReduceNode n = new ReduceNode(row, "State " + row);
non_terminal.non_terminal_shared nonTShared = non_terminal
.getShared(context);
for (int col = 0; col < parse_reduce_row.size(context); col++) {
lalr_state tState = table.under_state[row].under_non_term[col];
if (tState != null) {
non_terminal t = nonTShared.find(col);
ReduceNodeChild c = new ReduceNodeChild(n, t.toString(), t,
tState);
n.getChildren().add(c);
}
}
stateList.add(n);
}
treeViewer.refresh();
}
static class ReduceNode {
private final int index;
private final String name;
private final List<ReduceNodeChild> children;
public ReduceNode(int index, String name) {
this.index = index;
this.name = name;
this.children = new LinkedList<ReduceNodeChild>();
}
public int getIndex() {
return index;
}
public String getName() {
return name;
}
public List<ReduceNodeChild> getChildren() {
return children;
}
@Override
public boolean equals(Object o) {
if (o == null) {
return false;
}
if (o instanceof ReduceNode) {
ReduceNode n = (ReduceNode) o;
if (n.getName().equals(this.name)) {
return true;
}
}
return false;
}
@Override
public int hashCode() {
return name.hashCode();
}
}
static class ReduceNodeChild {
private final ReduceNode parent;
private final String name;
private final non_terminal symbol;
private final lalr_state state;
public ReduceNodeChild(ReduceNode parent, String name,
non_terminal symbol, lalr_state state) {
this.parent = parent;
this.name = name;
this.symbol = symbol;
this.state = state;
}
public ReduceNode getParent() {
return parent;
}
public String getName() {
return name;
}
public non_terminal getSymbol() {
return symbol;
}
public lalr_state getState() {
return state;
}
}
class ReduceNodeContentProvider implements ITreeContentProvider {
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
// TODO Auto-generated method stub
}
@Override
public Object[] getElements(Object inputElement) {
if (inputElement instanceof ReduceNode) {
return new ReduceNode[] { (ReduceNode) inputElement };
} else if (inputElement instanceof ReduceNode[]) {
return (ReduceNode[]) inputElement;
} else if (inputElement instanceof List) {
return ((List) inputElement).toArray();
}
return new Object[0];
}
@Override
public Object[] getChildren(Object parentElement) {
if (parentElement instanceof ReduceNode) {
ReduceNode n = (ReduceNode) parentElement;
return n.getChildren().toArray();
}
return null;
}
@Override
public Object getParent(Object element) {
if (element instanceof ReduceNodeChild) {
ReduceNodeChild c = (ReduceNodeChild) element;
return c.getParent();
}
return null;
}
@Override
public boolean hasChildren(Object element) {
if (element instanceof ReduceNode) {
ReduceNode n = (ReduceNode) element;
return !n.getChildren().isEmpty();
}
return false;
}
}
class ReduceNodeLabelProvider implements ITableLabelProvider {
@Override
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}
@Override
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
@Override
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}
@Override
public String getColumnText(Object element, int columnIndex) {
switch (columnIndex) {
case 0:
if (element instanceof ReduceNode) {
ReduceNode n = (ReduceNode) element;
return n.getName();
} else if (element instanceof ReduceNodeChild) {
ReduceNodeChild c = (ReduceNodeChild) element;
return c.getSymbol().name();
}
break;
case 1:
if (element instanceof ReduceNodeChild) {
ReduceNodeChild c = (ReduceNodeChild) element;
return "State "+c.getState().index();
}
break;
}
return "";
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment