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 tt {
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 return *index;
46 }
47
48 [[nodiscard]] bool atEOF() const noexcept {
49 return index == last;
50 }
51
52 skeleton_parse_context& operator++() noexcept {
53 tt_axiom(!atEOF());
54 location += *index;
55 ++index;
56 return *this;
57 }
58
59 skeleton_parse_context& operator+=(ssize_t x) noexcept {
60 for (ssize_t i = 0; i != x; ++i) {
61 ++(*this);
62 }
63 return *this;
64 }
65
66 bool starts_with(std::string_view text) const noexcept {
67 return std::string_view{index, last}.starts_with(text);
68 }
69
70 bool starts_with_and_advance_over(std::string_view text) noexcept {
71 if (starts_with(text)) {
72 *this += std::ssize(text);
73 return true;
74 } else {
75 return false;
76 }
77 }
78
79 bool advance_to(std::string_view text) noexcept {
80 while (!atEOF()) {
81 if (starts_with(text)) {
82 return true;
83 }
84 ++(*this);
85 }
86 return false;
87 }
88
89 bool advance_over(std::string_view text) noexcept {
90 if (advance_to(text)) {
91 *this += std::ssize(text);
92 return true;
93 } else {
94 return false;
95 }
96 }
97
98 std::unique_ptr<formula_node> parse_expression(std::string_view end_text);
99
100 std::unique_ptr<formula_node> parse_expression_and_advance_over(std::string_view end_text);
101
102 template<typename T, typename... Args>
103 void push(Args &&... args) {
104 statement_stack.push_back(std::make_unique<T>(std::forward<Args>(args)...));
105 }
106
107 [[nodiscard]] bool append(std::unique_ptr<skeleton_node> x) noexcept;
108
109 template<typename T, typename... Args>
110 [[nodiscard]] bool append(Args &&... args) noexcept {
111 if (statement_stack.size() > 0) {
112 return append(std::make_unique<T>(std::forward<Args>(args)...));
113 } else {
114 return false;
115 }
116 }
117
122 [[nodiscard]] bool pop() noexcept;
123
124 void start_of_text_segment(int back_track = 0) noexcept;
125 void end_of_text_segment();
126
127 [[nodiscard]] bool top_statement_is_do() const noexcept;
128
129 [[nodiscard]] bool found_elif(parse_location location, std::unique_ptr<formula_node> expression) noexcept;
130
131 [[nodiscard]] bool found_else(parse_location location) noexcept;
132
133 [[nodiscard]] bool found_while(parse_location location, std::unique_ptr<formula_node> expression) noexcept;
134
135 void include(parse_location location, std::unique_ptr<formula_node> expression);
136};
137
138}
STL namespace.
Definition formula_node.hpp:19
Definition formula_post_process_context.hpp:19
Definition parse_location.hpp:17
Definition skeleton_parse_context.hpp:18
bool pop() noexcept
Handle #end statement.
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
Definition URL.hpp:47
T push_back(T... args)
T size(T... args)