Seastar
High performance C++ framework for concurrent servers
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Modules Pages
ragel.hh
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2014 Cloudius Systems, Ltd.
20 */
21
22#pragma once
23
24#ifndef SEASTAR_MODULE
25#include <seastar/core/sstring.hh>
26#include <seastar/core/temporary_buffer.hh>
27#include <seastar/util/assert.hh>
28#include <seastar/util/eclipse.hh>
29#include <algorithm>
30#include <memory>
31#include <cassert>
32#include <optional>
33#include <seastar/util/modules.hh>
34#include <seastar/core/future.hh>
35#endif
36
37namespace seastar {
38
39// Support classes for Ragel parsers
40
41// Builds an sstring that can be scattered across multiple packets.
42//
43// Use a sstring_build::guard variable to designate each scattered
44// char array, and call mark_start() and mark_end() at the start
45// and end points, respectively. sstring_builder will collect data
46// from intervening segments, if needed.
47//
48// After mark_end() has been called, use the get() method to obtain
49// the built string.
50//
51// FIXME: switch to string_view.
52//
54 sstring _value;
55 const char* _start = nullptr;
56public:
57 class guard;
58public:
59 sstring get() && {
60 return std::move(_value);
61 }
62 void reset() {
63 _value = {};
64 _start = nullptr;
65 }
66 friend class guard;
67};
68
70 sstring_builder& _builder;
71 const char* _block_end;
72public:
73 guard(sstring_builder& builder, const char* block_start, const char* block_end)
74 : _builder(builder), _block_end(block_end) {
75 if (!_builder._value.empty()) {
76 mark_start(block_start);
77 }
78 }
79 ~guard() {
80 if (_builder._start) {
81 mark_end(_block_end);
82 }
83 }
84 void mark_start(const char* p) {
85 _builder._start = p;
86 }
87 void mark_end(const char* p) {
88 if (_builder._value.empty()) {
89 // avoid an allocation in the common case
90 _builder._value = sstring(_builder._start, p);
91 } else {
92 _builder._value += sstring(_builder._start, p);
93 }
94 _builder._start = nullptr;
95 }
96};
97
98SEASTAR_MODULE_EXPORT_BEGIN
99
100// CRTP
101template <typename ConcreteParser>
103protected:
104 int _fsm_cs;
105 std::unique_ptr<int[]> _fsm_stack = nullptr;
106 int _fsm_stack_size = 0;
107 int _fsm_top;
108 int _fsm_act;
109 char* _fsm_ts;
110 char* _fsm_te;
111 sstring_builder _builder;
112protected:
113 void init_base() {
114 _builder.reset();
115 }
116 void prepush() {
117 if (_fsm_top == _fsm_stack_size) {
118 auto old = _fsm_stack_size;
119 _fsm_stack_size = std::max(_fsm_stack_size * 2, 16);
120 SEASTAR_ASSERT(_fsm_stack_size > old);
121 std::unique_ptr<int[]> new_stack{new int[_fsm_stack_size]};
122 std::copy(_fsm_stack.get(), _fsm_stack.get() + _fsm_top, new_stack.get());
123 std::swap(_fsm_stack, new_stack);
124 }
125 }
126 void postpop() {}
127 sstring get_str() {
128 return std::move(_builder).get();
129 }
130public:
131 using unconsumed_remainder = std::optional<temporary_buffer<char>>;
133 char* p = buf.get_write();
134 char* pe = p + buf.size();
135 char* eof = buf.empty() ? pe : nullptr;
136 char* parsed = static_cast<ConcreteParser*>(this)->parse(p, pe, eof);
137 if (parsed) {
138 buf.trim_front(parsed - p);
139 return make_ready_future<unconsumed_remainder>(std::move(buf));
140 }
141 return make_ready_future<unconsumed_remainder>();
142 }
143};
144
145inline void trim_trailing_spaces_and_tabs(sstring& str) {
146 auto data = str.data();
147 size_t i;
148 for (i = str.size(); i > 0; --i) {
149 auto c = data[i-1];
150 if (!(c == ' ' || c == '\t')) {
151 break;
152 }
153 }
154 str.resize(i);
155}
156SEASTAR_MODULE_EXPORT_END
157
158}
A representation of a possibly not-yet-computed value.
Definition: future.hh:1197
Definition: ragel.hh:102
Definition: ragel.hh:69
Definition: ragel.hh:53
bool empty() const noexcept
Checks whether the buffer is empty.
Definition: temporary_buffer.hh:152
void trim_front(size_t pos) noexcept
Definition: temporary_buffer.hh:186
size_t size() const noexcept
Gets the buffer size.
Definition: temporary_buffer.hh:130
CharType * get_write() noexcept
Definition: temporary_buffer.hh:128
Seastar API namespace.
Definition: abort_on_ebadf.hh:26