#include "parser.h" #include #include #include #include static auto a() -> void { const std::array val{ "1", "2", "3", "4", "5", }; // write { deco::write_file file("out.deco"); auto& stream = file.stream; deco::serialize(stream, deco::make_list("std::array", val)); } // read { auto file = deco::read_file("out.deco"); auto& stream = file.stream; std::cout << file.str; std::array in_val; deco::serialize(stream, deco::make_list("std::array", in_val)); assert(in_val == val); } } namespace wparse { namespace { enum class c : int { none = 0, cntrl = 1, print = 2, space = 4, blank = 8, graph = 16, punct = 32, alnum = 64, alpha = 128, upper = 256, lower = 512, digit = 1024, xdigit = 2048, }; [[nodiscard]] constexpr auto operator|(c l, c r) -> c { return static_cast(static_cast(l) | static_cast(r)); } [[nodiscard, maybe_unused]] constexpr auto operator*(bool l, c r) -> c { return l ? r : c::none; } [[nodiscard, maybe_unused]] constexpr auto operator*(c l, bool r) -> c { return r ? l : c::none; } [[nodiscard]] constexpr auto in(int v, int min, int max) -> bool { return v >= min and v <= max; } [[nodiscard]] constexpr auto classify_r(int C) -> c { return c::cntrl * (in(C, '\x0', '\x1F') or C == '\x7F') | c::print * (in(C, ' ', '~')) | c::space * (in(C, '\t', '\r') or C == ' ') | c::blank * (C == '\t' or C == ' ') | c::graph * (in(C, '!', '~')) | c::punct * (in(C, '!', '/') or in(C, ':', '@') or in(C, '[', '`') or in(C, '{', '~')) | c::alnum * (in(C, 'A', 'Z') or in(C, 'a', 'z') or in(C, '0', '9')) | c::alpha * (in(C, 'A', 'Z') or in(C, 'a', 'z')) | c::upper * (in(C, 'A', 'Z')) | c::lower * (in(C, 'a', 'z')) | c::digit * (in(C, '0', '9')) | c::xdigit * (in(C, '0', '9')); } [[nodiscard]] constexpr auto make_char_table() -> std::array { std::array a{}; for (auto ch : kblib::range(256)) { a[kblib::signed_cast(ch)] = classify_r(ch); } return a; } [[nodiscard, maybe_unused]] constexpr auto classify(char ch) -> c { constexpr std::array table = make_char_table(); return table[kblib::signed_cast(+ch)]; } } // namespace } // namespace wparse /* EXAMPLES ; format: ; "string" ; [tokens] ; (syntax tree) "" [] (val) "a", [text "a"] (val (atext "a")) "{a}", [symbol "{", text "a", symbol "}"] (val (noderef (nname "a"))) "<0>" [symbol "<", text "0", symbol ">"] (val (atext (argref (aref 0)))) "<...>" [symbol "<", symbol "...", symbol ">"] (val (atext (argref (aref (elipsis nil nil))))) "{char:1 0}" [symbol "{", text "char", symbol ":", number "1", sep " ", number "0", symbol "}"] (val (noderef (nname "char") (flist 1 0))) "{Node!0:1 4}" [symbol "{", text "Node", symbol "<", text "f", symbol "(", text "p", symbol "|", text "p2", symbol ")", symbol ">", symbol "!", integer "0", symbol ":", number "1", sep " ", integer "4", symbol "}"] (val (noderef (nname (atext "a" (argref (function "f" (atext "p") (atext "p2"))))) (ilist (iref 0 1.0) (iref 4)))) "\"{text|<0>}{valString2|<0>}\"" [text "\"", symbol "{", text "text", symbol "|", symbol "<", text "0", symbol ">", symbol "}", symbol "{", text "valString2", symbol "|", symbol "<", text "0", symbol ">", symbol "}", text "\""] (val (atext "\"") (noderef (nname "text") (nargs (atext (argref 0)))) (noderef (nname "valString2") (nargs (atext (argref 0)))) (atext "\"")) "" [symbol "<", text "eq", symbol "(", symbol "#", aref "0", symbol "|", text "1", symbol "|", text "1", symbol "|", text "0", symbol ")", symbol ">"] (val (argref (fname "eq") (aref 0) 1 1 0)) "<+(#0|...)>" [symbol "<", text "+", symbol "(", symbol "#", aref "0", symbol "|", symbol "...", symbol ")", symbol ">"] (val (argref (fname "+") (aref 0) (aref (elipsis)))) "{Noun!0:4 1:2 2:1}{|-}{Noun/Root}" [symbol "{", text "Noun", symbol "!", integer "0", symbol ":", number "4", sep " ", integer "1", symbol ":", number "2", sep " ", integer "2", symbol ":", number "1", symbol "}", symbol "{", symbol "|", text "-", symbol "}", symbol "{", text "Noun/Root", symbol "}"] (val (noderef (nname "Noun") (ilist (iref 0 4.0) (iref 1 2.0) (iref 2 1.0))) (noderef (nname "") (nargs (atext "-"))) (noderef (nname "Noun/Root"))) "" */