|
8 | 8 | from latexify import constants
|
9 | 9 | from latexify import math_symbols
|
10 | 10 | from latexify import node_visitor_base
|
| 11 | +from latexify import exceptions |
11 | 12 |
|
12 | 13 |
|
13 | 14 | class LatexifyVisitor(node_visitor_base.NodeVisitorBase):
|
@@ -45,8 +46,10 @@ def __init__(
|
45 | 46 |
|
46 | 47 | self.assign_var = {}
|
47 | 48 |
|
48 |
| - def generic_visit(self, node, action): |
49 |
| - return str(node) |
| 49 | + def generic_visit(self, node, action) -> str: |
| 50 | + raise exceptions.LatexifyNotSupportedError( |
| 51 | + f"Unsupported AST: {type(node).__name__}" |
| 52 | + ) |
50 | 53 |
|
51 | 54 | def visit_Module(self, node, action): # pylint: disable=invalid-name
|
52 | 55 | return self.visit(node.body[0], "multi_lines")
|
@@ -78,7 +81,7 @@ def visit_FunctionDef(self, node, action): # pylint: disable=invalid-name
|
78 | 81 | elif isinstance(el, ast.Return):
|
79 | 82 | break
|
80 | 83 | if body_str == "":
|
81 |
| - raise ValueError("`return` missing") |
| 84 | + raise exceptions.LatexifySyntaxError("`return` missing") |
82 | 85 |
|
83 | 86 | return name_str, arg_strs, assign_vars, body_str
|
84 | 87 |
|
@@ -126,7 +129,7 @@ def visit_Call(self, node, action): # pylint: disable=invalid-name
|
126 | 129 | def _decorated_lstr_and_arg(node, callee_str, lstr):
|
127 | 130 | """Decorates lstr and get its associated arguments"""
|
128 | 131 | if callee_str == "sum" and isinstance(node.args[0], ast.GeneratorExp):
|
129 |
| - generator_info = self.visit(node.args[0], constants.actions.SET_BOUNDS) |
| 132 | + generator_info = self.visit(node.args[0], "set_bounds") |
130 | 133 | arg_str, comprehension = generator_info
|
131 | 134 | var_comp, args_comp = comprehension
|
132 | 135 | if len(args_comp) == 1:
|
@@ -293,14 +296,13 @@ def visit_If(self, node, action): # pylint: disable=invalid-name
|
293 | 296 | return latex + r", & \mathrm{otherwise} \end{array} \right."
|
294 | 297 |
|
295 | 298 | def visit_GeneratorExp_set_bounds(self, node): # pylint: disable=invalid-name
|
296 |
| - action = constants.actions.SET_BOUNDS |
297 | 299 | output = self.visit(node.elt)
|
298 | 300 | comprehensions = [
|
299 |
| - self.visit(generator, action) for generator in node.generators |
| 301 | + self.visit(generator, "set_bounds") for generator in node.generators |
300 | 302 | ]
|
301 | 303 | if len(comprehensions) == 1:
|
302 | 304 | return output, comprehensions[0]
|
303 |
| - raise TypeError( |
| 305 | + raise exceptions.LatexifyNotSupportedError( |
304 | 306 | "visit_GeneratorExp_sum() supports a single for clause"
|
305 | 307 | "but {} were given".format(len(comprehensions))
|
306 | 308 | )
|
@@ -351,6 +353,6 @@ def visit_comprehension_set_bounds(self, node): # pylint: disable=invalid-name
|
351 | 353 | args = [self.visit(arg) for arg in node.iter.args]
|
352 | 354 | if len(args) in (1, 2):
|
353 | 355 | return var, args
|
354 |
| - raise TypeError( |
| 356 | + raise exceptions.LatexifyNotSupportedError( |
355 | 357 | "Comprehension for sum only supports range func " "with 1 or 2 args"
|
356 | 358 | )
|
0 commit comments