Skip to content

Commit 66d83eb

Browse files
committed
Refact method syntax_error to error
It's a more convenient name since not only syntax errors are reported with this method, sino semantic and general ones.
1 parent 41a9555 commit 66d83eb

22 files changed

Lines changed: 177 additions & 183 deletions

api/check.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .constants import CLASS
1515
from .constants import SCOPE
1616
import api.errmsg
17-
from .errmsg import syntax_error
17+
from .errmsg import error
1818

1919

2020
__all__ = ['check_type',
@@ -52,11 +52,11 @@ def check_type(lineno, type_list, arg):
5252
return True
5353

5454
if len(type_list) == 1:
55-
syntax_error(lineno, "Wrong expression type '%s'. Expected '%s'" %
56-
(arg.type_, type_list[0]))
55+
error(lineno, "Wrong expression type '%s'. Expected '%s'" %
56+
(arg.type_, type_list[0]))
5757
else:
58-
syntax_error(lineno, "Wrong expression type '%s'. Expected one of '%s'"
59-
% (arg.type_, tuple(type_list)))
58+
error(lineno, "Wrong expression type '%s'. Expected one of '%s'"
59+
% (arg.type_, tuple(type_list)))
6060

6161
return False
6262

@@ -100,13 +100,13 @@ def check_call_arguments(lineno, id_, args):
100100

101101
if len(args) != len(entry.params):
102102
c = 's' if len(entry.params) != 1 else ''
103-
syntax_error(lineno, "Function '%s' takes %i parameter%s, not %i" %
104-
(id_, len(entry.params), c, len(args)))
103+
error(lineno, "Function '%s' takes %i parameter%s, not %i" %
104+
(id_, len(entry.params), c, len(args)))
105105
return False
106106

107107
for arg, param in zip(args, entry.params):
108108
if arg.class_ in (CLASS.var, CLASS.array) and param.class_ != arg.class_:
109-
syntax_error(lineno, "Invalid argument '{}'".format(arg.value))
109+
error(lineno, "Invalid argument '{}'".format(arg.value))
110110
return None
111111

112112
if not arg.typecast(param.type_):
@@ -115,11 +115,11 @@ def check_call_arguments(lineno, id_, args):
115115
if param.byref:
116116
from symbols.var import SymbolVAR
117117
if not isinstance(arg.value, SymbolVAR):
118-
syntax_error(lineno, "Expected a variable name, not an expression (parameter By Reference)")
118+
error(lineno, "Expected a variable name, not an expression (parameter By Reference)")
119119
return False
120120

121121
if arg.class_ not in (CLASS.var, CLASS.array):
122-
syntax_error(lineno, "Expected a variable or array name (parameter By Reference)")
122+
error(lineno, "Expected a variable or array name (parameter By Reference)")
123123
return False
124124

125125
arg.byref = True
@@ -128,7 +128,7 @@ def check_call_arguments(lineno, id_, args):
128128
arg.value.add_required_symbol(param)
129129

130130
if entry.forwarded: # The function / sub was DECLARED but not implemented
131-
syntax_error(lineno, "%s '%s' declared but not implemented" % (CLASS.to_string(entry.class_), entry.name))
131+
error(lineno, "%s '%s' declared but not implemented" % (CLASS.to_string(entry.class_), entry.name))
132132
return False
133133

134134
return True
@@ -172,8 +172,8 @@ def check_pending_labels(ast):
172172

173173
tmp = global_.SYMBOL_TABLE.get_entry(node.name)
174174
if tmp is None or tmp.class_ is CLASS.unknown:
175-
syntax_error(node.lineno, 'Undeclared identifier "%s"'
176-
% node.name)
175+
error(node.lineno, 'Undeclared identifier "%s"'
176+
% node.name)
177177
else:
178178
assert tmp.class_ == CLASS.label
179179
node.to_label(node)
@@ -194,7 +194,7 @@ def check_and_make_label(lbl, lineno):
194194
if lbl == int(lbl):
195195
id_ = str(int(lbl))
196196
else:
197-
syntax_error(lineno, 'Line numbers must be integers.')
197+
error(lineno, 'Line numbers must be integers.')
198198
return None
199199
else:
200200
id_ = lbl

api/errmsg.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from .config import OPTIONS
1515

1616
# Exports only these functions. Others
17-
__all__ = ['syntax_error', 'warning']
17+
__all__ = ['error', 'warning']
1818

1919

2020
def msg_output(msg):
@@ -31,7 +31,7 @@ def info(msg):
3131
OPTIONS.stderr.value.write("info: %s\n" % msg)
3232

3333

34-
def syntax_error(lineno, msg, fname=None):
34+
def error(lineno, msg, fname=None):
3535
""" Generic syntax error routine
3636
"""
3737
if fname is None:
@@ -40,7 +40,7 @@ def syntax_error(lineno, msg, fname=None):
4040
if global_.has_errors > OPTIONS.max_syntax_errors.value:
4141
msg = 'Too many errors. Giving up!'
4242

43-
msg = "%s:%i: %s" % (fname, lineno, msg)
43+
msg = "%s:%i: error: %s" % (fname, lineno, msg)
4444
msg_output(msg)
4545

4646
if global_.has_errors > OPTIONS.max_syntax_errors.value:
@@ -108,96 +108,96 @@ def warning_not_used(lineno, id_, kind='Variable'):
108108
# numeric expression.
109109
# ----------------------------------------
110110
def syntax_error_expected_string(lineno, _type):
111-
syntax_error(lineno, "Expected a 'string' type expression, got '%s' instead" % _type)
111+
error(lineno, "Expected a 'string' type expression, got '%s' instead" % _type)
112112

113113

114114
# ----------------------------------------
115115
# Syntax error: FOR variable should be X
116116
# instead of Y
117117
# ----------------------------------------
118118
def syntax_error_wrong_for_var(lineno, x, y):
119-
syntax_error(lineno, "FOR variable should be '%s' instead of '%s'" %
120-
(x, y))
119+
error(lineno, "FOR variable should be '%s' instead of '%s'" %
120+
(x, y))
121121

122122

123123
# ----------------------------------------
124124
# Syntax error: Initializer expression is
125125
# not constant
126126
# ----------------------------------------
127127
def syntax_error_not_constant(lineno):
128-
syntax_error(lineno, "Initializer expression is not constant.")
128+
error(lineno, "Initializer expression is not constant.")
129129

130130

131131
# ----------------------------------------
132132
# Syntax error: Id is neither an array nor
133133
# a function
134134
# ----------------------------------------
135135
def syntax_error_not_array_nor_func(lineno, varname):
136-
syntax_error(lineno, "'%s' is neither an array nor a function." % varname)
136+
error(lineno, "'%s' is neither an array nor a function." % varname)
137137

138138

139139
# ----------------------------------------
140140
# Syntax error: Id is neither an array nor
141141
# a function
142142
# ----------------------------------------
143143
def syntax_error_not_an_array(lineno, varname):
144-
syntax_error(lineno, "'%s' is not an array (or has not been declared yet)" % varname)
144+
error(lineno, "'%s' is not an array (or has not been declared yet)" % varname)
145145

146146

147147
# ----------------------------------------
148148
# Syntax error: function redefinition type
149149
# mismatch
150150
# ----------------------------------------
151151
def syntax_error_func_type_mismatch(lineno, entry):
152-
syntax_error(lineno, "Function '%s' (previously declared at %i) type mismatch" % (entry.name, entry.lineno))
152+
error(lineno, "Function '%s' (previously declared at %i) type mismatch" % (entry.name, entry.lineno))
153153

154154

155155
# ----------------------------------------
156156
# Syntax error: function redefinition parm.
157157
# mismatch
158158
# ----------------------------------------
159159
def syntax_error_parameter_mismatch(lineno, entry):
160-
syntax_error(lineno, "Function '%s' (previously declared at %i) parameter mismatch" % (entry.name, entry.lineno))
160+
error(lineno, "Function '%s' (previously declared at %i) parameter mismatch" % (entry.name, entry.lineno))
161161

162162

163163
# ----------------------------------------
164164
# Syntax error: can't convert value to the
165165
# given type.
166166
# ----------------------------------------
167167
def syntax_error_cant_convert_to_type(lineno, expr_str, type_):
168-
syntax_error(lineno, "Cant convert '%s' to type %s" % (expr_str, type_))
168+
error(lineno, "Cant convert '%s' to type %s" % (expr_str, type_))
169169

170170

171171
# ----------------------------------------
172172
# Syntax error: is a SUB not a FUNCTION
173173
# ----------------------------------------
174174
def syntax_error_is_a_sub_not_a_func(lineno, name):
175-
syntax_error(lineno, "'%s' is SUBROUTINE not a FUNCTION" % name)
175+
error(lineno, "'%s' is SUBROUTINE not a FUNCTION" % name)
176176

177177

178178
# ----------------------------------------
179179
# Syntax error: strict mode: missing type declaration
180180
# ----------------------------------------
181181
def syntax_error_undeclared_type(lineno: int, id_: str):
182-
syntax_error(lineno, "strict mode: missing type declaration for '%s'" % id_)
182+
error(lineno, "strict mode: missing type declaration for '%s'" % id_)
183183

184184

185185
# ----------------------------------------
186186
# Cannot assign a value to 'var'. It's not a variable
187187
# ----------------------------------------
188188
def syntax_error_cannot_assign_not_a_var(lineno, id_):
189-
syntax_error(lineno, "Cannot assign a value to '%s'. It's not a variable" % id_)
189+
error(lineno, "Cannot assign a value to '%s'. It's not a variable" % id_)
190190

191191

192192
# ----------------------------------------
193193
# Cannot assign a value to 'var'. It's not a variable
194194
# ----------------------------------------
195195
def syntax_error_address_must_be_constant(lineno):
196-
syntax_error(lineno, 'Address must be a numeric constant expression')
196+
error(lineno, 'Address must be a numeric constant expression')
197197

198198

199199
# ----------------------------------------
200200
# Cannot pass an array by value
201201
# ----------------------------------------
202202
def syntax_error_cannot_pass_array_by_value(lineno, id_):
203-
syntax_error(lineno, "Array parameter '%s' must be passed ByRef" % id_)
203+
error(lineno, "Array parameter '%s' must be passed ByRef" % id_)

api/symboltable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from . import global_
2020
from .config import OPTIONS
2121

22-
from .errmsg import syntax_error
22+
from .errmsg import error as syntax_error
2323
from .errmsg import warning_implicit_type
2424
from .errmsg import warning_not_used
2525
from .errmsg import syntax_error_func_type_mismatch

api/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ def read_txt_file(fname):
4242
pass
4343

4444
global_.FILENAME = fname
45-
errmsg.syntax_error(1, 'Invalid file encoding. Use one of: %s' % ', '.join(encodings))
45+
errmsg.error(1, 'Invalid file encoding. Use one of: %s' % ', '.join(encodings))
4646
return ''
4747

4848

arch/zx48k/translator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import api.check as check
1515

1616
from api.debug import __DEBUG__
17-
from api.errmsg import syntax_error
17+
from api.errmsg import error
1818
from api.config import OPTIONS
1919
from api.global_ import optemps
2020
from api.errors import InvalidLoopError
@@ -959,7 +959,7 @@ def default_value(cls, type_, expr): # TODO: This function must be moved to api
959959

960960
if isinstance(expr, (symbols.CONST, symbols.VAR)): # a constant expression like @label + 1
961961
if type_ in (cls.TYPE(TYPE.float_), cls.TYPE(TYPE.string)):
962-
syntax_error(expr.lineno, "Can't convert non-numeric value to {0} at compile time".format(type_.name))
962+
error(expr.lineno, "Can't convert non-numeric value to {0} at compile time".format(type_.name))
963963
return ['<ERROR>']
964964

965965
val = Translator.traverse_const(expr)

libzxbasm/asmlex.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import ply.lex as lex
1515
import sys
1616
from api.config import OPTIONS
17-
from api.errmsg import syntax_error
17+
from api.errmsg import error
1818

1919
_tokens = ('STRING', 'NEWLINE', 'CO',
2020
'ID', 'COMMA', 'PLUS', 'MINUS', 'LP', 'RP', 'LPP', 'RPP', 'MUL', 'DIV', 'POW', 'MOD',
@@ -422,7 +422,7 @@ def t_INITIAL_preproc_ERROR(self, t):
422422

423423
def t_INITIAL_preproc_error(self, t):
424424
# error handling rule
425-
syntax_error(t.lexer.lineno, "illegal character '%s'" % t.value[0])
425+
error(t.lexer.lineno, "illegal character '%s'" % t.value[0])
426426

427427
def __init__(self):
428428
""" Creates a new GLOBAL lexer instance

libzxbasm/asmparse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from ast_.tree import NotAnAstError
2323
from api.debug import __DEBUG__
2424
from api.config import OPTIONS
25-
from api.errmsg import syntax_error as error
25+
from api.errmsg import error as error
2626
from api.errmsg import warning
2727
from api import global_ as gl
2828
import api.utils

libzxbc/zxblex.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from ply import lex
1414
from .keywords import KEYWORDS as reserved
1515
import api
16-
from api.errmsg import syntax_error
16+
from api.errmsg import error
1717

1818

1919
ASM = '' # Set to asm block when commenting
@@ -249,7 +249,7 @@ def t_MACROS(t):
249249
t.type = t.value
250250
return t
251251

252-
syntax_error(t.lexer.lineno, "unknown macro '%s'" % t.value)
252+
error(t.lexer.lineno, "unknown macro '%s'" % t.value)
253253

254254

255255
def t_ADDRESSOF(t):
@@ -618,13 +618,13 @@ def t_INITIAL_bin_NEWLINE(t):
618618

619619
def t_INITIAL_bin_string_asm_preproc_comment_ERROR(t):
620620
r'.'
621-
syntax_error(t.lineno, "ignoring illegal character '%s'" % t.value[0])
621+
error(t.lineno, "ignoring illegal character '%s'" % t.value[0])
622622
return t
623623

624624

625625
# error handling rule
626626
def t_INITIAL_bin_string_asm_preproc_comment_error(t):
627-
syntax_error(t.lineno, "illegal character '%s'" % t.value[0])
627+
error(t.lineno, "illegal character '%s'" % t.value[0])
628628

629629

630630
# --------- END OF Token rules ---------

0 commit comments

Comments
 (0)