Skip to content

Commit

Permalink
#276 Correctly raise error on function names with dots outside class
Browse files Browse the repository at this point in the history
Fixes #276
  • Loading branch information
florianschanda committed Aug 7, 2023
1 parent b76e6e7 commit f01fbf9
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 8 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ Not quite compatible with Octave yet. See #43 [octave support](https://github.co

### 0.9.43-dev

* Fix parsing error surrounding function names (only in classes may
you use a dotted name). When such a function appeared outside a
class the tools would either incorrectly accept this name or crash.

* Fix hanging tools when an internal compiler error was raised.

Expand Down
24 changes: 16 additions & 8 deletions miss_hit_core/m_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -710,7 +710,7 @@ def parse_octave_script_file(self, l_pragmas):
# statements intermixed with (global) function definitions
while not self.peek_eof():
if self.peek("KEYWORD", "function"):
l_functions.append(self.parse_function_def())
l_functions.append(self.parse_function_def(in_class = False))
elif self.peek("KEYWORD", "pragma"):
l_more_pragmas.append(self.parse_annotation_pragma())
else:
Expand Down Expand Up @@ -751,7 +751,7 @@ def parse_function_list(self):
while self.peek("KEYWORD", "function") or \
self.apeek("KEYWORD", "pragma"):
if self.peek("KEYWORD", "function"):
l_functions.append(self.parse_function_def())
l_functions.append(self.parse_function_def(in_class = False))
else:
l_pragmas.append(self.parse_annotation_pragma())

Expand Down Expand Up @@ -780,7 +780,9 @@ def reorder_as_function_list(self, n_fdef):

return functions

def parse_function_signature(self):
def parse_function_signature(self, in_class):
assert isinstance(in_class, bool)

rv = Function_Signature()

# Parse returns. Either 'x' or a list '[x, y]'
Expand Down Expand Up @@ -842,18 +844,24 @@ def parse_function_signature(self):
self.match("KET")
self.ct.set_ast(rv)

if isinstance(n_name, Selection) and not in_class:
self.mh.error(n_name.loc(),
"dotted name is not permitted outside class")

rv.set_name(n_name)
rv.set_inputs(l_inputs)
rv.set_outputs(l_outputs)
self.match_eos(rv)
return rv

def parse_function_def(self):
def parse_function_def(self, in_class):
assert isinstance(in_class, bool)

self.match("KEYWORD", "function")
self.push_context("function")
t_fun = self.ct

n_sig = self.parse_function_signature()
n_sig = self.parse_function_signature(in_class)

l_body = []
l_nested = []
Expand Down Expand Up @@ -1059,9 +1067,9 @@ def parse_class_methods(self):

while not self.peek("KEYWORD", "end"):
if self.peek("KEYWORD", "function"):
rv.add_method(self.parse_function_def())
rv.add_method(self.parse_function_def(in_class = True))
else:
rv.add_method(self.parse_function_signature())
rv.add_method(self.parse_function_signature(in_class = True))

self.match("KEYWORD", "end")
self.ct.set_ast(rv)
Expand Down Expand Up @@ -1334,7 +1342,7 @@ def parse_statement(self):
self.mh.error(self.nt.location,
"nested function cannot appear inside"
" %s" % self.context[-1])
return self.parse_function_def()
return self.parse_function_def(in_class = False)
else:
self.mh.error(self.nt.location,
"expected valid statement,"
Expand Down
19 changes: 19 additions & 0 deletions tests/style/bug_276_ice_on_dodgy_function_name/expected_out.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<link rel="stylesheet" href="file:../../../docs/style.css">
<title>MISS_HIT Report</title>
</head>
<body>
<header>MISS_HIT Report</header>
<main>
<div></div>
<h1>Issues identified</h1>
<section>
<h2>test.m</h2>
<div class="message"><a href="matlab:opentoline('test.m', 1, 19)">test.m: line 1:</a> error: dotted name is not permitted outside class</div>
</section>
</main>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
=== PLAIN MODE ===
test.m: error: file is not auto-fixed because it contains parse errors
In test.m, line 1
| function pth = foo.bar()
| ^ error: dotted name is not permitted outside class
MISS_HIT Style Summary: 1 file(s) analysed, 2 error(s)

=== HTML MODE ===
MISS_HIT Style Summary: 1 file(s) analysed, 1 error(s)
3 changes: 3 additions & 0 deletions tests/style/bug_276_ice_on_dodgy_function_name/test.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function pth = foo.bar()

end
3 changes: 3 additions & 0 deletions tests/style/bug_276_ice_on_dodgy_function_name/test.m_fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function pth = foo.bar()

end

0 comments on commit f01fbf9

Please sign in to comment.