From e76625bf9092117fbe9c85ea9fe9b88ba95234cc Mon Sep 17 00:00:00 2001 From: Mark Long Date: Sat, 29 Oct 2016 17:21:49 -0500 Subject: [PATCH] Optimized Applesoft Parser and Formatter --- src/applesoftfile/ApplesoftRetokenizer.cpp | 66 +++++++++++----------- src/applesoftfile/ApplesoftRetokenizer.h | 14 ++--- src/applesoftfile/applesoftformatter.cxx | 23 +++++++- src/applesoftfile/applesofttoken.h | 2 - src/ui/viewers/applesoftfileviewer.cxx | 1 + src/ui/viewers/applesoftfileviewer.h | 2 +- 6 files changed, 61 insertions(+), 47 deletions(-) diff --git a/src/applesoftfile/ApplesoftRetokenizer.cpp b/src/applesoftfile/ApplesoftRetokenizer.cpp index ce75e0b..3de0408 100644 --- a/src/applesoftfile/ApplesoftRetokenizer.cpp +++ b/src/applesoftfile/ApplesoftRetokenizer.cpp @@ -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 tmptokens = QList::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 ApplesoftRetokenizer::retokenizeRems(QVector&datatokens) +QList ApplesoftRetokenizer::retokenizeRems(QList&tmptokens) { // Handle REMs ApplesoftToken token; - QVector replacements; - QVector tmptokens = datatokens; + QList replacements; + QByteArray buffer; bool inRem = false; @@ -219,11 +222,11 @@ QVector ApplesoftRetokenizer::retokenizeRems(QVector ApplesoftRetokenizer::retokenizeStrings(QVector&datatokens) +QList ApplesoftRetokenizer::retokenizeStrings(QList&tmptokens) { // Handle Strings - QVector replacements; - QVector tmptokens = datatokens; + QList replacements; + QString buffer; ApplesoftToken token; @@ -269,14 +272,14 @@ QVector ApplesoftRetokenizer::retokenizeStrings(QVector ApplesoftRetokenizer::retokenizeDataStatements(QVector&datatokens) +QList ApplesoftRetokenizer::retokenizeDataStatements(QList&tmptokens) { // Handle DATAs - QVector tmptokens = datatokens; - QVector replacements; + + QList replacements; ApplesoftToken token; - QVector datatokenbuffer; + QList datatokenbuffer; bool inData = false; while (!tmptokens.isEmpty()) { @@ -295,7 +298,7 @@ QVector ApplesoftRetokenizer::retokenizeDataStatements(QVector dataTokens; + QList dataTokens; dataTokens = retokenizeDataPayload(datatokenbuffer); replacements.append(dataTokens); datatokenbuffer.clear(); @@ -305,17 +308,17 @@ QVector ApplesoftRetokenizer::retokenizeDataStatements(QVector ApplesoftRetokenizer::retokenizeDataPayload(QVector& datatokens) +QList ApplesoftRetokenizer::retokenizeDataPayload(QList& tmptokens) { - QVector retval; + QList 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 ApplesoftRetokenizer::retokenizeDataPayload(QVector ApplesoftRetokenizer::retokenizeVariables(QVector&datatokens) +QList ApplesoftRetokenizer::retokenizeVariables(QList&tmptokens) { // Handle variable names - QList tmptokens = QList::fromVector(datatokens); ApplesoftToken token; QRegularExpression varregexp("[A-Za-z][A-Za-z0-9]*[$%]?\\(?"); @@ -356,7 +358,7 @@ QVector ApplesoftRetokenizer::retokenizeVariables(QVector 0x0000) { @@ -411,14 +413,12 @@ QVector ApplesoftRetokenizer::retokenizeVariables(QVector ApplesoftRetokenizer::retokenizeNumbers(QVector&datatokens) +QList ApplesoftRetokenizer::retokenizeNumbers(QList&tmptokens) { // Handle numbers - QList tmptokens = QList::fromVector(datatokens); ApplesoftToken token; QRegularExpression varregexp("[0-9]+(\\.[0-9]*)?"); @@ -427,7 +427,7 @@ QVector ApplesoftRetokenizer::retokenizeNumbers(QVector 0x0000) { @@ -470,11 +470,10 @@ QVector ApplesoftRetokenizer::retokenizeNumbers(QVector ApplesoftRetokenizer::retokenizeNegativeNumbers(QVector&datatokens) +QList ApplesoftRetokenizer::retokenizeNegativeNumbers(QList&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 ApplesoftRetokenizer::retokenizeNegativeNumbers(QVector< // Best bet would be to look at how AppleSoft handles these values. // A = - 0 is the same as 0 - QList tmptokens = QList::fromVector(datatokens); ApplesoftToken token; QMutableListIteratorit(tmptokens); @@ -539,6 +537,6 @@ QVector ApplesoftRetokenizer::retokenizeNegativeNumbers(QVector< } } - return tmptokens.toVector(); + return tmptokens; } diff --git a/src/applesoftfile/ApplesoftRetokenizer.h b/src/applesoftfile/ApplesoftRetokenizer.h index cedc0b6..229b2ba 100644 --- a/src/applesoftfile/ApplesoftRetokenizer.h +++ b/src/applesoftfile/ApplesoftRetokenizer.h @@ -27,13 +27,13 @@ private: void retokenizeLine(ApplesoftLine &line); - QVector retokenizeRems(QVector &datatokens); - QVector retokenizeStrings(QVector &datatokens); - QVector retokenizeDataStatements(QVector &datatokens); - QVector retokenizeDataPayload(QVector &datatokens); - QVector retokenizeVariables(QVector &datatokens); - QVector retokenizeNumbers(QVector &datatokens); - QVector retokenizeNegativeNumbers(QVector &datatokens); + QList retokenizeRems(QList &datatokens); + QList retokenizeStrings(QList &datatokens); + QList retokenizeDataStatements(QList &datatokens); + QList retokenizeDataPayload(QList &datatokens); + QList retokenizeVariables(QList &datatokens); + QList retokenizeNumbers(QList &datatokens); + QList retokenizeNegativeNumbers(QList &datatokens); QVector m_retokenized_lines; diff --git a/src/applesoftfile/applesoftformatter.cxx b/src/applesoftfile/applesoftformatter.cxx index 61fecab..c212072 100644 --- a/src/applesoftfile/applesoftformatter.cxx +++ b/src/applesoftfile/applesoftformatter.cxx @@ -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()); + } QVectorIteratortokenIt(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(); } diff --git a/src/applesoftfile/applesofttoken.h b/src/applesoftfile/applesofttoken.h index 7201d15..e2bd543 100644 --- a/src/applesoftfile/applesofttoken.h +++ b/src/applesoftfile/applesofttoken.h @@ -206,8 +206,6 @@ private: TokenType m_token_type; CommandType m_command_type; - QMap m_textcharformats; - static void initializeTokenTable(); }; diff --git a/src/ui/viewers/applesoftfileviewer.cxx b/src/ui/viewers/applesoftfileviewer.cxx index ce2f7a1..b215b24 100644 --- a/src/ui/viewers/applesoftfileviewer.cxx +++ b/src/ui/viewers/applesoftfileviewer.cxx @@ -211,6 +211,7 @@ void ApplesoftFileViewer::reformatText() { QTextDocument *doc = ui->textArea->document(); + doc->clear(); m_formatter->formatDocument(doc); QTextCursor cursor = ui->textArea->textCursor(); diff --git a/src/ui/viewers/applesoftfileviewer.h b/src/ui/viewers/applesoftfileviewer.h index cbd7233..6f9d260 100644 --- a/src/ui/viewers/applesoftfileviewer.h +++ b/src/ui/viewers/applesoftfileviewer.h @@ -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);