Commit d8805d54 authored by Benedikt Engeser's avatar Benedikt Engeser

Merge branch 'master' of git@github.com:jroith/cup-eclipse.git

parents f1e29d25 b771524c
package de.in.tum.www2.cup;
import de.in.tum.www2.cup.internal.lalr_state;
public abstract class Conflict
{
private lalr_state state;
public Conflict(lalr_state state) {
this.state = state;
}
}
package de.in.tum.www2.cup;
import java.util.ArrayList;
import java.util.List;
public class ConflictManager
{
private CupContext context;
private List<Conflict> conflicts;
public List<Conflict> getConflicts() {
return conflicts;
}
public ConflictManager(CupContext context) {
this.context = context;
this.conflicts = new ArrayList<Conflict>();
}
public void addConflict(Conflict conflict) {
this.conflicts.add(conflict);
}
}
......@@ -4,6 +4,7 @@ package de.in.tum.www2.cup;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import de.in.tum.www2.cup.internal.*;
public class CupContext
......@@ -14,6 +15,7 @@ public class CupContext
private Boolean showWarnings = true;
private ErrorManager errorManager;
private ConflictManager conflictManager;
private HashMap<String, Object> singletons = new HashMap<String, Object>();
public <T> T getForContext(Class<T> cls)
......@@ -79,8 +81,13 @@ public class CupContext
return errorManager;
}
public ConflictManager getConflictManager() {
return conflictManager;
}
public CupContext (IErrorReporter er) {
this.errorManager = new ErrorManager(er);
this.conflictManager = new ConflictManager(this);
}
}
......
......@@ -15,7 +15,7 @@ class Demo
{
public static void main(String[] blah) throws Exception {
IErrorReporter er = new DefaultErrorReporter();
IErrorReporter er = new NoopErrorReporter();
FileInputStream input = new FileInputStream("/Users/jroith/testcup/testspeed.cup");
......@@ -41,10 +41,15 @@ class Demo
System.out.println("QUESTION is a declared terminal! :-)");
// System.out.println("\nComputing tables ...");
// LALRResult lalrResult = LALRResult.Compute(context, context.start_production);
System.out.println("\nComputing tables ...");
LALRResult lalrResult = LALRResult.Compute(context, context.start_production);
for (Conflict conflict : context.getConflictManager().getConflicts()) {
System.out.println(conflict);
}
// System.out.println(lalrResult.getActionTable());
......
......@@ -30,13 +30,12 @@ public class LALRResult {
public parse_reduce_table getReduceTable() {
return reduce_table;
}
private LALRResult(CupContext context) {
this.context = context;
}
public static LALRResult Compute (CupContext context, production start_production) throws internal_error {
LALRResult result = new LALRResult(context);
non_terminal.compute_nullability(context);
......
package de.in.tum.www2.cup;
import de.in.tum.www2.cup.internal.lalr_state;
public class ReduceReduceConflict extends Conflict
{
public ReduceReduceConflict(lalr_state state) {
super(state);
}
// state (index)
// conflict between:
// item1 ( toString ; has corresponding the_production -> link to Production if in AST)
// item2 ( toString ; has corresponding the_production -> link to Production if in AST)
// symbols (lookahead)
// resolved in favour of item1 / item2 (the_production respectively)
}
package de.in.tum.www2.cup;
import de.in.tum.www2.cup.internal.lalr_state;
public class ShiftReduceConflict extends Conflict
{
public ShiftReduceConflict(lalr_state state) {
super(state);
}
// state (index)
// conflict between:
// item which could be reduced ; toSimpleString has corresponding the_production -> link to Production if in AST)
// item(s) which could be shifted (have corresponding the_production -> link to Production if in AST)
// symbol
// resolved in favour of shifting
}
\ No newline at end of file
......@@ -5,8 +5,12 @@ import java.util.Hashtable;
import java.util.Enumeration;
import java.util.Stack;
import de.in.tum.www2.cup.Conflict;
import de.in.tum.www2.cup.ConflictManager;
import de.in.tum.www2.cup.CupContext;
import de.in.tum.www2.cup.ErrorSource;
import de.in.tum.www2.cup.ReduceReduceConflict;
import de.in.tum.www2.cup.ShiftReduceConflict;
import de.in.tum.www2.cup.internal.production.production_shared;
/** This class represents a state in the LALR viable prefix recognition machine.
......@@ -835,6 +839,11 @@ public class lalr_state {
else
message+="the second production.\n";
Conflict conflict = new ReduceReduceConflict(this);
ConflictManager cm = context.getConflictManager();
cm.addConflict(conflict);
/* count the conflict */
context.num_conflicts++;
context.getErrorManager().Warning(ErrorSource.LALR, message);
......@@ -880,6 +889,11 @@ public class lalr_state {
message += " under symbol "+ shared.find(conflict_sym).name() + "\n"+
" Resolved in favor of shifting.\n";
Conflict conflict = new ShiftReduceConflict(this);
ConflictManager cm = context.getConflictManager();
cm.addConflict(conflict);
/* count the conflict */
context.num_conflicts++;
context.getErrorManager().Warning(ErrorSource.LALR, message);
......
......@@ -103,8 +103,7 @@ public class MultiPageEditor extends MultiPageEditorPart implements
void createConflictsPage(Jumper jumper) {
Composite composite = new Composite(getContainer(), SWT.NONE);
IDocument doc = editor.getDocument();
conflictsView = new CupConflictsView(composite, jumper, doc);
conflictsView = new CupConflictsView(composite, jumper, editor);
addPage(CONFLICTS_PAGE_INDEX, composite);
setPageText(CONFLICTS_PAGE_INDEX, "Conflicts");
}
......
......@@ -2,6 +2,6 @@ package de.tum.in.www2.cupplugin.model;
public interface ICupParserLaLrChangeObserver {
public void ModelChange(Model model);
public void modelChanged(Model model);
}
......@@ -123,7 +123,7 @@ public class Model {
this.lalrContext = lalrContext;
for(Object observer : modelObservers) {
if (observer instanceof ICupParserLaLrChangeObserver) {
((ICupParserLaLrChangeObserver)observer).ModelChange(this);
((ICupParserLaLrChangeObserver)observer).modelChanged(this);
}
}
//System.out.println(lalrModel.getReduceTable().toString());
......
......@@ -86,7 +86,7 @@ public class CupActionTableView implements ICupEditorPageVisibility,
System.out.println("CupActionTableView will become visible.");
isVisible = true;
if (!Controller.getInstance(editor).requestJobRun()) {
ModelChange(Model.getInstanceForDocument(doc));
modelChanged(Model.getInstanceForDocument(doc));
}
}
......@@ -106,7 +106,7 @@ public class CupActionTableView implements ICupEditorPageVisibility,
}
@Override
public void ModelChange(Model model) {
public void modelChanged(Model model) {
if (!isVisible) {
return;
}
......
......@@ -267,7 +267,7 @@ public class CupConflictGraphView implements ICupParserLaLrChangeObserver,
}
@Override
public void ModelChange(Model model) {
public void modelChanged(Model model) {
if (!isVisible) {
return;
}
......@@ -421,7 +421,7 @@ public class CupConflictGraphView implements ICupParserLaLrChangeObserver,
// TODO: Handle no rerun!!
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
ModelChange(Model.getInstanceForDocument(doc));
modelChanged(Model.getInstanceForDocument(doc));
}
}
......
package de.tum.in.www2.cupplugin.views;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import org.eclipse.jface.text.IDocument;
......@@ -13,17 +14,30 @@ import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import de.in.tum.www2.cup.ConflictManager;
import de.in.tum.www2.cup.LALRResult;
import de.in.tum.www2.cup.ast.ParserResult;
import de.tum.in.www2.cupplugin.Colors;
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.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 CupConflictsView {
public class CupConflictsView implements ICupEditorPageVisibility,
ICupParserLaLrChangeObserver, IRegisterForControllerChanges {
private static final int OUTER_MARGIN = 10; // TODO: share between panels.
private Jumper jumper;
private CupTextEditor editor;
private boolean isVisible;
private List<ConflictPanel> conflictPanels;
private ScrolledComposite scrolled;
private Composite list;
static class ConflictPanel extends Composite {
public ConflictPanel(Composite parent, int style) {
......@@ -36,10 +50,14 @@ public class CupConflictsView {
}
}
public CupConflictsView(Composite parent, Jumper jumper, IDocument doc) {
public CupConflictsView(Composite parent, Jumper jumper, CupTextEditor editor) {
this.editor = editor;
this.jumper = jumper;
this.conflictPanels = new ArrayList<ConflictPanel> ();
Controller.getInstance(editor).registerObserver(this);
Model.getInstanceForDocument(editor.getDocument()).registerModelObserver(this);
parent.setBackground(new Color(parent.getDisplay(), Colors.white));
FillLayout fillLayout = new FillLayout();
fillLayout.marginWidth = OUTER_MARGIN;
......@@ -59,11 +77,23 @@ public class CupConflictsView {
gl.makeColumnsEqualWidth = true;
list.setLayout(gl);
for (int i=0; i < 100; i++) {
conflictPanels.add (new ConflictPanel(list, SWT.NONE));
refreshScrolledLayout ();
}
private void matchPanelNumber(int number) {
int excessPanels = number - this.conflictPanels.size();
if (excessPanels > 0) {
// TODO!
// for (int i=0; i < excessPanels; i++)
// this.conflictPanels.remove(0);
} else {
while (this.conflictPanels.size() < number) {
conflictPanels.add (new ConflictPanel(list, SWT.NONE));
}
}
refreshScrolledLayout ();
System.out.println("conflictPanels size: " + this.conflictPanels.size());
}
private void refreshScrolledLayout() {
......@@ -75,15 +105,52 @@ public class CupConflictsView {
System.out.println("CupConflictsView disposed!");
}
@Override
public void willBecomeVisible() {
System.out.println("CupConflictsView will become visible.");
this.isVisible = true;
if (!Controller.getInstance(editor).requestJobRun()) {
// TODO: Handle no rerun!!
System.out.println("NO RERUN!!!");
Model model = Model.getInstanceForDocument(editor.getDocument());
LALRResult result = model.getLaLrResult();
ConflictManager cm = result.getContext().getConflictManager();
matchPanelNumber(cm.getConflicts().size());
}
}
@Override
public void becameHidden() {
this.isVisible = false;
System.out.println("CupConflictsView became hidden.");
}
@Override
public EnumSet<JobsToDo> getRequiredJobs() {
EnumSet<JobsToDo> requestedJobs = EnumSet.noneOf(JobsToDo.class);
if (isVisible) {
requestedJobs = EnumSet.of(JobsToDo.buildTable);
}
return requestedJobs;
}
@Override
public void modelChanged(Model model) {
System.out.println("MODEL CHANGED !!!!!!!!!!!");
LALRResult result = model.getLaLrResult();
if (result != null) {
ConflictManager cm = result.getContext().getConflictManager();
matchPanelNumber(cm.getConflicts().size());
} else {
matchPanelNumber(0);
}
refreshScrolledLayout ();
}
}
......@@ -261,7 +261,7 @@ public class CupReduceGraphView implements ICupParserLaLrChangeObserver,
// TODO: Handle no rerun!!
IDocument doc = editor.getDocumentProvider().getDocument(
editor.getEditorInput());
ModelChange(Model.getInstanceForDocument(doc));
modelChanged(Model.getInstanceForDocument(doc));
}
}
......@@ -289,7 +289,7 @@ public class CupReduceGraphView implements ICupParserLaLrChangeObserver,
}
@Override
public void ModelChange(Model model) {
public void modelChanged(Model model) {
if (!isVisible) {
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