Skip to content

Commit

Permalink
fix(parser): fixing row/col computation while backtracking in the par…
Browse files Browse the repository at this point in the history
…ser, leading to finally correct symbol underlined in errors
  • Loading branch information
SuperFola committed Dec 10, 2024
1 parent 9cfde62 commit db22968
Show file tree
Hide file tree
Showing 31 changed files with 54 additions and 56 deletions.
2 changes: 1 addition & 1 deletion src/arkreactor/Compiler/AST/BaseParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ namespace Ark::internal
view = view.substr(0, it_pos);
const auto nearest_newline_index = view.find_last_of('\n');
if (nearest_newline_index != std::string_view::npos)
m_filepos.col = it_pos - nearest_newline_index + 1;
m_filepos.col = it_pos - nearest_newline_index;
else
m_filepos.col = it_pos + 1;
}
Expand Down
12 changes: 6 additions & 6 deletions src/arkreactor/Compiler/AST/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,9 +752,9 @@ namespace Ark::internal
{
if (!accept(IsChar('(')))
return std::nullopt;
auto cursor = getCursor();
std::string comment;
newlineOrComment(&comment);
auto cursor = getCursor();

std::optional<Node> func;
if (auto atom = anyAtomOf({ NodeType::Symbol, NodeType::Field }); atom.has_value())
Expand Down Expand Up @@ -784,7 +784,6 @@ namespace Ark::internal
}

leaf->list().back().attachCommentAfter(comment);
setNodePosAndFilename(leaf->list().back());

comment.clear();
if (newlineOrComment(&comment))
Expand Down Expand Up @@ -821,8 +820,6 @@ namespace Ark::internal
}
leaf->list().back().attachCommentAfter(comment);

setNodePosAndFilename(leaf->list().back());

expect(IsChar(']'));
return leaf;
}
Expand Down Expand Up @@ -860,8 +857,10 @@ namespace Ark::internal

std::optional<Node> Parser::anyAtomOf(const std::initializer_list<NodeType> types)
{
auto cursor = getCursor();
if (auto value = atom(); value.has_value())
{
setNodePosAndFilename(value.value(), cursor);
for (const auto type : types)
{
if (value->nodeType() == type)
Expand All @@ -873,14 +872,15 @@ namespace Ark::internal

std::optional<Node> Parser::nodeOrValue()
{
auto cursor = getCursor();
if (auto value = atom(); value.has_value())
{
setNodePosAndFilename(value.value());
setNodePosAndFilename(value.value(), cursor);
return value;
}
if (auto sub_node = node(); sub_node.has_value())
{
setNodePosAndFilename(sub_node.value());
setNodePosAndFilename(sub_node.value(), cursor);
return sub_node;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At a @ 2:12
At a @ 2:10
1 | (let a [])
2 | (append! a 5)
| ^
| ^
3 |
MutabilityError: Can not modify the constant list `a' using `append!'
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
At test_func1!2 @ 13:41
At test_func1!2 @ 13:28
10 |
11 | (let test_func (fun (a b c) (* a b c)))
12 | (let test_func1 (partial test_func 1))
13 | (let test_func1_2 (partial test_func1!2))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ^~~~~~~~~~~~
14 |
When expanding `$argcount', expected a known function name, got unbound variable test_func1!2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
At &cap @ 2:17
At &cap @ 2:16
1 | (let bar (fun () (let cap 0)))
2 | (let foo (fun (&cap) ()))
| ^~~~
| ^~~~
3 | (print foo bar)
4 |
Can not capture cap because it is referencing a variable defined in an unreachable scope.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At a @ 2:12
At a @ 2:10
1 | (let a [])
2 | (concat! a [5])
| ^
| ^
3 |
MutabilityError: Can not modify the constant list `a' using `concat!'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At nil? @ 2:15
At nil? @ 2:10
1 | (let ba0 (fun (a b) ()))
2 | (ba0 0 0 nil?)
| ^~~~~~~~~~~~~~
| ^~~~
3 |
Found a free standing operator: `nil?`
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
At (foo (begin)) @ 2:3
At (foo (begin)) @ 2:2
1 | (let foo (fun (a) ()))
2 | (foo {})
| ^~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At 0 @ 2:10
At 0 @ 2:8
1 | ($ defun (name args body) (let name (fun args body)))
2 | (defun 0 (a b) (+ a b))
| ^
| ^
3 |
Can not use a Number to define a variable
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At ( @ 2:7
At ( @ 2:6
1 | (let foo (fun () ()))
2 | (let (foo a b) 5)
| ^
| ^
3 |
let needs a symbol
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
At (empty? 1 2) @ 2:10
At (empty? 1 2) @ 2:8
1 | ($ a (empty? 1 2))
2 | (print a)
| ^~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
At (head 1 2) @ 2:10
At (head 1 2) @ 2:8
1 | ($ a (head 1 2))
2 | (print a)
| ^~~~~~~~~
3 |
| ^
When expanding `head' inside a macro, got 2 arguments, expected 1
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
At (len 1 2) @ 2:10
At (len 1 2) @ 2:8
1 | ($ a (len 1 2))
2 | (print a)
| ^~~~~~~~~
3 |
| ^
When expanding `len' inside a macro, got 2 arguments, expected 1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
At ($paste b (list)) @ 2:10
At ($paste b (list)) @ 2:8
1 | ($ a ($paste b []))
2 | (print a)
| ^~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
At (foo 1 2) @ 4:3
At (foo 1 2) @ 4:2
1 | ($ foo (a b c ...d)
2 | (+ a b c ...d))
3 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
At ($symcat 5 2) @ 2:10
At ($symcat 5 2) @ 2:8
1 | ($ a ($symcat 5 2))
2 | (print a)
| ^~~~~~~~~
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
At (tail 1 2) @ 2:10
At (tail 1 2) @ 2:8
1 | ($ a (tail 1 2))
2 | (print a)
| ^~~~~~~~~
3 |
| ^
When expanding `tail' inside a macro, got 2 arguments, expected 1
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
At ($ partial (func ...defargs) (begin ($ bloc (suffix-dup a (len defargs))) (fun (bloc) (func ...defargs bloc)) ($undef bloc))) @ 17:18
At ($ partial (func ...defargs) (begin ($ bloc (suffix-dup a (len defargs))) (fun (bloc) (func ...defargs bloc)) ($undef bloc))) @ 17:17
14 | (* a b)
15 | (let inner (partial te t_func 0))
16 | (let est_func
17 | ($ partial (func ...defargs) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 | ($ bloc (suffix-dup a (len defargs)))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
19 | (fun (bloc) (func ...defargs bloc))
Max macro unification depth reached (256). You may have a macro trying to evaluate itself, try splitting your code in multiple nodes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
At (foo 1 2) @ 4:3
At (foo 1 2) @ 4:2
1 | ($ foo (a b c)
2 | (+ a b c))
3 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At a @ 2:9
At a @ 2:7
1 | (let a [])
2 | (pop! a 1)
| ^
| ^
3 |
MutabilityError: Can not modify the constant list `a' using `pop!'
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At d @ 2:12
At d @ 2:10
1 | (mut d [4 5 6])
2 | (append! d d)
| ^
| ^
3 |
MutabilityError: Can not append! the list `d' to itself
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At d @ 2:12
At d @ 2:10
1 | (mut d [4 5 6])
2 | (concat! d d)
| ^
| ^
3 |
MutabilityError: Can not concat! the list `d' to itself
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
At (foo 1 2 3 4) @ 4:3
At (foo 1 2 3 4) @ 4:2
1 | ($ foo (a b c)
2 | (+ a b c))
3 |
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
At bar @ 3:12
At bar @ 3:8
1 | (import package.b)
2 |
3 | (print bar)
| ^~~~~~~~~~~
| ^~~
4 |
Unbound variable "bar". However, it exists in a namespace as "b:bar", did you forget to prefix it with its namespace?
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
At b @ 1:9
At b @ 1:8
1 | (let a b)
| ^~~~~~~~~
| ^
2 |
Unbound variable error "b" (variable is used but not defined)
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At ber @ 2:12
At ber @ 2:8
1 | (let bar 12)
2 | (let a ber)
| ^~~~~~~~~~~
| ^~~
3 |
Unbound variable error "ber" (did you mean "bar"?)
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
At 1 @ 1:10
At 1 @ 1:9
1 | ($undef 1)
| ^~~~~~~~~~
| ^
2 |
When expanding `$undef', got a Number. Can not un-define a macro without a valid name
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
At ...defargs @ 2:29
At ...defargs @ 2:18
1 | ($ partial {
2 | (fun (a) (func ...defargs)) })
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| ^~~~~~~~~~
3 |
4 | (let b (partial))
Found an unevaluated spread: `defargs'
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
At abs @ 3:10
At abs @ 3:9
1 | (import package.c :odd)
2 |
3 | (print (abs 2))
| ^~~
| ^~~
4 |
Unbound variable "abs". However, it exists in a namespace as "c:abs", did you forget to add it to the symbol list while importing?
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
At args @ 1:29
At args @ 1:25
1 | ($ defun (let name (fun args nil)))
| ^~~~
| ^~~~
2 | (let foo 1)
3 | (let a 2)
Expected a well formed argument(s) list, got a Symbol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

(fun (a) {
a })

(call me maybe)
(call
(fun () {
Expand Down

0 comments on commit db22968

Please sign in to comment.