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
31
33enum class HoleType {
35 kString,
37 kInt,
39 kNumber,
41 kBool,
43 kWord,
45 kLine,
47 kRest,
51 kTime,
53 kJson,
54};
55
56std::string_view HoleTypeName(HoleType type);
57
59std::optional<HoleType> HoleTypeFromName(std::string_view name);
60
62struct Hole {
64 std::string name;
67 size_t position = 0;
68};
69
71struct Pattern {
72 struct Piece {
74 bool is_hole = false;
76 std::string literal;
78 };
79
80 std::vector<Piece> pieces;
82 std::vector<Hole> holes;
83
86 [[nodiscard]] bool AllNamed() const;
87};
88
90struct Compiled {
93 std::string error;
95 size_t column = 0;
96
97 [[nodiscard]] bool ok() const { return error.empty(); }
98};
99
101Compiled Compile(std::string_view text);
102
104struct Capture {
105 std::string_view text;
106};
107
112std::optional<std::vector<Capture>> Match(const Pattern& pattern,
113 std::string_view subject);
114
115} // namespace a11::flow::pattern
116
117#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:596
std::string type
The type and what follows it: string required, list[a11.Chunk] stream.
Definition format.cc:52
std::string name
The declaration name and its colon.
Definition format.cc:50
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:412
Compiled Compile(std::string_view text)
Compile a pattern, saying what is wrong rather than throwing.
Definition pattern.cc:302
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:278
HoleType
Literal text and named holes used by match, such as age={age:int}.
Definition pattern.h:33
@ 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:252
std::string subject
What to call it in the message: ‘'Source’, orthe pattern`.
Definition resolve.cc:6292
What one hole took: a view into the subject that was matched.
Definition pattern.h:104
std::string_view text
Definition pattern.h:105
A pattern, or what is wrong with the text it was written as.
Definition pattern.h:90
size_t column
Where the problem is, counting characters from the start of the pattern.
Definition pattern.h:95
bool ok() const
Definition pattern.h:97
std::string error
Empty when it compiled. Otherwise what is wrong, as a sentence.
Definition pattern.h:93
Pattern pattern
Definition pattern.h:91
One capture position in a pattern.
Definition pattern.h:62
HoleType type
Definition pattern.h:65
std::string name
The field it fills. Empty for {}, which is read by position.
Definition pattern.h:64
size_t position
Which hole this is, counting from zero, which is what {} is read by.
Definition pattern.h:67
Definition pattern.h:72
Hole hole
Definition pattern.h:77
std::string literal
The literal's text. A run of whitespace in it matches any run.
Definition pattern.h:76
bool is_hole
A hole when true, and otherwise a literal to match.
Definition pattern.h:74
A compiled pattern: literals and holes, in the order they were written.
Definition pattern.h:71
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:293
std::vector< Hole > holes
Every hole, in order. What a shape is built from.
Definition pattern.h:82
std::vector< Piece > pieces
Definition pattern.h:80