dot access on aliases

This commit is contained in:
2021-10-14 17:32:17 -04:00
parent c35c68a395
commit 961ad75451
3 changed files with 31 additions and 1 deletions

View File

@@ -129,7 +129,26 @@ class Reader {
};
// Because macro keys are sorted by length and peekChars(0) returns "", this will be used as the default reader macro:
readTable[""] = (stream:Stream, k) -> Symbol(nextToken(stream, "a symbol name"));
readTable[""] = (stream:Stream, k) -> {
var position = stream.position();
var token = nextToken(stream, "a symbol name");
// Process dot-access on alias identifiers
return if (token.indexOf(".") != -1) {
try {
Std.parseFloat(token);
Symbol(token);
} catch (err) {
var tokenParts = token.split(".");
var fieldExp = Symbol(tokenParts.shift());
while (tokenParts.length > 0) {
fieldExp = FieldExp(tokenParts.shift(), fieldExp.withPos(position));
}
fieldExp;
}
} else {
Symbol(token);
};
}
return readTable;
}

View File

@@ -309,6 +309,10 @@ class BasicTestCase extends Test {
function testLetThrow() {
_testLetThrow();
}
function testDotAccessOnAlias() {
_testDotAccessOnAlias();
}
}
class BasicObject {

View File

@@ -540,3 +540,10 @@
(Assert.fail)}
(catch [:String e]
(Assert.equals "the error we want" e))))
// Test dot-access on identifiers aliases
(var objWithField (object field 5))
(var float 0.5) // This should still read as a float, not a dot access on a variable called 0
(defAlias &ident owf objWithField)
(function _testDotAccessOnAlias []
(Assert.equals 5 owf.field))