Skip to content

Commit

Permalink
Traktor: Support for block comments in syntax highlighting.
Browse files Browse the repository at this point in the history
  • Loading branch information
apistol78 committed Apr 11, 2024
1 parent 6150e29 commit be622b1
Show file tree
Hide file tree
Showing 12 changed files with 210 additions and 98 deletions.
7 changes: 2 additions & 5 deletions code/Ui/SyntaxRichEdit/SyntaxLanguage.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Ui/SyntaxRichEdit/SyntaxLanguage.h"

namespace traktor
namespace traktor::ui
{
namespace ui
{

T_IMPLEMENT_RTTI_CLASS(L"traktor.ui.SyntaxLanguage", SyntaxLanguage, Object)

}
}
20 changes: 12 additions & 8 deletions code/Ui/SyntaxRichEdit/SyntaxLanguage.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -10,6 +10,7 @@

#include <list>
#include "Core/Object.h"
#include "Core/Ref.h"
#include "Ui/SyntaxRichEdit/SyntaxTypes.h"

// import/export mechanism.
Expand All @@ -20,10 +21,8 @@
# define T_DLLCLASS T_DLLIMPORT
#endif

namespace traktor
namespace traktor::ui
{
namespace ui
{

/*! Syntax highlight language.
* \ingroup UI
Expand All @@ -40,26 +39,33 @@ class T_DLLCLASS SyntaxLanguage : public Object
StString,
StNumber,
StSelf,
StComment,
StLineComment,
StBlockComment,
StFunction,
StType,
StKeyword,
StSpecial,
StPreprocessor
};

class IContext : public IRefCount {};

/*! Return line comment token.
*/
virtual std::wstring lineComment() const = 0;

/*! Create consume context. */
virtual Ref< SyntaxLanguage::IContext > createContext() const = 0;

/*! Consume line text.
*
* \param context Consume context.
* \param text Single line of text, same line will continue until fully consumed.
* \param outState Output text state.
* \param outConsumedChars Number of consumed characters.
* \return True if successful.
*/
virtual bool consume(const std::wstring& text, State& outState, int& outConsumedChars) const = 0;
virtual bool consume(SyntaxLanguage::IContext* context, const std::wstring& text, State& outState, int& outConsumedChars) const = 0;

/*! Extract code outline.
*
Expand All @@ -70,6 +76,4 @@ class T_DLLCLASS SyntaxLanguage : public Object
virtual void outline(int32_t line, const std::wstring& text, std::list< SyntaxOutline >& outOutline) const = 0;
};

}
}

81 changes: 66 additions & 15 deletions code/Ui/SyntaxRichEdit/SyntaxLanguageGlsl.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022-2023 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand Down Expand Up @@ -332,6 +332,26 @@ const wchar_t* c_glslTypes[] =
nullptr
};

class SyntaxLanguageContextGlsl: public RefCountImpl< SyntaxLanguage::IContext >
{
public:
bool m_blockComment = false;
};

bool match(const std::wstring_view& text, const std::wstring_view& patt)
{
if (text.length() < patt.length())
return false;

for (size_t i = 0; i < patt.length(); ++i)
{
if (text[i] != patt[i])
return false;
}

return true;
}

}

T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.ui.SyntaxLanguageGlsl", 0, SyntaxLanguageGlsl, SyntaxLanguage)
Expand All @@ -341,26 +361,57 @@ std::wstring SyntaxLanguageGlsl::lineComment() const
return L"//";
}

bool SyntaxLanguageGlsl::consume(const std::wstring& text, State& outState, int& outConsumedChars) const
Ref< SyntaxLanguage::IContext > SyntaxLanguageGlsl::createContext() const
{
return new SyntaxLanguageContextGlsl();
}

bool SyntaxLanguageGlsl::consume(SyntaxLanguage::IContext* context, const std::wstring& text, State& outState, int& outConsumedChars) const
{
int ln = int(text.length());
SyntaxLanguageContextGlsl* cx = static_cast< SyntaxLanguageContextGlsl* >(context);
int32_t ln = (int32_t)text.length();
T_ASSERT(ln > 0);

// Line comment.
if (ln >= 2)
if (cx->m_blockComment)
{
if (text[0] == L'/' && text[1] == L'/')
if (match(text, L"*/"))
{
outState = StComment;
cx->m_blockComment = false;

outState = StBlockComment;
outConsumedChars = 2;
for (int i = 2; i < ln; ++i)
{
if (text[i] == L'\n' || text[i] == L'\r')
break;
++outConsumedChars;
}
return true;
}
else
{
outState = StBlockComment;
outConsumedChars = 1;
return true;
}
}

// Line comment.
if (match(text, L"//"))
{
outState = StLineComment;
outConsumedChars = 2;
for (int32_t i = 2; i < ln; ++i)
{
if (text[i] == L'\n' || text[i] == L'\r')
break;
++outConsumedChars;
}
return true;
}

// Block comment.
if (match(text, L"/*"))
{
cx->m_blockComment = true;

outState = StBlockComment;
outConsumedChars = 2;
return true;
}

// String
Expand All @@ -369,7 +420,7 @@ bool SyntaxLanguageGlsl::consume(const std::wstring& text, State& outState, int&
outState = StString;
outConsumedChars = 1;

for (int i = 1; i < ln; ++i)
for (int32_t i = 1; i < ln; ++i)
{
++outConsumedChars;
if (text[i] == L'\"')
Expand All @@ -385,7 +436,7 @@ bool SyntaxLanguageGlsl::consume(const std::wstring& text, State& outState, int&
outState = StNumber;
outConsumedChars = 1;

int i = 1;
int32_t i = 1;

// Integer or float.
for (; i < ln && text[i] >= L'0' && text[i] <= L'9'; ++i)
Expand Down
12 changes: 5 additions & 7 deletions code/Ui/SyntaxRichEdit/SyntaxLanguageGlsl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -18,10 +18,8 @@
# define T_DLLCLASS T_DLLIMPORT
#endif

namespace traktor
namespace traktor::ui
{
namespace ui
{

/*! Syntax highlight GLSL language.
* \ingroup UI
Expand All @@ -33,11 +31,11 @@ class T_DLLCLASS SyntaxLanguageGlsl : public SyntaxLanguage
public:
virtual std::wstring lineComment() const override final;

virtual bool consume(const std::wstring& text, State& outState, int& outConsumedChars) const override final;
virtual Ref< SyntaxLanguage::IContext > createContext() const override final;

virtual bool consume(SyntaxLanguage::IContext* context, const std::wstring& text, State& outState, int& outConsumedChars) const override final;

virtual void outline(int32_t line, const std::wstring& text, std::list< SyntaxOutline >& outOutline) const override final;
};

}
}

16 changes: 9 additions & 7 deletions code/Ui/SyntaxRichEdit/SyntaxLanguageHlsl.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Ui/SyntaxRichEdit/SyntaxLanguageHlsl.h"

namespace traktor
namespace traktor::ui
{
namespace ui
{

T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.ui.SyntaxLanguageHlsl", 0, SyntaxLanguageHlsl, SyntaxLanguage)

Expand All @@ -20,7 +18,12 @@ std::wstring SyntaxLanguageHlsl::lineComment() const
return L"//";
}

bool SyntaxLanguageHlsl::consume(const std::wstring& text, State& outState, int& outConsumedChars) const
Ref< SyntaxLanguage::IContext > SyntaxLanguageHlsl::createContext() const
{
return nullptr;
}

bool SyntaxLanguageHlsl::consume(SyntaxLanguage::IContext* context, const std::wstring& text, State& outState, int& outConsumedChars) const
{
int ln = int(text.length());
T_ASSERT(ln > 0);
Expand All @@ -30,7 +33,7 @@ bool SyntaxLanguageHlsl::consume(const std::wstring& text, State& outState, int&
{
if (text[0] == L'/' && text[1] == L'/')
{
outState = StComment;
outState = StLineComment;
outConsumedChars = 2;
for (int i = 2; i < ln; ++i)
{
Expand Down Expand Up @@ -250,5 +253,4 @@ void SyntaxLanguageHlsl::outline(int32_t line, const std::wstring& text, std::li
{
}

}
}
12 changes: 5 additions & 7 deletions code/Ui/SyntaxRichEdit/SyntaxLanguageHlsl.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
Expand All @@ -18,10 +18,8 @@
# define T_DLLCLASS T_DLLIMPORT
#endif

namespace traktor
namespace traktor::ui
{
namespace ui
{

/*! Syntax highlight HLSL or CG language.
* \ingroup UI
Expand All @@ -33,11 +31,11 @@ class T_DLLCLASS SyntaxLanguageHlsl : public SyntaxLanguage
public:
virtual std::wstring lineComment() const override final;

virtual bool consume(const std::wstring& text, State& outState, int& outConsumedChars) const override final;
virtual Ref< SyntaxLanguage::IContext > createContext() const override final;

virtual bool consume(SyntaxLanguage::IContext* context, const std::wstring& text, State& outState, int& outConsumedChars) const override final;

virtual void outline(int32_t line, const std::wstring& text, std::list< SyntaxOutline >& outOutline) const override final;
};

}
}

16 changes: 9 additions & 7 deletions code/Ui/SyntaxRichEdit/SyntaxLanguageJs.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
/*
* TRAKTOR
* Copyright (c) 2022 Anders Pistol.
* Copyright (c) 2022-2024 Anders Pistol.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "Ui/SyntaxRichEdit/SyntaxLanguageJs.h"

namespace traktor
namespace traktor::ui
{
namespace ui
{

T_IMPLEMENT_RTTI_FACTORY_CLASS(L"traktor.ui.SyntaxLanguageJs", 0, SyntaxLanguageJs, SyntaxLanguage)

Expand All @@ -20,7 +18,12 @@ std::wstring SyntaxLanguageJs::lineComment() const
return L"//";
}

bool SyntaxLanguageJs::consume(const std::wstring& text, State& outState, int& outConsumedChars) const
Ref< SyntaxLanguage::IContext > SyntaxLanguageJs::createContext() const
{
return nullptr;
}

bool SyntaxLanguageJs::consume(SyntaxLanguage::IContext* context, const std::wstring& text, State& outState, int& outConsumedChars) const
{
int ln = int(text.length());
T_ASSERT(ln > 0);
Expand All @@ -30,7 +33,7 @@ bool SyntaxLanguageJs::consume(const std::wstring& text, State& outState, int& o
{
if (text[0] == L'/' && text[1] == L'/')
{
outState = StComment;
outState = StLineComment;
outConsumedChars = 2;
for (int i = 2; i < ln; ++i)
{
Expand Down Expand Up @@ -119,5 +122,4 @@ void SyntaxLanguageJs::outline(int32_t line, const std::wstring& text, std::list
{
}

}
}
Loading

0 comments on commit be622b1

Please sign in to comment.