#include "parser.h" #include 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 c operator|(c l, c r) { return static_cast(static_cast(l) | static_cast(r)); } [[nodiscard, maybe_unused]] constexpr c operator*(bool l, c r) { return l ? r : c::none; } [[nodiscard, maybe_unused]] constexpr c operator*(c l, bool r) { return r ? l : c::none; } [[nodiscard]] constexpr bool in(int v, int min, int max) { return v >= min && v <= max; } [[nodiscard]] constexpr c classify_r(int c) { return c::cntrl * (in(c, '\x0', '\x1F') || c == '\x7F') | c::print * (in(c, ' ', '~')) | c::space * (in(c, '\t', '\r') || c == ' ') | c::blank * (c == '\t' || c == ' ') | c::graph * (in(c, '!', '~')) | c::punct * (in(c, '!', '/') || in(c, ':', '@') || in(c, '[', '`') || in(c, '{', '~')) | c::alnum * (in(c, 'A', 'Z') || in(c, 'a', 'z') || in(c, '0', '9')) | c::alpha * (in(c, 'A', 'Z') || 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 std::array make_char_table() { std::array a{}; for (auto ch : kblib::range(256)) { a[kblib::signed_cast(ch)] = classify_r(ch); } return a; } [[nodiscard, maybe_unused]] constexpr c classify(char ch) { 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"))) "" */