Skip to content

Commit e267da6

Browse files
committed
API: ParseTree.check adjustments
1 parent af0677b commit e267da6

6 files changed

Lines changed: 219 additions & 9 deletions

File tree

spec/api/v3/input/check_spec.lua

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
local util = require("spec.util")
2+
local teal = require("teal")
3+
4+
describe("Input.check", function()
5+
it("can check Teal code", function()
6+
local tl_code = [[
7+
local foo: string = "hello"
8+
local planet: integer = 3
9+
]]
10+
11+
local compiler = teal.compiler()
12+
local input = compiler:input(tl_code)
13+
local module, check_err = input:check()
14+
15+
assert(module)
16+
assert.same(0, #check_err.syntax_errors)
17+
assert.same(0, #check_err.type_errors)
18+
assert.same(2, #check_err.warnings)
19+
end)
20+
21+
it("reports syntax errors from the lexer", function()
22+
local tl_code = [[
23+
2.e + 1
24+
]]
25+
26+
local compiler = teal.compiler()
27+
local input = compiler:input(tl_code)
28+
local module, check_err = input:check()
29+
30+
assert.is_nil(module)
31+
assert.same(1, #check_err.syntax_errors)
32+
assert.same(0, #check_err.type_errors)
33+
assert.same(0, #check_err.warnings)
34+
end)
35+
36+
it("reports syntax errors from parsing", function()
37+
local tl_code = [[
38+
if if if
39+
]]
40+
41+
local compiler = teal.compiler()
42+
local input = compiler:input(tl_code)
43+
local module, check_err = input:check()
44+
45+
assert.is_nil(module)
46+
assert.same(3, #check_err.syntax_errors)
47+
assert.same(0, #check_err.type_errors)
48+
assert.same(0, #check_err.warnings)
49+
end)
50+
51+
it("reports type errors from checking", function()
52+
local tl_code = [[
53+
local x: number = "oops"
54+
]]
55+
56+
local compiler = teal.compiler()
57+
local input = compiler:input(tl_code)
58+
local module, check_err = input:check()
59+
60+
assert(module)
61+
assert.same(0, #check_err.syntax_errors)
62+
assert.same(1, #check_err.type_errors)
63+
assert.same(1, #check_err.warnings)
64+
end)
65+
end)

spec/api/v3/input/parse_spec.lua

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
local util = require("spec.util")
2+
local teal = require("teal")
3+
4+
describe("Input.parse", function()
5+
it("can parse Teal code", function()
6+
local tl_code = [[
7+
local foo: string = "hello"
8+
local planet: integer = 3
9+
]]
10+
11+
local compiler = teal.compiler()
12+
local input = compiler:input(tl_code)
13+
local parse_tree, err = input:parse()
14+
15+
assert(parse_tree.ast)
16+
end)
17+
18+
it("reports syntax errors from the lexer", function()
19+
local tl_code = [[
20+
2.e + 1
21+
]]
22+
23+
local compiler = teal.compiler()
24+
local input = compiler:input(tl_code)
25+
local parse_tree, err = input:parse()
26+
27+
assert(parse_tree.ast)
28+
assert.same(1, #err)
29+
assert.same(err[1].msg, "malformed number")
30+
end)
31+
32+
it("reports syntax errors from parsing", function()
33+
local tl_code = [[
34+
if if if
35+
]]
36+
37+
local compiler = teal.compiler()
38+
local input = compiler:input(tl_code)
39+
local parse_tree, err = input:parse()
40+
41+
assert(parse_tree.ast)
42+
assert.same(3, #err)
43+
assert.same(err[1].msg, "syntax error")
44+
assert.same(err[1].y, 1)
45+
assert.same(err[1].x, 13)
46+
end)
47+
end)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
local util = require("spec.util")
2+
local teal = require("teal")
3+
4+
describe("ParseTree.check", function()
5+
it("can check Teal code", function()
6+
local tl_code = [[
7+
local foo: string = "hello"
8+
local planet: integer = 3
9+
]]
10+
11+
local compiler = teal.compiler()
12+
local input = compiler:input(tl_code)
13+
local parse_tree = input:parse()
14+
local module, check_err = parse_tree:check()
15+
16+
assert(module)
17+
assert.same(0, #check_err.syntax_errors)
18+
assert.same(0, #check_err.type_errors)
19+
assert.same(2, #check_err.warnings)
20+
end)
21+
22+
it("reports syntax errors from the lexer", function()
23+
local tl_code = [[
24+
2.e + 1
25+
]]
26+
27+
local compiler = teal.compiler()
28+
local input = compiler:input(tl_code)
29+
local parse_tree = input:parse()
30+
local module, check_err = parse_tree:check()
31+
32+
assert.is_nil(module)
33+
assert.same(1, #check_err.syntax_errors)
34+
assert.same(0, #check_err.type_errors)
35+
assert.same(0, #check_err.warnings)
36+
end)
37+
38+
it("reports syntax errors from parsing", function()
39+
local tl_code = [[
40+
if if if
41+
]]
42+
43+
local compiler = teal.compiler()
44+
local input = compiler:input(tl_code)
45+
local parse_tree = input:parse()
46+
local module, check_err = parse_tree:check()
47+
48+
assert.is_nil(module)
49+
assert.same(3, #check_err.syntax_errors)
50+
assert.same(0, #check_err.type_errors)
51+
assert.same(0, #check_err.warnings)
52+
end)
53+
54+
it("reports type errors from checking", function()
55+
local tl_code = [[
56+
local x: number = "oops"
57+
]]
58+
59+
local compiler = teal.compiler()
60+
local input = compiler:input(tl_code)
61+
local parse_tree = input:parse()
62+
local module, check_err = parse_tree:check()
63+
64+
assert(module)
65+
assert.same(0, #check_err.syntax_errors)
66+
assert.same(1, #check_err.type_errors)
67+
assert.same(1, #check_err.warnings)
68+
end)
69+
70+
it("when type reporting on, return both module and type errors from checking", function()
71+
local tl_code = [[
72+
local x: number = "oops"
73+
]]
74+
75+
local compiler = teal.compiler()
76+
compiler:enable_type_reporting(true)
77+
local input = compiler:input(tl_code)
78+
local parse_tree = input:parse()
79+
local module, check_err = parse_tree:check()
80+
81+
assert(module)
82+
assert.same(0, #check_err.syntax_errors)
83+
assert.same(1, #check_err.type_errors)
84+
assert.same(1, #check_err.warnings)
85+
end)
86+
end)

teal/init.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,15 @@ end
324324

325325

326326
function ParseTree:check(module_name)
327+
if #self.syntax_errors > 0 and not self.env.keep_going then
328+
local result = self.env.loaded[self.filename]
329+
local _, check_err = module_from_result(result)
330+
return nil, check_err
331+
end
332+
327333
local result = check.check(self.ast, self.env, self.filename)
328334
if result then
329-
if self.env.keep_going then
330-
result.syntax_errors = self.syntax_errors
331-
end
335+
result.syntax_errors = self.syntax_errors
332336

333337
if result.ast then
334338
lua_compat.apply(result)

teal/init.tl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,11 +324,15 @@ end
324324
--------------------------------------------------------------------------------
325325

326326
function ParseTree:check(module_name?: string): Module, CheckError
327+
if #self.syntax_errors > 0 and not self.env.keep_going then
328+
local result = self.env.loaded[self.filename]
329+
local _, check_err = module_from_result(result)
330+
return nil, check_err
331+
end
332+
327333
local result = check.check(self.ast as parser.Node, self.env, self.filename)
328334
if result then
329-
if self.env.keep_going then
330-
result.syntax_errors = self.syntax_errors
331-
end
335+
result.syntax_errors = self.syntax_errors
332336

333337
if result.ast then
334338
lua_compat.apply(result)

tl.lua

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17952,11 +17952,15 @@ end
1795217952

1795317953

1795417954
function ParseTree:check(module_name)
17955+
if #self.syntax_errors > 0 and not self.env.keep_going then
17956+
local result = self.env.loaded[self.filename]
17957+
local _, check_err = module_from_result(result)
17958+
return nil, check_err
17959+
end
17960+
1795517961
local result = check.check(self.ast, self.env, self.filename)
1795617962
if result then
17957-
if self.env.keep_going then
17958-
result.syntax_errors = self.syntax_errors
17959-
end
17963+
result.syntax_errors = self.syntax_errors
1796017964

1796117965
if result.ast then
1796217966
lua_compat.apply(result)

0 commit comments

Comments
 (0)