Skip to content

Commit

Permalink
add logic for optional chaining (#21)
Browse files Browse the repository at this point in the history
* add logic for optional chaining

* remove console log

* format doc

* fix test

* fix uts
  • Loading branch information
nadiapadalka committed Oct 25, 2023
1 parent ab5322a commit 7569e86
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/parser/Parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,7 @@ export class Parser {
Lexeme.Newline,
Lexeme.Colon,
Lexeme.Eof,
Lexeme.Identifier,
...additionalterminators
);
}
Expand Down Expand Up @@ -1415,6 +1416,8 @@ export class Parser {
while (true) {
if (match(Lexeme.LeftParen)) {
expr = finishCall(expr);
} else if (match(Lexeme.Print)) {
// doing nothing as invalid check was before
} else if (match(Lexeme.LeftSquare)) {
indexedGet();
} else if (match(Lexeme.Dot)) {
Expand Down
13 changes: 13 additions & 0 deletions test/e2e/resources/multi-file/test1.brs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ sub Main()
print("function in same file: " + sameFileFunc())
print("function in different file: " + differentFileFunc())
print("function with dependency: " + dependentFunc())
print("result" + testOptionalChaining())
end sub

function sameFileFunc()
Expand All @@ -11,3 +12,15 @@ end function
function dependencyFunc()
return "from dependencyFunc()"
end function

function testOptionalChaining()
result = responseData?.data.metricsData?.addOnsStepStart
responseData = {
data:{
metricsData:{
addOnsStepStart : "print"
}
}
}
return result
end function
32 changes: 31 additions & 1 deletion test/parser/expression/Indexing.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const brs = require("../../../lib");
const { Lexeme } = brs.lexer;
const { Int32 } = brs.types;
const { Int32, BrsInvalid } = brs.types;

const { token, identifier, EOF } = require("../ParserTests");

Expand Down Expand Up @@ -28,6 +28,36 @@ describe("parser indexing", () => {
expect(statements).toMatchSnapshot();
});

test("Expression with identifier", () => {
let { statements, errors } = parser.parse([
identifier("_"),
token(Lexeme.Equal, "="),
identifier("bar"),
token(Lexeme.Print, "?"),
token(Lexeme.Dot, "."),
identifier("foo"),
EOF,
]);
expect(errors).toEqual([]);
expect(statements).toBeDefined();
expect(statements).not.toBeNull();
});

test("Expression with invalid", () => {
let { statements, errors } = parser.parse([
identifier("_"),
token(Lexeme.Equal, "="),
token(Lexeme.Invalid, "invalid", BrsInvalid.Instance),
token(Lexeme.Print, "?"),
token(Lexeme.Dot, "."),
identifier("bar"),
EOF,
]);
expect(errors).toEqual([]);
expect(statements).toBeDefined();
expect(statements).not.toBeNull();
});

test("bracketed", () => {
let { statements, errors } = parser.parse([
identifier("_"),
Expand Down

0 comments on commit 7569e86

Please sign in to comment.