• Post Reply Bookmark Topic Watch Topic
  • New Topic

Scanner versus PartitionScanner  RSS feed

 
Barry Burd
Author
Ranch Hand
Posts: 133
10
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create an editor using SWT/JFace for a tiny language called URM. The URM language consists of only three statement types:

(1) while r != 0
...
end

(2) r++

(3) r--

There's nothing else in the language. I want to color each statement type with a different color, observing nesting as necessary. (So an r++ statement inside a while statement should be in the r++type color, not the while-type color.) I've created partition scanner and scanner, but I must not be doing it correctly because all I see is black text. Can anyone help?

public class URMPartitionScanner extends RuleBasedPartitionScanner {

public final static String DEFAULT_PART = "default_part";
public final static String WHILE_PART = "while_part";

public URMPartitionScanner() {


IPredicateRule[] rules = new IPredicateRule[1];
rules[0] = new MultiLineRule("while", "end", new Token(WHILE_PART)); setPredicateRules(rules);
}
}



public class InsideWhileScanner extends RuleBasedScanner {

public InsideWhileScanner(ColorManager colorManager) {

IToken defaultColorToken = new Token(new TextAttribute(colorManager
.getColor(colorManager.DEFAULT)));
IToken incrementColorToken = new Token(new TextAttribute(colorManager
.getColor(colorManager.INCREMENT)));
IToken decrementColorToken = new Token(new TextAttribute(colorManager
.getColor(colorManager.DECREMENT)));

IRule[] rules = new IRule[3];
rules[0] = new SingleLineRule("r", "++", incrementColorToken);
rules[1] = new SingleLineRule("r", "--", decrementColorToken);

rules[2] = new WhitespaceRule(new IWhitespaceDetector() {
public boolean isWhitespace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
});
setRules(rules);
}
}


public class DefaultScanner extends RuleBasedScanner {
ColorManager colorManager;
public DefaultScanner(ColorManager colorManager) {
// BARRY - are the insidewhilescanner and the default scanner exactly the same?
IToken defaultColorToken = new Token(new TextAttribute(colorManager
.getColor(colorManager.DEFAULT)));
IToken incrementColorToken = new Token(new TextAttribute(colorManager
.getColor(colorManager.INCREMENT)));
IToken decrementColorToken = new Token(new TextAttribute(colorManager
.getColor(colorManager.DECREMENT)));

IRule[] rules = new IRule[3];
rules[0] = new SingleLineRule("r", "++", incrementColorToken);
rules[1] = new SingleLineRule("r", "--", decrementColorToken);

rules[2] = new WhitespaceRule(new IWhitespaceDetector() {
public boolean isWhitespace(char c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
}
});
setRules(rules);
}
}


public class URMConfiguration extends SourceViewerConfiguration {
PresentationReconciler reconciler;
ColorManager colorManager;
DefaultDamagerRepairer repairer;

public URMConfiguration(ColorManager colorManager) {
this.colorManager = colorManager;
}
public IPresentationReconciler getPresentationReconciler(
ISourceViewer sourceViewer) {
PresentationReconciler reconciler = new PresentationReconciler();
RuleBasedScanner scanner = new RuleBasedScanner();

InsideWhileScanner whileScanner = new InsideWhileScanner(colorManager);
whileScanner.setDefaultReturnToken(new Token(colorManager.getColor(colorManager.DEFAULT)));
repairer = new DefaultDamagerRepairer(scanner);
reconciler.setDamager(repairer, URMPartitionScanner.WHILE_PART);
reconciler.setRepairer(repairer, URMPartitionScanner.WHILE_PART);
DefaultScanner defaultScanner = new DefaultScanner(colorManager);
whileScanner.setDefaultReturnToken(new Token(colorManager.getColor(colorManager.DEFAULT)));
repairer = new DefaultDamagerRepairer(scanner);
reconciler.setDamager(repairer, IDocument.DEFAULT_CONTENT_TYPE);
reconciler.setRepairer(repairer, IDocument.DEFAULT_CONTENT_TYPE);


return reconciler;
}
}
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!