Commit bd5e24dd authored by Sebastian Pretscher's avatar Sebastian Pretscher

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

parents 5cb54e48 96575236
......@@ -26,7 +26,7 @@
<mkdir dir="${classes}"/>
<mkdir dir="${result}"/>
<javac srcdir="${java}" destdir="${classes}">
<javac srcdir="${java}" includeantruntime="false" destdir="${classes}">
<classpath refid="libraries" />
</javac>
......
......@@ -140,9 +140,6 @@ action code {:
protected String multipart_name = new String();
protected Stack multipart_names = new Stack();
// TODO: this is a hack, since we can't store the CODE_STRING offsets properly.
private int prod_part_offset;
/** table of declared symbols -- contains production parts indexed by name */
protected Hashtable symbols = new Hashtable();
......@@ -341,13 +338,15 @@ spec ::=
package_spec ::=
PACKAGE : p
multipart_id
multipart_id : mi
{:
/* save the package name */
context.package_name = multipart_name;
parserResult.pack = new de.in.tum.www2.cup.ast.Package(
new Name (multipart_name, null), // TODO: from, to
new Name (multipart_name,
Range.fromLocations(mixleft, mixright)
),
new Range(Position.fromLocation(pxleft),
Position.fromComplexSymbolRight((ComplexSymbol) cur_token)));
......@@ -372,15 +371,15 @@ import_list ::=
import_spec ::=
IMPORT : import_token
import_id
import_id : ii
SEMI : semi {:
/* save this import on the imports list */
context.import_list.push(multipart_name);
Import imprt = new Import(
new Name (multipart_name, null), // TODO: from, to
new Range(Position.fromLocation(import_tokenxleft),
Position.fromLocation(semixright)));
new Name (multipart_name,
Range.fromLocations(iixleft, iixright)),
Range.fromLocations(import_tokenxleft, semixright));
parserResult.imports.add(imprt);
/* reset the accumulated multipart name */
......@@ -510,58 +509,41 @@ symbol_list ::= symbol_list symbol | symbol;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
symbol ::=
TERMINAL
TERMINAL : t
type_id : tid
declares_term : lst {:
//jrTODO: BUG? this is strange!!! -> there must be a better way
// to refer to these terminals.
ComplexSymbol terminal = (ComplexSymbol) stack.elementAt(stack.size()-2);
parserResult.symbols.add(new TypedTerminalDeclaration(
(String) tid,
(List<Terminal>) lst,
new Range(Position.fromComplexSymbolLeft(terminal),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
new Range(Position.fromLocation(txleft),
Position.fromLocation(txright))));
:}
|
TERMINAL
TERMINAL : t
declares_term : lst {:
//jrTODO: BUG? this is strange!!! -> there must be a better way
// to refer to these terminals.
ComplexSymbol terminal = (ComplexSymbol) stack.elementAt(stack.size()-2);
parserResult.symbols.add(new TerminalDeclaration(
(List<Terminal>) lst,
new Range(Position.fromComplexSymbolLeft(terminal),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
new Range(Position.fromLocation(txleft),
Position.fromLocation(txright))));
;
:}
|
non_terminal
non_terminal : nt
type_id : tid
declares_non_term : lst {:
//jrTODO: BUG? this is strange!!! -> there must be a better way
// to refer to these terminals.
ComplexSymbol nonTerminal = (ComplexSymbol) stack.elementAt(stack.size()-3);
parserResult.symbols.add(new TypedNonTerminalDeclaration(
(String) tid,
(List<NonTerminal>) lst,
new Range(Position.fromComplexSymbolLeft(nonTerminal),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
new Range(Position.fromLocation(ntxleft),
Position.fromLocation(ntxright))));
:}
|
non_terminal
non_terminal : nt
declares_non_term:lst {:
//jrTODO: BUG? this is strange!!! -> there must be a better way
// to refer to these terminals.
ComplexSymbol nonTerminal = (ComplexSymbol) stack.elementAt(stack.size()-2);
parserResult.symbols.add(new NonTerminalDeclaration(
(List<NonTerminal>) lst,
new Range(Position.fromComplexSymbolLeft(nonTerminal),
Position.fromComplexSymbolRight((ComplexSymbol) stack.peek()))));
new Range(Position.fromLocation(ntxleft),
Position.fromLocation(ntxright))));
:}
|
......@@ -809,7 +791,7 @@ rhs_list ::= rhs_list : lst BAR rhs : item {:
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
rhs ::=
prod_part_list PERCENT_PREC term_id:term_name
prod_part_list : ppl PERCENT_PREC term_id:term_name
{:
symbol sym = null;
if (lhs_nt != null)
......@@ -846,6 +828,16 @@ rhs ::=
p = new production(context, lhs_nt, rhs_parts, rhs_pos);
}
Range range = Range.fromLocations(term_namexleft, term_namexright);
RESULT = new ProductionRight(p.index(),
p.get_embedded_actions(),
ast_get_rhs_parts_copy(),
new Terminal(
new Name(term_name, range),
(Range) range.clone()
),
Range.fromLocations(pplxleft, term_namexright));
// p.index();
/* if we have no start non-terminal declared and this is
......@@ -875,9 +867,6 @@ rhs ::=
ast_new_rhs();
}
}
// jrTODO
// RESULT = "blub!!!";
/* reset the rhs accumulation in any case */
new_rhs();
......@@ -896,6 +885,7 @@ rhs ::=
RESULT = new ProductionRight(p.index(),
p.get_embedded_actions(),
ast_get_rhs_parts_copy(),
null,
Range.fromLocations(pplxleft, pplxright));
......@@ -931,13 +921,7 @@ rhs ::=
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
prod_part_list ::= prod_part_list {:
// TODO: This is not quite correct, but it seems that we can't get the correct offset
// of CODE_STRING below. So we get the offset of the previous token here.
//
prod_part_offset = ((ComplexSymbol) cur_token).xright.getOffset();
:} prod_part | empty;
prod_part_list ::= prod_part_list prod_part | empty;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
......@@ -958,8 +942,7 @@ prod_part ::=
add_rhs_part(add_lab(symb, labid));
ProductionSymbolRef ref = null;
Range range = new Range (Position.fromLocation(symidxleft),
Position.fromLocation(symidxright));
Range range = Range.fromLocations(symidxleft, symidxright);
if (labid == null)
ref = new ProductionSymbolRef(new Name(symid, range), (Range) range.clone());
else
......@@ -973,20 +956,9 @@ prod_part ::=
int debug_id = get_new_debug_id();
add_rhs_part(new action_part(attach_debug_symbol(debug_id, code_str)));
// TODO: assign inner range as well: code_strxleft, code_strxright
// TODO: this is a quick and dirty workaround.
// it means, however, that the columns and offsets don't properly match up!
int offsetEnd = prod_part_offset + code_str.length();
Position pos1 = new Position(
code_strxleft.getLine(), code_strxleft.getColumn(), prod_part_offset);
Position pos2 = new Position(
code_strxright.getLine(), code_strxright.getColumn(), offsetEnd);
ast_add_rhs_part(new ActionCodeBlock(debug_id, code_str,
new Range(pos1, pos2)
Range.fromLocations(code_strxleft, code_strxright)
));
:}
;
......@@ -1090,8 +1062,7 @@ new_term_id ::=
symbols.put(term_id,
new symbol_part(new terminal(context, term_id, multipart_name)));
Range range = new Range(Position.fromLocation(term_idxleft),
Position.fromLocation(term_idxright));
Range range = Range.fromLocations(term_idxleft, term_idxright);
RESULT = new Terminal(new Name(term_id, range), (Range) range.clone());
}
......@@ -1127,8 +1098,7 @@ new_non_term_id ::=
// jrTODO: positions!
// jrTODO: non_term_id vs. multipart_name
Range range = new Range(Position.fromLocation(non_term_idxleft),
Position.fromLocation(non_term_idxright));
Range range = Range.fromLocations(non_term_idxleft, non_term_idxright);
RESULT = new NonTerminal(new Name(non_term_id, range), (Range) range.clone());
}
:}
......
package de.in.tum.www2.cup;
import de.in.tum.www2.cup.internal.lalr_state;
public abstract class Conflict
{
private lalr_state state;
public lalr_state getState() {
return 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) {
System.out.println("DEBUG: ADDING CONFLICT TO " + this + "!"); // TODO: remove
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,9 +15,9 @@ 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");
FileInputStream input = new FileInputStream("/Users/jroith/testcup/test.cup");
CupParser parser = new CupParser(er, input);
ParserResult res = parser.parse();
......@@ -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);
......@@ -59,8 +58,7 @@ public class LALRResult {
// if we have more conflicts than we expected issue a message and die
if (context.num_conflicts > expect_conflicts)
{
System.out.println("*** More conflicts encountered than expected " +
"-- parser generation aborted");
System.err.println("*** More conflicts encountered than expected.");
return result;
}
......
package de.in.tum.www2.cup;
import java.util.List;
import de.in.tum.www2.cup.internal.lalr_item;
import de.in.tum.www2.cup.internal.lalr_state;
public class ReduceReduceConflict extends Conflict
{
private lalr_item conflict1;
private lalr_item conflict2;
public lalr_item getConflictItem1() {
return conflict1;
}
public lalr_item getConflictItem2() {
return conflict2;
}
public ReduceReduceConflict(lalr_state state, lalr_item conflict1, lalr_item conflict2) {
super(state);
this.conflict1 = conflict1;
this.conflict2 = conflict2;
}
// 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 java.util.List;
import de.in.tum.www2.cup.internal.lalr_item;
import de.in.tum.www2.cup.internal.lalr_state;
public class ShiftReduceConflict extends Conflict
{
private lalr_item conflict1;
private List<lalr_item> conflict2;
public lalr_item getConflictItem1() {
return conflict1;
}
public List<lalr_item> getConflictItems2() {
return conflict2;
}
public ShiftReduceConflict(lalr_state state, lalr_item conflict1, List<lalr_item> conflict2) {
super(state);
this.conflict1 = conflict1;
this.conflict2 = conflict2;
}
// 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
......@@ -7,9 +7,6 @@ public class LabeledProductionSymbolRef extends ProductionSymbolRef
{
public String label; // TODO: Name class?
// TODO: this should point to the declaration, after being resolved by a visitor.
public Production declaration;
public LabeledProductionSymbolRef(Name name, String label, Range range) {
super(name, range);
this.label = label;
......
......@@ -9,7 +9,7 @@ public class Production extends AbstractNode
implements IWithName, IHasDeclarationReference
{
private NonTerminal declarationRef;
private Name lhs;
private Name lhs; // TODO: This should really be a NonTerminal class
private List<ProductionRight> rhs;
@Override
......
......@@ -10,6 +10,7 @@ import de.in.tum.www2.cup.Range;
public class ProductionRight extends AbstractNode {
private List<IProductionRightPart> lst;
private Terminal precedenceLike;
private int index;
private List<Integer> extractedActionProductions;
......@@ -25,6 +26,10 @@ public class ProductionRight extends AbstractNode {
return index;
}
public boolean hasSpecifiedPrecedence() {
return precedenceLike != null;
}
public List<Integer> getExtractedActionProductions() {
return extractedActionProductions;
}
......@@ -39,17 +44,23 @@ public class ProductionRight extends AbstractNode {
public ProductionRight(int index,
List<Integer> extractedActionProductions,
List<IProductionRightPart> lst, Range range) {
List<IProductionRightPart> lst,
Terminal precedenceLike,
Range range) {
super(range);
this.precedenceLike = precedenceLike;
this.index = index;
this.extractedActionProductions = extractedActionProductions;
this.lst = lst;
if (lst != null) {
for (IProductionRightPart prp : lst)
for (IProductionRightPart prp : lst) {
if (prp != null) {
if (prp instanceof AbstractNode)
((AbstractNode) prp).setParent(this);
}
if (precedenceLike != null)
precedenceLike.setParent(this);
}
}
}
......@@ -83,6 +94,8 @@ public class ProductionRight extends AbstractNode {
for (IProductionRightPart ip : lst)
ip.accept(visitor, childArg);
}
if (precedenceLike != null)
precedenceLike.accept(visitor, childArg);
}
visitor.postVisit(this, data);
}
......@@ -109,6 +122,11 @@ public class ProductionRight extends AbstractNode {
builder.append(",");
builder.append("\n");
}
if (precedenceLike != null) {
builder.append("%prec ");
builder.append(precedenceLike);
builder.append("\n");
}
}
}
package de.in.tum.www2.cup.internal;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Enumeration;
import java.util.List;
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 +841,13 @@ public class lalr_state {
else
message+="the second production.\n";
// TODO: add symbols!
// TODO: add resolved in favour of.
Conflict conflict = new ReduceReduceConflict(this, itm1, itm2);
ConflictManager cm = context.getConflictManager();
cm.addConflict(conflict);
/* count the conflict */
context.num_conflicts++;
context.getErrorManager().Warning(ErrorSource.LALR, message);
......@@ -859,6 +872,8 @@ public class lalr_state {
String message = "*** Shift/Reduce conflict found in state #"+index()+"\n" +
" between " + red_itm.to_simple_string()+"\n";
List<lalr_item> conflictingItems = new ArrayList<lalr_item>();
/* find and report on all items that shift under our conflict symbol */
for (Enumeration itms = items().all(); itms.hasMoreElements(); )
{
......@@ -871,15 +886,21 @@ public class lalr_state {
shift_sym = itm.symbol_after_dot();
if (!shift_sym.is_non_term() && shift_sym.index() == conflict_sym)
{
/* yes, report on it */
conflictingItems.add(itm);
/* yes, report on it */
message += " and " + itm.to_simple_string()+"\n";
}
}
}
}
terminal.terminal_shared shared = terminal.getShared(context);
message += " under symbol "+ shared.find(conflict_sym).name() + "\n"+
" Resolved in favor of shifting.\n";
Conflict conflict = new ShiftReduceConflict(this, red_itm, conflictingItems);
ConflictManager cm = context.getConflictManager();
cm.addConflict(conflict);
/* count the conflict */
context.num_conflicts++;
context.getErrorManager().Warning(ErrorSource.LALR, message);
......
......@@ -38,7 +38,7 @@ import java.io.InputStreamReader;
private StringBuffer sb;
private ComplexSymbolFactory symbolFactory;
private int csline,cscolumn;
private int csline, cscolumn, csoffset;
public Symbol symbol(String name, int code){
return symbolFactory.newSymbol(name, code,
......@@ -98,7 +98,7 @@ ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*
">" { return symbol("GT",GT); }
"<" { return symbol("LT",LT); }
{Comment} { }
"{:" { sb = new StringBuffer(); csline=yyline+1; cscolumn=yycolumn+1; yybegin(CODESEG); }
"{:" { sb = new StringBuffer(); csline=yyline+1; cscolumn=yycolumn+1; csoffset=yychar; yybegin(CODESEG); }
"package" { return symbol("PACKAGE",PACKAGE); }
"import" { return symbol("IMPORT",IMPORT); }
"class" { return symbol("CLASS",CLASS); }
......@@ -123,7 +123,7 @@ ident = ([:jletter:] | "_" ) ([:jletterdigit:] | [:jletter:] | "_" )*
}
<CODESEG> {
":}" { yybegin(YYINITIAL); return symbolFactory.newSymbol("CODE_STRING",CODE_STRING, new Location(csline, cscolumn),new Location(yyline+1,yycolumn+1+yylength()), sb.toString()); }
":}" { yybegin(YYINITIAL); return symbolFactory.newSymbol("CODE_STRING",CODE_STRING, new Location(csline, cscolumn, csoffset),new Location(yyline+1,yycolumn+1+yylength(), csoffset+yylength()), sb.toString()); }
.|\n { sb.append(yytext()); }
}
......
......@@ -23,7 +23,8 @@ Require-Bundle: org.eclipse.ui,
org.eclipse.jdt.core;bundle-version="3.10.0",
org.eclipse.jdt.ui;bundle-version="3.10.1",
org.eclipse.ant.core;bundle-version="3.3.0",
org.eclipse.text
org.eclipse.text,
org.eclipse.ui.forms;bundle-version="3.6.100"
Bundle-RequiredExecutionEnvironment: JavaSE-1.8
Bundle-ActivationPolicy: lazy
Bundle-ClassPath: .
......@@ -2,6 +2,7 @@ package de.tum.in.www2.cupplugin;
import org.eclipse.core.resources.IResourceChangeEvent;
import org.eclipse.core.resources.IResourceChangeListener;
import org.eclipse.core.resources.IResourceDelta;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.plugin.AbstractUIPlugin;
......@@ -18,13 +19,9 @@ public class Activator extends AbstractUIPlugin {
public class DebugListener implements IResourceChangeListener {
public void resourceChanged(IResourceChangeEvent event) {
System.out.println("GOT EVENT: " + event.getType());
Debugger.getBreakpointMapper().remap();
if (event.getType() != IResourceChangeEvent.POST_BUILD)
return;
System.out.println("GOT POST BUILD EVENT!");
Debugger.getBreakpointMapper().remap();
}
}
......@@ -39,7 +36,8 @@ public class Activator extends AbstractUIPlugin {
*/
public Activator() {
IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.addResourceChangeListener(new DebugListener());
workspace.addResourceChangeListener(new DebugListener(),
IResourceChangeEvent.POST_BUILD);
}
/*
......
package de.tum.in.www2.cupplugin;
import org.eclipse.swt.graphics.RGB;
public class Colors {
public static final RGB white = new RGB (255, 255, 255);
public static final RGB darkGray = new RGB (20, 20, 20);
// public static final RGB darkGray = new RGB (30, 30, 30);
public static final RGB gray = new RGB (150, 150, 150);
public static final RGB lightGray = new RGB (240, 240, 240);
public static final RGB blue = new RGB (53, 87, 197);
public static final RGB brightBlue = new RGB (38, 99, 236);
public static final RGB green = new RGB (44, 173, 57);
public static final RGB orange = new RGB (220, 150, 50);
public static final RGB purple = new RGB (177, 33, 114);
public static final RGB blueGreen = new RGB (43, 126, 136);
public static final RGB red = new RGB (255, 0, 0);
public static final RGB pink = new RGB (255, 0, 255);
public static final RGB brown = new RGB (132, 90, 49);
/*
public static final RGB stateGreen = new RGB (155, 218, 158);
public static final RGB stateBlue = new RGB (155, 208, 128);
public static final RGB stateRed = new RGB (218, 155, 173);
public static final RGB stateYellow = new RGB (218, 214, 155);
*/
//public static final RGB stateGreen = new RGB (196, 232, 197);
public static final RGB stateGreen = new RGB (146, 203, 135);