Add BYE keyword, indent/outdent FOR/NEXT loops (still need to refactor to eliminate duplicate spaces between two tokens)

This commit is contained in:
2009-01-02 20:45:40 +00:00
parent cfd8c21c26
commit 51f442d291
4 changed files with 60 additions and 22 deletions

View File

@ -31,6 +31,7 @@ import com.webcodepro.applecommander.util.BusinessBASICTokenizer;
* Filter the given file as an Apple /// Business BASIC file.
* <p>
* Date created: Dec 15, 2008 11:12:10 PM
*
* @author David Schmidt
*/
public class BusinessBASICFileFilter implements FileFilter {
@ -42,7 +43,9 @@ public class BusinessBASICFileFilter implements FileFilter {
}
/**
* Process the given FileEntry and return a text image of the Business BASIC file.
* Process the given FileEntry and return a text image of the Business BASIC
* file.
*
* @see com.webcodepro.applecommander.storage.FileFilter#filter(FileEntry)
*/
public byte[] filter(FileEntry fileEntry) {
@ -50,6 +53,7 @@ public class BusinessBASICFileFilter implements FileFilter {
PrintWriter printWriter = new PrintWriter(byteArray, true);
BusinessBASICTokenizer tokenizer = new BusinessBASICTokenizer(fileEntry);
boolean firstLine = true;
int nest = 0;
while (tokenizer.hasMoreTokens()) {
BusinessBASICToken token = tokenizer.getNextToken();
if (token == null) {
@ -61,11 +65,21 @@ public class BusinessBASICFileFilter implements FileFilter {
printWriter.println();
}
printWriter.print(token.getLineNumber());
printWriter.print(" "); //$NON-NLS-1$
if (nest > 0) {
for (int i = 1; i < nest; i++)
printWriter.print(" "); //$NON-NLS-1$
}
/*
if (token.isIndenter())
nest ++;
else if (token.isOutdenter())
nest --;
*/
//printWriter.print(" "); //$NON-NLS-1$
} else if (token.isToken()) {
printWriter.print(token.getTokenString());
} else {
printWriter.print(token.getStringValue());
printWriter.print(" "+token.getStringValue());
}
}
printWriter.close();

View File

@ -29,7 +29,6 @@ import com.webcodepro.applecommander.ui.swt.FileViewerWindow;
import com.webcodepro.applecommander.ui.swt.util.contentadapter.StyledTextAdapter;
import com.webcodepro.applecommander.util.BusinessBASICToken;
import com.webcodepro.applecommander.util.BusinessBASICTokenizer;
import com.webcodepro.applecommander.util.BusinessBASICTokenizer;
/**
* Provides a view of a syntax-colored Apple /// Business BASIC program listing.
@ -71,6 +70,8 @@ public class BusinessBASICFilterAdapter extends FilterAdapter {
BusinessBASICTokenizer tokenizer = new BusinessBASICTokenizer(getFileEntry());
boolean firstLine = true;
boolean firstData = true;
int nestLevels = 0;
while (tokenizer.hasMoreTokens()) {
BusinessBASICToken token = tokenizer.getNextToken();
if (token == null) {
@ -81,13 +82,22 @@ public class BusinessBASICFilterAdapter extends FilterAdapter {
} else {
styledText.append("\n"); //$NON-NLS-1$
}
firstData = true;
styledText.append(Integer.toString(token.getLineNumber()));
styledText.append(" "); //$NON-NLS-1$
styledText.append(" "); //$NON-NLS-1$
if (nestLevels > 0) {
for (int i = 0; i < nestLevels; i++)
styledText.append(" "); //$NON-NLS-1$
}
} else if (token.isCommandSeparator() || token.isExpressionSeparator()) {
styledText.append(token.getStringValue());
firstData = false;
} else if (token.isEndOfCommand()) {
styledText.append("\n"); //$NON-NLS-1$
firstData = false;
} else if (token.isString()) {
if (firstData)
styledText.append(" "); //$NON-NLS-1$
int caretOffset = styledText.getCharCount();
styledText.append(token.getStringValue());
StyleRange styleRange = new StyleRange();
@ -95,6 +105,7 @@ public class BusinessBASICFilterAdapter extends FilterAdapter {
styleRange.length = token.getStringValue().length();
styleRange.foreground = getGreenColor();
styledText.setStyleRange(styleRange);
firstData = false;
} else if (token.isToken()) {
int caretOffset = styledText.getCharCount();
styledText.append(token.getTokenString());
@ -104,7 +115,12 @@ public class BusinessBASICFilterAdapter extends FilterAdapter {
//styleRange.fontStyle = SWT.BOLD;
styleRange.foreground = getBlueColor();
styledText.setStyleRange(styleRange);
firstData = false;
if (token.isIndenter()) {
nestLevels ++; }
else if (token.isOutdenter()) {
nestLevels --; }
}
}
}
}
}

View File

@ -10,46 +10,54 @@ public class BusinessBASICToken {
private byte tokenValue;
private String tokenString;
private String stringValue;
public BusinessBASICToken(int lineNumber) {
this.lineNumber = lineNumber;
}
public BusinessBASICToken(byte tokenValue, String tokenString) {
this.tokenValue = tokenValue;
this.tokenString = tokenString;
}
public BusinessBASICToken(String stringValue) {
this.stringValue = stringValue;
}
public boolean isCommandSeparator() {
return ":".equals(stringValue); //$NON-NLS-1$
}
public boolean isLineNumber() {
return !isToken() && !isString();
}
public boolean isEndOfCommand() {
return isLineNumber() || isCommandSeparator();
}
public boolean isToken() {
return tokenString != null;
}
public boolean isString() {
return stringValue != null;
}
public boolean isExpressionSeparator() {
return isCommandSeparator()
|| ",".equals(stringValue) //$NON-NLS-1$
|| ";".equals(stringValue); //$NON-NLS-1$
}
public boolean isIndenter() {
return isToken() && tokenString.equals(" FOR "); //$NON-NLS-1$
}
public boolean isOutdenter() {
return isToken() && tokenString.equals(" NEXT "); //$NON-NLS-1$
}
/**
* Get the line number.
*/
@ -90,4 +98,4 @@ public class BusinessBASICToken {
return getStringValue();
}
}
}
}

