Skip to content

Commit 18abd22

Browse files
committed
YQL-20496: Fix highlighting again
Fixed [issues](https://nda.ya.ru/t/e98IQkef7Mhm96): 1. Types had higher precedence than UDF and so `Yson::Parse` was badly highlighted. So function unit was split to let types to highlight `Decimal(...)` correctly, as well as UDFs. 2. There was a hack in a Monaco generator (probably I forgot to fix it) with hardcoded rules for embedded language, so JSON rule was forgotted to be deleted. commit_hash:b24527891525dad65bfbafbc209fd3cb7f087f32
1 parent 5c1bc00 commit 18abd22

File tree

6 files changed

+49
-44
lines changed

6 files changed

+49
-44
lines changed

yql/essentials/sql/v1/highlight/sql_highlight.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,19 @@ TUnit MakeUnit<EUnitKind::TypeIdentifier>(Syntax& s) {
140140
};
141141
}
142142

143-
template <>
144-
TUnit MakeUnit<EUnitKind::FunctionIdentifier>(Syntax& s) {
143+
TUnit MakeUnitUDF(Syntax& s) {
145144
return {
146145
.Kind = EUnitKind::FunctionIdentifier,
147146
.Patterns = {
148147
{s.Concat({"ID_PLAIN", "NAMESPACE", "ID_PLAIN"})},
148+
},
149+
};
150+
}
151+
152+
TUnit MakeUnitBuiltin(Syntax& s) {
153+
return {
154+
.Kind = EUnitKind::FunctionIdentifier,
155+
.Patterns = {
149156
{s.Get("ID_PLAIN"), s.Get("LPAREN")},
150157
},
151158
};
@@ -242,8 +249,9 @@ THighlighting MakeHighlighting(const NSQLReflect::TLexerGrammar& grammar) {
242249
h.Units.emplace_back(MakeUnit<EUnitKind::Comment>(s));
243250
h.Units.emplace_back(MakeUnit<EUnitKind::Punctuation>(s));
244251
h.Units.emplace_back(MakeUnit<EUnitKind::OptionIdentifier>(s));
252+
h.Units.emplace_back(MakeUnitUDF(s));
245253
h.Units.emplace_back(MakeUnit<EUnitKind::TypeIdentifier>(s));
246-
h.Units.emplace_back(MakeUnit<EUnitKind::FunctionIdentifier>(s));
254+
h.Units.emplace_back(MakeUnitBuiltin(s));
247255
h.Units.emplace_back(MakeUnit<EUnitKind::Literal>(s));
248256
h.Units.emplace_back(MakeUnit<EUnitKind::Keyword>(s));
249257
h.Units.emplace_back(MakeUnit<EUnitKind::QuotedIdentifier>(s));

yql/essentials/sql/v1/highlight/ut/suite.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@
5353
["Math::Sin", "FFFFFFFFF"],
5454
["Math", "IIII"],
5555
["Math::", "IIII#PP"],
56-
["::Sin", "PP#III"]
56+
["::Sin", "PP#III"],
57+
["Yson::Parse", "FFFFFFFFFFF"]
5758
],
5859
"SQL": [
5960
["SELECT id, alias from users", "KKKKKK#_#II#P#_#IIIII#_#KKKK#_#IIIII"],

yql/essentials/tools/yql_highlight/generator_monarch.cpp

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#include "highlighting.h"
44

5+
#include <yql/essentials/utils/yql_panic.h>
6+
57
#include <contrib/libs/re2/re2/re2.h>
68

79
#include <library/cpp/json/json_writer.h>
@@ -139,37 +141,12 @@ NJson::TJsonValue ToMonarchMultiLineState(const TUnit& unit, const TRangePattern
139141

140142
NJson::TJsonValue json;
141143
if (unit.Kind == EUnitKind::StringLiteral) {
142-
json.AppendValue(NJson::TJsonArray{
143-
"#py",
144-
NJson::TJsonMap{
145-
{"token", "string.python"},
146-
{"nextEmbedded", "python"},
147-
{"next", "@embedded"},
148-
{"goBack", 3},
149-
},
150-
});
151-
json.AppendValue(NJson::TJsonArray{
152-
"\\/\\/js",
153-
NJson::TJsonMap{
154-
{"token", "string.js"},
155-
{"nextEmbedded", "javascript"},
156-
{"next", "@embedded"},
157-
{"goBack", 4},
158-
},
159-
});
160-
json.AppendValue(NJson::TJsonArray{
161-
"{",
162-
NJson::TJsonMap{
163-
{"token", "string.js"},
164-
{"nextEmbedded", "javascript"},
165-
{"next", "@embedded"},
166-
{"goBack", 1},
167-
},
168-
});
169-
if (auto embedded = EmbeddedLanguage(pattern)) {
170-
json.AppendValue(NJson::TJsonArray{
171-
"[^" + begin + "]",
172-
*embedded});
144+
for (const auto& range : unit.RangePatterns) {
145+
if (auto embedded = EmbeddedLanguage(range)) {
146+
YQL_ENSURE(range.BeginPlain.StartsWith("@@"));
147+
TString tag = RE2::QuoteMeta(range.BeginPlain.substr(2));
148+
json.AppendValue(NJson::TJsonArray{std::move(tag), *embedded});
149+
}
173150
}
174151
if (escape) {
175152
json.AppendValue(NJson::TJsonArray{*escape, group + ".escape"});

yql/essentials/tools/yql_highlight/generator_textmate.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <contrib/libs/re2/re2/re2.h>
66

7+
#include <library/cpp/iterator/enumerate.h>
78
#include <library/cpp/json/json_value.h>
89
#include <library/cpp/json/json_writer.h>
910
#include <library/cpp/on_disk/tar_archive/archive_writer.h>
@@ -100,13 +101,13 @@ TString ToTextMateGroup(EUnitKind kind) {
100101
}
101102
}
102103

103-
TString ToTextMateName(EUnitKind kind) {
104-
return ToString(kind);
104+
TString ToTextMateName(EUnitKind kind, size_t index) {
105+
return ToString(kind) + ToString(index);
105106
}
106107

107-
NTextMate::TMatcher TextMateMultilinePattern(const TUnit& unit, const TRangePattern& range) {
108+
NTextMate::TMatcher TextMateMultilinePattern(const TUnit& unit, size_t index, const TRangePattern& range) {
108109
return NTextMate::TMatcher{
109-
.Name = ToTextMateName(unit.Kind),
110+
.Name = ToTextMateName(unit.Kind, index),
110111
.Group = ToTextMateGroup(unit.Kind),
111112
.Pattern = NTextMate::TRange{
112113
.Begin = RE2::QuoteMeta(range.BeginPlain),
@@ -116,9 +117,9 @@ NTextMate::TMatcher TextMateMultilinePattern(const TUnit& unit, const TRangePatt
116117
};
117118
}
118119

119-
NTextMate::TMatcher ToTextMatePattern(const TUnit& unit, const NSQLTranslationV1::TRegexPattern& pattern) {
120+
NTextMate::TMatcher ToTextMatePattern(const TUnit& unit, size_t index, const NSQLTranslationV1::TRegexPattern& pattern) {
120121
return NTextMate::TMatcher{
121-
.Name = ToTextMateName(unit.Kind),
122+
.Name = ToTextMateName(unit.Kind, index),
122123
.Group = ToTextMateGroup(unit.Kind),
123124
.Pattern = ToTextMateRegex(unit, pattern),
124125
};
@@ -133,16 +134,16 @@ NTextMate::TLanguage ToTextMateLanguage(const THighlighting& highlighting) {
133134
.FileType = highlighting.Extension,
134135
};
135136

136-
for (const TUnit& unit : highlighting.Units) {
137+
for (const auto& [index, unit] : Enumerate(highlighting.Units)) {
137138
if (unit.IsCodeGenExcluded) {
138139
continue;
139140
}
140141

141142
for (const NSQLTranslationV1::TRegexPattern& pattern : unit.Patterns) {
142-
language.Matchers.emplace_back(ToTextMatePattern(unit, pattern));
143+
language.Matchers.emplace_back(ToTextMatePattern(unit, index, pattern));
143144
}
144145
for (const TRangePattern& range : unit.RangePatterns) {
145-
language.Matchers.emplace_back(TextMateMultilinePattern(unit, range));
146+
language.Matchers.emplace_back(TextMateMultilinePattern(unit, index, range));
146147
}
147148
}
148149

yql/essentials/tools/yql_highlight/ut/query.yql

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ FROM my_table AS t;
8787
SELECT ListCreate(OptionalType(DataType("String")));
8888
$match = Re2::Match("[ax]+\\d");
8989

90+
SELECT Yson::Parse("");
91+
9092
$py = Python3::foo(@@#py
9193
from yql.typing import *
9294

@@ -102,3 +104,18 @@ def foo(
102104
@@);
103105

104106
SELECT 1;
107+
108+
$r = (
109+
PROCESS $r USING Streaming::ProcessInline(
110+
TableRows(),
111+
@@#!/usr/bin/env bash
112+
# Feel free to use any other common interpreter in she-bang or bring
113+
# your own in attached file
114+
115+
awk $1 '{ printf "%05d %s\n", NF-1, $0; }' | sed -e 's/n/N/g'
116+
# Prepends zero-padded count of "n" letters to each row
117+
# then replaces all "n" with "N"
118+
@@,
119+
["-Fn"]
120+
)
121+
);

yql/essentials/tools/yql_highlight/ut/query.yqls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,6 @@
2828
return x + y.a + z
2929
@@))
3030
(let func (Udf 'SimpleUdf.Repeat))
31+
(let func (Udf 'Yson.Parse))
3132
(return world)
3233
)

0 commit comments

Comments
 (0)