From b09fa00cca1a9baa0327a33d865fc970f77d4db0 Mon Sep 17 00:00:00 2001 From: couriersud Date: Fri, 18 Oct 2019 17:57:55 +0200 Subject: Netlist: code maintenance and improvements. [Couriersud] - Added support for line markers to the preprocessor and parser. - Added support for include processing to the preprocessor. - Moved sources base type to plib to be used for preprocessor includes. This enables to include e.g. from rom memory regions. - Renamed some defines --- src/lib/netlist/plib/pparser.h | 67 +++++++++++++++++++++++++++--------------- 1 file changed, 44 insertions(+), 23 deletions(-) (limited to 'src/lib/netlist/plib/pparser.h') diff --git a/src/lib/netlist/plib/pparser.h b/src/lib/netlist/plib/pparser.h index 5332ee2518c..ced7e500cad 100644 --- a/src/lib/netlist/plib/pparser.h +++ b/src/lib/netlist/plib/pparser.h @@ -12,9 +12,11 @@ #include "pstream.h" #include "pstring.h" +#include "putil.h" // psource_t + //#include #include - +#include namespace plib { class ptokenizer @@ -28,6 +30,7 @@ public: , m_px(m_cur_line.begin()) , m_unget(0) , m_string('"') + , m_support_line_markers(true) // FIXME { } @@ -42,6 +45,7 @@ public: TOKEN, STRING, COMMENT, + LINEMARKER, UNKNOWN, ENDOFFILE }; @@ -156,6 +160,7 @@ private: token_id_t m_tok_comment_start; token_id_t m_tok_comment_end; token_id_t m_tok_line_comment; + bool m_support_line_markers; }; @@ -174,26 +179,7 @@ public: using defines_map_type = std::unordered_map; - explicit ppreprocessor(defines_map_type *defines = nullptr); - - ~ppreprocessor() override - { - delete rdbuf(); - } - - template - ppreprocessor & process(T &&istrm) - { - putf8_reader reader(std::forward(istrm)); - pstring line; - while (reader.readline(line)) - { - m_lineno++; - line = process_line(line); - m_outbuf += decltype(m_outbuf)(line.c_str()) + static_cast(10); - } - return *this; - } + explicit ppreprocessor(psource_collection_t<> &sources, defines_map_type *defines = nullptr); COPYASSIGN(ppreprocessor, delete) ppreprocessor &operator=(ppreprocessor &&src) = delete; @@ -201,10 +187,10 @@ public: ppreprocessor(ppreprocessor &&s) noexcept : std::istream(new readbuffer(this)) , m_defines(std::move(s.m_defines)) + , m_sources(s.m_sources) , m_expr_sep(std::move(s.m_expr_sep)) , m_if_flag(s.m_if_flag) , m_if_level(s.m_if_level) - , m_lineno(s.m_lineno) , m_outbuf(std::move(s.m_outbuf)) , m_pos(s.m_pos) , m_state(s.m_state) @@ -212,6 +198,19 @@ public: { } + ~ppreprocessor() override + { + delete rdbuf(); + } + + template + ppreprocessor & process(T &&istrm) + { + m_stack.emplace(input_context(std::forward(istrm),"","")); + process_stack(); + return *this; + } + protected: class readbuffer : public std::streambuf @@ -242,6 +241,7 @@ protected: ? std::char_traits::eof() : std::char_traits::to_int_type(*this->gptr()); } + private: ppreprocessor *m_strm; std::array m_buf; @@ -261,20 +261,41 @@ private: LINE_CONTINUATION }; + void process_stack(); + pstring process_line(pstring line); pstring process_comments(pstring line); defines_map_type m_defines; + psource_collection_t<> &m_sources; std::vector m_expr_sep; std::uint_least64_t m_if_flag; // 31 if levels int m_if_level; - int m_lineno; + + struct input_context + { + template + input_context(T &&istrm, const pstring &local_path, const pstring &name) + : m_reader(std::forward(istrm)) + , m_lineno(0) + , m_local_path(local_path) + , m_name(name) + {} + + putf8_reader m_reader; + int m_lineno; + pstring m_local_path; + pstring m_name; + }; + + std::stack m_stack; pstring_t m_outbuf; std::istream::pos_type m_pos; state_e m_state; pstring m_line; bool m_comment; + }; } // namespace plib -- cgit v1.2.3-70-g09d2