package tool.editors.text.sql;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jface.text.TextAttribute;
import org.eclipse.jface.text.rules.EndOfLineRule;
import org.eclipse.jface.text.rules.IRule;
import org.eclipse.jface.text.rules.IToken;
import org.eclipse.jface.text.rules.MultiLineRule;
import org.eclipse.jface.text.rules.RuleBasedScanner;
import org.eclipse.jface.text.rules.SingleLineRule;
import org.eclipse.jface.text.rules.Token;
import org.eclipse.jface.text.rules.WhitespaceRule;
import org.eclipse.jface.text.rules.WordRule;
import org.eclipse.swt.SWT;
/**
* The SQLCodeScanner is a RuleBaseScanner.This class finds SQL comments and
* keywords, as the user edits the document. It is "programmed" with a sequence
* of rules that evaluates and returns the offset and the length of the last
* found token.
*/
public class SQLScanner extends RuleBasedScanner
implements
ISQLSyntax {
/**
* Constructor for SQLCodeScanner. The SQLCodeScanner, is a RuleBaseScanner.
* The code scanner creates tokens for keywords, types, and constants. The
* token is constructed with a TextAttribute. The TextAttribute is constructed
* with a color and font. A list of rules with the corresponding token are
* created. The method ends with setting the scanner's set of rules
*/
public SQLScanner() {
SQLColorProvider provider = new SQLColorProvider();
IToken keyword = new Token(new TextAttribute(provider
.getColor(SQLColorProvider.KEYWORD), provider
.getColor(SQLColorProvider.BACKGROUND), SWT.BOLD | SWT.ITALIC));
IToken type = new Token(new TextAttribute(provider
.getColor(SQLColorProvider.TYPE), provider
.getColor(SQLColorProvider.BACKGROUND), SWT.BOLD));
IToken string = new Token(new TextAttribute(provider
.getColor(SQLColorProvider.STRING)));
IToken comment = new Token(new TextAttribute(provider
.getColor(SQLColorProvider.SINGLE_LINE_COMMENT)));
// IToken other = new Token(new TextAttribute(provider
// .getColor(SQLColorProvider.DEFAULT)));
IToken other = new Token(new TextAttribute(provider
.getColor(SQLColorProvider.DEFAULT), provider
.getColor(SQLColorProvider.BACKGROUND), SWT.ITALIC));
setDefaultReturnToken(other);
List<IRule> rules = new ArrayList<IRule>();
// Add rule for single line comments.
rules.add(new EndOfLineRule("//", comment));
rules.add(new EndOfLineRule("--", comment));
// Add rules for multi-line comments
rules.add(new MultiLineRule("/*", "*/", comment, (char) 0,
true));
// Add rule for strings and character constants.
rules.add(new SingleLineRule("\"", "\"", string, '\\'));
rules.add(new SingleLineRule("'", "'", string, '\\'));
// Add generic whitespace rule.
rules.add(new WhitespaceRule(new SQLWhiteSpaceDetector()));
// Add word rule for keywords, types, and constants.
WordRule wordRule = new WordRule(new SQLWordDetector(),
other);
for (int i = 0; i < reservedwords.length; i++)
wordRule.addWord(reservedwords[i], keyword);
for (int i = 0; i < types.length; i++)
wordRule.addWord(types[i], type);
for (int i = 0; i < constants.length; i++)
wordRule.addWord(constants[i], type);
rules.add(wordRule);
IRule[] result = new IRule[rules.size()];
rules.toArray(result);
setRules(result);
}
}