Commit e3a37c9c authored by Alexander A. Maly's avatar Alexander A. Maly

AStyling KumirAnalizer

parent 2d0be449
Pipeline #2520 failed with stages
in 2 minutes and 5 seconds
......@@ -7,6 +7,7 @@
class QMenu;
class QWidget;
class QIODevice;
namespace Shared
{
......
......@@ -9,6 +9,8 @@
#include <kumir2-libs/extensionsystem/pluginmanager.h>
#include <kumir2/actorinterface.h>
#include <QTextStream>
namespace KumirAnalizer
{
......
......@@ -8,6 +8,9 @@
#include <kumir2-libs/dataformats/ast.h>
#include <QVector>
class QDir;
namespace KumirAnalizer
{
......@@ -40,16 +43,24 @@ public:
bool isKnownLexem(const QString &lexem, int lineNo, int colNo, const QString &context) const;
public slots:
void setSourceDirName(const QString &dirName);
Shared::Analizer::TextAppend closingBracketSuggestion(int lineNo) const;
QStringList importModuleSuggestion(int lineNo) const;
QString sourceText() const;
std::string rawSourceData() const;
void setSourceText(const QString &text);
QList<Shared::Analizer::Suggestion> suggestAutoComplete(int lineNo, const QString &before, const QString &after) const;
Shared::Analizer::ApiHelpItem itemUnderCursor(const QString &text, int lineNo, int colNo, bool includeRightBound) const;
QList<Shared::Analizer::Suggestion> suggestAutoComplete(
int lineNo,
const QString &before,
const QString &after
) const;
Shared::Analizer::ApiHelpItem itemUnderCursor(
const QString &text,
int lineNo, int colNo,
bool includeRightBound
) const;
QList<Shared::Analizer::Error> errors() const;
......@@ -82,7 +93,6 @@ public slots:
private /*typedefs*/:
typedef AST::Data AST_Data;
typedef QList<AST::StatementPtr> *LAS;
enum CompilationStage {
......@@ -95,8 +105,8 @@ private /*typedefs*/:
TextStatementPtr end;
bool teacher;
inline ModuleStatementsBlock(): teacher(false) {}
inline operator bool() const
ModuleStatementsBlock(): teacher(false) {}
operator bool() const
{
return statements.size() > 0;
}
......@@ -104,14 +114,23 @@ private /*typedefs*/:
private /*methods*/:
const AST::AlgorithmPtr findAlgorhitmByLine(const AST::ModulePtr mod, int lineNo) const;
void createModuleFromActor_stage1(Shared::ActorInterface *actor, quint8 forcedId);
void createModuleFromActor_stage2(Shared::ActorInterface *actor);
QStringList gatherExtraTypeNames(const AST::ModulePtr currentModule) const;
const AST::AlgorithmPtr findAlgorhitmByLine(
const AST::ModulePtr mod,
int lineNo
) const;
void createModuleFromActor_stage1(
Shared::ActorInterface *actor,
quint8 forcedId
);
void createModuleFromActor_stage2(
Shared::ActorInterface *actor
);
QStringList gatherExtraTypeNames(
const AST::ModulePtr currentModule
) const;
void removeAllVariables(const AST::VariablePtr var);
......@@ -119,9 +138,6 @@ private /*methods*/:
void setHiddenText(const QString &text, int baseLineNo);
/** Find algorhitm in AST by real line number */
static AST::AlgorithmPtr findAlgorhitmByPos(AST::DataPtr data, int pos);
......@@ -136,14 +152,15 @@ private /*methods*/:
* @param OUT alg - algorhitm reference
* @return true on found, false if not found
*/
static bool findInstructionsBlock(AST::DataPtr data
, const QList<TextStatement *> _statements
, LAS &lst
, int &begin
, int &end
, AST::ModulePtr &mod
, AST::AlgorithmPtr &alg
static bool findInstructionsBlock(
AST::DataPtr data,
const QList<TextStatement *> _statements,
LAS &lst,
int &begin, int &end,
AST::ModulePtr &mod,
AST::AlgorithmPtr &alg
);
/** Find context of AST instructions list,
* containing provided lexem groups
* @param IN data - AST
......@@ -155,13 +172,14 @@ private /*methods*/:
* @param OUT alg - algorhitm reference
* @return true on found, false if not found
*/
static bool findInstructionsBlock(AST::DataPtr data
, const QList<TextStatement *> _statements
, int pos
, LAS &lst
, int &outPos
, AST::ModulePtr &mod
, AST::AlgorithmPtr &alg
static bool findInstructionsBlock(
AST::DataPtr data,
const QList<TextStatement *> _statements,
int pos,
LAS &lst,
int &outPos,
AST::ModulePtr &mod,
AST::AlgorithmPtr &alg
);
/** Find instruction in AST by source text position
......@@ -171,26 +189,23 @@ private /*methods*/:
* @param IN colNo - column number in source text line
* @return pointer to lexem group and lexem, or null pointers pair if not found
*/
static QPair<TextStatementPtr, AST::LexemPtr> findSourceLexemContext(AST::DataPtr data
, const QList<TextStatementPtr> _statements
, int lineNo
, int colNo
, bool includeRightBound
static QPair<TextStatementPtr, AST::LexemPtr> findSourceLexemContext(
AST::DataPtr data,
const QList<TextStatementPtr> _statements,
int lineNo, int colNo,
bool includeRightBound
);
void doCompilation(
QList<TextStatementPtr> &allStatements,
CompilationStage stage
);
void doCompilation(QList<TextStatementPtr> &allStatements, CompilationStage stage);
static QList<struct ModuleStatementsBlock> splitIntoModules(const QList<TextStatementPtr> &_statements);
static QList<struct ModuleStatementsBlock> splitIntoModules(
const QList<TextStatementPtr> &_statements
);
private /*fields*/:
std::vector<Shared::ActorInterface *> _builtinModules;
class Lexer *_lexer;
......@@ -207,13 +222,8 @@ private /*fields*/:
static QStringList _AlwaysAvailableModulesName;
static QLocale::Language _NativeLanguage;
bool _teacherMode;
class KumirAnalizerPlugin *_plugin;
};
} // namespace KumirAnalizer
......
......@@ -4,26 +4,24 @@
#include <kumir2-libs/errormessages/errormessages.h>
#include "lexer.h"
#include <QtCore>
#include "kumfilehandler.h"
#include "quickreferencewidget.h"
using namespace KumirAnalizer;
KumirAnalizerPlugin::KumirAnalizerPlugin()
: ExtensionSystem::KPlugin()
, teacherMode_(false)
, kumFileHandler_(new KumFileHandler(this))
, _quickReferenceWidget(NULL)
: ExtensionSystem::KPlugin()
, teacherMode_(false)
, kumFileHandler_(new KumFileHandler(this))
, _quickReferenceWidget(NULL)
{
analizers_ = QVector<Analizer*> (128, NULL);
analizers_ = QVector<Analizer *> (128, NULL);
}
Shared::Analizer::SourceFileInterface* KumirAnalizerPlugin::sourceFileHandler()
Shared::Analizer::SourceFileInterface *KumirAnalizerPlugin::sourceFileHandler()
{
return kumFileHandler_;
return kumFileHandler_;
}
......@@ -33,51 +31,50 @@ KumirAnalizerPlugin::~KumirAnalizerPlugin()
QWidget *KumirAnalizerPlugin::languageQuickReferenceWidget()
{
bool hasGui = true;
bool hasGui = true;
#ifdef Q_WS_X11
hasGui = getenv("DISPLAY")!=0;
hasGui = getenv("DISPLAY") != 0;
#endif
if (!_quickReferenceWidget && hasGui) {
_quickReferenceWidget = new QuickReferenceWidget(this);
}
return _quickReferenceWidget;
if (!_quickReferenceWidget && hasGui) {
_quickReferenceWidget = new QuickReferenceWidget(this);
}
return _quickReferenceWidget;
}
void KumirAnalizerPlugin::createPluginSpec()
{
_pluginSpec.name = "KumirAnalizer";
_pluginSpec.provides.append("Analizer");
_pluginSpec.gui = false;
_pluginSpec.name = "KumirAnalizer";
_pluginSpec.provides.append("Analizer");
_pluginSpec.gui = false;
}
QString KumirAnalizerPlugin::initialize(const QStringList &configurationArguments,
const ExtensionSystem::CommandLine &runtimeArguments)
const ExtensionSystem::CommandLine &runtimeArguments)
{
QLocale::Language language = QLocale::Russian;
QLocale::Language language = QLocale::Russian;
// Analizer::setModuleAlwaysAvailable(QString::fromUtf8("Стандартные функции"));
Q_FOREACH (const QString &arg, configurationArguments) {
if (arg.startsWith("language=")) {
const QString lang = arg.mid(9);
const QLocale loc(lang);
if (loc.language()!=QLocale::C) {
language = loc.language();
break;
}
}
else if (arg.startsWith("preload=")) {
const QByteArray modname = arg.mid(8).toLatin1();
Analizer::setModuleAlwaysAvailable(modname);
}
}
Analizer::setSourceLanguage(myResourcesDir(), language);
Shared::ErrorMessages::loadMessages("KumirAnalizer");
teacherMode_ = configurationArguments.contains("teacher", Qt::CaseInsensitive);
return "";
Q_FOREACH (const QString &arg, configurationArguments) {
if (arg.startsWith("language=")) {
const QString lang = arg.mid(9);
const QLocale loc(lang);
if (loc.language() != QLocale::C) {
language = loc.language();
break;
}
} else if (arg.startsWith("preload=")) {
const QByteArray modname = arg.mid(8).toLatin1();
Analizer::setModuleAlwaysAvailable(modname);
}
}
Analizer::setSourceLanguage(myResourcesDir(), language);
Shared::ErrorMessages::loadMessages("KumirAnalizer");
teacherMode_ = configurationArguments.contains("teacher", Qt::CaseInsensitive);
return "";
}
void KumirAnalizerPlugin::start()
......@@ -90,10 +87,10 @@ void KumirAnalizerPlugin::stop()
}
Shared::Analizer::InstanceInterface * KumirAnalizerPlugin::createInstance()
Shared::Analizer::InstanceInterface *KumirAnalizerPlugin::createInstance()
{
Analizer * instance = new Analizer(this, teacherMode_);
return instance;
Analizer *instance = new Analizer(this, teacherMode_);
return instance;
}
#if QT_VERSION < 0x050000
......
#include "lexer.h"
#include <kumir2-libs/errormessages/errormessages.h>
#include <QDir>
#include <QTextStream>
using namespace Shared;
......@@ -18,10 +20,10 @@ Lexer::~Lexer()
}
int Lexer::splitIntoStatements(
const QStringList &lines
, int baseLineNo
, QList<TextStatementPtr> &statements
, const QStringList &extraTypeNames
const QStringList &lines,
int baseLineNo,
QList<TextStatementPtr> &statements,
const QStringList &extraTypeNames
) const {
int errorsCount = 0;
for (int i = 0; i < lines.size(); i++) {
......
......@@ -4,6 +4,10 @@
#include "statement.h"
#include <kumir2-libs/dataformats/ast_variabletype.h>
#include <QSet>
#include <QLocale>
class QDir;
namespace KumirAnalizer
{
......
......@@ -5,6 +5,12 @@
#include <deque>
#include <algorithm>
#include <QDir>
#include <QFile>
#include <QTextStream>
#include <QBuffer>
#include <QDebug>
#define isTerminal(x) QRegExp(QString::fromUtf8("!?[a-zа-я][a-zа-я_,]*")).exactMatch(x)
#define isNeterminal(x) QRegExp(QString::fromUtf8("[А-ЯA-Z][А-ЯA-Z1-9_*]*")).exactMatch(x)
#define isNagruzka(x) ( x.startsWith("{") && x.endsWith("}") )
......
......@@ -7,6 +7,11 @@
#include <kumir2-libs/dataformats/ast_algorhitm.h>
#include <kumir2-libs/dataformats/ast_statement.h>
#include <QVector>
#include <QStack>
#include <QMetaMethod>
class QDir;
namespace KumirAnalizer
{
......
......@@ -13,164 +13,168 @@
#include <QMouseEvent>
#include <QResizeEvent>
#include <QVBoxLayout>
#include <QScrollBar>
#include <QPushButton>
#include <QDebug>
namespace KumirAnalizer {
namespace KumirAnalizer
{
QuickReferenceWidget::QuickReferenceWidget(KumirAnalizerPlugin * plugin)
: QWidget(0)
, _plugin(plugin)
, ui(new Ui::QuickReferenceWidget)
QuickReferenceWidget::QuickReferenceWidget(KumirAnalizerPlugin *plugin)
: QWidget(0)
, _plugin(plugin)
, ui(new Ui::QuickReferenceWidget)
{
this->setObjectName("quickReferenceWidget");
ui->setupUi(this);
this->setObjectName("quickReferenceWidget");
ui->setupUi(this);
ui->statementsContents->setLayout(new FlowLayout);
ui->typesContents->setLayout(new FlowLayout);
ui->functionsContents->setLayout(new FlowLayout);
ui->statementsContents->setLayout(new FlowLayout);
ui->typesContents->setLayout(new FlowLayout);
ui->functionsContents->setLayout(new FlowLayout);
_builtInModules.append(new StdLibModules::RTL);
if (Analizer::isModuleAlwaysEnabled(QString::fromUtf8("Файлы")))
_builtInModules.append(new StdLibModules::Files);
if (Analizer::isModuleAlwaysEnabled(QString::fromUtf8("Строки")))
_builtInModules.append(new StdLibModules::Strings);
_builtInModules.append(new StdLibModules::RTL);
if (Analizer::isModuleAlwaysEnabled(QString::fromUtf8("Файлы"))) {
_builtInModules.append(new StdLibModules::Files);
}
if (Analizer::isModuleAlwaysEnabled(QString::fromUtf8("Строки"))) {
_builtInModules.append(new StdLibModules::Strings);
}
createKeywordsList();
createTypesList();
createFunctionsList();
createKeywordsList();
createTypesList();
createFunctionsList();
installEventFilter(this);
installEventFilter(this);
relayoutBlocks(this->width());
reloadStyleSheet();
relayoutBlocks(this->width());
reloadStyleSheet();
}
void QuickReferenceWidget::handleButtonPressed()
{
QPushButton * btn = qobject_cast<QPushButton*>(sender());
const QString name = btn->text();
QWidget const * group = btn->parentWidget();
int topicNumber = -1;
if (group == ui->statementsContents)
topicNumber = StatementTopic;
else if (group == ui->typesContents)
topicNumber = TypeTopic;
else if (group == ui->functionsContents)
topicNumber = FunctionTopic;
Q_EMIT openTopicInDocumentation(topicNumber, name);
QPushButton *btn = qobject_cast<QPushButton *>(sender());
const QString name = btn->text();
QWidget const *group = btn->parentWidget();
int topicNumber = -1;
if (group == ui->statementsContents) {
topicNumber = StatementTopic;
} else if (group == ui->typesContents) {
topicNumber = TypeTopic;
} else if (group == ui->functionsContents) {
topicNumber = FunctionTopic;
}
Q_EMIT openTopicInDocumentation(topicNumber, name);
}
void QuickReferenceWidget::relayoutBlocks(int width)
{
Q_UNUSED(width);
Q_UNUSED(width);
}
bool QuickReferenceWidget::eventFilter(QObject *obj, QEvent *evt)
{
if (obj == this && QEvent::MouseButtonPress == evt->type()) {
QMouseEvent * event = static_cast<QMouseEvent*>(evt);
if (Qt::RightButton == event->button()) {
#ifndef QT_NO_DEBUG
reloadStyleSheet();
qDebug() << "Reload style sheet";
#endif
}
}
else if (obj == this && QEvent::Resize == evt->type()) {
QResizeEvent * event = static_cast<QResizeEvent*>(evt);
relayoutBlocks(event->size().width());
}
return false;
if (obj == this && QEvent::MouseButtonPress == evt->type()) {
QMouseEvent *event = static_cast<QMouseEvent *>(evt);
if (Qt::RightButton == event->button()) {
#ifndef QT_NO_DEBUG
reloadStyleSheet();
qDebug() << "Reload style sheet";
#endif
}
} else if (obj == this && QEvent::Resize == evt->type()) {
QResizeEvent *event = static_cast<QResizeEvent *>(evt);
relayoutBlocks(event->size().width());
}
return false;
}
void QuickReferenceWidget::reloadStyleSheet()
{
const QString cssFileName = _plugin->myResourcesDir().absoluteFilePath("quick_reference_page.css");
QFile cssFile(cssFileName);
if (cssFile.open(QIODevice::ReadOnly|QIODevice::Text)) {
const QString cssData = QString::fromUtf8(cssFile.readAll());
cssFile.close();
setStyleSheet(cssData);
}
const QString cssFileName = _plugin->myResourcesDir().absoluteFilePath("quick_reference_page.css");
QFile cssFile(cssFileName);
if (cssFile.open(QIODevice::ReadOnly | QIODevice::Text)) {
const QString cssData = QString::fromUtf8(cssFile.readAll());
cssFile.close();
setStyleSheet(cssData);
}
}
void QuickReferenceWidget::createKeywordsList()
{
static const char * items =
"использовать алг нач кон дано надо утв "
"нц кц кц_при для раз пока от до шаг выход "
"если то иначе все выбор при ввод вывод нс "
"не и или рез арг аргрез знач"
;
static const QStringList itemsList = QString::fromUtf8(items).split(" ");
Q_FOREACH(const QString & item, itemsList) {
QPushButton * btn = new QPushButton(ui->statementsContents);
connect(btn, SIGNAL(clicked()), this, SLOT(handleButtonPressed()));
btn->setText(item);
btn->setCursor(Qt::PointingHandCursor);
ui->statementsContents->layout()->addWidget(btn);
}
static const char *items =
"использовать алг нач кон дано надо утв "
"нц кц кц_при для раз пока от до шаг выход "
"если то иначе все выбор при ввод вывод нс "
"не и или рез арг аргрез знач"
;
static const QStringList itemsList = QString::fromUtf8(items).split(" ");
Q_FOREACH (const QString &item, itemsList) {
QPushButton *btn = new QPushButton(ui->statementsContents);
connect(btn, SIGNAL(clicked()), this, SLOT(handleButtonPressed()));
btn->setText(item);
btn->setCursor(Qt::PointingHandCursor);
ui->statementsContents->layout()->addWidget(btn);
}
}
void QuickReferenceWidget::createTypesList()
{
static const char * BasicTypes =
"цел вещ лог сим лит"
;
static const QStringList BasicTypesList = QString::fromUtf8(BasicTypes).split(" ");
QStringList itemsList;
Q_FOREACH(const QString & item, BasicTypesList) {
itemsList.append(item);
}
Q_FOREACH(Shared::ActorInterface * actor, _builtInModules) {
Q_FOREACH(Shared::ActorInterface::RecordSpecification type, actor->typeList()) {
const QString typeName =