diff --git a/src/numen/lexer.cpp b/src/numen/lexer.cpp index 536368d..941d35d 100644 --- a/src/numen/lexer.cpp +++ b/src/numen/lexer.cpp @@ -101,6 +101,11 @@ std::optional Lexer::next() { break; } if (c == '"' || c == '\'') { + // glued to a number it is a prime mark, a unit name as in 5'10" + if (m_cursor > 0 && isDigit(m_data[m_cursor - 1])) { + ++m_cursor; + return makeToken(TokenType::String, String{.data = getSelection()}); + } state = State::StringLiteral; stringLiteralOpener = c; startPos += 1; diff --git a/src/numen/parser.cpp b/src/numen/parser.cpp index da7361c..ecca342 100644 --- a/src/numen/parser.cpp +++ b/src/numen/parser.cpp @@ -260,6 +260,31 @@ std::optional Parser::parseConversionTarget() { return target; } +// 5'10" is 5' + 10", and the closing mark can be left off as in 5'10. the lexer +// only reads a quote as a prime mark when it is glued to a number +std::unique_ptr Parser::parsePrimes(std::unique_ptr major) { + const auto isMark = [](const std::optional &tok, std::string_view mark) { + return tok && tok->type == Lexer::TokenType::String && tok->raw == mark; + }; + + auto mark = m_lexer.peak(); + if (!isMark(mark, "'") && !isMark(mark, "\"")) return major; + m_lexer.next(); + + auto expr = makeUnit(std::move(major), NamedUnit{.terms = {NamedUnitTerm{.name = mark->raw}}}); + if (mark->raw != "'") return expr; + + auto minor = m_lexer.peakIf(Lexer::TokenType::Number); + if (!minor || !mark->isAdjacent(*minor)) return expr; + m_lexer.next(); + + if (isMark(m_lexer.peak(), "\"")) m_lexer.next(); + + auto rhs = + makeUnit(makeNumberExpr(minor->asNumber()->n), NamedUnit{.terms = {NamedUnitTerm{.name = "\""}}}); + return makeBinExpr(std::move(expr), std::move(rhs), "+"); +} + // Jan 18 2021 // 18 Jan 2021 // 18 Jan @@ -908,7 +933,7 @@ std::unique_ptr Parser::parseTerm() { if (auto tok = m_lexer.peak()) { if (auto n = parseNumber()) { - expr = makeNumberExpr(*n); + expr = parsePrimes(makeNumberExpr(*n)); } else if (auto name = m_lexer.peakIf(Lexer::TokenType::String)) { if (auto next = m_lexer.peak(1); next && next->raw == "(") { m_lexer.next(); diff --git a/src/numen/parser.hpp b/src/numen/parser.hpp index 00f4a94..5305bd6 100644 --- a/src/numen/parser.hpp +++ b/src/numen/parser.hpp @@ -228,6 +228,7 @@ class Parser { std::unique_ptr pratParse(int minPrec = 0); std::unique_ptr parseMul() { return pratParse(4); } std::optional parseNumber(); + std::unique_ptr parsePrimes(std::unique_ptr major); std::unique_ptr parseUntil(); bool isFunctionParameterSeparator(std::string_view tok) const; diff --git a/src/numen/unit.cpp b/src/numen/unit.cpp index a5519a0..730e21b 100644 --- a/src/numen/unit.cpp +++ b/src/numen/unit.cpp @@ -216,6 +216,9 @@ const UnitDef *CompoundUnit::sole() const { CompoundUnit soleUnit(UnitDef def) { return CompoundUnit{{UnitTerm{.def = std::move(def)}}}; } std::vector UnitDatabase::matchExact(std::string_view q) const { + // a unit with no symbol would otherwise match the empty name + if (q.empty()) return {}; + return m_units | std::views::filter([&](const UnitDef &unit) { return equalsIgnoreCase(unit.id, q) || unit.symbol == q || std::ranges::any_of(unit.aliases, [&](auto &&str) { return equalsIgnoreCase(str, q); }); diff --git a/tests/corpus/prime.corpus b/tests/corpus/prime.corpus new file mode 100644 index 0000000..33ed477 --- /dev/null +++ b/tests/corpus/prime.corpus @@ -0,0 +1,42 @@ +# prime notation: a quote glued to a number is feet (') or inches (") +5' => 5ft +5" => 5in +1.5' => 1.5ft +5'10" => 5.833333ft +6'0" => 6ft + +# the closing inch mark can be left off +5'10 => 5.833333ft +5'2 => 5.166667ft + +# the inches are never read as a squared unit +5'2" => 5.166667ft +5'3 => 5.25ft + +# conversions +5'10" to cm => 177.8cm +5'10 to cm => 177.8cm +5'10" to in => 70in +5'11.5" to cm => 181.61cm +5' to cm => 152.4cm +5" to cm => 12.7cm + +# arithmetic on the compound +5'10" + 2" => 6ft +2 * 5'10" => 11.666667ft +-5'10" => -5.833333ft + +# only lengths for now: the minutes and seconds reading is not supported +5'10" to s => !error Incompatible units: foot (length) to second (duration) + +# an empty name is never a unit (it used to read as parsec) +5 '' => 5 + +# a quote that is not glued to a number still delimits text +"5'10" => 5'10 +'5"' => 5" +5 'abc' => 5 +'hello world' => hello world +upper('a1') => A1 +"12:30" => 12:30 +'unfinished => unfinished