Commit e983a3c5 authored by Johannes Roith's avatar Johannes Roith

More location patch fixes.

parent 59d0e015
......@@ -140,6 +140,9 @@ 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();
......@@ -413,7 +416,7 @@ code_parts ::=
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
action_code_part ::=
ACTION CODE CODE_STRING : user_code opt_semi
ACTION : act CODE CODE_STRING : user_code opt_semi : smi
{:
if (context.action_code!=null)
errMan.Warning(ErrorSource.Parser,
......@@ -423,12 +426,13 @@ action_code_part ::=
context.action_code = user_code;
int debug_id = get_new_debug_id();
// TODO: attach inner range! based on user_codexleft, user_codexright
parserResult.actionCode = new SpecialCodeBlock(
debug_id,
SpecialCodeBlock.BlockType.Action,
user_code,
new Range(Position.fromLocation(user_codexleft),
Position.fromLocation(user_codexright)));
new Range(Position.fromLocation(actxleft),
Position.fromLocation(smixright)));
}
:}
;
......@@ -436,7 +440,7 @@ action_code_part ::=
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
parser_code_part ::=
PARSER CODE CODE_STRING:user_code opt_semi
PARSER : prs CODE CODE_STRING:user_code opt_semi : smi
{:
if (context.parser_code!=null)
errMan.Warning(ErrorSource.Parser,
......@@ -449,8 +453,8 @@ parser_code_part ::=
debug_id,
SpecialCodeBlock.BlockType.Parser,
user_code,
new Range(Position.fromLocation(user_codexleft),
Position.fromLocation(user_codexright)));
new Range(Position.fromLocation(prsxleft),
Position.fromLocation(smixright)));
}
:}
;
......@@ -458,7 +462,7 @@ parser_code_part ::=
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
init_code ::=
INIT WITH CODE_STRING:user_code opt_semi
INIT : init WITH CODE_STRING:user_code opt_semi : smi
{:
if (context.init_code!=null)
errMan.Warning(ErrorSource.Parser, "Redundant init code (skipping)",
......@@ -470,8 +474,8 @@ init_code ::=
debug_id,
SpecialCodeBlock.BlockType.Init,
user_code,
new Range(Position.fromLocation(user_codexleft),
Position.fromLocation(user_codexright)));
new Range(Position.fromLocation(initxleft),
Position.fromLocation(smixright)));
}
:}
;
......@@ -479,7 +483,7 @@ init_code ::=
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
scan_code ::=
SCAN WITH CODE_STRING:user_code opt_semi
SCAN : scan WITH CODE_STRING:user_code opt_semi : smi
{:
if (context.scan_code!=null)
errMan.Warning(ErrorSource.Parser, "Redundant scan code (skipping)",
......@@ -492,8 +496,8 @@ scan_code ::=
debug_id,
SpecialCodeBlock.BlockType.Scan,
user_code,
new Range(Position.fromLocation(user_codexleft),
Position.fromLocation(user_codexright)));
new Range(Position.fromLocation(scanxleft),
Position.fromLocation(smixright)));
}
:}
......@@ -927,7 +931,13 @@ rhs ::=
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
prod_part_list ::= prod_part_list prod_part | empty;
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;
/*. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . */
......@@ -957,16 +967,27 @@ prod_part ::=
ast_add_rhs_part(ref);
}
:}
|
CODE_STRING : code_str
| CODE_STRING : code_str
{:
/* add a new production 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,
Range.fromLocations(code_strxleft, code_strxright)
)); // jrTODO: positions
new Range(pos1, pos2)
));
:}
;
......
......@@ -13,9 +13,12 @@ public class Position {
public int getOffsetFromStart() { return offsetFromStart; }
public void move(int diffLines, int diffColumns, int diffOffset) {
this.line += diffLines;
this.column += diffColumns;
this.offsetFromStart += diffOffset;
if (this.line >= 0)
this.line += diffLines;
if (this.column >= 0)
this.column += diffColumns;
if (this.offsetFromStart >= 0)
this.offsetFromStart += diffOffset;
}
public Position(int line, int column, int offsetFromStart) {
......
......@@ -6,22 +6,27 @@ import de.in.tum.www2.cup.ast.*;
public class LocationPatchVisitor extends WildcardVisitor<Object>
{
private int fixAfterOffset;
private int fixColumnUntilOffset;
private int diffLines;
private int diffColumns;
private int diffOffset;
public LocationPatchVisitor(int fixAfterOffset,
public LocationPatchVisitor(int fixAfterOffset, int fixColumnUntilOffset,
int diffLines, int diffColumns, int diffOffset) {
this.fixAfterOffset = fixAfterOffset;
this.fixColumnUntilOffset = fixColumnUntilOffset;
this.diffLines = diffLines;
this.diffColumns = diffColumns;
this.diffOffset = diffOffset;
}
private void fixPosition(String comment, AbstractNode node, Position pos) {
if (pos != null && pos.getOffsetFromStart() > fixAfterOffset) {
System.out.print("fixing " + comment + "..." + node.getClass().getName() + " from " + pos);
pos.move(diffLines, diffColumns, diffOffset);
if (pos != null && pos.getOffsetFromStart() >= fixAfterOffset) {
int cols = diffColumns;
if (fixColumnUntilOffset != -1 && pos.getOffsetFromStart() >= fixColumnUntilOffset)
cols = 0;
System.out.print("fixing (diffColumns: " + diffColumns + ", cols: " + cols + ") " + comment + "..." + node.getClass().getName() + " from " + pos);
pos.move(diffLines, cols, diffOffset);
System.out.println(" to " + pos);
}
}
......@@ -33,7 +38,7 @@ public class LocationPatchVisitor extends WildcardVisitor<Object>
@Override
public void postVisitWildcard(AbstractNode node, Object data) {
if (node != null) {
if (node != null) {
fixPosition("begin", node, node.getBegin());
fixPosition("end", node, node.getEnd());
}
......
......@@ -68,44 +68,56 @@ public class RefResolutionVisitor extends Visitor<Object>
@Override public Object preVisit (NonTerminalDeclaration node, Object data) {
if (node == null)
return data;
for(NonTerminal nt : node.getNonTerminals()) {
String name = nt.getNameStringOrNull();
declarations.put(name, nt);
resolveDefinitionFrom(name, nt);
}
if (node != null)
for(NonTerminal nt : node.getNonTerminals()) {
if (nt != null) {
String name = nt.getNameStringOrNull();
declarations.put(name, nt);
resolveDefinitionFrom(name, nt);
}
}
return data;
}
@Override public Object preVisit (TerminalDeclaration node, Object data) {
if (node == null)
return data;
for(Terminal t : node.getTerminals())
declarations.put(t.getNameStringOrNull(), t);
if (node != null)
for(Terminal t : node.getTerminals())
if (t != null)
declarations.put(t.getNameStringOrNull(), t);
return data;
}
@Override public Object preVisit (TypedNonTerminalDeclaration node, Object data) {
if (node == null)
return data;
for(NonTerminal nt : node.getNonTerminals()) {
String name = nt.getNameStringOrNull();
declarations.put(name, nt);
resolveDefinitionFrom(name, nt);
}
if (node.getNonTerminals() != null)
for(NonTerminal nt : node.getNonTerminals()) {
if (nt != null) {
String name = nt.getNameStringOrNull();
declarations.put(name, nt);
resolveDefinitionFrom(name, nt);
}
}
return data;
}
@Override public Object preVisit (TypedTerminalDeclaration node, Object data) {
for(Terminal t : node.getTerminals())
declarations.put(t.getNameStringOrNull(), t);
if (node.getTerminals() != null)
for(Terminal t : node.getTerminals())
if (t != null)
declarations.put(t.getNameStringOrNull(), t);
return data;
}
@Override public Object preVisit (Precedence node, Object data) {
if (node == null)
return data;
for(Terminal t : node.getTerminals())
resolveDeclarationFrom(t.getNameStringOrNull(), t);
if (node.getTerminals() != null)
for(Terminal t : node.getTerminals())
if (t != null)
resolveDeclarationFrom(t.getNameStringOrNull(), t);
return data;
}
......
......@@ -36,6 +36,8 @@ class CTEDocumentListener implements IDocumentListener {
}
private void patchModel(DocumentEvent event) {
// long timerBefore = System.currentTimeMillis(); // TODO: remove!
IDocument document = event.getDocument();
Model model = Model.getInstanceForDocument(document);
......@@ -47,51 +49,64 @@ class CTEDocumentListener implements IDocumentListener {
int offset = event.getOffset();
int offsetOfLineTwo = -1;
int linesAdded = 0;
int addedLastLineColumns = 0;
try {
int startLineNumber = document.getLineOfOffset(offset);
try {
offsetOfLineTwo = document.getLineOffset(startLineNumber+1);
} catch (BadLocationException e) {
e.printStackTrace();
}
int endOffset = offset + event.getText().length();
int endLineNumber = document.getLineOfOffset(endOffset);
addedLastLineColumns = endOffset
- document.getLineOffset(endLineNumber);
linesAdded = endLineNumber - startLineNumber;
System.out.println("PROCESSING DocumentEvent: ");
/* System.out.println("PROCESSING DocumentEvent: ");
System.out.println(" Offset: " + offset);
System.out.println(" Replaced: " + event.getLength());
System.out.println(" New: " + event.getText().length());
System.out.println(" linesRemoved: " + linesRemoved);
System.out.println(" linesAdded: " + linesAdded);
*/
} catch (BadLocationException e) {
e.printStackTrace();
}
// NOTE: we do not try to fix the replaced part.
int fixColumnUntilOffset = offsetOfLineTwo;
int fixAfterOffset = offset + event.getLength();
int diffOffset = event.getText().length() - event.getLength();
int diffLines = linesAdded - linesRemoved;
int diffColumns = addedLastLineColumns - removedLastLineColumns;
System.out.println(" fixAfterOffset: " + fixAfterOffset);
/* System.out.println(" fixAfterOffset: " + fixAfterOffset);
System.out.println(" diffOffset: " + diffOffset);
System.out.println(" diffLines: " + diffLines);
System.out.println(" diffColumns: " + diffColumns);
*/
System.out.println(" fixAfterOffset: " + fixAfterOffset);
System.out.println(" offsetOfLineTwo: " + offsetOfLineTwo);
LocationPatchVisitor visitor = new LocationPatchVisitor(fixAfterOffset,
diffLines, diffColumns, diffOffset);
LocationPatchVisitor visitor = new LocationPatchVisitor(fixAfterOffset,
fixColumnUntilOffset, diffLines, diffColumns, diffOffset);
ast.accept(visitor, null);
// long timerAfter = System.currentTimeMillis(); // TODO: remove!
// System.out.println("Location patch took: " + (timerAfter - timerBefore) + " millis.");
}
public void documentChanged(DocumentEvent event) {
patchModel(event);
// System.out.println("starting job chain"); // Log message TODO:
// DELETE!
Controller controller = Controller.getInstance(event.getDocument());
controller.notifyChange(event);
}
......
......@@ -119,15 +119,11 @@ public class CupTextEditor extends TextEditor {
@Override
public void doSave(IProgressMonitor monitor) {
super.doSave(monitor);
System.out.println(">>>>>>>>>>>>> CupTextEditor: saved!");
if (getModel().getAstModelRevisionNumber() != RevisionManager.get(getDocument())) {
System.out.println("REVISION NUMBERS DO NOT MATCH!");
// TODO: run the job manually?
Controller controller = Controller.getInstance(this);
controller.requestJobRun();
}
// TODO: maybe synchronize the debug breakpoints here.
PluginUtility.fullSyntaxRefresh(getSourceViewer());
}
......
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