xo-tokenizer: move to scm ns + print() diagnostic

This commit is contained in:
Roland Conybeare 2024-08-05 14:44:39 -04:00
commit 2d0336058e
8 changed files with 49 additions and 19 deletions

View file

@ -9,7 +9,7 @@
#include <new> #include <new>
namespace xo { namespace xo {
namespace tok { namespace scm {
/** /**
* @class buffer buffer.hpp * @class buffer buffer.hpp
* *
@ -318,7 +318,7 @@ namespace xo {
swap(buffer<CharT> & lhs, buffer<CharT> & rhs) { swap(buffer<CharT> & lhs, buffer<CharT> & rhs) {
lhs.swap(rhs); lhs.swap(rhs);
} }
} /*namespace tok*/ } /*namespace scm*/
} /*namespace xo*/ } /*namespace xo*/
/* end buffer.hpp */ /* end buffer.hpp */

View file

@ -7,7 +7,7 @@
#include <cassert> #include <cassert>
namespace xo { namespace xo {
namespace tok { namespace scm {
/** @class span compression/span.hpp /** @class span compression/span.hpp
* *
* @brief Represents a contiguous memory range, without ownership. * @brief Represents a contiguous memory range, without ownership.
@ -137,5 +137,5 @@ namespace xo {
x.print(os); x.print(os);
return os; return os;
} }
} /*namespace tok*/ } /*namespace scm*/
} /*namespace xo*/ } /*namespace xo*/

View file

@ -8,11 +8,12 @@
#include "tokentype.hpp" #include "tokentype.hpp"
#include "xo/indentlog/print/tag.hpp" #include "xo/indentlog/print/tag.hpp"
#include <stdexcept> #include <stdexcept>
#include <ostream>
#include <string> #include <string>
#include <cstdint> #include <cstdint>
namespace xo { namespace xo {
namespace tok { namespace scm {
namespace detail { namespace detail {
/* compute a * b^p, p >= 0 */ /* compute a * b^p, p >= 0 */
constexpr double constexpr double
@ -101,6 +102,9 @@ namespace xo {
**/ **/
double f64_value() const; double f64_value() const;
/** print human-readable token representation on stream @p os **/
void print(std::ostream & os) const;
private: private:
/** category for this token **/ /** category for this token **/
tokentype tk_type_ = tokentype::tk_invalid; tokentype tk_type_ = tokentype::tk_invalid;
@ -327,7 +331,25 @@ namespace xo {
return retval; return retval;
} /*f64_value*/ } /*f64_value*/
} /*Namespace tok*/
template <typename CharT>
void
token<CharT>::print(std::ostream & os) const {
os << "<token"
<< xtag("type", tk_type_)
<< xtag("text", text_)
<< ">";
} /*print*/
template <typename CharT>
inline std::ostream &
operator<< (std::ostream & os,
const token<CharT> & tk)
{
tk.print(os);
return os;
}
} /*Namespace scm*/
} /*namespace xo*/ } /*namespace xo*/

View file

@ -11,7 +11,7 @@
#include <cassert> #include <cassert>
namespace xo { namespace xo {
namespace tok { namespace scm {
/** /**
* Use: * Use:
* @code * @code
@ -619,7 +619,7 @@ namespace xo {
return tk; return tk;
} /*notify_eof*/ } /*notify_eof*/
} /*namespace tok*/ } /*namespace scm*/
} /*namespace xo*/ } /*namespace xo*/
/* end tokenizer.hpp */ /* end tokenizer.hpp */

View file

@ -9,7 +9,7 @@
#include <ostream> #include <ostream>
namespace xo { namespace xo {
namespace tok { namespace scm {
/** @enum tokentype /** @enum tokentype
* @brief enum to identify different schematica input token types * @brief enum to identify different schematica input token types
* *
@ -124,6 +124,9 @@ namespace xo {
/** keyword 'in' **/ /** keyword 'in' **/
tk_in, tk_in,
/** keyword 'end' **/
tk_end,
n_tokentype /* comes last, counts #of entries */ n_tokentype /* comes last, counts #of entries */
}; /*tokentype*/ }; /*tokentype*/
@ -135,8 +138,7 @@ namespace xo {
os << tokentype_descr(tk_type); os << tokentype_descr(tk_type);
return os; return os;
} }
} /*namespace tok*/ } /*namespace scm*/
} /*namespace xo*/ } /*namespace xo*/
/* end tokentype.hpp */ /* end tokentype.hpp */

View file

@ -6,7 +6,7 @@
#include "tokentype.hpp" #include "tokentype.hpp"
namespace xo { namespace xo {
namespace tok { namespace scm {
char const * char const *
tokentype_descr(tokentype tk_type) tokentype_descr(tokentype tk_type)
{ {
@ -18,27 +18,33 @@ namespace xo {
CASE(tk_string); CASE(tk_string);
CASE(tk_symbol); CASE(tk_symbol);
CASE(tk_leftparen); CASE(tk_leftparen);
CASE(tk_rightparen); CASE(tk_rightparen);
CASE(tk_leftbracket); CASE(tk_leftbracket);
CASE(tk_rightbracket); CASE(tk_rightbracket);
CASE(tk_leftbrace); CASE(tk_leftbrace);
CASE(tk_rightbrace); CASE(tk_rightbrace);
CASE(tk_leftangle); CASE(tk_leftangle);
CASE(tk_rightangle); CASE(tk_rightangle);
CASE(tk_dot); CASE(tk_dot);
CASE(tk_comma); CASE(tk_comma);
CASE(tk_colon); CASE(tk_colon);
CASE(tk_doublecolon); CASE(tk_doublecolon);
CASE(tk_semicolon); CASE(tk_semicolon);
CASE(tk_singleassign); CASE(tk_singleassign);
CASE(tk_assign); CASE(tk_assign);
CASE(tk_yields); CASE(tk_yields);
CASE(tk_type); CASE(tk_type);
CASE(tk_def); CASE(tk_def);
CASE(tk_lambda); CASE(tk_lambda);
CASE(tk_if); CASE(tk_if);
CASE(tk_let); CASE(tk_let);
CASE(tk_in); CASE(tk_in);
CASE(tk_end);
case tokentype::tk_invalid: case tokentype::tk_invalid:
case tokentype::n_tokentype: case tokentype::n_tokentype:
@ -49,7 +55,7 @@ namespace xo {
return "???"; return "???";
} /*tokentype_descr*/ } /*tokentype_descr*/
} /*namespace tok*/ } /*namespace scm*/
} /*namespace xo*/ } /*namespace xo*/

View file

@ -8,8 +8,8 @@
#include <memory> #include <memory>
namespace xo { namespace xo {
using token = xo::tok::token<char>; using token = xo::scm::token<char>;
using xo::tok::tokentype; using xo::scm::tokentype;
namespace ut { namespace ut {
struct testcase_i64 { struct testcase_i64 {

View file

@ -7,9 +7,9 @@
#include <catch2/catch.hpp> #include <catch2/catch.hpp>
namespace xo { namespace xo {
using xo::tok::tokentype; using xo::scm::tokentype;
using token = xo::tok::token<char>; using token = xo::scm::token<char>;
using xo::tok::span; using xo::scm::span;
namespace ut { namespace ut {
namespace { namespace {
@ -111,7 +111,7 @@ namespace xo {
INFO(xtag("i_tc", i_tc)); INFO(xtag("i_tc", i_tc));
using tokenizer using tokenizer
= xo::tok::tokenizer<char>; = xo::scm::tokenizer<char>;
tokenizer tkz; tokenizer tkz;
tokenizer::span_type tokenizer::span_type