Files
UnrealEngineUWP/Engine/Source/Runtime/Slate/Public/Framework/Text/SyntaxTokenizer.h
jack cai 9ccf699c97 Hlsl Syntax Highlighter:
+Created a custom syntax tokenizer for Hlsl Highlighter. The vanilla tokenizer does a greedy match, which can lead to cases like "FloatA" to be tokenized as "Float" and "A". The new tokenizer is more specific about when to check for a match
+Added ISyntaxTokenizer to be used as a based class for all syntax tokenizers

#jira UE-144112
#rb Halfdan.Ingvarsson
#fyi Jamie.Dale
#preflight skip

[CL 19380707 by jack cai in ue5-main branch]
2022-03-14 20:06:57 -04:00

80 lines
1.8 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
class SLATE_API ISyntaxTokenizer
{
public:
/** Denotes the type of token */
enum class ETokenType : uint8
{
/** Syntax token matching the tokenizing rules provided */
Syntax,
/** String token containing some literal text */
Literal,
};
/** A token referencing a range in the original text */
struct FToken
{
FToken(const ETokenType InType, const FTextRange& InRange)
: Type(InType)
, Range(InRange)
{
}
ETokenType Type;
FTextRange Range;
};
/** A line containing a series of tokens */
struct FTokenizedLine
{
FTextRange Range;
TArray<FToken> Tokens;
};
virtual ~ISyntaxTokenizer() {};
virtual void Process(TArray<FTokenizedLine>& OutTokenizedLines, const FString& Input) = 0;
};
/**
* Tokenize the text based upon the given rule set
*/
class SLATE_API FSyntaxTokenizer : public ISyntaxTokenizer
{
public:
/** Rule used to match syntax token types */
struct FRule
{
FRule(FString InMatchText)
: MatchText(MoveTemp(InMatchText))
{
}
FString MatchText;
};
/**
* Create a new tokenizer which will use the given rules to match syntax tokens
* @param InRules Rules to control the tokenizer, processed in-order so the most greedy matches must come first
*/
static TSharedRef< FSyntaxTokenizer > Create(TArray<FRule> InRules);
virtual ~FSyntaxTokenizer();
virtual void Process(TArray<FTokenizedLine>& OutTokenizedLines, const FString& Input) override;
private:
FSyntaxTokenizer(TArray<FRule> InRules);
void TokenizeLineRanges(const FString& Input, const TArray<FTextRange>& LineRanges, TArray<FTokenizedLine>& OutTokenizedLines);
/** Rules to control the tokenizer, processed in-order so the most greedy matches must come first */
TArray<FRule> Rules;
};