diff options
author | 2019-03-25 22:44:58 +0100 | |
---|---|---|
committer | 2019-03-25 22:44:58 +0100 | |
commit | c24473ddff715ecec2e258a6eb38960cf8c8e98e (patch) | |
tree | 8ea44b6396a6129913c0aac13859b5de9965e972 /src/lib/netlist/plib/pparser.h | |
parent | 009cba4fb8102102168ef32870892438327f3705 (diff) | |
parent | 598cd5227223c3b04ca31f0dbc1981256d9ea3ff (diff) |
conflict resolution (nw)
Diffstat (limited to 'src/lib/netlist/plib/pparser.h')
-rw-r--r-- | src/lib/netlist/plib/pparser.h | 107 |
1 files changed, 83 insertions, 24 deletions
diff --git a/src/lib/netlist/plib/pparser.h b/src/lib/netlist/plib/pparser.h index 7ec517255c8..1eca20e99ad 100644 --- a/src/lib/netlist/plib/pparser.h +++ b/src/lib/netlist/plib/pparser.h @@ -8,20 +8,27 @@ #ifndef PPARSER_H_ #define PPARSER_H_ -#include "pstring.h" #include "plists.h" #include "pstream.h" +#include "pstring.h" -#include <unordered_map> #include <cstdint> +#include <unordered_map> + namespace plib { -class ptokenizer : nocopyassignmove +class ptokenizer { public: - explicit ptokenizer(plib::putf8_reader &strm); + template <typename T> + ptokenizer(T &&strm) // NOLINT(misc-forwarding-reference-overload, bugprone-forwarding-reference-overload) + : m_strm(std::forward<T>(strm)), m_lineno(0), m_cur_line(""), m_px(m_cur_line.begin()), m_unget(0), m_string('"') + { + } + + COPYASSIGNMOVE(ptokenizer, delete) - virtual ~ptokenizer(); + virtual ~ptokenizer() = default; enum token_type { @@ -91,22 +98,23 @@ public: void require_token(const token_id_t &token_num); void require_token(const token_t &tok, const token_id_t &token_num); - token_id_t register_token(pstring token) + token_id_t register_token(const pstring &token) { token_id_t ret(m_tokens.size()); m_tokens.emplace(token, ret); return ret; } - void set_identifier_chars(pstring s) { m_identifier_chars = s; } - void set_number_chars(pstring st, pstring rem) { m_number_chars_start = st; m_number_chars = rem; } - void set_string_char(pstring::code_t c) { m_string = c; } - void set_whitespace(pstring s) { m_whitespace = s; } - void set_comment(pstring start, pstring end, pstring line) + ptokenizer & identifier_chars(pstring s) { m_identifier_chars = std::move(s); return *this; } + ptokenizer & number_chars(pstring st, pstring rem) { m_number_chars_start = std::move(st); m_number_chars = std::move(rem); return *this; } + ptokenizer & string_char(pstring::value_type c) { m_string = c; return *this; } + ptokenizer & whitespace(pstring s) { m_whitespace = std::move(s); return *this; } + ptokenizer & comment(const pstring &start, const pstring &end, const pstring &line) { m_tok_comment_start = register_token(start); m_tok_comment_end = register_token(end); m_tok_line_comment = register_token(line); + return *this; } token_t get_token_internal(); @@ -118,17 +126,17 @@ protected: private: void skipeol(); - pstring::code_t getc(); - void ungetc(pstring::code_t c); + pstring::value_type getc(); + void ungetc(pstring::value_type c); bool eof() { return m_strm.eof(); } - putf8_reader &m_strm; + putf8_reader m_strm; int m_lineno; pstring m_cur_line; pstring::const_iterator m_px; - pstring::code_t m_unget; + pstring::value_type m_unget; /* tokenizer stuff follows ... */ @@ -137,7 +145,7 @@ private: pstring m_number_chars_start; std::unordered_map<pstring, token_id_t> m_tokens; pstring m_whitespace; - pstring::code_t m_string; + pstring::value_type m_string; token_id_t m_tok_comment_start; token_id_t m_tok_comment_end; @@ -145,7 +153,7 @@ private: }; -class ppreprocessor : plib::nocopyassignmove +class ppreprocessor : public pistream { public: @@ -158,29 +166,80 @@ public: pstring m_replace; }; - explicit ppreprocessor(std::vector<define_t> *defines = nullptr); - virtual ~ppreprocessor() {} + using defines_map_type = std::unordered_map<pstring, define_t>; + + explicit ppreprocessor(defines_map_type *defines = nullptr); + ~ppreprocessor() override = default; + + template <typename T> + ppreprocessor & process(T &&istrm) + { + putf8_reader reader(std::forward<T>(istrm)); + pstring line; + while (reader.readline(line)) + { + m_lineno++; + line = process_line(line); + m_buf += decltype(m_buf)(line.c_str()) + static_cast<char>(10); + } + return *this; + } + + COPYASSIGN(ppreprocessor, delete) + ppreprocessor &operator=(ppreprocessor &&src) = delete; - void process(putf8_reader &istrm, putf8_writer &ostrm); + + ppreprocessor(ppreprocessor &&s) noexcept + : m_defines(std::move(s.m_defines)) + , m_expr_sep(std::move(s.m_expr_sep)) + , m_ifflag(s.m_ifflag) + , m_level(s.m_level) + , m_lineno(s.m_lineno) + , m_buf(std::move(s.m_buf)) + , m_pos(s.m_pos) + , m_state(s.m_state) + , m_comment(s.m_comment) + { + } protected: - double expr(const std::vector<pstring> &sexpr, std::size_t &start, int prio); + + size_type vread(value_type *buf, const size_type n) override; + void vseek(const pos_type n) override + { + plib::unused_var(n); + /* FIXME throw exception - should be done in base unless implemented */ + } + pos_type vtell() const override { return m_pos; } + + int expr(const std::vector<pstring> &sexpr, std::size_t &start, int prio); define_t *get_define(const pstring &name); pstring replace_macros(const pstring &line); virtual void error(const pstring &err); private: - pstring process_line(const pstring &line); + enum state_e + { + PROCESS, + LINE_CONTINUATION + }; + pstring process_line(pstring line); + pstring process_comments(pstring line); - std::unordered_map<pstring, define_t> m_defines; + defines_map_type m_defines; std::vector<pstring> m_expr_sep; std::uint_least64_t m_ifflag; // 31 if levels int m_level; int m_lineno; + pstring_t<pu8_traits> m_buf; + pos_type m_pos; + state_e m_state; + pstring m_line; + bool m_comment; }; -} +} // namespace plib #endif /* PPARSER_H_ */ |