diff --git a/src/backends/sage/CMakeLists.txt b/src/backends/sage/CMakeLists.txt index e5c89cc8..1f1f4eb9 100644 --- a/src/backends/sage/CMakeLists.txt +++ b/src/backends/sage/CMakeLists.txt @@ -1,35 +1,37 @@ set( SageBackend_SRCS sagebackend.cpp sagesession.cpp sageexpression.cpp sageextensions.cpp sagekeywords.cpp sagehighlighter.cpp sagecompletionobject.cpp ) kconfig_add_kcfg_files(SageBackend_SRCS settings.kcfgc) install(FILES sagebackend.kcfg DESTINATION ${KDE_INSTALL_KCFGDIR}) ki18n_wrap_ui(SageBackend_SRCS settings.ui) add_backend(sagebackend ${SageBackend_SRCS}) -target_link_libraries( cantor_sagebackend KF5::Pty) +target_link_libraries( cantor_sagebackend + KF5::Pty + KF5::SyntaxHighlighting) if(BUILD_TESTING) add_executable( testsage testsage.cpp) add_test(testsage testsage) target_link_libraries( testsage Qt5::Test cantorlibs cantortest ) endif() install(FILES cantor_sage.knsrc DESTINATION ${KDE_INSTALL_CONFDIR}) install( PROGRAMS cantor-execsage DESTINATION ${KDE_INSTALL_DATADIR}/cantor/sagebackend ) diff --git a/src/backends/sage/sagehighlighter.cpp b/src/backends/sage/sagehighlighter.cpp index fb85991d..a0525b3e 100644 --- a/src/backends/sage/sagehighlighter.cpp +++ b/src/backends/sage/sagehighlighter.cpp @@ -1,79 +1,33 @@ /* 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2009 Alexander Rieder */ #include "sagehighlighter.h" #include "sagekeywords.h" -#include - SageHighlighter::SageHighlighter(QObject* parent) : Cantor::DefaultHighlighter(parent) { addRule(QRegExp(QLatin1String("[A-Za-z0-9_]+(?=\\()")), functionFormat()); - QStringList keywords; - //Preprocessor - keywords = SageKeywords::instance()->keywords(); - //specialvars - keywords << QLatin1String("None") << QLatin1String("self") << QLatin1String("True") << QLatin1String("true") << QLatin1String("False") << QLatin1String("false") - << QLatin1String("NotImplemented") << QLatin1String("Ellipsis"); - addKeywords(keywords); - - QStringList builtinFunctions; - builtinFunctions << QLatin1String("__future__") << QLatin1String("__import__") << QLatin1String("__name__") << QLatin1String("abs") - << QLatin1String("all") << QLatin1String("any") << QLatin1String("apply") << QLatin1String("basestring") << QLatin1String("bool") - << QLatin1String("buffer") << QLatin1String("callable") << QLatin1String("chr") << QLatin1String("classmethod") - << QLatin1String("cmp") << QLatin1String("coerce") << QLatin1String("compile") << QLatin1String("complex") << QLatin1String("delattr") - << QLatin1String("dict") << QLatin1String("dir") << QLatin1String("divmod") << QLatin1String("enumerate") << QLatin1String("eval") - << QLatin1String("execfile") << QLatin1String("file") << QLatin1String("filter") << QLatin1String("float") << QLatin1String("frozenset") - << QLatin1String("getattr") << QLatin1String("globals") << QLatin1String("hasattr") << QLatin1String("hash") << QLatin1String("hex") - << QLatin1String("id") << QLatin1String("input") << QLatin1String("int") << QLatin1String("intern") << QLatin1String("isinstance") - << QLatin1String("issubclass") << QLatin1String("iter") << QLatin1String("len") << QLatin1String("list") << QLatin1String("locals") - << QLatin1String("long") << QLatin1String("map") << QLatin1String("max") << QLatin1String("min") << QLatin1String("object") << QLatin1String("oct") - << QLatin1String("open") << QLatin1String("ord") << QLatin1String("pow") << QLatin1String("property") << QLatin1String("range") - << QLatin1String("raw_input") << QLatin1String("reduce") << QLatin1String("reload") << QLatin1String("repr") << QLatin1String("reversed") - << QLatin1String("round") << QLatin1String("set") << QLatin1String("setattr") << QLatin1String("slice") << QLatin1String("sorted") << QLatin1String("staticmethod") - << QLatin1String("str") << QLatin1String("sum") << QLatin1String("super") << QLatin1String("tuple") << QLatin1String("type") << QLatin1String("unichr") - << QLatin1String("unicode") << QLatin1String("vars") << QLatin1String("xrange") << QLatin1String("zip"); - addRules(builtinFunctions, functionFormat()); - - addRule(QRegExp(QLatin1String("\\S*[a-zA-Z\\-\\_]+\\S*\\.(?!\\d)")), objectFormat()); - - QStringList exceptionPatterns; - exceptionPatterns<< QLatin1String("ArithmeticError") << QLatin1String("AssertionError") << QLatin1String("AttributeError") - << QLatin1String("BaseException") << QLatin1String("DeprecationWarning") << QLatin1String("EnvironmentError") - << QLatin1String("EOFError") << QLatin1String("Exception") << QLatin1String("FloatingPointError") - << QLatin1String("FutureWarning") << QLatin1String("GeneratorExit") << QLatin1String("IOError") - << QLatin1String("ImportError") << QLatin1String("ImportWarning") << QLatin1String("IndexError") - << QLatin1String("KeyError") << QLatin1String("KeyboardInterrupt") << QLatin1String("LookupError") - << QLatin1String("MemoryError") << QLatin1String("NameError") << QLatin1String("NotImplementedError") - << QLatin1String("OSError") << QLatin1String("OverflowError") << QLatin1String("PendingDeprecationWarning") - << QLatin1String("ReferenceError") << QLatin1String("RuntimeError") << QLatin1String("RuntimeWarning") - << QLatin1String("StandardError") << QLatin1String("StopIteration") << QLatin1String("SyntaxError") - << QLatin1String("SyntaxWarning") << QLatin1String("SystemError") << QLatin1String("SystemExit") - << QLatin1String("TypeError") << QLatin1String("UnboundLocalError") << QLatin1String("UserWarning") - << QLatin1String("UnicodeError") << QLatin1String("UnicodeWarning") << QLatin1String("UnicodeEncodeError") - << QLatin1String("UnicodeDecodeError") << QLatin1String("UnicodeTranslateError") << QLatin1String("ValueError") - << QLatin1String("Warning") << QLatin1String("WindowsError") << QLatin1String("ZeroDivisionError"); - addRules(exceptionPatterns, objectFormat()); + addKeywords(SageKeywords::instance()->keywords()); + addFunctions(SageKeywords::instance()->functions()); + addVariables(SageKeywords::instance()->variables()); - addRule(QRegExp(QLatin1String("\"[^\"]*\"")), stringFormat()); - addRule(QRegExp(QLatin1String("'[^']*'")), stringFormat()); addRule(QRegExp(QLatin1String("#[^\n]*")), commentFormat()); } diff --git a/src/backends/sage/sagekeywords.cpp b/src/backends/sage/sagekeywords.cpp index 11e89dc5..685c3e4d 100644 --- a/src/backends/sage/sagekeywords.cpp +++ b/src/backends/sage/sagekeywords.cpp @@ -1,76 +1,68 @@ /* 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 2 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, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. --- Copyright (C) 2012 Martin Kuettler */ #include "sagekeywords.h" +#include +#include + #include SageKeywords* SageKeywords::instance() { static SageKeywords* inst=nullptr; if(inst==nullptr) { inst = new SageKeywords(); inst->loadKeywords(); } return inst; } void SageKeywords::loadKeywords() { - //Begin m_keywords initialization - m_keywords << QLatin1String("and"); - m_keywords << QLatin1String("as"); - m_keywords << QLatin1String("assert"); - m_keywords << QLatin1String("break"); - m_keywords << QLatin1String("class"); - m_keywords << QLatin1String("continue"); - m_keywords << QLatin1String("def"); - m_keywords << QLatin1String("del"); - m_keywords << QLatin1String("elif"); - m_keywords << QLatin1String("else"); - m_keywords << QLatin1String("except"); - m_keywords << QLatin1String("exec"); - m_keywords << QLatin1String("finally"); - m_keywords << QLatin1String("for"); - m_keywords << QLatin1String("from"); - m_keywords << QLatin1String("global"); - m_keywords << QLatin1String("if"); - m_keywords << QLatin1String("import"); - m_keywords << QLatin1String("in"); - m_keywords << QLatin1String("is"); - m_keywords << QLatin1String("lambda"); - m_keywords << QLatin1String("not"); - m_keywords << QLatin1String("or"); - m_keywords << QLatin1String("pass"); - m_keywords << QLatin1String("print"); - m_keywords << QLatin1String("raise"); - m_keywords << QLatin1String("return"); - m_keywords << QLatin1String("try"); - m_keywords << QLatin1String("while"); - m_keywords << QLatin1String("with"); - m_keywords << QLatin1String("yield"); - //Finish m_keywords initialization + KSyntaxHighlighting::Repository m_repository; + KSyntaxHighlighting::Definition definition = m_repository.definitionForName(QLatin1String("Python")); + + m_keywords << definition.keywordList(QLatin1String("import")); + m_keywords << definition.keywordList(QLatin1String("defs")); + m_keywords << definition.keywordList(QLatin1String("operators")); + m_keywords << definition.keywordList(QLatin1String("flow")); + + m_functions << definition.keywordList(QLatin1String("builtinfuncs")); + m_functions << definition.keywordList(QLatin1String("overloaders")); + + m_variables << definition.keywordList(QLatin1String("specialvars")); } const QStringList& SageKeywords::keywords() const { return m_keywords; } + +const QStringList& SageKeywords::functions() const +{ + return m_functions; +} + +const QStringList& SageKeywords::variables() const +{ + return m_variables; +} diff --git a/src/backends/sage/sagekeywords.h b/src/backends/sage/sagekeywords.h index 95587470..229114ca 100644 --- a/src/backends/sage/sagekeywords.h +++ b/src/backends/sage/sagekeywords.h @@ -1,52 +1,54 @@ /* Copyright 2012 Martin Kuettler 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 2 of the License or (at your option) version 3 or any later version accepted by the membership of KDE e.V. (or its successor approved by the membership of KDE e.V.), which shall act as a proxy defined in Section 14 of version 3 of the license. 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 . */ #ifndef _SAGEKEYWORDS_H #define _SAGEKEYWORDS_H #include /** Class to store all Sage keywords (i.e. Python keywords) It is similar to MaximaKeywords or ScilabKeywords, but for Sage we only need to store actual keywords, as variables and functions can be fetched from the backend. */ class SageKeywords { private: SageKeywords() = default; ~SageKeywords() = default; public: static SageKeywords* instance(); const QStringList& keywords() const; - //const QStringList& functions() const; - //const QStringList& variables() const; + const QStringList& functions() const; + const QStringList& variables() const; private: void loadKeywords(); QStringList m_keywords; + QStringList m_functions; + QStringList m_variables; }; #endif /* _SAGEKEYWORDS_H */