TextMateLib 1.0
Modern C++ implementation of the TextMate syntax highlighting engine
Loading...
Searching...
No Matches
encodedTokenAttributes.cpp
1#include "encodedTokenAttributes.h"
2#include <iostream>
3#include <bitset>
4#include <sstream>
5
6namespace tml {
7
8namespace EncodedTokenAttributesHelper {
9
10std::string toBinaryStr(EncodedTokenAttributes encodedTokenAttributes) {
11 std::bitset<32> bits(encodedTokenAttributes);
12 return bits.to_string();
13}
14
15void print(EncodedTokenAttributes encodedTokenAttributes) {
16 int languageId = getLanguageId(encodedTokenAttributes);
17 StandardTokenType tokenType = getTokenType(encodedTokenAttributes);
18 int fontStyle = getFontStyle(encodedTokenAttributes);
19 int foreground = getForeground(encodedTokenAttributes);
20 int background = getBackground(encodedTokenAttributes);
21}
22
23int getLanguageId(EncodedTokenAttributes encodedTokenAttributes) {
24 return (encodedTokenAttributes & EncodedTokenDataConsts::LANGUAGEID_MASK) >>
25 EncodedTokenDataConsts::LANGUAGEID_OFFSET;
26}
27
28StandardTokenType getTokenType(EncodedTokenAttributes encodedTokenAttributes) {
29 return static_cast<StandardTokenType>(
30 (encodedTokenAttributes & EncodedTokenDataConsts::TOKEN_TYPE_MASK) >>
31 EncodedTokenDataConsts::TOKEN_TYPE_OFFSET
32 );
33}
34
35bool containsBalancedBrackets(EncodedTokenAttributes encodedTokenAttributes) {
36 return (encodedTokenAttributes & EncodedTokenDataConsts::BALANCED_BRACKETS_MASK) != 0;
37}
38
39int getFontStyle(EncodedTokenAttributes encodedTokenAttributes) {
40 return (encodedTokenAttributes & EncodedTokenDataConsts::FONT_STYLE_MASK) >>
41 EncodedTokenDataConsts::FONT_STYLE_OFFSET;
42}
43
44int getForeground(EncodedTokenAttributes encodedTokenAttributes) {
45 return (encodedTokenAttributes & EncodedTokenDataConsts::FOREGROUND_MASK) >>
46 EncodedTokenDataConsts::FOREGROUND_OFFSET;
47}
48
49int getBackground(EncodedTokenAttributes encodedTokenAttributes) {
50 return (static_cast<uint32_t>(encodedTokenAttributes) &
51 static_cast<uint32_t>(EncodedTokenDataConsts::BACKGROUND_MASK)) >>
52 EncodedTokenDataConsts::BACKGROUND_OFFSET;
53}
54
56 EncodedTokenAttributes encodedTokenAttributes,
57 int languageId,
59 bool* containsBalancedBracketsParam,
60 int fontStyle,
61 int foreground,
62 int background
63) {
64 int _languageId = getLanguageId(encodedTokenAttributes);
65 int _tokenType = static_cast<int>(getTokenType(encodedTokenAttributes));
66 int _containsBalancedBracketsBit = containsBalancedBrackets(encodedTokenAttributes) ? 1 : 0;
67 int _fontStyle = getFontStyle(encodedTokenAttributes);
68 int _foreground = getForeground(encodedTokenAttributes);
69 int _background = getBackground(encodedTokenAttributes);
70
71 if (languageId != 0) {
72 _languageId = languageId;
73 }
74 if (tokenType != OptionalStandardTokenType::NotSet) {
75 _tokenType = static_cast<int>(fromOptionalTokenType(tokenType));
76 }
77 if (containsBalancedBracketsParam != nullptr) {
78 _containsBalancedBracketsBit = *containsBalancedBracketsParam ? 1 : 0;
79 }
80 if (fontStyle != static_cast<int>(FontStyle::NotSet)) {
81 _fontStyle = fontStyle;
82 }
83 if (foreground != 0) {
84 _foreground = foreground;
85 }
86 if (background != 0) {
87 _background = background;
88 }
89
90 return static_cast<EncodedTokenAttributes>(
91 ((_languageId << EncodedTokenDataConsts::LANGUAGEID_OFFSET) |
92 (_tokenType << EncodedTokenDataConsts::TOKEN_TYPE_OFFSET) |
93 (_containsBalancedBracketsBit << EncodedTokenDataConsts::BALANCED_BRACKETS_OFFSET) |
94 (_fontStyle << EncodedTokenDataConsts::FONT_STYLE_OFFSET) |
95 (_foreground << EncodedTokenDataConsts::FOREGROUND_OFFSET) |
96 (_background << EncodedTokenDataConsts::BACKGROUND_OFFSET))
97 );
98}
99
100} // namespace EncodedTokenAttributesHelper
101
102} // namespace tml
@ NotSet
Styling not specified (inherit or use default)
StandardTokenType
Standard TextMate token type for syntax classification.
Definition types.h:136
OptionalStandardTokenType
Standard token type with optional (unknown) state.
Definition types.h:145
@ NotSet
Type not determined or not applicable.
int32_t EncodedTokenAttributes
Compact 32-bit encoding of a token's attributes.
Definition types.h:128