Skip to content

Commit

Permalink
fix unexpected eof
Browse files Browse the repository at this point in the history
+ unexpected eof handled
  • Loading branch information
michal-kapala committed Mar 25, 2024
1 parent f73d745 commit c5d7360
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
10 changes: 10 additions & 0 deletions src/frontend/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import { Position, Token, TokenType } from "./types";
*/
export default class Parser {
private tokens: Token[] = [];
private endTagStart?: Position;
private endTagEnd?: Position;

/**
* Determines if the parsing is complete and the EOF was reached.
Expand Down Expand Up @@ -206,6 +208,10 @@ export default class Parser {
// Remove <trans> and </trans>, restore EOF
this.tokens.shift();
const last = this.tokens.pop() as Token;
if(last.type === TokenType.CloseTransTag) {
this.endTagStart = last.begin;
this.endTagEnd = last.end;
}
this.tokens.push(new Token("EndOfFile", TokenType.EOF, last.end, last.end));
return result;
}
Expand Down Expand Up @@ -647,6 +653,10 @@ export default class Parser {
case TokenType.SingleQuoteString:
case TokenType.DoubleQuoteString:
return new StringLiteral(this.consume());
case TokenType.EOF:
const start = this.endTagStart ?? tk.begin;
const end = this.endTagEnd ?? tk.end;
return new InvalidExpr("EOF", "Expected expression before the end of script.", start, end);
default:
// consume the token to prevent infinite loops
const unk = this.consume();
Expand Down
12 changes: 1 addition & 11 deletions test.jb
Original file line number Diff line number Diff line change
@@ -1,13 +1,3 @@
<trans>
// user-defined system variable
someFlag = true;
$myGlobalVar = 'someFlag: ' + someFlag;
WriteToOperationLog($myGlobalVar);
if(someFlag,
$myGlobalVar = 'something',
$myGlobalVar = 'something else'
);
// assignment of an unknown token
$bad = 5345bnj435;
#x
x +
</trans>
8 changes: 8 additions & 0 deletions tests/typechecker/errors.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,12 @@ msg = Case(
// Local variable 'x' hasn't been initialized
expect(result.diagnostics[4].error).toStrictEqual(true);
});

test('Missing RHS at the EOF.', function() {
const script = `<trans>x + </trans>`;
const result = typecheck(script);
expect(result.diagnostics.length).toStrictEqual(1);
// Expected expression before the end of script.
expect(result.diagnostics[0].error).toStrictEqual(true);
});
});

0 comments on commit c5d7360

Please sign in to comment.