Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/numen/lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ std::optional<Lexer::Token> 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;
Expand Down
27 changes: 26 additions & 1 deletion src/numen/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,31 @@ std::optional<NamedUnit> 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<Expression> Parser::parsePrimes(std::unique_ptr<Expression> major) {
const auto isMark = [](const std::optional<Lexer::Token> &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
Expand Down Expand Up @@ -908,7 +933,7 @@ std::unique_ptr<Expression> 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();
Expand Down
1 change: 1 addition & 0 deletions src/numen/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ class Parser {
std::unique_ptr<Expression> pratParse(int minPrec = 0);
std::unique_ptr<Expression> parseMul() { return pratParse(4); }
std::optional<numen::Value> parseNumber();
std::unique_ptr<Expression> parsePrimes(std::unique_ptr<Expression> major);
std::unique_ptr<Expression> parseUntil();
bool isFunctionParameterSeparator(std::string_view tok) const;

Expand Down
3 changes: 3 additions & 0 deletions src/numen/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,9 @@ const UnitDef *CompoundUnit::sole() const {
CompoundUnit soleUnit(UnitDef def) { return CompoundUnit{{UnitTerm{.def = std::move(def)}}}; }

std::vector<UnitDef> 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); });
Expand Down
42 changes: 42 additions & 0 deletions tests/corpus/prime.corpus
Original file line number Diff line number Diff line change
@@ -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
Loading