Loading...
Searching...
No Matches
parser.h
Go to the documentation of this file.
1/*
2 * Copyright (c) 2003-2023, 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
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,
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#if !NO_ASSERTS
68 void push_token(const token_t& tok) const {
69 assert(&tok == &lookahead);
70 use_lookahead = true;
71 }
72#else
73 void push_token(const token_t&) const {
74 use_lookahead = true;
75 }
76#endif // !NO_ASSERTS
77 void push_token() const {
78 use_lookahead = true;
79 }
80
81 ptr_op_t parse_value_term(std::istream& in,
82 const parse_flags_t& flags) const;
83 ptr_op_t parse_call_expr(std::istream& in,
84 const parse_flags_t& flags) const;
85 ptr_op_t parse_dot_expr(std::istream& in,
86 const parse_flags_t& flags) const;
87 ptr_op_t parse_unary_expr(std::istream& in,
88 const parse_flags_t& flags) const;
89 ptr_op_t parse_mul_expr(std::istream& in,
90 const parse_flags_t& flags) const;
91 ptr_op_t parse_add_expr(std::istream& in,
92 const parse_flags_t& flags) const;
93 ptr_op_t parse_logic_expr(std::istream& in,
94 const parse_flags_t& flags) const;
95 ptr_op_t parse_and_expr(std::istream& in,
96 const parse_flags_t& flags) const;
97 ptr_op_t parse_or_expr(std::istream& in,
98 const parse_flags_t& flags) const;
99 ptr_op_t parse_querycolon_expr(std::istream& in,
100 const parse_flags_t& flags) const;
101 ptr_op_t parse_comma_expr(std::istream& in,
102 const parse_flags_t& flags) const;
103 ptr_op_t parse_lambda_expr(std::istream& in,
104 const parse_flags_t& flags) const;
105 ptr_op_t parse_assign_expr(std::istream& in,
106 const parse_flags_t& flags) const;
107 ptr_op_t parse_value_expr(std::istream& in,
108 const parse_flags_t& flags) const;
109
110public:
111 parser_t() : use_lookahead(false) {
112 TRACE_CTOR(parser_t, "");
113 }
117
118 ptr_op_t parse(std::istream& in,
119 const parse_flags_t& flags = PARSE_DEFAULT,
120 const optional<string>& original_string = boost::none);
121};
122
123} // 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
T & downcast(U &object)
Definition utils.h:468
@ 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)
void expected(const char wanted, const int c)
enum ledger::expr_t::token_t::kind_t kind