HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
skeleton_parse_context.hpp
1// Copyright Take Vos 2020.
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
14namespace hi::inline v1 {
15
16struct skeleton_node;
17
20 using const_iterator = typename std::string_view::const_iterator;
21
22 statement_stack_type statement_stack;
23
24 parse_location location;
25 const_iterator index;
26 const_iterator last;
27
28 std::optional<const_iterator> text_segment_start;
29
34
35 skeleton_parse_context() = delete;
37 skeleton_parse_context &operator=(skeleton_parse_context const &other) = delete;
39 skeleton_parse_context &operator=(skeleton_parse_context &&other) = delete;
40 ~skeleton_parse_context() = default;
41
42 skeleton_parse_context(URL const &url, const_iterator first, const_iterator last);
43
44 [[nodiscard]] char const &operator*() const noexcept
45 {
46 return *index;
47 }
48
49 [[nodiscard]] bool atEOF() const noexcept
50 {
51 return index == last;
52 }
53
54 skeleton_parse_context &operator++() noexcept
55 {
56 hi_axiom(!atEOF());
57 location += *index;
58 ++index;
59 return *this;
60 }
61
62 skeleton_parse_context &operator+=(ssize_t x) noexcept
63 {
64 for (ssize_t i = 0; i != x; ++i) {
65 ++(*this);
66 }
67 return *this;
68 }
69
70 bool starts_with(std::string_view text) const noexcept
71 {
72 return std::string_view{index, last}.starts_with(text);
73 }
74
75 bool starts_with_and_advance_over(std::string_view text) noexcept
76 {
77 if (starts_with(text)) {
78 *this += ssize(text);
79 return true;
80 } else {
81 return false;
82 }
83 }
84
85 bool advance_to(std::string_view text) noexcept
86 {
87 while (!atEOF()) {
88 if (starts_with(text)) {
89 return true;
90 }
91 ++(*this);
92 }
93 return false;
94 }
95
96 bool advance_over(std::string_view text) noexcept
97 {
98 if (advance_to(text)) {
99 *this += ssize(text);
100 return true;
101 } else {
102 return false;
103 }
104 }
105
106 std::unique_ptr<formula_node> parse_expression(std::string_view end_text);
107
108 std::unique_ptr<formula_node> parse_expression_and_advance_over(std::string_view end_text);
109
110 template<typename T, typename... Args>
111 void push(Args &&...args)
112 {
113 statement_stack.push_back(std::make_unique<T>(std::forward<Args>(args)...));
114 }
115
116 [[nodiscard]] bool append(std::unique_ptr<skeleton_node> x) noexcept;
117
118 template<typename T, typename... Args>
119 [[nodiscard]] bool append(Args &&...args) noexcept
120 {
121 if (statement_stack.size() > 0) {
122 return append(std::make_unique<T>(std::forward<Args>(args)...));
123 } else {
124 return false;
125 }
126 }
127
132 [[nodiscard]] bool pop() noexcept;
133
134 void start_of_text_segment(int back_track = 0) noexcept;
135 void end_of_text_segment();
136
137 [[nodiscard]] bool top_statement_is_do() const noexcept;
138
139 [[nodiscard]] bool found_elif(parse_location location, std::unique_ptr<formula_node> expression) noexcept;
140
141 [[nodiscard]] bool found_else(parse_location location) noexcept;
142
143 [[nodiscard]] bool found_while(parse_location location, std::unique_ptr<formula_node> expression) noexcept;
144
145 void include(parse_location location, std::unique_ptr<formula_node> expression);
146};
147
148} // namespace hi::inline v1
STL namespace.
Definition formula_node.hpp:29
Definition formula_post_process_context.hpp:19
Definition parse_location.hpp:17
Definition skeleton_parse_context.hpp:18
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:33
bool pop() noexcept
Handle #end statement.
Definition URL.hpp:47
T push_back(T... args)
T size(T... args)