diff options
author | Robert Hensing <robert@roberthensing.nl> | 2023-04-16 14:07:35 +0200 |
---|---|---|
committer | Robert Hensing <robert@roberthensing.nl> | 2023-04-16 14:07:35 +0200 |
commit | b6125772d7d5f82d48873fc93a7f261832154b14 (patch) | |
tree | 2700c6ab861df273c949739d06d6b1e6745d1819 /src/libexpr/print.cc | |
parent | 28a5cdde02964306e7eb443f696c8d5d59ebf9e9 (diff) |
libexpr: Move identifier-like printing to print.cc
Diffstat (limited to 'src/libexpr/print.cc')
-rw-r--r-- | src/libexpr/print.cc | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/src/libexpr/print.cc b/src/libexpr/print.cc index 282903b72..d08672cfc 100644 --- a/src/libexpr/print.cc +++ b/src/libexpr/print.cc @@ -25,4 +25,54 @@ printLiteralBool(std::ostream & str, bool boolean) return str; } +std::ostream & +printIdentifier(std::ostream & str, std::string_view s) { + if (s.empty()) + str << "\"\""; + else if (s == "if") // FIXME: handle other keywords + str << '"' << s << '"'; + else { + char c = s[0]; + if (!((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_')) { + printLiteralString(str, s); + return str; + } + for (auto c : s) + if (!((c >= 'a' && c <= 'z') || + (c >= 'A' && c <= 'Z') || + (c >= '0' && c <= '9') || + c == '_' || c == '\'' || c == '-')) { + printLiteralString(str, s); + return str; + } + str << s; + } + return str; +} + +// FIXME: keywords +static bool isVarName(std::string_view s) +{ + if (s.size() == 0) return false; + char c = s[0]; + if ((c >= '0' && c <= '9') || c == '-' || c == '\'') return false; + for (auto & i : s) + if (!((i >= 'a' && i <= 'z') || + (i >= 'A' && i <= 'Z') || + (i >= '0' && i <= '9') || + i == '_' || i == '-' || i == '\'')) + return false; + return true; +} + +std::ostream & +printAttributeName(std::ostream & str, std::string_view name) { + if (isVarName(name)) + str << name; + else + printLiteralString(str, name); + return str; +} + + } |