HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
skeleton_parse_context.hpp
1// Copyright Take Vos 2020-2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "../formula/formula.hpp"
8#include "../strings.hpp"
9#include "../algorithm.hpp"
10#include <memory>
11#include <string_view>
12#include <optional>
13#include <filesystem>
14
15namespace hi::inline v1 {
16
17struct skeleton_node;
18
21 using const_iterator = typename std::string_view::const_iterator;
22
23 statement_stack_type statement_stack;
24
25 parse_location location;
26 const_iterator index;
27 const_iterator last;
28
29 std::optional<const_iterator> text_segment_start;
30
35
36 skeleton_parse_context() = delete;
38 skeleton_parse_context &operator=(skeleton_parse_context const &other) = delete;
40 skeleton_parse_context &operator=(skeleton_parse_context &&other) = delete;
41 ~skeleton_parse_context() = default;
42
43 skeleton_parse_context(std::filesystem::path const &path, const_iterator first, const_iterator last);
44
45 [[nodiscard]] char const &operator*() const noexcept
46 {
47 return *index;
48 }
49
50 [[nodiscard]] bool atEOF() const noexcept
51 {
52 return index == last;
53 }
54
55 skeleton_parse_context &operator++() noexcept
56 {
57 hi_assert(not atEOF());
58 location += *index;
59 ++index;
60 return *this;
61 }
62
63 skeleton_parse_context &operator+=(ssize_t x) noexcept
64 {
65 for (ssize_t i = 0; i != x; ++i) {
66 ++(*this);
67 }
68 return *this;
69 }
70
71 bool starts_with(std::string_view text) const noexcept
72 {
73 return std::string_view{index, last}.starts_with(text);
74 }
75
76 bool starts_with_and_advance_over(std::string_view text) noexcept
77 {
78 if (starts_with(text)) {
79 *this += ssize(text);
80 return true;
81 } else {
82 return false;
83 }
84 }
85
86 bool advance_to(std::string_view text) noexcept
87 {
88 while (!atEOF()) {
89 if (starts_with(text)) {
90 return true;
91 }
92 ++(*this);
93 }
94 return false;
95 }
96
97 bool advance_over(std::string_view text) noexcept
98 {
99 if (advance_to(text)) {
100 *this += ssize(text);
101 return true;
102 } else {
103 return false;
104 }
105 }
106
107 std::unique_ptr<formula_node> parse_expression(std::string_view end_text);
108
109 std::unique_ptr<formula_node> parse_expression_and_advance_over(std::string_view end_text);
110
111 template<typename T, typename... Args>
112 void push(Args &&...args)
113 {
114 statement_stack.push_back(std::make_unique<T>(std::forward<Args>(args)...));
115 }
116
117 [[nodiscard]] bool append(std::unique_ptr<skeleton_node> x) noexcept;
118
119 template<typename T, typename... Args>
120 [[nodiscard]] bool append(Args &&...args) noexcept
121 {
122 if (statement_stack.size() > 0) {
123 return append(std::make_unique<T>(std::forward<Args>(args)...));
124 } else {
125 return false;
126 }
127 }
128
133 [[nodiscard]] bool pop() noexcept;
134
135 void start_of_text_segment(int back_track = 0) noexcept;
136 void end_of_text_segment();
137
138 [[nodiscard]] bool top_statement_is_do() const noexcept;
139
140 [[nodiscard]] bool found_elif(parse_location location, std::unique_ptr<formula_node> expression) noexcept;
141
142 [[nodiscard]] bool found_else(parse_location location) noexcept;
143
144 [[nodiscard]] bool found_while(parse_location location, std::unique_ptr<formula_node> expression) noexcept;
145
146 void include(parse_location location, formula_node &expression);
147};
148
149} // namespace hi::inline v1
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
Definition formula_node.hpp:28
Definition formula_post_process_context.hpp:19
Definition parse_location.hpp:18
Definition skeleton_parse_context.hpp:19
formula_post_process_context post_process_context
Post process context is used to record functions that are defined in the template being parsed.
Definition skeleton_parse_context.hpp:34
bool pop() noexcept
Handle #end statement.
T push_back(T... args)
T size(T... args)