- enhancement: Syntaxer for lua.

This commit is contained in:
Roy Qu 2023-02-16 12:26:35 +08:00
parent 0bedb63fa2
commit 46dd637770
19 changed files with 1351 additions and 25 deletions

View File

@ -26,6 +26,7 @@ Red Panda C++ Version 2.12
- enhancement: New "embed assembly" template. - enhancement: New "embed assembly" template.
- enhancement: New "Hello GAS" and "GAS and C" templates for linux and win64. - enhancement: New "Hello GAS" and "GAS and C" templates for linux and win64.
- fix: Wrong selection position after delete in column mode. - fix: Wrong selection position after delete in column mode.
- enhancement: Syntaxer for lua.
Red Panda C++ Version 2.11 Red Panda C++ Version 2.11

View File

@ -942,6 +942,14 @@ void Editor::keyPressEvent(QKeyEvent *event)
handled=true; handled=true;
return; return;
} }
} else if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::LUA) {
if (ch=='.') {
mLastIdCharPressed++;
processCommand(QSynedit::EditCommand::Char,ch,nullptr);
showCompletion("",false,CodeCompletionType::KeywordsOnly);
handled=true;
return;
}
} else if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::ATTAssembly) { } else if (syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::ATTAssembly) {
if ((mLastIdCharPressed==0) && (ch=='.')) { if ((mLastIdCharPressed==0) && (ch=='.')) {
mLastIdCharPressed++; mLastIdCharPressed++;
@ -3232,8 +3240,11 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete, CodeComple
(attr->tokenType() == QSynedit::TokenType::String) && (attr->tokenType() == QSynedit::TokenType::String) &&
(attr->tokenType() != QSynedit::TokenType::Character)) { (attr->tokenType() != QSynedit::TokenType::Character)) {
return; return;
} else if (type==CodeCompletionType::KeywordsOnly && syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::ATTAssembly) { } else if (type==CodeCompletionType::KeywordsOnly && syntaxer() ) {
if (syntaxer()->language()==QSynedit::ProgrammingLanguage::ATTAssembly)
word = getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpATTASMKeywords); word = getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpATTASMKeywords);
else
word = getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpKeywords);
} else if ( } else if (
(attr->tokenType() != QSynedit::TokenType::Operator) && (attr->tokenType() != QSynedit::TokenType::Operator) &&
(attr->tokenType() != QSynedit::TokenType::Space) && (attr->tokenType() != QSynedit::TokenType::Space) &&
@ -3295,8 +3306,16 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete, CodeComple
keywords = QSynedit::ASMSyntaxer::ATTRegisters; keywords = QSynedit::ASMSyntaxer::ATTRegisters;
else else
keywords = QSynedit::ASMSyntaxer::Instructions; keywords = QSynedit::ASMSyntaxer::Instructions;
} else {
int pos = word.lastIndexOf(".");
if (pos>=0) {
QString scopeWord=word.left(pos);
word = word.mid(pos+1);
QMap<QString, QSet<QString> > scopedKeywords = syntaxer()->scopedKeywords();
keywords = scopedKeywords.value(scopeWord, QSet<QString>());
} else } else
keywords = syntaxer()->keywords(); keywords = syntaxer()->keywords();
}
} else { } else {
if (mUseCppSyntax) { if (mUseCppSyntax) {
foreach (const QString& keyword, CppKeywords.keys()) { foreach (const QString& keyword, CppKeywords.keys()) {
@ -4462,6 +4481,17 @@ QString getWordAtPosition(QSynedit::QSynEdit *editor, const QSynedit::BufferCoor
} }
} }
if (purpose == Editor::WordPurpose::wpKeywords) {
while ((wordBegin >= 0) && (wordBegin < len)) {
if (editor->isIdentChar(s[wordBegin])) {
wordBegin--;
} else if (s[wordBegin] == '.') {
wordBegin--;
} else
break;
}
}
// Copy backward until # // Copy backward until #
if (purpose == Editor::WordPurpose::wpDirective) { if (purpose == Editor::WordPurpose::wpDirective) {
while ((wordBegin >= 0) && (wordBegin < len)) { while ((wordBegin >= 0) && (wordBegin < len)) {

View File

@ -97,6 +97,7 @@ public:
wpJavadoc, //javadoc wpJavadoc, //javadoc
wpInformation, // walk backwards over words, array, functions, parents, forwards over words wpInformation, // walk backwards over words, array, functions, parents, forwards over words
wpATTASMKeywords, wpATTASMKeywords,
wpKeywords
}; };
enum class TipType { enum class TipType {

View File

@ -208,7 +208,6 @@ MainWindow::MainWindow(QWidget *parent)
e.reason()); e.reason());
} }
// ui->actionIndent->setShortcut(Qt::Key_Tab); // ui->actionIndent->setShortcut(Qt::Key_Tab);
// ui->actionUnIndent->setShortcut(Qt::Key_Tab | Qt::ShiftModifier); // ui->actionUnIndent->setShortcut(Qt::Key_Tab | Qt::ShiftModifier);
@ -9461,4 +9460,3 @@ void MainWindow::on_actionGNU_Assembler_Manual_triggered()
{ {
QDesktopServices::openUrl(QUrl("https://sourceware.org/binutils/docs/as/index.html")); QDesktopServices::openUrl(QUrl("https://sourceware.org/binutils/docs/as/index.html"));
} }

View File

@ -21,6 +21,7 @@
#include "qsynedit/syntaxer/cpp.h" #include "qsynedit/syntaxer/cpp.h"
#include "qsynedit/syntaxer/asm.h" #include "qsynedit/syntaxer/asm.h"
#include "qsynedit/syntaxer/glsl.h" #include "qsynedit/syntaxer/glsl.h"
#include "qsynedit/syntaxer/lua.h"
#include "qsynedit/syntaxer/makefile.h" #include "qsynedit/syntaxer/makefile.h"
#include "qsynedit/constants.h" #include "qsynedit/constants.h"
@ -46,6 +47,8 @@ QSynedit::PSyntaxer SyntaxerManager::getSyntaxer(QSynedit::ProgrammingLanguage l
return std::make_shared<QSynedit::MakefileSyntaxer>(); return std::make_shared<QSynedit::MakefileSyntaxer>();
case QSynedit::ProgrammingLanguage::GLSL: case QSynedit::ProgrammingLanguage::GLSL:
return std::make_shared<QSynedit::GLSLSyntaxer>(); return std::make_shared<QSynedit::GLSLSyntaxer>();
case QSynedit::ProgrammingLanguage::LUA:
return std::make_shared<QSynedit::LuaSyntaxer>();
default: default:
return QSynedit::PSyntaxer(); return QSynedit::PSyntaxer();
} }
@ -68,6 +71,8 @@ QSynedit::PSyntaxer SyntaxerManager::getSyntaxer(const QString &filename)
return getSyntaxer(QSynedit::ProgrammingLanguage::Assembly); return getSyntaxer(QSynedit::ProgrammingLanguage::Assembly);
} else if (suffix == "s" || suffix == "S") { } else if (suffix == "s" || suffix == "S") {
return getSyntaxer(QSynedit::ProgrammingLanguage::ATTAssembly); return getSyntaxer(QSynedit::ProgrammingLanguage::ATTAssembly);
} else if (suffix == "lua") {
return getSyntaxer(QSynedit::ProgrammingLanguage::LUA);
} else if (basename.compare("makefile", Qt::CaseInsensitive)==0) { } else if (basename.compare("makefile", Qt::CaseInsensitive)==0) {
return getSyntaxer(QSynedit::ProgrammingLanguage::Makefile); return getSyntaxer(QSynedit::ProgrammingLanguage::Makefile);
} else if (suffix.isEmpty()) { } else if (suffix.isEmpty()) {

View File

@ -37,6 +37,7 @@ SystemConsts::SystemConsts(): mDefaultFileFilters()
addDefaultFileFilter(QObject::tr("Header files"),"*.h *.hh *.hpp"); addDefaultFileFilter(QObject::tr("Header files"),"*.h *.hh *.hpp");
addDefaultFileFilter(QObject::tr("GAS files"),"*.s *.S"); addDefaultFileFilter(QObject::tr("GAS files"),"*.s *.S");
addDefaultFileFilter(QObject::tr("ASM files"),"*.asm"); addDefaultFileFilter(QObject::tr("ASM files"),"*.asm");
addDefaultFileFilter(QObject::tr("Lua files"),"*.lua");
addFileFilter(mIconFileFilters, QObject::tr("Icon files"), "*.ico"); addFileFilter(mIconFileFilters, QObject::tr("Icon files"), "*.ico");

View File

@ -38,6 +38,7 @@ SOURCES += qsynedit/codefolding.cpp \
qsynedit/syntaxer/cpp.cpp \ qsynedit/syntaxer/cpp.cpp \
qsynedit/syntaxer/customhighlighterv1.cpp \ qsynedit/syntaxer/customhighlighterv1.cpp \
qsynedit/syntaxer/glsl.cpp \ qsynedit/syntaxer/glsl.cpp \
qsynedit/syntaxer/lua.cpp \
qsynedit/types.cpp \ qsynedit/types.cpp \
qsynedit/syntaxer/makefile.cpp \ qsynedit/syntaxer/makefile.cpp \
qsynedit/syntaxer/syntaxer.cpp qsynedit/syntaxer/syntaxer.cpp
@ -63,6 +64,7 @@ HEADERS += \
qsynedit/syntaxer/cpp.h \ qsynedit/syntaxer/cpp.h \
qsynedit/syntaxer/customhighlighterv1.h \ qsynedit/syntaxer/customhighlighterv1.h \
qsynedit/syntaxer/glsl.h \ qsynedit/syntaxer/glsl.h \
qsynedit/syntaxer/lua.h \
qsynedit/syntaxer/makefile.h \ qsynedit/syntaxer/makefile.h \
qsynedit/syntaxer/syntaxer.h qsynedit/syntaxer/syntaxer.h

View File

@ -260,7 +260,10 @@ bool ASMSyntaxer::isATT() const
void ASMSyntaxer::setATT(bool newATT) void ASMSyntaxer::setATT(bool newATT)
{ {
if (mATT!=newATT) {
mATT = newATT; mATT = newATT;
mKeywordsCache.clear();
}
} }
void ASMSyntaxer::CommentProc() void ASMSyntaxer::CommentProc()
@ -612,17 +615,19 @@ void ASMSyntaxer::resetState()
mHasTrailingSpaces = false; mHasTrailingSpaces = false;
} }
QSet<QString> ASMSyntaxer::keywords() const QSet<QString> ASMSyntaxer::keywords()
{ {
QSet<QString> result=Instructions; if (mKeywordsCache.isEmpty()) {
mKeywordsCache=Instructions;
if (!isATT()) { if (!isATT()) {
result.unite(Directives); mKeywordsCache.unite(Directives);
result.unite(Registers); mKeywordsCache.unite(Registers);
} else { } else {
result.unite(ATTDirectives); mKeywordsCache.unite(ATTDirectives);
result.unite(ATTRegisters); mKeywordsCache.unite(ATTRegisters);
} }
return result; }
return mKeywordsCache;
} }
const PTokenAttribute &ASMSyntaxer::directiveAttribute() const const PTokenAttribute &ASMSyntaxer::directiveAttribute() const

View File

@ -73,6 +73,7 @@ private:
PTokenAttribute mRegisterAttribute; PTokenAttribute mRegisterAttribute;
PTokenAttribute mLabelAttribute; PTokenAttribute mLabelAttribute;
bool mATT; bool mATT;
QSet<QString> mKeywordsCache;
private: private:
void CommentProc(); void CommentProc();
@ -114,7 +115,7 @@ public:
public: public:
QSet<QString> keywords() const override; QSet<QString> keywords() override;
bool isATT() const; bool isATT() const;
void setATT(bool newATT); void setATT(bool newATT);

View File

@ -1651,7 +1651,7 @@ bool CppSyntaxer::isIdentChar(const QChar &ch) const
return ch=='_' || ch.isDigit() || ch.isLetter(); return ch=='_' || ch.isDigit() || ch.isLetter();
} }
QSet<QString> CppSyntaxer::keywords() const QSet<QString> CppSyntaxer::keywords()
{ {
QSet<QString> set=Keywords; QSet<QString> set=Keywords;
set.unite(mCustomTypeKeywords); set.unite(mCustomTypeKeywords);

View File

@ -197,7 +197,7 @@ public:
// SynHighlighter interface // SynHighlighter interface
public: public:
QSet<QString> keywords() const override; QSet<QString> keywords() override;
// SynHighlighter interface // SynHighlighter interface
public: public:

View File

@ -1439,7 +1439,7 @@ bool GLSLSyntaxer::isIdentChar(const QChar &ch) const
return ch=='_' || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9'); return ch=='_' || (ch>='a' && ch<='z') || (ch>='A' && ch<='Z') || (ch>='0' && ch<='9');
} }
QSet<QString> GLSLSyntaxer::keywords() const QSet<QString> GLSLSyntaxer::keywords()
{ {
return Keywords; return Keywords;
} }

View File

@ -191,7 +191,7 @@ public:
// SynHighlighter interface // SynHighlighter interface
public: public:
QSet<QString> keywords() const override; QSet<QString> keywords() override;
// Highlighter interface // Highlighter interface
public: public:

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,184 @@
/*
* Copyright (C) 2020-2022 Roy Qu (royqh1979@gmail.com)
*
* 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 Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#ifndef QSYNEDIT_LUA_SYNTAXER_H
#define QSYNEDIT_LUA_SYNTAXER_H
#include "syntaxer.h"
#include <QSet>
#include <QMap>
namespace QSynedit {
class LuaSyntaxer: public Syntaxer
{
enum class TokenId {
Comment,
Identifier,
Key,
Null,
Number,
Space,
String,
StringEscapeSeq,
Symbol,
Unknown,
Float,
Hex,
HexFloat
};
enum RangeState {
rsUnknown,
rsComment,
rsLongComment,
rsString,
rsMultiLineDirective,
rsStringEscapeSeq,
rsSpace,
rsRawStringNotEscaping,
rsRawStringEnd,
rsLongString /* long string must be placed at end to record levels */
};
public:
explicit LuaSyntaxer();
LuaSyntaxer(const LuaSyntaxer&)=delete;
LuaSyntaxer operator=(const LuaSyntaxer&)=delete;
const PTokenAttribute &invalidAttribute() const;
const PTokenAttribute &numberAttribute() const;
const PTokenAttribute &floatAttribute() const;
const PTokenAttribute &hexAttribute() const;
const PTokenAttribute &stringEscapeSequenceAttribute() const;
const PTokenAttribute &charAttribute() const;
static const QSet<QString> Keywords;
static const QSet<QString> LibFunctions;
static const QMap<QString,QSet<QString>> LibTables;
TokenId getTokenId();
private:
void braceCloseProc();
void braceOpenProc();
void colonProc();
void commentProc();
void equalProc();
void greaterProc();
void identProc();
void longCommentProc();
void lowerProc();
void minusProc();
void nullProc();
void pointProc();
void processChar();
void roundCloseProc();
void roundOpenProc();
void slashProc();
void stringProc();
void tildeProc();
void numberProc();
void spaceProc();
void squareCloseProc();
void squareOpenProc();
// void stringEndProc();
void stringEscapeSeqProc();
void unknownProc();
void popIndents(IndentType indentType);
void pushIndents(IndentType indentType, int line=-1);
private:
bool mAsmStart;
SyntaxState mRange;
// SynRangeState mSpaceRange;
QString mLine;
int mLineSize;
int mRun;
int mStringLen;
int mToIdent;
int mTokenPos;
TokenId mTokenId;
int mLineNumber;
int mLeftBraces;
int mRightBraces;
QSet<QString> mCustomTypeKeywords;
QSet<QString> mKeywordsCache;
PTokenAttribute mInvalidAttribute;
PTokenAttribute mNumberAttribute;
PTokenAttribute mFloatAttribute;
PTokenAttribute mHexAttribute;
PTokenAttribute mStringEscapeSequenceAttribute;
PTokenAttribute mCharAttribute;
// SynHighligterBase interface
public:
bool getTokenFinished() const override;
bool isLastLineCommentNotFinished(int state) const override;
bool isLastLineStringNotFinished(int state) const override;
bool eol() const override;
QString getToken() const override;
const PTokenAttribute &getTokenAttribute() const override;
int getTokenPos() override;
void next() override;
void setLine(const QString &newLine, int lineNumber) override;
bool isKeyword(const QString &word) override;
void setState(const SyntaxState& rangeState) override;
void resetState() override;
QString languageName() override;
ProgrammingLanguage language() override;
// SynHighlighter interface
public:
SyntaxState getState() const override;
// SynHighlighter interface
public:
bool isIdentChar(const QChar &ch) const override;
// SynHighlighter interface
public:
QSet<QString> keywords() override;
// SynHighlighter interface
public:
QString foldString(QString startLine) override;
const QSet<QString> &customTypeKeywords() const;
void setCustomTypeKeywords(const QSet<QString> &newCustomTypeKeywords);
// Highlighter interface
public:
bool supportBraceLevel() override;
// Syntaxer interface
public:
QMap<QString, QSet<QString> > scopedKeywords() override;
};
}
#endif // SYNEDITCPPHIGHLIGHTER_H

View File

@ -686,7 +686,7 @@ void MakefileSyntaxer::resetState()
mHasTrailingSpaces = false; mHasTrailingSpaces = false;
} }
QSet<QString> MakefileSyntaxer::keywords() const QSet<QString> MakefileSyntaxer::keywords()
{ {
return Directives; return Directives;
} }

View File

@ -154,7 +154,7 @@ public:
bool isIdentChar(const QChar& ch) const override; bool isIdentChar(const QChar& ch) const override;
public: public:
QSet<QString> keywords() const override; QSet<QString> keywords() override;
}; };

View File

@ -93,11 +93,16 @@ void Syntaxer::nextToEol()
next(); next();
} }
QSet<QString> Syntaxer::keywords() const QSet<QString> Syntaxer::keywords()
{ {
return QSet<QString>(); return QSet<QString>();
} }
QMap<QString, QSet<QString> > Syntaxer::scopedKeywords()
{
return QMap<QString, QSet<QString> >();
}
QString Syntaxer::foldString(QString /*startLine*/) QString Syntaxer::foldString(QString /*startLine*/)
{ {
return " ... }"; return " ... }";

View File

@ -99,6 +99,7 @@ enum class ProgrammingLanguage {
CPP, CPP,
GLSL, GLSL,
Makefile, Makefile,
LUA,
Custom Custom
}; };
@ -169,7 +170,8 @@ public:
virtual void setState(const SyntaxState& rangeState) = 0; virtual void setState(const SyntaxState& rangeState) = 0;
virtual void setLine(const QString& newLine, int lineNumber) = 0; virtual void setLine(const QString& newLine, int lineNumber) = 0;
virtual void resetState() = 0; virtual void resetState() = 0;
virtual QSet<QString> keywords() const; virtual QSet<QString> keywords();
virtual QMap<QString,QSet<QString>> scopedKeywords();
virtual QString languageName() = 0; virtual QString languageName() = 0;
virtual ProgrammingLanguage language() = 0; virtual ProgrammingLanguage language() = 0;