diff --git a/src/applesoftfile/applesoftfile.cxx b/src/applesoftfile/applesoftfile.cxx index ca323f1..edd8bc0 100644 --- a/src/applesoftfile/applesoftfile.cxx +++ b/src/applesoftfile/applesoftfile.cxx @@ -1,5 +1,9 @@ #include "applesoftfile.h" #include +#include +#include +#include +#include ApplesoftFile::ApplesoftFile(QByteArray data) : GenericFile(data) { @@ -32,6 +36,7 @@ QByteArray ApplesoftFile::rawData() { void ApplesoftFile::parse(quint16 start_address) { + int idx = 0; quint8 val = 0; @@ -96,19 +101,25 @@ QByteArray ApplesoftFile::extraData() void Retokenizer::retokenize(ApplesoftLine &line) { - Q_UNUSED(line); + line.tokens = retokenizeRems(line.tokens); + line.tokens = retokenizeStrings(line.tokens); + line.tokens = retokenizeDataStatements(line.tokens); + line.tokens = retokenizeVariables(line.tokens); + line.tokens = retokenizeNumbers(line.tokens); - QVector replacements; - QVector tmptokens = line.tokens; +} + +QVector Retokenizer::retokenizeRems(QVector&datatokens) +{ + // Handle REMs ApplesoftToken token; - + QVector replacements; + QVector tmptokens = datatokens; QByteArray buffer; bool inRem = false; - // Handle REMs - while (!tmptokens.isEmpty()) { token = tmptokens.takeFirst(); @@ -132,14 +143,17 @@ void Retokenizer::retokenize(ApplesoftLine &line) inRem = false; } - line.tokens = replacements; - replacements.clear(); - - // Handle DATAs + return replacements; +} +QVector Retokenizer::retokenizeStrings(QVector&datatokens) +{ // Handle Strings + QVector replacements; + QVector tmptokens = datatokens; + QString buffer; + ApplesoftToken token; - tmptokens = line.tokens; bool inString = false; while (!tmptokens.isEmpty()) @@ -148,45 +162,241 @@ void Retokenizer::retokenize(ApplesoftLine &line) if (token.getTokenId() >= 0x80) { replacements.append(token); - continue; - } + // continue; + } else - if (token.getByteValue() == '"') + if (token.getWordValue() == '"') { if (!inString) { inString = true; - buffer.append(token.getByteValue()); - continue; + buffer.append(token.getWordValue()); + // continue; } else { - buffer.append(token.getByteValue()); + buffer.append(token.getWordValue()); ApplesoftToken strtoken(ApplesoftToken::StringTokenVal, buffer); replacements.append(strtoken); buffer.clear(); inString = false; - continue; +// continue; } - } + } else if (inString) { - buffer.append(token.getByteValue()); - continue; - } + buffer.append(token.getWordValue()); + // continue; + } else replacements.append(token); } - line.tokens = replacements; - + return replacements; } -QByteArray Retokenizer::retokenizePart(QByteArray part) { - QByteArray retval; +QVector Retokenizer::retokenizeDataStatements(QVector&datatokens) +{ + // Handle DATAs + QVector tmptokens = datatokens; + QVector replacements; + ApplesoftToken token; - Q_UNUSED(part); + QVector datatokenbuffer; + bool inData = false; + while (!tmptokens.isEmpty()) + { + token = tmptokens.takeFirst(); + if (!inData) { + replacements.append(token); + if (token.getTokenId() == ApplesoftToken::ASData) + { + inData = true; + } + } + else + { + datatokenbuffer.append(token); + } + } + if (inData) { + QVector dataTokens; + dataTokens = processDataPayload(datatokenbuffer); + replacements.append(dataTokens); + datatokenbuffer.clear(); + inData = false; + } + return replacements; +} + + +QVector Retokenizer::processDataPayload(QVector& datatokens) +{ + QVector retval; + + ApplesoftToken token; + + QString stringbuffer; + + while (!datatokens.isEmpty()) + { + token = datatokens.takeFirst(); + if (token.getTokenId() == ApplesoftToken::StringTokenVal) + { + ApplesoftToken newToken(ApplesoftToken::DataStringTokenVal, token.getStringValue()); + retval.append(newToken); + continue; + } + if (token.getWordValue() == ',') + { + if (!stringbuffer.isEmpty()) + { + ApplesoftToken datastrtoken(ApplesoftToken::DataStringTokenVal, stringbuffer); + retval.append(datastrtoken); + stringbuffer.clear(); + } + retval.append(token); + continue; + + } + stringbuffer.append(token.getWordValue()); + } + if (!stringbuffer.isEmpty()) + { + ApplesoftToken datastrtoken(ApplesoftToken::DataStringTokenVal, stringbuffer); + retval.append(datastrtoken); + stringbuffer.clear(); + } return retval; } +QVector Retokenizer::retokenizeVariables(QVector&datatokens) +{ + // Handle variable names + QList tmptokens = QList::fromVector(datatokens); + ApplesoftToken token; + + QRegularExpression varregexp("[A-Za-z][A-Za-z0-9]*[$%]?\\(?"); + + QString parsestring; + // Parse the tokens to find assist + for (int idx = 0; idx < tmptokens.count();idx++) + { + token = datatokens.at(idx); + + if (token.getTokenId() < 0x0080 && token.getTokenId() > 0x0000) + { + parsestring.append(QChar(token.getWordValue())); + } + else + { + parsestring.append("_"); + } + } + QList matchstack; + QRegularExpressionMatchIterator matches = varregexp.globalMatch(parsestring); + // qDebug() << parsestring; + while (matches.hasNext()) { + QRegularExpressionMatch rematch = matches.next(); + matchstack.push_front(rematch); + +// qDebug() << "Capture " << " = " << rematch.capturedTexts() << "From: " << rematch.capturedStart() +// << "To: " << rematch.capturedEnd()-1 << "("< Retokenizer::retokenizeNumbers(QVector&datatokens) +{ + // Handle numbers + QList tmptokens = QList::fromVector(datatokens); + ApplesoftToken token; + + QRegularExpression varregexp("[0-9]+(\\.[0-9]*)?"); + + QString parsestring; + // Parse the tokens to find assist + for (int idx = 0; idx < tmptokens.count();idx++) + { + token = datatokens.at(idx); + + if (token.getTokenId() < 0x0080 && token.getTokenId() > 0x0000) + { + parsestring.append(QChar(token.getWordValue())); + } + else + { + parsestring.append("_"); + } + } + QList matchstack; + QRegularExpressionMatchIterator matches = varregexp.globalMatch(parsestring); +// qDebug() << parsestring; + while (matches.hasNext()) { + QRegularExpressionMatch rematch = matches.next(); + matchstack.push_front(rematch); + +// qDebug() << "Capture " << " = " << rematch.capturedTexts() << "From: " << rematch.capturedStart() +// << "To: " << rematch.capturedEnd()-1 << "("< retokenizeRems(QVector &datatokens); + static QVector retokenizeStrings(QVector &datatokens); + static QVector retokenizeDataStatements(QVector &datatokens); + static QVector processDataPayload(QVector &datatokens); + static QVector retokenizeVariables(QVector &datatokens); + static QVector retokenizeNumbers(QVector &datatokens); }; + #endif // APPLESOFTFILE_H diff --git a/src/applesoftfile/applesoftformatter.cxx b/src/applesoftfile/applesoftformatter.cxx index 33ae731..5e59656 100644 --- a/src/applesoftfile/applesoftformatter.cxx +++ b/src/applesoftfile/applesoftformatter.cxx @@ -23,45 +23,45 @@ QString ApplesoftFormatter::formatText() foreach (ApplesoftLine line, m_file->getLines()) { QString linestring = QString("%1 ").arg(line.linenum,5,10,QChar(' ')); + retval.append(linestring); foreach (ApplesoftToken token, line.tokens) { QString tokenstr = token.getRawPrintableString(); if (m_format_options.testFlag(BreakAfterReturn)) { - if (token.getByteValue() == ApplesoftToken::ASReturn) + if (token.getTokenId() == ApplesoftToken::ASReturn) { tokenstr += "\n"; } } -#define DEBUGTOKENS +#define noDEBUGTOKENS #ifdef DEBUGTOKENS if (token.getTokenId() >= 0x80) { - tokenstr = QString(" {%1} ").arg(uint16ToHex(token.getTokenId())); - // tokenstr = " __ "; - } - else - { - tokenstr = QString("%1").arg(QChar(token.getByteValue())); + tokenstr = QString("{%1 (%2)}").arg(tokenstr).arg(uint16ToHex(token.getTokenId())); + // tokenstr = " __ "; } + #endif - if (m_format_options.testFlag(ShowCtrlChars)) { + // if (m_format_options.testFlag(ShowCtrlChars)) + { tokenstr.replace(QChar(0x7f),QChar(0x2401)); for (int idx = 1; idx <= 0x1f; idx++) { if (idx == '\n') continue; - if (idx == '\t') continue; tokenstr.replace(QChar(idx),QChar(idx+0x2400)); + // tokenstr.replace(QChar(idx), QString("<%1>").arg(uint8ToHex(idx))); } } - retval.append(tokenstr); } retval.append("\n"); + } + return retval; } diff --git a/src/applesoftfile/applesofttoken.cxx b/src/applesoftfile/applesofttoken.cxx index 1bd8bda..95fe9c7 100644 --- a/src/applesoftfile/applesofttoken.cxx +++ b/src/applesoftfile/applesofttoken.cxx @@ -89,16 +89,59 @@ void ApplesoftToken::setValue(QVariant value) m_payload = value; } +QString ApplesoftToken::getHtmlPrintableString() +{ + QString baseval = getRawPrintableString().toHtmlEscaped(); + + if (getTokenId() <= 0x7f) + return QString("%1").arg(baseval); + if (getTokenId() <= 0xff) + return QString("%1").arg(baseval); + if (getTokenId() == ApplesoftToken::StringTokenVal) + return QString("%1").arg(baseval); + if (getTokenId() == ApplesoftToken::DataStringTokenVal) + return QString("%1").arg(baseval); + if (getTokenId() == ApplesoftToken::RemStringTokenVal) + return QString("%1").arg(baseval); + if (getTokenId() == ApplesoftToken::IntegerTokenVal || getTokenId() == ApplesoftToken::FloatAryVarTokenVal) + return QString("%1").arg(baseval); + + + + return QString("%1").arg(baseval); + +} + QString ApplesoftToken::getRawPrintableString() { if (m_token_id == 0x00) { return ""; } else if (m_token_id <= 0x7f) { - return QString(QChar(m_token_id)); + return QString((m_token_id)); } else if (m_token_id <= 0xff) { return m_tokens[m_token_id]; } else if (m_token_id == StringTokenVal) { - return getByteStringValue(); + return getStringValue(); + } else if (m_token_id == RemStringTokenVal) { + return getStringValue(); + } else if (m_token_id == DataStringTokenVal) { + return getStringValue(); + } else if (m_token_id == IntegerTokenVal) { + return getStringValue(); + } else if (m_token_id == FloatTokenVal) { + return getStringValue(); + } else if (m_token_id == IntVarTokenVal) { + return getStringValue(); + } else if (m_token_id == IntAryVarTokenVal) { + return getStringValue(); + } else if (m_token_id == FloatVarTokenVal) { + return getStringValue(); + } else if (m_token_id == FloatAryVarTokenVal) { + return getStringValue(); + } else if (m_token_id == StringVarTokenVal) { + return getStringValue(); + } else if (m_token_id == StringAryVarTokenVal) { + return getStringValue(); } else { return "[temp undefined]"; } diff --git a/src/applesoftfile/applesofttoken.h b/src/applesoftfile/applesofttoken.h index 27fe5b8..9c7f8f3 100644 --- a/src/applesoftfile/applesofttoken.h +++ b/src/applesoftfile/applesofttoken.h @@ -16,6 +16,8 @@ public: static const quint16 IntegerTokenVal = 0x103; static const quint16 FloatTokenVal = 0x104; + + static const quint16 IntVarTokenVal = 0x105; static const quint16 IntAryVarTokenVal = 0x106; @@ -25,6 +27,8 @@ public: static const quint16 StringVarTokenVal = 0x109; static const quint16 StringAryVarTokenVal = 0x10A; + + static const quint16 ASEnd; static const quint16 ASFor; static const quint16 ASNext; static const quint16 ASData; static const quint16 ASInput; static const quint16 ASDel; @@ -134,6 +138,7 @@ public: QVariant getValue() const { return m_payload; } QByteArray getByteStringValue() const { return m_payload.toByteArray(); } + QString getStringValue() const { return m_payload.toString(); } quint16 getWordValue() const { return (quint16) (m_payload.toUInt() & 0xFFFF); } quint8 getByteValue() const { return (quint8) (m_payload.toUInt() & 0xFF); } @@ -147,6 +152,7 @@ public: return m_tokens[token]; } + QString getHtmlPrintableString(); private: static QMap m_tokens; diff --git a/src/ui/catalogwidget.cxx b/src/ui/catalogwidget.cxx index d39951f..ed93ffd 100644 --- a/src/ui/catalogwidget.cxx +++ b/src/ui/catalogwidget.cxx @@ -61,7 +61,7 @@ void CatalogWidget::prepForNewDisk(QString filename, DiskFile *disk) QString CatalogWidget::createToolTip(FileDescriptiveEntry &fde) { QString retval; - +qDebug() << AppleString(fde.filename).printable().trimmed(); retval += AppleString(fde.filename).printable().trimmed() + "\n"; retval += QString("Type: %1\n").arg(fde.fileType()); retval += QString("Sectors: %1 (%2 bytes)\n").arg(fde.lengthInSectors).arg(fde.lengthInSectors*256); diff --git a/src/ui/viewers/applesoftfileviewer.cxx b/src/ui/viewers/applesoftfileviewer.cxx index 9cbfccd..cddb7d5 100644 --- a/src/ui/viewers/applesoftfileviewer.cxx +++ b/src/ui/viewers/applesoftfileviewer.cxx @@ -12,8 +12,8 @@ ApplesoftFileViewer::ApplesoftFileViewer(QWidget *parent) : setWindowTitle(title); m_formatter = new ApplesoftFormatter(this); - m_formatter->setFlags(ApplesoftFormatter::PrettyFlags | ApplesoftFormatter::BreakAfterReturn); - + //m_formatter->setFlags(ApplesoftFormatter::PrettyFlags | ApplesoftFormatter::BreakAfterReturn); + m_formatter->setFlags(ApplesoftFormatter::AllFlags); connect(ui->findButton,SIGNAL(clicked(bool)), SLOT(findText())); m_isFirstFind = true; ui->textArea->setUndoRedoEnabled(false); diff --git a/src/ui/viewers/applesoftfileviewer.ui b/src/ui/viewers/applesoftfileviewer.ui index 3c310c8..632846f 100644 --- a/src/ui/viewers/applesoftfileviewer.ui +++ b/src/ui/viewers/applesoftfileviewer.ui @@ -53,8 +53,10 @@ - Courier 10 Pitch - 10 + Misc Fixed + 14 + 75 + true