Loading...
Searching...
No Matches
parser.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2025, John Wiegley. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 * - Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * - Neither the name of New Artisans LLC nor the names of its
16 * contributors may be used to endorse or promote products derived from
17 * this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
35
42#pragma once
43
44#include "token.h"
45#include "op.h"
46
47namespace ledger {
48
49class expr_t::parser_t : public noncopyable
50{
51 mutable token_t lookahead;
52 mutable bool use_lookahead;
53
54 token_t& next_token(std::istream& in, const parse_flags_t& tflags,
55 const optional<token_t::kind_t>& expecting = none) const {
56 if (use_lookahead)
57 use_lookahead = false;
58 else
59 lookahead.next(in, tflags);
60
61 if (expecting && lookahead.kind != *expecting)
62 lookahead.expected(*expecting);
63
64 return lookahead;
65 }
66
67 void push_token(const token_t& tok) const {
68 assert(&tok == &lookahead);
69 use_lookahead = true;
70 }
71
72 void push_token() const {
73 use_lookahead = true;
74 }
75
76 ptr_op_t parse_value_term(std::istream& in,
77 const parse_flags_t& flags) const;
78 ptr_op_t parse_call_expr(std::istream& in,
79 const parse_flags_t& flags) const;
80 ptr_op_t parse_dot_expr(std::istream& in,
81 const parse_flags_t& flags) const;
82 ptr_op_t parse_unary_expr(std::istream& in,
83 const parse_flags_t& flags) const;
84 ptr_op_t parse_mul_expr(std::istream& in,
85 const parse_flags_t& flags) const;
86 ptr_op_t parse_add_expr(std::istream& in,
87 const parse_flags_t& flags) const;
88 ptr_op_t parse_logic_expr(std::istream& in,
89 const parse_flags_t& flags) const;
90 ptr_op_t parse_and_expr(std::istream& in,
91 const parse_flags_t& flags) const;
92 ptr_op_t parse_or_expr(std::istream& in,
93 const parse_flags_t& flags) const;
94 ptr_op_t parse_querycolon_expr(std::istream& in,
95 const parse_flags_t& flags) const;
96 ptr_op_t parse_comma_expr(std::istream& in,
97 const parse_flags_t& flags) const;
98 ptr_op_t parse_lambda_expr(std::istream& in,
99 const parse_flags_t& flags) const;
100 ptr_op_t parse_assign_expr(std::istream& in,
101 const parse_flags_t& flags) const;
102 ptr_op_t parse_value_expr(std::istream& in,
103 const parse_flags_t& flags) const;
104
105public:
106 parser_t() : use_lookahead(false) {
107 TRACE_CTOR(parser_t, "");
108 }
109 ~parser_t() throw() {
111 }
112
113 ptr_op_t parse(std::istream& in,
115 const optional<string>& original_string = boost::none);
116};
117
118} // namespace ledger
#define TRACE_DTOR(cls)
Definition utils.h:144
#define TRACE_CTOR(cls, args)
Definition utils.h:143
#define assert(x)
Definition utils.h:92
flags::basic_t< parse_flags_enum_t, uint_least8_t > parse_flags_t
Definition amount.h:79
@ PARSE_DEFAULT
Definition amount.h:68
intrusive_ptr< op_t > ptr_op_t
Definition expr.h:57
ptr_op_t parse(std::istream &in, const parse_flags_t &flags=PARSE_DEFAULT, const optional< string > &original_string=boost::none)
void next(std::istream &in, const parse_flags_t &flags)