A11 (C++ runtime)
Native C++ implementation of the A11 streaming action runtime
Loading...
Searching...
No Matches
pattern.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 The A11 Authors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef A11_FLOW_PATTERN_H_
18#define A11_FLOW_PATTERN_H_
19
20#include <cstddef>
21#include <optional>
22#include <string>
23#include <string_view>
24#include <vector>
25
26namespace a11::flow::pattern {
27
59enum class HoleType {
61 kString,
63 kInt,
65 kNumber,
67 kBool,
69 kWord,
71 kLine,
73 kRest,
77 kTime,
79 kJson,
80};
81
82std::string_view HoleTypeName(HoleType type);
83
85std::optional<HoleType> HoleTypeFromName(std::string_view name);
86
88struct Hole {
90 std::string name;
93 size_t position = 0;
94};
95
97struct Pattern {
98 struct Piece {
100 bool is_hole = false;
102 std::string literal;
104 };
105
106 std::vector<Piece> pieces;
108 std::vector<Hole> holes;
109
112 [[nodiscard]] bool AllNamed() const;
113};
114
116struct Compiled {
119 std::string error;
121 size_t column = 0;
122
123 [[nodiscard]] bool ok() const { return error.empty(); }
124};
125
127Compiled Compile(std::string_view text);
128
130struct Capture {
131 std::string_view text;
132};
133
138std::optional<std::vector<Capture>> Match(const Pattern& pattern,
139 std::string_view subject);
140
141} // namespace a11::flow::pattern
142
143#endif // A11_FLOW_PATTERN_H_
std::optional< std::string > text
The text, where the value is a string literal or a name bound to one.
Definition discover.cc:612
std::string type
The type and what follows it: string required, list[a11.Chunk] stream.
Definition format.cc:51
std::string name
The name and its colon, which travel together because they always do.
Definition format.cc:49
Definition pattern.cc:28
std::optional< std::vector< Capture > > Match(const Pattern &pattern, std::string_view subject)
Match pattern anywhere in subject, taking the earliest fit.
Definition pattern.cc:443
Compiled Compile(std::string_view text)
Compile a pattern, saying what is wrong rather than throwing.
Definition pattern.cc:333
std::optional< HoleType > HoleTypeFromName(std::string_view name)
The type that name spells, or nothing where it is not one of them.
Definition pattern.cc:309
HoleType
The pattern language match reads: literal text and named holes.
Definition pattern.h:59
@ kBool
true or false, in either case.
@ kString
Anything up to the next literal, on one line. The default.
@ kJson
Text to be read as JSON.
@ kInt
A whole number, with an optional sign.
@ kWord
A run of anything that is not whitespace.
@ kTime
Text to be read as an instant: RFC 3339, or a bare date.
@ kNumber
A number, with an optional sign and fraction.
@ kDuration
Text to be read as a duration: 250ms, 1m30s.
@ kRest
Everything left, line breaks and all. Only as the last piece.
@ kLine
The rest of the line.
std::string_view HoleTypeName(HoleType type)
Definition pattern.cc:283
std::string subject
What to call it in the message: ‘'Source’, orthe pattern`.
Definition resolve.cc:3580
What one hole took: a view into the subject that was matched.
Definition pattern.h:130
std::string_view text
Definition pattern.h:131
A pattern, or what is wrong with the text it was written as.
Definition pattern.h:116
size_t column
Where the problem is, counting characters from the start of the pattern.
Definition pattern.h:121
bool ok() const
Definition pattern.h:123
std::string error
Empty when it compiled. Otherwise what is wrong, as a sentence.
Definition pattern.h:119
Pattern pattern
Definition pattern.h:117
One capture position in a pattern.
Definition pattern.h:88
HoleType type
Definition pattern.h:91
std::string name
The field it fills. Empty for {}, which is read by position.
Definition pattern.h:90
size_t position
Which hole this is, counting from zero, which is what {} is read by.
Definition pattern.h:93
Definition pattern.h:98
Hole hole
Definition pattern.h:103
std::string literal
The literal's text. A run of whitespace in it matches any run.
Definition pattern.h:102
bool is_hole
A hole when true, and otherwise a literal to match.
Definition pattern.h:100
A compiled pattern: literals and holes, in the order they were written.
Definition pattern.h:97
bool AllNamed() const
Whether every hole has a name, which is what makes the result a record with fields rather than one re...
Definition pattern.cc:324
std::vector< Hole > holes
Every hole, in order. What a shape is built from.
Definition pattern.h:108
std::vector< Piece > pieces
Definition pattern.h:106