📄 aclsltokenmarker.java
字号:
/******************************************************************
* JADE - Java Agent DEvelopment Framework is a framework to develop
* multi-agent systems in compliance with the FIPA specifications.
* Copyright (C) 2002 TILAB S.p.A.
*
* This file is donated by Acklin B.V. to the JADE project.
*
*
* GNU Lesser General Public License
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation,
* version 2.1 of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
* ***************************************************************/
package jade.tools.gui;
import javax.swing.text.Segment;
import jade.domain.FIPAAgentManagement.FIPAManagementOntology;
/**
* ACL/SL token marker. The original file is written by Slava Pestov
* (www.gjt.org) and altered to fit ACL/SL.
*
* @author Chris van Aart - Acklin B.V., the Netherlands & Slava Pestov
* @created June 8, 2002
*/
public class ACLSLTokenMarker {
public ACLSLTokenMarker() {
this(getKeywords());
lastLine = -1;
}
public ACLSLTokenMarker(KeywordMap keywords) {
lastLine = -1;
this.keywords = keywords;
}
private static KeywordMap getKeywords() {
if (aclSLKeywords == null) {
aclSLKeywords = new KeywordMap(false);
/* FIXME Need new method to get a vocabulary from an ontology
jade.util.leap.List agentMgt = FIPAAgentManagementOntology.instance().getVocabulary();
for (int i = 0; i < agentMgt.size(); i++) {
String roleName = (String)agentMgt.get(i);
aclSLKeywords.add(roleName, ACLToken.KEYWORD2);
try {
SlotDescriptor[] slots = FIPAAgentManagementOntology.instance().getSlots(roleName);
for (int j = 0; j < slots.length; j++) {
aclSLKeywords.add(slots[j].getName(), ACLToken.LABEL);
}
}
catch (Exception ex) {
}
} */
aclSLKeywords.add("inv", ACLToken.INVALID);
aclSLKeywords.add("ams", ACLToken.LABEL);
aclSLKeywords.add("df", ACLToken.LABEL);
aclSLKeywords.add("rma", ACLToken.LABEL);
aclSLKeywords.add("l1", ACLToken.LITERAL1);
aclSLKeywords.add("l2", ACLToken.LITERAL2);
/* FIXME Need new method to get a vocabulary from an ontology
jade.util.leap.List li = BasicOntology.instance().getVocabulary();
for (int i = 0; i < li.size(); i++) {
aclSLKeywords.add((String)li.get(i), ACLToken.OPERATOR);
} */
aclSLKeywords.add("wantarray", ACLToken.KEYWORD3);
aclSLKeywords.add("warn", ACLToken.KEYWORD3);
aclSLKeywords.add("write", ACLToken.KEYWORD3);
aclSLKeywords.add("m", S_ONE);
aclSLKeywords.add("q", S_ONE);
aclSLKeywords.add("qq", S_ONE);
aclSLKeywords.add("qw", S_ONE);
aclSLKeywords.add("qx", S_ONE);
aclSLKeywords.add("s", S_TWO);
aclSLKeywords.add("tr", S_TWO);
aclSLKeywords.add("y", S_TWO);
}
return aclSLKeywords;
}
/**
* Returns true if the next line should be repainted. This will return true
* after a line has been tokenized that starts a multiline token that
* continues onto the next line.
*
* @return The NextLineRequested value
*/
public boolean isNextLineRequested() {
return nextLineRequested;
}
public ACLToken markTokens(Segment line, int lineIndex) {
if (lineIndex >= length) {
throw new IllegalArgumentException("Tokenizing invalid line: "
+ lineIndex);
}
lastToken = null;
LineInfo info = lineInfo[lineIndex];
LineInfo prev;
if (lineIndex == 0) {
prev = null;
}
else {
prev = lineInfo[lineIndex - 1];
}
byte oldToken = info.token;
byte token = markTokensImpl(prev == null ? ACLToken.NULL : prev.token, line, lineIndex);
info.token = token;
/*
This is a foul hack. It stops nextLineRequested
from being cleared if the same line is marked twice.
Why is this necessary? It's all JEditTextArea's fault.
When something is inserted into the text, firing a
document event, the insertUpdate() method shifts the
caret (if necessary) by the amount inserted.
All caret movement is handled by the select() method,
which eventually pipes the new position to scrollTo()
and calls repaint().
Note that at this point in time, the new line hasn't
yet been painted; the caret is moved first.
scrollTo() calls offsetToX(), which tokenizes the line
unless it is being called on the last line painted
(in which case it uses the text area's painter cached
token list). What scrollTo() does next is irrelevant.
After scrollTo() has done it's job, repaint() is
called, and eventually we end up in paintLine(), whose
job is to paint the changed line. It, too, calls
markTokens().
The problem was that if the line started a multiline
token, the first markTokens() (done in offsetToX())
would set nextLineRequested (because the line end
token had changed) but the second would clear it
(because the line was the same that time) and therefore
paintLine() would never know that it needed to repaint
subsequent lines.
This bug took me ages to track down, that's why I wrote
all the relevant info down so that others wouldn't
duplicate it.
*/
if (!(lastLine == lineIndex && nextLineRequested)) {
nextLineRequested = (oldToken != token);
}
lastLine = lineIndex;
addToken(0, ACLToken.END);
return firstToken;
}
/**
* Returns if the token marker supports tokens that span multiple lines. If
* this is true, the object using this token marker is required to pass all
* lines in the document to the <code>markTokens()</code> method (in turn).
* <p>
*
* The default implementation returns true; it should be overridden to
* return false on simpler token markers for increased speed.
*
* @return Description of the Returned Value
*/
public boolean supportsMultilineTokens() {
return true;
}
/**
* Informs the token marker that lines have been inserted into the
* document. This inserts a gap in the <code>lineInfo</code> array.
*
* @param index The first line number
* @param lines The number of lines
*/
public void insertLines(int index, int lines) {
if (lines <= 0) {
return;
}
length += lines;
ensureCapacity(length);
int len = index + lines;
System.arraycopy(lineInfo, index, lineInfo, len,
lineInfo.length - len);
for (int i = index + lines - 1; i >= index; i--) {
lineInfo[i] = new LineInfo();
}
}
/**
* Informs the token marker that line have been deleted from the document.
* This removes the lines in question from the <code>lineInfo</code> array.
*
* @param index The first line number
* @param lines The number of lines
*/
public void deleteLines(int index, int lines) {
if (lines <= 0) {
return;
}
int len = index + lines;
length -= lines;
System.arraycopy(lineInfo, len, lineInfo,
index, lineInfo.length - len);
}
public byte markTokensImpl(byte _token, Segment line, int lineIndex) {
char[] array = line.array;
int offset = line.offset;
token = _token;
lastOffset = offset;
lastKeyword = offset;
matchChar = '\0';
matchCharBracket = false;
matchSpacesAllowed = false;
int length = line.count + offset;
if (token == ACLToken.LITERAL1 && lineIndex != 0
&& lineInfo[lineIndex - 1].obj != null) {
String str = (String)lineInfo[lineIndex - 1].obj;
if (str != null && str.length() == line.count
&& ACLSyntaxUtilities.regionMatches(false, line,
offset, str)) {
addToken(line.count, token);
return ACLToken.NULL;
}
else {
addToken(line.count, token);
lineInfo[lineIndex].obj = str;
return token;
}
}
boolean backslash = false;
loop :
for (int i = offset; i < length; i++) {
int i1 = (i + 1);
char c = array[i];
if (c == '\\') {
backslash = !backslash;
continue;
}
switch (token) {
case ACLToken.NULL:
switch (c) {
case '#':
if (backslash) {
backslash = false;
}
else {
if (doKeyword(line, i, c)) {
break;
}
addToken(i - lastOffset, token);
addToken(length - i, ACLToken.COMMENT1);
lastOffset = lastKeyword = length;
break loop;
}
break;
case '.':
case '@':
backslash = false;
if (length - i > 1) {
if (doKeyword(line, i, c)) {
break;
}
else {
addToken(i - lastOffset, token);
lastOffset = lastKeyword = i;
token = ACLToken.KEYWORD2;
}
}
break;
/*
case '.':
if (backslash)
backslash = false;
else
{
if (doKeyword(line, i, c))
break;
addToken(i - lastOffset, token);
addToken(length - i, Token.OPERATOR);
lastOffset = lastKeyword = length;
break loop;
}
break;
*/
/*
/ case '.':
/ case ':':
/ case '/':
if (backslash) {
backslash = false;
}
else {
if (doKeyword(line, i, c)) {
break;
}
token = ACLToken.KEYWORD2;
addToken(i - lastOffset, token);
addToken(length - i, ACLToken.KEYWORD2);
lastOffset = lastKeyword = length;
/ break loop;
token = ACLToken.NULL;
}
break;
*/
/*
case '@':
if (backslash)
{
backslash = false;
}
else
{
if (doKeyword(line, i, c))
{
break;
}
addToken(length - i, Token.LITERAL1);
lastOffset = lastKeyword = i;
break loop;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -