- 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 "Hello GAS" and "GAS and C" templates for linux and win64.
- fix: Wrong selection position after delete in column mode.
- enhancement: Syntaxer for lua.
Red Panda C++ Version 2.11

View File

@ -942,6 +942,14 @@ void Editor::keyPressEvent(QKeyEvent *event)
handled=true;
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) {
if ((mLastIdCharPressed==0) && (ch=='.')) {
mLastIdCharPressed++;
@ -3232,8 +3240,11 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete, CodeComple
(attr->tokenType() == QSynedit::TokenType::String) &&
(attr->tokenType() != QSynedit::TokenType::Character)) {
return;
} else if (type==CodeCompletionType::KeywordsOnly && syntaxer() && syntaxer()->language()==QSynedit::ProgrammingLanguage::ATTAssembly) {
word = getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpATTASMKeywords);
} else if (type==CodeCompletionType::KeywordsOnly && syntaxer() ) {
if (syntaxer()->language()==QSynedit::ProgrammingLanguage::ATTAssembly)
word = getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpATTASMKeywords);
else
word = getWordAtPosition(this,caretXY(),pBeginPos,pEndPos, WordPurpose::wpKeywords);
} else if (
(attr->tokenType() != QSynedit::TokenType::Operator) &&
(attr->tokenType() != QSynedit::TokenType::Space) &&
@ -3295,8 +3306,16 @@ void Editor::showCompletion(const QString& preWord,bool autoComplete, CodeComple
keywords = QSynedit::ASMSyntaxer::ATTRegisters;
else
keywords = QSynedit::ASMSyntaxer::Instructions;
} else
keywords = syntaxer()->keywords();
} 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
keywords = syntaxer()->keywords();
}
} else {
if (mUseCppSyntax) {
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 #
if (purpose == Editor::WordPurpose::wpDirective) {
while ((wordBegin >= 0) && (wordBegin < len)) {

View File

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

View File

@ -208,7 +208,6 @@ MainWindow::MainWindow(QWidget *parent)
e.reason());
}
// ui->actionIndent->setShortcut(Qt::Key_Tab);
// 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"));
}

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -197,7 +197,7 @@ public:
// SynHighlighter interface
public:
QSet<QString> keywords() const override;
QSet<QString> keywords() override;
// SynHighlighter interface
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');
}
QSet<QString> GLSLSyntaxer::keywords() const
QSet<QString> GLSLSyntaxer::keywords()
{
return Keywords;
}

View File

@ -191,7 +191,7 @@ public:
// SynHighlighter interface
public:
QSet<QString> keywords() const override;
QSet<QString> keywords() override;
// Highlighter interface
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;
}
QSet<QString> MakefileSyntaxer::keywords() const
QSet<QString> MakefileSyntaxer::keywords()
{
return Directives;
}

View File

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

View File

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

View File

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