plcopen 0.22.0
载入中...
搜索中...
未找到
language.h
浏览该文件的文档.
1#pragma once
2
3#include <algorithm>
4#include <cstddef>
5#include <cstdint>
6#include <set>
7#include <string>
8#include <string_view>
9#include <utility>
10#include <vector>
11
12#include "st/binding_manifest.h"
13#include "st/compile.h"
14#include "st/conv.h"
15#include "st/lexer.h"
17#include "st/type_desc.h"
18
19// D1 load/tool-domain ST document model. The compiler, lexer, conversion
20// matrix, standard-function manifest and generated FB binding manifest remain
21// the language authorities; this layer only projects them into editor queries.
22
23namespace plcopen::core::st
24{
25
27{
28 std::int32_t line = 0;
29 std::int32_t character = 0;
30};
31
33{
34 return left.line == right.line && left.character == right.character;
35}
36
38{
39 return !(left == right);
40}
41
47
61
63{
65 std::string code;
66 std::string message;
67 bool warning = false;
68};
69
76
78{
79 std::vector<LanguageCompletionItem> items;
80 bool is_incomplete = false;
81};
82
89
91{
92 bool found = false;
94 std::string contents;
95};
96
98{
99 std::size_t reparsed_pous = 0;
100 std::size_t reused_pous = 0;
101};
102
103inline char language_ascii_lower(char value)
104{
105 return value >= 'A' && value <= 'Z' ? static_cast<char>(value - 'A' + 'a') : value;
106}
107
108inline bool language_ascii_iequals(std::string_view left, std::string_view right)
109{
110 if (left.size() != right.size())
111 return false;
112 for (std::size_t index = 0; index < left.size(); ++index)
113 {
115 {
116 return false;
117 }
118 }
119 return true;
120}
121
123{
124
125inline std::string lower_copy(std::string_view text)
126{
127 std::string result;
128 result.reserve(text.size());
129 for (char value : text)
130 result.push_back(language_ascii_lower(value));
131 return result;
132}
133
134inline std::string trim_copy(std::string_view text)
135{
136 std::size_t begin = 0;
137 while (begin < text.size() && (text[begin] == ' ' || text[begin] == '\t' ||
138 text[begin] == '\r' || text[begin] == '\n'))
139 {
140 ++begin;
141 }
142 std::size_t end = text.size();
143 while (end > begin && (text[end - 1U] == ' ' || text[end - 1U] == '\t' ||
144 text[end - 1U] == '\r' || text[end - 1U] == '\n'))
145 {
146 --end;
147 }
148 return std::string(text.substr(begin, end - begin));
149}
150
151inline std::string canonical_type(std::string_view text)
152{
153 std::string result;
154 result.reserve(text.size());
155 for (char value : text)
156 {
157 if (value != ' ' && value != '\t' && value != '\r' && value != '\n')
158 {
159 result.push_back(language_ascii_lower(value));
160 }
161 }
162 return result;
163}
164
166{
168 std::size_t begin = 0;
169 std::size_t end = 0;
170 std::string lower;
171};
172
173inline std::vector<TokenView> tokenize(std::string_view source)
174{
175 std::vector<TokenView> result;
176 Lexer lexer(source);
177 for (;;)
178 {
179 const Token token = lexer.next();
180 if (token.kind == TokenKind::end_of_input)
181 break;
182 if (token.text.empty())
183 continue;
184 const char *const data = token.text.data();
185 if (data < source.data() || data > source.data() + source.size())
186 {
187 continue;
188 }
189 const std::size_t begin = static_cast<std::size_t>(data - source.data());
190 if (begin + token.text.size() > source.size())
191 continue;
192 result.push_back({token.kind, begin, begin + token.text.size(), lower_copy(token.text)});
193 }
194 return result;
195}
196
197inline bool identifier(const TokenView &token) { return token.kind == TokenKind::identifier; }
198
199inline bool var_opener(std::string_view lower)
200{
201 return lower == "var" || lower == "var_input" || lower == "var_output" ||
202 lower == "var_in_out" || lower == "var_temp" || lower == "var_external" ||
203 lower == "var_global";
204}
205
206inline std::uint64_t content_hash(std::string_view text)
207{
208 std::uint64_t hash = 1469598103934665603ULL;
209 for (unsigned char value : text)
210 {
211 hash ^= value;
212 hash *= 1099511628211ULL;
213 }
214 return hash;
215}
216
217inline std::uint32_t decode_utf8(std::string_view text, std::size_t at, std::size_t &bytes)
218{
219 const unsigned char first = static_cast<unsigned char>(text[at]);
220 if (first < 0x80U)
221 {
222 bytes = 1;
223 return first;
224 }
225 const auto continuation = [&](std::size_t index)
226 { return index < text.size() && (static_cast<unsigned char>(text[index]) & 0xc0U) == 0x80U; };
227 if ((first & 0xe0U) == 0xc0U && continuation(at + 1U))
228 {
229 bytes = 2;
230 return static_cast<std::uint32_t>(((first & 0x1fU) << 6U) |
231 (static_cast<unsigned char>(text[at + 1U]) & 0x3fU));
232 }
233 if ((first & 0xf0U) == 0xe0U && continuation(at + 1U) && continuation(at + 2U))
234 {
235 bytes = 3;
236 return static_cast<std::uint32_t>(
237 ((first & 0x0fU) << 12U) | ((static_cast<unsigned char>(text[at + 1U]) & 0x3fU) << 6U) |
238 (static_cast<unsigned char>(text[at + 2U]) & 0x3fU));
239 }
240 if ((first & 0xf8U) == 0xf0U && continuation(at + 1U) && continuation(at + 2U) &&
241 continuation(at + 3U))
242 {
243 bytes = 4;
244 return static_cast<std::uint32_t>(
245 ((first & 0x07U) << 18U) |
246 ((static_cast<unsigned char>(text[at + 1U]) & 0x3fU) << 12U) |
247 ((static_cast<unsigned char>(text[at + 2U]) & 0x3fU) << 6U) |
248 (static_cast<unsigned char>(text[at + 3U]) & 0x3fU));
249 }
250 bytes = 1;
251 return 0xfffdU;
252}
253
254inline LanguagePosition position_from_offset(std::string_view text, std::size_t offset)
255{
256 offset = std::min(offset, text.size());
257 LanguagePosition result;
258 std::size_t at = 0;
259 while (at < offset)
260 {
261 if (text[at] == '\r' && at + 1U < offset && text[at + 1U] == '\n')
262 {
263 ++result.line;
264 result.character = 0;
265 at += 2U;
266 continue;
267 }
268 if (text[at] == '\n')
269 {
270 ++result.line;
271 result.character = 0;
272 ++at;
273 continue;
274 }
275 std::size_t bytes = 1;
276 const std::uint32_t scalar = decode_utf8(text, at, bytes);
277 result.character += scalar > 0xffffU ? 2 : 1;
278 at += std::min(bytes, offset - at);
279 }
280 return result;
281}
282
283inline bool offset_from_position(std::string_view text, LanguagePosition position,
284 std::size_t &offset)
285{
286 if (position.line < 0 || position.character < 0)
287 return false;
288 std::size_t at = 0;
289 std::int32_t line = 0;
290 while (line < position.line && at < text.size())
291 {
292 if (text[at] == '\r' && at + 1U < text.size() && text[at + 1U] == '\n')
293 {
294 at += 2U;
295 ++line;
296 }
297 else if (text[at] == '\n')
298 {
299 ++at;
300 ++line;
301 }
302 else
303 {
304 ++at;
305 }
306 }
307 if (line != position.line)
308 return false;
309
310 std::int32_t character = 0;
311 while (at < text.size() && text[at] != '\r' && text[at] != '\n')
312 {
313 if (character == position.character)
314 {
315 offset = at;
316 return true;
317 }
318 std::size_t bytes = 1;
319 const std::uint32_t scalar = decode_utf8(text, at, bytes);
320 const std::int32_t width = scalar > 0xffffU ? 2 : 1;
321 if (character + width > position.character)
322 return false;
323 character += width;
324 at += bytes;
325 }
326 if (character != position.character)
327 return false;
328 offset = at;
329 return true;
330}
331
332inline LanguageRange range_from_offsets(std::string_view text, std::size_t begin, std::size_t end)
333{
334 return {position_from_offset(text, begin), position_from_offset(text, end)};
335}
336
351
352inline const char *symbol_class_name(SymbolClass value)
353{
354 switch (value)
355 {
357 return "local";
359 return "input";
361 return "output";
363 return "in_out";
365 return "temp";
367 return "external";
369 return "global";
371 return "function";
373 return "function block";
375 return "program";
377 return "type";
378 }
379 return "symbol";
380}
381
402
403inline SymbolClass declaration_class(std::string_view opener)
404{
405 if (opener == "var_input")
406 return SymbolClass::input;
407 if (opener == "var_output")
408 return SymbolClass::output;
409 if (opener == "var_in_out")
410 return SymbolClass::inout;
411 if (opener == "var_temp")
412 return SymbolClass::temp;
413 if (opener == "var_external")
415 if (opener == "var_global")
416 return SymbolClass::global;
417 return SymbolClass::local;
418}
419
421{
422 std::string name;
423 std::string lower;
424 std::string type;
425 std::string type_lower;
426 std::string owner;
428 std::size_t begin = 0;
429 std::size_t end = 0;
430};
431
432inline std::string symbol_detail(const IndexedSymbol &symbol)
433{
434 std::string result = symbol_class_name(symbol.symbol_class);
435 result.push_back(' ');
436 result += symbol.name;
437 if (!symbol.type.empty())
438 {
439 result += " : ";
440 result += symbol.type;
441 }
442 return result;
443}
444
445inline void parse_declaration_block(std::string_view source, const std::vector<TokenView> &tokens,
446 std::size_t first, std::size_t last, SymbolClass symbol_class,
447 std::string_view owner, std::vector<IndexedSymbol> &symbols)
448{
449 std::size_t statement = first;
450 while (statement < last)
451 {
452 std::size_t finish = statement;
453 while (finish < last && tokens[finish].kind != TokenKind::semicolon)
454 {
455 ++finish;
456 }
457 std::size_t colon = statement;
458 while (colon < finish && tokens[colon].kind != TokenKind::colon)
459 {
460 ++colon;
461 }
462 if (colon < finish)
463 {
464 std::size_t type_first = colon + 1U;
465 while (type_first < finish && tokens[type_first].kind == TokenKind::comma)
466 {
467 ++type_first;
468 }
469 std::size_t type_last = type_first;
470 while (type_last < finish && tokens[type_last].kind != TokenKind::assign)
471 {
472 ++type_last;
473 }
474 std::string type;
475 if (type_first < type_last)
476 {
477 type =
478 trim_copy(source.substr(tokens[type_first].begin,
479 tokens[type_last - 1U].end - tokens[type_first].begin));
480 }
481 for (std::size_t name = statement; name < colon; ++name)
482 {
483 if (tokens[name].lower == "at")
484 break;
485 if (!identifier(tokens[name]))
486 continue;
487 IndexedSymbol symbol;
488 symbol.name = std::string(
489 source.substr(tokens[name].begin, tokens[name].end - tokens[name].begin));
490 symbol.lower = tokens[name].lower;
491 symbol.type = type;
493 symbol.owner = std::string(owner);
494 symbol.symbol_class = symbol_class;
495 symbol.begin = tokens[name].begin;
496 symbol.end = tokens[name].end;
497 symbols.push_back(static_cast<IndexedSymbol &&>(symbol));
498 }
499 }
500 statement = finish < last ? finish + 1U : last;
501 }
502}
503
505{
506 std::string key;
507 std::string content;
508 std::string name;
509 std::string lower;
510 std::string return_type;
512 std::uint64_t hash = 0;
513 std::size_t base = 0;
514 std::size_t end = 0;
515 std::size_t name_begin = 0;
516 std::size_t name_end = 0;
517 std::vector<IndexedSymbol> symbols;
518};
519
520inline SymbolClass pou_class(std::string_view opener)
521{
522 if (opener == "function")
524 if (opener == "function_block")
527}
528
529inline const char *pou_closer(std::string_view opener)
530{
531 if (opener == "function")
532 return "end_function";
533 if (opener == "function_block")
534 return "end_function_block";
535 return "end_program";
536}
537
538inline PouCache parse_pou(std::string_view content, std::size_t base, std::size_t absolute_end)
539{
540 PouCache cache;
541 cache.content = std::string(content);
542 cache.hash = content_hash(content);
543 cache.base = base;
544 cache.end = absolute_end;
545 const std::vector<TokenView> tokens = tokenize(content);
546 if (tokens.empty())
547 return cache;
548
549 const std::string opener = tokens.front().lower;
550 cache.symbol_class = pou_class(opener);
551 std::size_t name_index = 1U;
552 while (name_index < tokens.size() && !identifier(tokens[name_index]))
553 {
554 ++name_index;
555 }
556 if (name_index < tokens.size())
557 {
558 cache.name = std::string(content.substr(tokens[name_index].begin,
559 tokens[name_index].end - tokens[name_index].begin));
560 cache.lower = tokens[name_index].lower;
561 cache.name_begin = tokens[name_index].begin;
562 cache.name_end = tokens[name_index].end;
563 }
564 cache.key = opener + ":" + cache.lower;
565
566 if (cache.symbol_class == SymbolClass::function_ && name_index + 2U < tokens.size() &&
567 tokens[name_index + 1U].kind == TokenKind::colon)
568 {
569 cache.return_type = std::string(
570 content.substr(tokens[name_index + 2U].begin,
571 tokens[name_index + 2U].end - tokens[name_index + 2U].begin));
572 }
573
574 for (std::size_t index = name_index + 1U; index < tokens.size(); ++index)
575 {
576 if (!var_opener(tokens[index].lower) || tokens[index].lower == "var_global")
577 {
578 continue;
579 }
580 std::size_t end = index + 1U;
581 while (end < tokens.size() && tokens[end].lower != "end_var")
582 ++end;
583 parse_declaration_block(content, tokens, index + 1U, end,
584 declaration_class(tokens[index].lower), cache.key, cache.symbols);
585 index = end;
586 }
587 return cache;
588}
589
590inline std::vector<PouCache> segment_pous(std::string_view source)
591{
592 std::vector<PouCache> result;
593 const std::vector<TokenView> tokens = tokenize(source);
594 for (std::size_t index = 0; index < tokens.size(); ++index)
595 {
596 const std::string &opener = tokens[index].lower;
597 if (opener != "function" && opener != "function_block" && opener != "program")
598 {
599 continue;
600 }
601 const char *const closer = pou_closer(opener);
602 std::size_t end_token = index + 1U;
603 while (end_token < tokens.size() && tokens[end_token].lower != closer)
604 {
605 ++end_token;
606 }
607 const std::size_t end = end_token < tokens.size() ? tokens[end_token].end : source.size();
608 const std::size_t begin = tokens[index].begin;
609 PouCache segment;
610 segment.content = std::string(source.substr(begin, end - begin));
611 segment.hash = content_hash(segment.content);
612 segment.base = begin;
613 segment.end = end;
614 segment.symbol_class = pou_class(opener);
615 std::size_t name = index + 1U;
616 while (name < tokens.size() && tokens[name].begin < end && !identifier(tokens[name]))
617 {
618 ++name;
619 }
620 if (name < tokens.size() && tokens[name].begin < end)
621 {
622 segment.name = std::string(
623 source.substr(tokens[name].begin, tokens[name].end - tokens[name].begin));
624 segment.lower = tokens[name].lower;
625 segment.name_begin = tokens[name].begin - begin;
626 segment.name_end = tokens[name].end - begin;
627 }
628 segment.key = opener + ":" + segment.lower;
629 result.push_back(static_cast<PouCache &&>(segment));
630 index = end_token < tokens.size() ? end_token : tokens.size();
631 }
632 return result;
633}
634
635inline const char *pin_direction_name(PinDirection direction)
636{
637 switch (direction)
638 {
640 return "input";
642 return "output";
644 return "in_out";
645 }
646 return "input";
647}
648
649inline std::string pin_type_name(const BindingManifest &manifest, const BindingPinDesc &pin)
650{
651 const TypeDesc *const type = manifest.type_table().get(pin.type_id);
652 return type == nullptr ? std::string{} : type->name;
653}
654
655inline std::string pin_detail(const BindingManifest &manifest, const BindingPinDesc &pin,
656 std::string_view display)
657{
658 std::string result = pin_direction_name(pin.direction);
659 result.push_back(' ');
660 result.append(display.data(), display.size());
661 const std::string type = pin_type_name(manifest, pin);
662 if (!type.empty())
663 {
664 result += " : ";
665 result += type;
666 }
667 return result;
668}
669
670inline bool variable_class(SymbolClass value)
671{
672 return value == SymbolClass::local || value == SymbolClass::input ||
673 value == SymbolClass::output || value == SymbolClass::inout ||
674 value == SymbolClass::temp || value == SymbolClass::external ||
675 value == SymbolClass::global;
676}
677
678} // namespace language_detail
679
681{
682 public:
683 static constexpr std::size_t max_completion_items = 8192U;
684
685 LanguageDocument() = default;
686
687 explicit LanguageDocument(std::string source) { update(static_cast<std::string &&>(source)); }
688
689 LanguageUpdateReport update(std::string source)
690 {
691 std::vector<language_detail::PouCache> next = language_detail::segment_pous(source);
692 std::vector<bool> used(pous_.size(), false);
694 for (language_detail::PouCache &candidate : next)
695 {
696 bool reused = false;
697 for (std::size_t index = 0; index < pous_.size(); ++index)
698 {
699 if (used[index] || pous_[index].key != candidate.key ||
700 pous_[index].hash != candidate.hash ||
701 pous_[index].content != candidate.content)
702 {
703 continue;
704 }
705 const std::size_t base = candidate.base;
706 const std::size_t end = candidate.end;
707 candidate = pous_[index];
708 candidate.base = base;
709 candidate.end = end;
710 used[index] = true;
711 reused = true;
712 ++report.reused_pous;
713 break;
714 }
715 if (!reused)
716 {
717 candidate =
718 language_detail::parse_pou(candidate.content, candidate.base, candidate.end);
719 ++report.reparsed_pous;
720 }
721 }
722
723 text_ = static_cast<std::string &&>(source);
724 pous_ = static_cast<std::vector<language_detail::PouCache> &&>(next);
725 tokens_ = language_detail::tokenize(text_);
726 rebuild_symbols();
727 rebuild_diagnostics();
728 last_update_ = report;
729 return report;
730 }
731
732 const std::string &text() const noexcept { return text_; }
733
734 const std::vector<LanguageDiagnostic> &diagnostics() const noexcept { return diagnostics_; }
735
736 const LanguageUpdateReport &last_update() const noexcept { return last_update_; }
737
739 {
741 std::size_t offset = 0;
742 if (!language_detail::offset_from_position(text_, position, offset))
743 {
744 return result;
745 }
746 const language_detail::PouCache *const pou = pou_at(offset);
747 const std::string owner = pou == nullptr ? std::string{} : pou->key;
748
749 const std::size_t dot_index = previous_token_index(offset);
750 if (dot_index < tokens_.size() && tokens_[dot_index].kind == TokenKind::dot &&
751 dot_index > 0U)
752 {
753 const language_detail::TokenView &instance = tokens_[dot_index - 1U];
755 resolve_variable(instance.lower, owner);
756 if (variable != nullptr)
757 {
758 complete_pins(*variable, result);
759 }
760 return result;
761 }
762
763 std::set<std::string> seen;
764 const auto add = [&](std::string label, std::string detail, LanguageSymbolKind kind)
765 {
766 const std::string lower = language_detail::lower_copy(label);
767 if (!seen.insert(lower).second)
768 return true;
769 if (result.items.size() >= max_completion_items)
770 {
771 result.is_incomplete = true;
772 return false;
773 }
774 result.items.push_back(
775 {static_cast<std::string &&>(label), static_cast<std::string &&>(detail), kind});
776 return true;
777 };
778
779 for (const language_detail::IndexedSymbol &symbol : symbols_)
780 {
781 if (!owner.empty() && symbol.owner == owner &&
782 language_detail::variable_class(symbol.symbol_class))
783 {
784 if (!add(symbol.name, language_detail::symbol_detail(symbol),
785 language_detail::public_kind(symbol.symbol_class)))
786 {
787 return result;
788 }
789 }
790 }
791 for (const language_detail::IndexedSymbol &symbol : symbols_)
792 {
793 if (symbol.symbol_class == language_detail::SymbolClass::global)
794 {
795 if (!add(symbol.name, language_detail::symbol_detail(symbol),
797 {
798 return result;
799 }
800 }
801 }
802 for (const language_detail::IndexedSymbol &symbol : symbols_)
803 {
804 if (symbol.symbol_class == language_detail::SymbolClass::function_ ||
805 symbol.symbol_class == language_detail::SymbolClass::function_block ||
806 symbol.symbol_class == language_detail::SymbolClass::program ||
807 symbol.symbol_class == language_detail::SymbolClass::type)
808 {
809 if (!add(symbol.name, language_detail::symbol_detail(symbol),
810 language_detail::public_kind(symbol.symbol_class)))
811 {
812 return result;
813 }
814 }
815 }
816
817 const BindingManifest &manifest = binding_manifest();
818 for (TypeId id = builtin::bool_; id <= builtin::last; ++id)
819 {
820 const TypeDesc *const type = manifest.type_table().get(id);
821 if (type != nullptr &&
822 !add(type->name, "builtin type " + type->name, LanguageSymbolKind::type))
823 {
824 return result;
825 }
826 }
828 {
829 if (!add(std::string(keyword.lower), "keyword " + std::string(keyword.lower),
831 {
832 return result;
833 }
834 }
836 {
838 continue;
839 if (!add(std::string(keyword.lower), "keyword " + std::string(keyword.lower),
841 {
842 return result;
843 }
844 }
846 {
847 if (function.registered &&
848 !add(std::string(function.name), "standard function " + std::string(function.name),
850 {
851 return result;
852 }
853 }
854 add_conversions(add, result);
855 if (result.is_incomplete)
856 return result;
857
858 for (std::size_t index = 0; index < manifest.fb_count(); ++index)
859 {
861 if (!add(std::string(metadata.name),
862 "standard function block " + std::string(metadata.name),
864 {
865 return result;
866 }
867 }
868 return result;
869 }
870
872 {
873 LanguageDefinition result;
874 std::size_t offset = 0;
875 if (!language_detail::offset_from_position(text_, position, offset))
876 {
877 return result;
878 }
879 const std::size_t token_index = token_at(offset);
880 if (token_index >= tokens_.size())
881 return result;
882 const language_detail::PouCache *const pou = pou_at(offset);
883 const std::string owner = pou == nullptr ? std::string{} : pou->key;
884
885 if (token_index >= 2U && tokens_[token_index - 1U].kind == TokenKind::dot)
886 {
888 resolve_variable(tokens_[token_index - 2U].lower, owner);
889 if (variable != nullptr)
890 {
891 const language_detail::IndexedSymbol *const pin =
892 resolve_user_pin(*variable, tokens_[token_index].lower);
893 if (pin != nullptr)
894 return definition_of(*pin);
895 }
896 return result;
897 }
898
899 const language_detail::IndexedSymbol *const symbol =
900 resolve_symbol(tokens_[token_index].lower, owner);
901 return symbol == nullptr ? result : definition_of(*symbol);
902 }
903
905 {
906 LanguageHover result;
907 std::size_t offset = 0;
908 if (!language_detail::offset_from_position(text_, position, offset))
909 {
910 return result;
911 }
912 const std::size_t token_index = token_at(offset);
913 if (token_index >= tokens_.size())
914 return result;
915 const language_detail::PouCache *const pou = pou_at(offset);
916 const std::string owner = pou == nullptr ? std::string{} : pou->key;
917
918 if (token_index >= 2U && tokens_[token_index - 1U].kind == TokenKind::dot)
919 {
921 resolve_variable(tokens_[token_index - 2U].lower, owner);
922 if (variable != nullptr)
923 {
924 const language_detail::IndexedSymbol *const pin =
925 resolve_user_pin(*variable, tokens_[token_index].lower);
926 if (pin != nullptr)
927 {
928 return hover_of(*pin, token_index);
929 }
930 const BindingPinDesc *standard_pin = nullptr;
931 std::string display;
932 if (resolve_standard_pin(*variable, tokens_[token_index].lower, standard_pin,
933 display))
934 {
935 result.found = true;
937 text_, tokens_[token_index].begin, tokens_[token_index].end);
938 result.contents =
939 language_detail::pin_detail(binding_manifest(), *standard_pin, display);
940 return result;
941 }
942 }
943 return result;
944 }
945
946 const std::string &lower = tokens_[token_index].lower;
947 const language_detail::IndexedSymbol *const symbol = resolve_symbol(lower, owner);
948 if (symbol != nullptr)
949 return hover_of(*symbol, token_index);
950
951 StandardFunction function;
952 if (resolve_standard_function(lower, function))
953 {
954 result.found = true;
955 result.range = language_detail::range_from_offsets(text_, tokens_[token_index].begin,
956 tokens_[token_index].end);
958 {
959 if (language_ascii_iequals(entry.name, lower))
960 {
961 result.contents = "standard function " + std::string(entry.name);
962 break;
963 }
964 }
965 return result;
966 }
967 ConvDesc conversion;
968 if (resolve_conversion(lower, conversion))
969 {
970 result.found = true;
971 result.range = language_detail::range_from_offsets(text_, tokens_[token_index].begin,
972 tokens_[token_index].end);
973 result.contents = "conversion function " + std::string(tokens_[token_index].lower);
974 return result;
975 }
976 const BindingManifest &manifest = binding_manifest();
978 if (manifest.type_table().find(lower, type_id) == TypeError::ok)
979 {
980 const TypeDesc *const type = manifest.type_table().get(type_id);
981 result.found = type != nullptr;
982 result.range = language_detail::range_from_offsets(text_, tokens_[token_index].begin,
983 tokens_[token_index].end);
984 if (type != nullptr)
985 result.contents = "builtin type " + type->name;
986 return result;
987 }
988 for (std::size_t index = 0; index < manifest.fb_count(); ++index)
989 {
990 if (manifest.fb(index).lower_name == lower)
991 {
992 result.found = true;
994 text_, tokens_[token_index].begin, tokens_[token_index].end);
995 result.contents =
996 "standard function block " + std::string(generated::kStBindingFbs[index].name);
997 return result;
998 }
999 }
1000 return result;
1001 }
1002
1003 private:
1004 void rebuild_symbols()
1005 {
1006 symbols_.clear();
1007 for (const language_detail::PouCache &pou : pous_)
1008 {
1010 declaration.name = pou.name;
1011 declaration.lower = pou.lower;
1012 declaration.type = pou.return_type;
1013 declaration.type_lower = language_detail::canonical_type(pou.return_type);
1014 declaration.symbol_class = pou.symbol_class;
1015 declaration.begin = pou.base + pou.name_begin;
1016 declaration.end = pou.base + pou.name_end;
1017 symbols_.push_back(static_cast<language_detail::IndexedSymbol &&>(declaration));
1018 for (const language_detail::IndexedSymbol &relative : pou.symbols)
1019 {
1020 language_detail::IndexedSymbol absolute = relative;
1021 absolute.begin += pou.base;
1022 absolute.end += pou.base;
1023 symbols_.push_back(static_cast<language_detail::IndexedSymbol &&>(absolute));
1024 }
1025 }
1026
1027 for (std::size_t index = 0; index < tokens_.size(); ++index)
1028 {
1029 if (inside_pou(tokens_[index].begin))
1030 continue;
1031 if (tokens_[index].lower == "type")
1032 {
1033 std::size_t name = index + 1U;
1034 while (name < tokens_.size() && !language_detail::identifier(tokens_[name]))
1035 {
1036 ++name;
1037 }
1038 if (name < tokens_.size())
1039 {
1040 language_detail::IndexedSymbol symbol;
1041 symbol.name = std::string(
1042 text_.substr(tokens_[name].begin, tokens_[name].end - tokens_[name].begin));
1043 symbol.lower = tokens_[name].lower;
1044 symbol.symbol_class = language_detail::SymbolClass::type;
1045 symbol.begin = tokens_[name].begin;
1046 symbol.end = tokens_[name].end;
1047 symbols_.push_back(static_cast<language_detail::IndexedSymbol &&>(symbol));
1048 }
1049 }
1050 else if (tokens_[index].lower == "var_global")
1051 {
1052 std::size_t end = index + 1U;
1053 while (end < tokens_.size() && tokens_[end].lower != "end_var")
1054 {
1055 ++end;
1056 }
1057 language_detail::parse_declaration_block(text_, tokens_, index + 1U, end,
1059 symbols_);
1060 index = end;
1061 }
1062 }
1063 }
1064
1065 void rebuild_diagnostics()
1066 {
1067 diagnostics_.clear();
1068 const CompileResult compiled = st::compile(text_);
1069 diagnostics_.reserve(compiled.diagnostics.size());
1070 for (const Diagnostic &source : compiled.diagnostics)
1071 {
1072 LanguageDiagnostic diagnostic;
1073 diagnostic.range = diagnostic_range(source);
1074 diagnostic.code = to_string(source.code);
1075 diagnostic.message = source.message;
1076 diagnostic.warning = source.code == DiagCode::warning_program_unmapped;
1077 diagnostics_.push_back(static_cast<LanguageDiagnostic &&>(diagnostic));
1078 }
1079 }
1080
1081 LanguageRange diagnostic_range(const Diagnostic &diagnostic) const
1082 {
1083 std::size_t line_start = 0;
1084 std::int32_t line = 1;
1085 while (line < std::max<std::int32_t>(diagnostic.line, 1) && line_start < text_.size())
1086 {
1087 if (text_[line_start] == '\r' && line_start + 1U < text_.size() &&
1088 text_[line_start + 1U] == '\n')
1089 {
1090 line_start += 2U;
1091 ++line;
1092 }
1093 else if (text_[line_start] == '\n')
1094 {
1095 ++line_start;
1096 ++line;
1097 }
1098 else
1099 {
1100 ++line_start;
1101 }
1102 }
1103 std::size_t line_end = line_start;
1104 while (line_end < text_.size() && text_[line_end] != '\r' && text_[line_end] != '\n')
1105 {
1106 ++line_end;
1107 }
1108 const std::size_t column =
1109 diagnostic.column <= 1 ? 0U : static_cast<std::size_t>(diagnostic.column - 1);
1110 const std::size_t begin = std::min(line_start + column, line_end);
1111 for (const language_detail::TokenView &token : tokens_)
1112 {
1113 if (token.begin <= begin && begin < token.end)
1114 {
1115 return language_detail::range_from_offsets(text_, token.begin, token.end);
1116 }
1117 }
1118 if (begin >= line_end)
1119 {
1120 return language_detail::range_from_offsets(text_, begin, begin);
1121 }
1122 std::size_t bytes = 1;
1123 (void)language_detail::decode_utf8(text_, begin, bytes);
1124 return language_detail::range_from_offsets(text_, begin, std::min(begin + bytes, line_end));
1125 }
1126
1127 bool inside_pou(std::size_t offset) const { return pou_at(offset) != nullptr; }
1128
1129 const language_detail::PouCache *pou_at(std::size_t offset) const
1130 {
1131 for (const language_detail::PouCache &pou : pous_)
1132 {
1133 if (pou.base <= offset && offset < pou.end)
1134 return &pou;
1135 }
1136 return nullptr;
1137 }
1138
1139 std::size_t token_at(std::size_t offset) const
1140 {
1141 for (std::size_t index = 0; index < tokens_.size(); ++index)
1142 {
1143 if (tokens_[index].begin <= offset && offset < tokens_[index].end)
1144 {
1145 return index;
1146 }
1147 }
1148 return tokens_.size();
1149 }
1150
1151 std::size_t previous_token_index(std::size_t offset) const
1152 {
1153 std::size_t result = tokens_.size();
1154 for (std::size_t index = 0; index < tokens_.size(); ++index)
1155 {
1156 if (tokens_[index].end > offset)
1157 break;
1158 result = index;
1159 }
1160 return result;
1161 }
1162
1163 const language_detail::IndexedSymbol *unique_symbol(std::string_view lower,
1164 std::string_view owner,
1166 bool use_exact) const
1167 {
1168 const language_detail::IndexedSymbol *result = nullptr;
1169 for (const language_detail::IndexedSymbol &symbol : symbols_)
1170 {
1171 if (symbol.lower != lower || symbol.owner != owner ||
1172 (use_exact && symbol.symbol_class != exact))
1173 {
1174 continue;
1175 }
1176 if (result != nullptr)
1177 return nullptr;
1178 result = &symbol;
1179 }
1180 return result;
1181 }
1182
1183 const language_detail::IndexedSymbol *resolve_variable(std::string_view lower,
1184 std::string_view owner) const
1185 {
1186 const language_detail::IndexedSymbol *local = nullptr;
1187 for (const language_detail::IndexedSymbol &symbol : symbols_)
1188 {
1189 if (symbol.lower != lower || symbol.owner != owner ||
1190 !language_detail::variable_class(symbol.symbol_class) ||
1191 symbol.symbol_class == language_detail::SymbolClass::global)
1192 {
1193 continue;
1194 }
1195 if (local != nullptr)
1196 return nullptr;
1197 local = &symbol;
1198 }
1199 if (local != nullptr)
1200 return local;
1201 return unique_symbol(lower, {}, language_detail::SymbolClass::global, true);
1202 }
1203
1204 const language_detail::IndexedSymbol *resolve_symbol(std::string_view lower,
1205 std::string_view owner) const
1206 {
1207 const language_detail::IndexedSymbol *const variable = resolve_variable(lower, owner);
1208 if (variable != nullptr)
1209 return variable;
1210
1211 const language_detail::IndexedSymbol *result = nullptr;
1212 for (const language_detail::IndexedSymbol &symbol : symbols_)
1213 {
1214 if (symbol.lower != lower || !symbol.owner.empty() ||
1215 symbol.symbol_class == language_detail::SymbolClass::global)
1216 {
1217 continue;
1218 }
1219 if (result != nullptr)
1220 return nullptr;
1221 result = &symbol;
1222 }
1223 return result;
1224 }
1225
1226 const language_detail::IndexedSymbol *
1227 resolve_user_pin(const language_detail::IndexedSymbol &variable, std::string_view pin) const
1228 {
1229 std::string owner;
1230 for (const language_detail::PouCache &pou : pous_)
1231 {
1233 pou.lower == variable.type_lower)
1234 {
1235 if (!owner.empty())
1236 return nullptr;
1237 owner = pou.key;
1238 }
1239 }
1240 if (owner.empty())
1241 return nullptr;
1242 const language_detail::IndexedSymbol *result = nullptr;
1243 for (const language_detail::IndexedSymbol &symbol : symbols_)
1244 {
1245 if (symbol.owner != owner || symbol.lower != pin ||
1246 (symbol.symbol_class != language_detail::SymbolClass::input &&
1247 symbol.symbol_class != language_detail::SymbolClass::output &&
1248 symbol.symbol_class != language_detail::SymbolClass::inout))
1249 {
1250 continue;
1251 }
1252 if (result != nullptr)
1253 return nullptr;
1254 result = &symbol;
1255 }
1256 return result;
1257 }
1258
1259 bool resolve_standard_pin(const language_detail::IndexedSymbol &variable,
1260 std::string_view pin_name, const BindingPinDesc *&pin,
1261 std::string &display) const
1262 {
1263 const BindingManifest &manifest = binding_manifest();
1264 for (std::size_t fb_index = 0; fb_index < manifest.fb_count(); ++fb_index)
1265 {
1266 const BindingFbDesc &fb = manifest.fb(fb_index);
1267 if (fb.lower_name != variable.type_lower)
1268 continue;
1269 const generated::StBindingFbMetadata &metadata = generated::kStBindingFbs[fb_index];
1270 for (std::size_t pin_index = 0; pin_index < fb.pin_count; ++pin_index)
1271 {
1272 if (!language_ascii_iequals(fb.pins[pin_index].lower_name, pin_name))
1273 {
1274 continue;
1275 }
1276 pin = &fb.pins[pin_index];
1277 display =
1278 std::string(generated::kStBindingPins[metadata.first_pin + pin_index].name);
1279 return true;
1280 }
1281 }
1282 return false;
1283 }
1284
1285 void complete_pins(const language_detail::IndexedSymbol &variable,
1286 LanguageCompletionList &result) const
1287 {
1288 std::string owner;
1289 for (const language_detail::PouCache &pou : pous_)
1290 {
1292 pou.lower == variable.type_lower)
1293 {
1294 if (!owner.empty())
1295 return;
1296 owner = pou.key;
1297 }
1298 }
1299 if (!owner.empty())
1300 {
1301 std::set<std::string> seen;
1302 for (const language_detail::IndexedSymbol &symbol : symbols_)
1303 {
1304 if (symbol.owner != owner ||
1305 (symbol.symbol_class != language_detail::SymbolClass::input &&
1306 symbol.symbol_class != language_detail::SymbolClass::output &&
1307 symbol.symbol_class != language_detail::SymbolClass::inout) ||
1308 !seen.insert(symbol.lower).second)
1309 {
1310 continue;
1311 }
1312 result.items.push_back({symbol.name, language_detail::symbol_detail(symbol),
1314 }
1315 return;
1316 }
1317
1318 const BindingManifest &manifest = binding_manifest();
1319 for (std::size_t fb_index = 0; fb_index < manifest.fb_count(); ++fb_index)
1320 {
1321 const BindingFbDesc &fb = manifest.fb(fb_index);
1322 if (fb.lower_name != variable.type_lower)
1323 continue;
1324 const generated::StBindingFbMetadata &metadata = generated::kStBindingFbs[fb_index];
1325 for (std::size_t pin_index = 0; pin_index < fb.pin_count; ++pin_index)
1326 {
1327 const std::string display =
1328 std::string(generated::kStBindingPins[metadata.first_pin + pin_index].name);
1329 result.items.push_back(
1330 {display, language_detail::pin_detail(manifest, fb.pins[pin_index], display),
1332 }
1333 return;
1334 }
1335 }
1336
1337 LanguageDefinition definition_of(const language_detail::IndexedSymbol &symbol) const
1338 {
1339 LanguageDefinition result;
1340 result.found = true;
1341 result.range = language_detail::range_from_offsets(text_, symbol.begin, symbol.end);
1342 result.selection = result.range;
1343 return result;
1344 }
1345
1346 LanguageHover hover_of(const language_detail::IndexedSymbol &symbol,
1347 std::size_t token_index) const
1348 {
1349 LanguageHover result;
1350 result.found = true;
1351 result.range = language_detail::range_from_offsets(text_, tokens_[token_index].begin,
1352 tokens_[token_index].end);
1353 result.contents = language_detail::symbol_detail(symbol);
1354 return result;
1355 }
1356
1357 template <typename Add>
1358 static void add_conversions(const Add &add, LanguageCompletionList &result)
1359 {
1360 for (Type from : kConvTypes)
1361 {
1362 for (Type to : kConvTypes)
1363 {
1364 if (conv_kind(from, to) == ConvKind::unsupported)
1365 continue;
1366 std::string label = to_string(from);
1367 label += "_TO_";
1368 label += to_string(to);
1369 if (!add(label, "conversion function " + label,
1371 {
1372 return;
1373 }
1374 }
1375 }
1376 const char *const fixed[] = {
1377 "TIME_TO_LINT", "LINT_TO_TIME", "TRUNC_INT", "TRUNC_DINT", "TRUNC_LINT",
1378 "USINT_TO_CHAR", "CHAR_TO_USINT", "UDINT_TO_WCHAR", "WCHAR_TO_UDINT",
1379 };
1380 for (const char *label : fixed)
1381 {
1382 const std::string text = label;
1383 if (!add(text, "conversion function " + text, LanguageSymbolKind::standard_function))
1384 {
1385 return;
1386 }
1387 }
1388 (void)result;
1389 }
1390
1391 std::string text_;
1392 std::vector<language_detail::TokenView> tokens_;
1393 std::vector<language_detail::PouCache> pous_;
1394 std::vector<language_detail::IndexedSymbol> symbols_;
1395 std::vector<LanguageDiagnostic> diagnostics_;
1396 LanguageUpdateReport last_update_;
1397};
1398
1399} // namespace plcopen::core::st
std::size_t fb_count() const noexcept
const BindingFbDesc & fb(std::size_t index) const noexcept
const TypeTable & type_table() const noexcept
const std::vector< LanguageDiagnostic > & diagnostics() const noexcept
const LanguageUpdateReport & last_update() const noexcept
LanguageHover hover(LanguagePosition position) const
const std::string & text() const noexcept
LanguageCompletionList complete(LanguagePosition position) const
static constexpr std::size_t max_completion_items
LanguageUpdateReport update(std::string source)
LanguageDefinition definition(LanguagePosition position) const
TypeError find(std::string_view name, TypeId &out) const noexcept
const TypeDesc * get(TypeId id) const noexcept
constexpr UnsupportedEntry kUnsupported[]
constexpr KeywordEntry kKeywords[]
定义 lexer.h:70
constexpr std::array< StBindingPinMetadata, 1476 > kStBindingPins
constexpr std::array< StBindingFbMetadata, 134 > kStBindingFbs
void parse_declaration_block(std::string_view source, const std::vector< TokenView > &tokens, std::size_t first, std::size_t last, SymbolClass symbol_class, std::string_view owner, std::vector< IndexedSymbol > &symbols)
std::string pin_detail(const BindingManifest &manifest, const BindingPinDesc &pin, std::string_view display)
bool offset_from_position(std::string_view text, LanguagePosition position, std::size_t &offset)
std::uint32_t decode_utf8(std::string_view text, std::size_t at, std::size_t &bytes)
PouCache parse_pou(std::string_view content, std::size_t base, std::size_t absolute_end)
SymbolClass declaration_class(std::string_view opener)
std::string trim_copy(std::string_view text)
const char * pou_closer(std::string_view opener)
bool identifier(const TokenView &token)
std::uint64_t content_hash(std::string_view text)
std::string pin_type_name(const BindingManifest &manifest, const BindingPinDesc &pin)
LanguagePosition position_from_offset(std::string_view text, std::size_t offset)
std::string canonical_type(std::string_view text)
LanguageSymbolKind public_kind(SymbolClass value)
std::vector< TokenView > tokenize(std::string_view source)
const char * symbol_class_name(SymbolClass value)
std::vector< PouCache > segment_pous(std::string_view source)
LanguageRange range_from_offsets(std::string_view text, std::size_t begin, std::size_t end)
std::string lower_copy(std::string_view text)
bool variable_class(SymbolClass value)
const char * pin_direction_name(PinDirection direction)
SymbolClass pou_class(std::string_view opener)
bool var_opener(std::string_view lower)
std::string symbol_detail(const IndexedSymbol &symbol)
std::uint32_t TypeId
constexpr const auto & standard_function_manifest()
CompileResult compile(std::string_view source, const CompileOptions &options=CompileOptions{})
constexpr bool operator!=(SymbolId left, SymbolId right) noexcept
constexpr const char * to_string(ConvKind kind)
定义 conv.h:90
bool resolve_standard_function(std::string_view lower, StandardFunction &function)
char language_ascii_lower(char value)
constexpr ConvKind conv_kind(Type from, Type to)
定义 conv.h:42
constexpr TypeId type_id(Type type)
定义 types.h:52
constexpr bool operator==(SymbolId left, SymbolId right) noexcept
const BindingManifest & binding_manifest()
bool resolve_conversion(std::string_view lower, ConvDesc &desc)
定义 conv.h:118
bool language_ascii_iequals(std::string_view left, std::string_view right)
constexpr TypeId invalid_type_id
constexpr Type kConvTypes[]
定义 conv.h:34
std::vector< LanguageCompletionItem > items
std::string_view text