Optimized Applesoft Parser and Formatter

This commit is contained in:
Mark Long 2016-10-29 17:21:49 -05:00
parent c6d4020310
commit e76625bf90
6 changed files with 61 additions and 47 deletions

View File

@ -175,20 +175,23 @@ void ApplesoftRetokenizer::retokenizeLinesForFormatting()
void ApplesoftRetokenizer::retokenizeLine(ApplesoftLine &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);
line.tokens = retokenizeNegativeNumbers(line.tokens);
qDebug() << "Retokenize line";
QList<ApplesoftToken> tmptokens = QList<ApplesoftToken>::fromVector(line.tokens);
tmptokens = retokenizeRems(tmptokens);
tmptokens = retokenizeStrings(tmptokens);
tmptokens = retokenizeDataStatements(tmptokens);
tmptokens = retokenizeVariables(tmptokens);
tmptokens = retokenizeNumbers(tmptokens);
tmptokens = retokenizeNegativeNumbers(tmptokens);
line.tokens = tmptokens.toVector();
}
QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeRems(QVector<ApplesoftToken>&datatokens)
QList<ApplesoftToken> ApplesoftRetokenizer::retokenizeRems(QList<ApplesoftToken>&tmptokens)
{
// Handle REMs
ApplesoftToken token;
QVector<ApplesoftToken> replacements;
QVector<ApplesoftToken> tmptokens = datatokens;
QList<ApplesoftToken> replacements;
QByteArray buffer;
bool inRem = false;
@ -219,11 +222,11 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeRems(QVector<ApplesoftTo
return replacements;
}
QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeStrings(QVector<ApplesoftToken>&datatokens)
QList<ApplesoftToken> ApplesoftRetokenizer::retokenizeStrings(QList<ApplesoftToken>&tmptokens)
{
// Handle Strings
QVector<ApplesoftToken> replacements;
QVector<ApplesoftToken> tmptokens = datatokens;
QList<ApplesoftToken> replacements;
QString buffer;
ApplesoftToken token;
@ -269,14 +272,14 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeStrings(QVector<Applesof
return replacements;
}
QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeDataStatements(QVector<ApplesoftToken>&datatokens)
QList<ApplesoftToken> ApplesoftRetokenizer::retokenizeDataStatements(QList<ApplesoftToken>&tmptokens)
{
// Handle DATAs
QVector<ApplesoftToken> tmptokens = datatokens;
QVector<ApplesoftToken> replacements;
QList<ApplesoftToken> replacements;
ApplesoftToken token;
QVector<ApplesoftToken> datatokenbuffer;
QList<ApplesoftToken> datatokenbuffer;
bool inData = false;
while (!tmptokens.isEmpty())
{
@ -295,7 +298,7 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeDataStatements(QVector<A
}
}
if (inData) {
QVector<ApplesoftToken> dataTokens;
QList<ApplesoftToken> dataTokens;
dataTokens = retokenizeDataPayload(datatokenbuffer);
replacements.append(dataTokens);
datatokenbuffer.clear();
@ -305,17 +308,17 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeDataStatements(QVector<A
}
QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeDataPayload(QVector<ApplesoftToken>& datatokens)
QList<ApplesoftToken> ApplesoftRetokenizer::retokenizeDataPayload(QList<ApplesoftToken>& tmptokens)
{
QVector<ApplesoftToken> retval;
QList<ApplesoftToken> retval;
ApplesoftToken token;
QString stringbuffer;
while (!datatokens.isEmpty())
while (!tmptokens.isEmpty())
{
token = datatokens.takeFirst();
token = tmptokens.takeFirst();
if (token.getTokenId() == ApplesoftToken::StringTokenVal)
{
ApplesoftToken newToken(ApplesoftToken::DataStringTokenVal, token.getStringValue());
@ -344,10 +347,9 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeDataPayload(QVector<Appl
}
return retval;
}
QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeVariables(QVector<ApplesoftToken>&datatokens)
QList<ApplesoftToken> ApplesoftRetokenizer::retokenizeVariables(QList<ApplesoftToken>&tmptokens)
{
// Handle variable names
QList<ApplesoftToken> tmptokens = QList<ApplesoftToken>::fromVector(datatokens);
ApplesoftToken token;
QRegularExpression varregexp("[A-Za-z][A-Za-z0-9]*[$%]?\\(?");
@ -356,7 +358,7 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeVariables(QVector<Apples
// Parse the tokens to find assist
for (int idx = 0; idx < tmptokens.count();idx++)
{
token = datatokens.at(idx);
token = tmptokens.at(idx);
if (token.getTokenId() < 0x0080 && token.getTokenId() > 0x0000)
{
@ -411,14 +413,12 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeVariables(QVector<Apples
}
datatokens = tmptokens.toVector();
return datatokens;
return tmptokens;
}
QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeNumbers(QVector<ApplesoftToken>&datatokens)
QList<ApplesoftToken> ApplesoftRetokenizer::retokenizeNumbers(QList<ApplesoftToken>&tmptokens)
{
// Handle numbers
QList<ApplesoftToken> tmptokens = QList<ApplesoftToken>::fromVector(datatokens);
ApplesoftToken token;
QRegularExpression varregexp("[0-9]+(\\.[0-9]*)?");
@ -427,7 +427,7 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeNumbers(QVector<Applesof
// Parse the tokens to find assist
for (int idx = 0; idx < tmptokens.count();idx++)
{
token = datatokens.at(idx);
token = tmptokens.at(idx);
if (token.getTokenId() < 0x0080 && token.getTokenId() > 0x0000)
{
@ -470,11 +470,10 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeNumbers(QVector<Applesof
}
datatokens = tmptokens.toVector();
return datatokens;
return tmptokens;
}
QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeNegativeNumbers(QVector<ApplesoftToken>&datatokens)
QList<ApplesoftToken> ApplesoftRetokenizer::retokenizeNegativeNumbers(QList<ApplesoftToken>&tmptokens)
{
//TODO: Code to make determination of negative numbers vs. unary minus/math formulas.
// Prefixed '-' tokens for negative numbers should get merged with the integer value token.
@ -500,7 +499,6 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeNegativeNumbers(QVector<
// Best bet would be to look at how AppleSoft handles these values.
// A = - 0 is the same as 0
QList<ApplesoftToken> tmptokens = QList<ApplesoftToken>::fromVector(datatokens);
ApplesoftToken token;
QMutableListIterator<ApplesoftToken>it(tmptokens);
@ -539,6 +537,6 @@ QVector<ApplesoftToken> ApplesoftRetokenizer::retokenizeNegativeNumbers(QVector<
}
}
return tmptokens.toVector();
return tmptokens;
}

View File

@ -27,13 +27,13 @@ private:
void retokenizeLine(ApplesoftLine &line);
QVector<ApplesoftToken> retokenizeRems(QVector<ApplesoftToken> &datatokens);
QVector<ApplesoftToken> retokenizeStrings(QVector<ApplesoftToken> &datatokens);
QVector<ApplesoftToken> retokenizeDataStatements(QVector<ApplesoftToken> &datatokens);
QVector<ApplesoftToken> retokenizeDataPayload(QVector<ApplesoftToken> &datatokens);
QVector<ApplesoftToken> retokenizeVariables(QVector<ApplesoftToken> &datatokens);
QVector<ApplesoftToken> retokenizeNumbers(QVector<ApplesoftToken> &datatokens);
QVector<ApplesoftToken> retokenizeNegativeNumbers(QVector<ApplesoftToken> &datatokens);
QList<ApplesoftToken> retokenizeRems(QList<ApplesoftToken> &datatokens);
QList<ApplesoftToken> retokenizeStrings(QList<ApplesoftToken> &datatokens);
QList<ApplesoftToken> retokenizeDataStatements(QList<ApplesoftToken> &datatokens);
QList<ApplesoftToken> retokenizeDataPayload(QList<ApplesoftToken> &datatokens);
QList<ApplesoftToken> retokenizeVariables(QList<ApplesoftToken> &datatokens);
QList<ApplesoftToken> retokenizeNumbers(QList<ApplesoftToken> &datatokens);
QList<ApplesoftToken> retokenizeNegativeNumbers(QList<ApplesoftToken> &datatokens);
QVector<ApplesoftLine> m_retokenized_lines;

View File

@ -304,17 +304,28 @@ void ApplesoftFormatter::formatDocument(QTextDocument *doc)
doc->clear();
QTextCursor cursor(doc);
cursor.beginEditBlock();
bool synhl = (m_format_options.testFlag(SyntaxHighlighting));
foreach (ApplesoftLine line, m_file->getLines())
{
QString linestring = QString("%1 ").arg(line.linenum,5,10,QChar(' '));
cursor.insertText(linestring,ApplesoftToken::textFormat(ApplesoftToken::LineNumberTokenVal));
if (synhl)
{
cursor.insertText(linestring,ApplesoftToken::textFormat(ApplesoftToken::LineNumberTokenVal));
}
else
{
cursor.insertText(linestring,ApplesoftToken::defaultTextFormat());
}
QVectorIterator<ApplesoftToken>tokenIt(line.tokens);
bool isBranchTarget = false;
while (tokenIt.hasNext())
{
ApplesoftToken token = tokenIt.next();
bool isBranchTarget = false;
if (token.isOptFmtToken())
{
switch (token.getTokenId())
@ -365,7 +376,11 @@ void ApplesoftFormatter::formatDocument(QTextDocument *doc)
else
{
QString tokenstr = token.getRawPrintableString();
QTextCharFormat fmt = token.textFormat();
QTextCharFormat fmt = ApplesoftToken::defaultTextFormat();
if (synhl) fmt = token.textFormat();
if (token.getTokenId() == ApplesoftToken::IntegerTokenVal)
{
@ -388,6 +403,7 @@ void ApplesoftFormatter::formatDocument(QTextDocument *doc)
tokenstr = HEXPREFIX+uint32ToHex(ui32val);
}
} // isShowIntsAsHex
isBranchTarget = false;
}
if (m_format_options.testFlag(ShowCtrlChars))
@ -412,4 +428,5 @@ void ApplesoftFormatter::formatDocument(QTextDocument *doc)
cursor.insertBlock();
} // foreach line
cursor.endEditBlock();
}

View File

@ -206,8 +206,6 @@ private:
TokenType m_token_type;
CommandType m_command_type;
QMap<TextCharFormatType,QTextCharFormat> m_textcharformats;
static void initializeTokenTable();
};

View File

@ -211,6 +211,7 @@ void ApplesoftFileViewer::reformatText()
{
QTextDocument *doc = ui->textArea->document();
doc->clear();
m_formatter->formatDocument(doc);
QTextCursor cursor = ui->textArea->textCursor();

View File

@ -46,7 +46,7 @@ public slots:
protected slots:
void toggleWordWrap(bool enabled);
void setSyntaxHighlighting(bool enabled, ReformatRule reformat);
void setSyntaxHighlighting(bool enabled, ReformatRule reformat = ForceReformat);
void setIndentCode(bool enabled, ReformatRule reformat = ForceReformat);
void setIntsAsHex(bool enabled, ReformatRule reformat = ForceReformat);
void setBreakAfterReturn(bool enabled, ReformatRule reformat = ForceReformat);