View File

@ -1,7 +1,7 @@
/*
* AppleCommander - An Apple ][ image utility.
* Copyright (C) 2008 by David Schmidt
* robgreene at users.sourceforge.net
* david__schmidt at users.sourceforge.net
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
@ -41,7 +41,7 @@ import com.webcodepro.applecommander.storage.FileEntry;
public class BusinessBASICTokenizer {
private static String tokens[] = { // starts at $80
" END ", " FOR ", " NEXT ", " INPUT ", " OUTPUT ", " DIM ", " READ ", " WRITE ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
" OPEN ", " CLOSE ", " *error* ", " TEXT ", " *error* ", " *error* ", " *error* ", " *error* ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
" OPEN ", " CLOSE ", " *error* ", " TEXT ", " *error* ", " BYE ", " *error* ", " *error* ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
" *error* ", " *error* ", " *error* ", " WINDOW ", " INVOKE ", " PERFORM ", " *error* ", " *error* ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
" FRE ", " HPOS ", " VPOS ", " ERRLIN ", " ERR ", " KBD ", " EOF ", " TIME$ ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
" DATE$ ", " PREFIX$ ", " EXFN. ", " EXFN%. ", " OUTREC ", " INDENT ", " *error* ", " *error* ", //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ //$NON-NLS-6$ //$NON-NLS-7$ //$NON-NLS-8$
@ -78,21 +78,21 @@ public class BusinessBASICTokenizer {
public BusinessBASICTokenizer(FileEntry fileEntry) {
this(fileEntry.getFileData());
}
/**
* Constructor for BusinessBASICTokenizer.
*/
public BusinessBASICTokenizer(byte[] fileData) {
this.fileData = fileData;
}
/**
* Indicates if there are more tokens in the Business BASIC program.
*/
public boolean hasMoreTokens() {
return (offset < fileData.length);
}
/**
* Answer with the next token in the Business BASIC program. This may be
* code, string pieces, line numbers.