Skip to content

Commit eff7ab5

Browse files
committed
More test cases were added
1 parent 939f001 commit eff7ab5

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

jac/jaclang/compiler/parser.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,15 @@ def feed_current_token(iparser: jl.InteractiveParser, tok: jl.Token) -> bool:
189189
return feed_current_token(iparser, e.token)
190190

191191
if Tok.KW_WITH.name in iparser.accepts():
192-
self.log_error(
193-
"Arbitary statements must be declared inside an entry block",
194-
self.error_to_token(e),
195-
)
192+
# Copy and feed the `with` token cause we don't want to actually
193+
# feed the `with` token in the main parser state.
194+
ipcopy = iparser.copy(deepcopy_values=True)
195+
ipcopy.feed_token(jl.Token(Tok.KW_WITH.name, "with"))
196+
if Tok.KW_ENTRY.name in ipcopy.accepts():
197+
self.log_error(
198+
"Arbitary statements must be declared inside an entry block",
199+
self.error_to_token(e),
200+
)
196201
return False
197202

198203
# We're calling try_feed_missing_token twice here because the first missing

jac/jaclang/langserve/tests/server_test/test_lang_serve.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ def test_open_with_syntax_error():
6262
try:
6363
helper.open_document()
6464
helper.assert_has_diagnostics(
65-
count=2, message_contains="Unexpected token 'error'"
65+
count=2,
66+
message_contains="Arbitary statements must be declared inside an entry block",
6667
)
6768

6869
diagnostics = helper.get_diagnostics()

jac/jaclang/tests/test_language.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,15 +194,42 @@ def test_assignment_list_no_infinite_loop():
194194

195195
def test_free_code_outside_entry_reports_error(capsys):
196196
"""Free python code outside an entry should report an error."""
197+
198+
# Case 1: Free python code not wrapped in `with entry { ... }` is invalid
197199
prog = JacProgram()
198-
# Free python code not wrapped in `with entry { ... }` is invalid
199200
code = 'print("hello world");'
200201
# Compile and capture stderr via pytest capsys
201202
prog.compile(use_str=code, file_path="test.jac")
202203
captured = capsys.readouterr()
204+
203205
# The parser change logs this specific message when encountering free code
204206
assert "Arbitary statements must be declared inside an entry block" in captured.err
205207

208+
# Case 2: This is unexpected token as well but not the same error
209+
prog = JacProgram()
210+
code = """
211+
with entry {
212+
*;
213+
}
214+
"""
215+
prog.compile(use_str=code, file_path="test.jac")
216+
captured = capsys.readouterr()
217+
assert (
218+
"Arbitary statements must be declared inside an entry block" not in captured.err
219+
)
220+
221+
# Case 3: `with` context manager block
222+
prog = JacProgram()
223+
code = """
224+
with entry {
225+
with open('some/file.txt', 'r') as f {
226+
print(f.read());
227+
}
228+
"""
229+
prog.compile(use_str=code, file_path="test.jac")
230+
captured = capsys.readouterr()
231+
assert len(prog.errors_had) == 0
232+
206233

207234
def test_need_import(fixture_path, capture_stdout):
208235
"""Test importing python."""

0 commit comments

Comments
 (0)