/src/WasmEdge/include/validator/component_name.h
Line | Count | Source |
1 | | // SPDX-License-Identifier: Apache-2.0 |
2 | | // SPDX-FileCopyrightText: Copyright The WasmEdge Authors |
3 | | #pragma once |
4 | | |
5 | | #include "common/errcode.h" |
6 | | #include "common/expected.h" |
7 | | |
8 | | #include <cstdint> |
9 | | #include <string_view> |
10 | | |
11 | | namespace WasmEdge { |
12 | | namespace Validator { |
13 | | namespace Component { |
14 | | |
15 | | /// A parsed extern name of a component import or export. |
16 | | class ExternName { |
17 | | public: |
18 | | enum class Kind : uint8_t { |
19 | | Invalid, |
20 | | Constructor, |
21 | | Method, |
22 | | Static, |
23 | | InterfaceType, |
24 | | Label |
25 | | }; |
26 | | |
27 | | /// Fragments as views into the input; the kind decides which are set. |
28 | | struct Detail { |
29 | | std::string_view Resource; // Constructor, Method, Static |
30 | | std::string_view Method; // Method, Static |
31 | | std::string_view Namespace; // InterfaceType |
32 | | std::string_view Package; // InterfaceType |
33 | | std::string_view Interface; // InterfaceType |
34 | | std::string_view Version; // InterfaceType |
35 | | }; |
36 | | |
37 | | /// Returns true if Input is a label: <first-fragment> ( '-' <fragment> )*. |
38 | | static bool isKebabString(std::string_view Input) noexcept; |
39 | | |
40 | | /// Parses Name into this object, which stays Invalid on failure. |
41 | | Expect<void> parse(std::string_view Name) noexcept; |
42 | | |
43 | 73.9k | Kind getKind() const noexcept { return NameKind; } |
44 | 22.8k | std::string_view getOriginalName() const noexcept { return OriName; } |
45 | | /// The name without its annotation tag; empty when the name carries none. |
46 | 19 | std::string_view getNoTagName() const noexcept { return NoTagName; } |
47 | 2 | const Detail &getDetail() const noexcept { return NameDetail; } |
48 | | /// The `versionsuffix` attribute: canonversion plus Suffix must be a semver. |
49 | | Expect<void> checkVersionSuffix(std::string_view Suffix) const noexcept; |
50 | | |
51 | | private: |
52 | | // Cursor primitives, all consuming from Rest. |
53 | | bool tryRead(std::string_view Prefix) noexcept; |
54 | | bool readUntil(char Delim, std::string_view &Output) noexcept; |
55 | | std::string_view readLabelChars() noexcept; |
56 | | |
57 | | // One member per production, consuming the rest after its leading tag. |
58 | | Expect<void> parsePlainName() noexcept; |
59 | | Expect<void> parseInterfaceName() noexcept; |
60 | | |
61 | | // Grammar checks over a substring; the semver scanners stay unlogged. |
62 | | Expect<void> checkWordsLabel(std::string_view Label, |
63 | | std::string_view What) const noexcept; |
64 | | bool isCanonVersion(std::string_view V) const noexcept; |
65 | | Expect<void> scanSemver(std::string_view V) const noexcept; |
66 | | Expect<void> scanSemverIdentifiers(std::string_view Idents, |
67 | | bool CheckLeadingZeros) const noexcept; |
68 | | |
69 | | std::string_view OriName; |
70 | | std::string_view NoTagName; |
71 | | std::string_view Rest; |
72 | | Kind NameKind = Kind::Invalid; |
73 | | Detail NameDetail; |
74 | | }; |
75 | | |
76 | | } // namespace Component |
77 | | } // namespace Validator |
78 | | } // namespace WasmEdge |