summaryrefslogtreecommitdiffstats
path: root/src/libixion/formula_parser.hpp
blob: 3c28b679d132ddb2d5d94ebe342bd23ebc9892c5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_IXION_FORMULA_PARSER_HPP
#define INCLUDED_IXION_FORMULA_PARSER_HPP

#include "ixion/exceptions.hpp"
#include "ixion/formula_tokens.hpp"

#include "lexer_tokens.hpp"

#include <string>

namespace ixion {

class formula_name_resolver;
class model_context;

/**
 * Class formula_parser parses a series of primitive (or lexer) tokens
 * passed on from the lexer, and turn them into a series of formula tokens.
 * It also picks up a list of cells that it depends on.
 */
class formula_parser
{
    formula_parser() = delete;
    formula_parser(const formula_parser&) = delete;
    formula_parser& operator=(const formula_parser&) = delete;

public:
    class parse_error : public general_error
    {
    public:
        parse_error(const ::std::string& msg);
    };

    formula_parser(const lexer_tokens_t& tokens, model_context& cxt, const formula_name_resolver& resolver);
    ~formula_parser();

    void set_origin(const abs_address_t& pos);
    void parse();

    formula_tokens_t& get_tokens();

private:

    void primitive();
    void name();
    void literal();
    void value();
    void less();
    void greater();

    const lexer_token& get_token() const;
    bool has_token() const;
    bool has_next() const;
    void next();
    void prev();

private:
    lexer_tokens_t::const_iterator m_itr_cur;
    lexer_tokens_t::const_iterator m_itr_end;

    const lexer_tokens_t&   m_tokens; // lexer tokens of this expression
    model_context&   m_context;
    formula_tokens_t        m_formula_tokens;
    abs_address_t           m_pos;    // reference position (usually current cell). always absolute.

    const formula_name_resolver& m_resolver;
};

}

#endif

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */