FuncSketch
Loading...
Searching...
No Matches
expression_grammar.h
Go to the documentation of this file.
1/*
2 * Copyright 2026 MusicScience37 (Kenta Kabashima)
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 */
20#pragma once
21
22#include <string>
23
24#include <boost/phoenix.hpp>
25#include <boost/spirit/home/qi.hpp>
26
28
29namespace func_sketch::parser {
30
35 : public boost::spirit::qi::grammar<std::string::const_iterator,
36 ParsedExpression(), boost::spirit::ascii::space_type> {
37public:
39 using IteratorType = std::string::const_iterator;
40
45
51 [[nodiscard]] const std::string& error_message() const noexcept;
52
56 void clear_error_message() noexcept;
57
66 [[nodiscard]] ParsedExpression parse(const std::string& str);
67
68private:
74 template <typename T>
75 using Rule = boost::spirit::qi::rule<IteratorType, T(),
76 boost::spirit::ascii::space_type>;
77
80
83
86
89
92
95
98
101
104
107
110
112 std::string error_message_;
113};
114
115} // namespace func_sketch::parser
const std::string & error_message() const noexcept
Get the error message from the last parse attempt.
Rule< ParsedFunctionCallExpression > function_call_expr_rule_
Rule of function call expressions.
Rule< ParsedLiteral > literal_rule_
Rule of literals.
boost::spirit::qi::rule< IteratorType, T(), boost::spirit::ascii::space_type > Rule
Type of rules.
void clear_error_message() noexcept
Clear the error message.
Rule< ParsedExpression > term_expr_rule_
Rule of term expressions.
Rule< Complex > imaginary_number_rule_
Rule of imaginary numbers.
std::string::const_iterator IteratorType
Type of iterator.
Rule< ParsedExpression > expr_rule_
Rule of the whole expression.
ParsedExpression parse(const std::string &str)
Parse with error handling.
Rule< ParsedExpression > sum_expr_rule_
Rule of sum expressions.
Rule< ParsedExpression > value_expr_rule_
Rule of value expressions.
Rule< ParsedExpression > factor_expr_rule_
Rule of factor expressions.
Rule< ParsedExpression > atomic_value_expr_rule_
Rule of atomic value expressions.
Rule< ParsedExpression > unary_expr_rule_
Rule of unary expressions.
std::string error_message_
Error message from the last parse attempt.
Rule< ParsedIdentifier > identifier_rule_
Rule of identifiers.
std::complex< Real > Complex
Type of complex numbers in this project.
Definition of ParsedExpression structure.
boost::variant< ParsedLiteral, ParsedIdentifier, boost::recursive_wrapper< ParsedFunctionCallExpression >, boost::recursive_wrapper< ParsedUnaryExpression >, boost::recursive_wrapper< ParsedBinaryExpression > > ParsedExpression
Variant of parsed expressions.
Struct of parsed function call expressions.
Struct of parsed identifiers.