Skip to content

Commit 5605768

Browse files
authored
Merge pull request #411 from boriel/bugfix/invalid_include_filename_error
Bugfix/invalid include filename error
2 parents 16f3371 + 9f06bd5 commit 5605768

8 files changed

Lines changed: 43 additions & 18 deletions

File tree

src/api/errmsg.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
# ----------------------------------------------------------------------
1111

1212
import sys
13+
from typing import Optional
14+
1315
from . import global_
1416
from .config import OPTIONS
1517

@@ -31,7 +33,7 @@ def info(msg):
3133
OPTIONS.stderr.write("info: %s\n" % msg)
3234

3335

34-
def error(lineno, msg, fname=None):
36+
def error(lineno, msg, fname: Optional[str] = None):
3537
""" Generic syntax error routine
3638
"""
3739
if fname is None:
@@ -49,7 +51,7 @@ def error(lineno, msg, fname=None):
4951
global_.has_errors += 1
5052

5153

52-
def warning(lineno, msg, fname=None):
54+
def warning(lineno, msg, fname: Optional[str] = None):
5355
""" Generic warning error routine
5456
"""
5557
if fname is None:

src/libzxbc/zxbparser.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3490,11 +3490,9 @@ def p_error(p):
34903490

34913491
if p is not None:
34923492
if p.type != 'NEWLINE':
3493-
msg = "Syntax Error. Unexpected token '%s' <%s>" % \
3494-
(p.value, p.type)
3495-
3493+
msg = "Syntax Error. Unexpected token '%s' <%s>" % (p.value, p.type)
34963494
else:
3497-
msg = "Unexpected end of file"
3495+
msg = "Unexpected end of line"
34983496
error(p.lexer.lineno, msg)
34993497
else:
35003498
msg = "Unexpected end of file"

src/libzxbpp/prepro/output.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@
66
Need the global OPTION object
77
"""
88

9+
from typing import Optional
10+
911
import src.api.errmsg
1012

13+
1114
CURRENT_FILE = [] # The current file being processed
1215

1316

14-
def error(lineno, str_):
15-
src.api.errmsg.error(lineno, str_)
17+
def error(lineno, str_, fname: Optional[str] = None):
18+
src.api.errmsg.error(lineno, str_, fname=fname)
1619

1720

18-
def warning(lineno, str_):
19-
src.api.errmsg.warning(lineno, str_)
21+
def warning(lineno, str_, fname: Optional[str] = None):
22+
src.api.errmsg.warning(lineno, str_, fname=fname)

src/libzxbpp/zxbpp.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -740,10 +740,14 @@ def p_argstring_argstring(p):
740740

741741
def p_error(p):
742742
if p is not None:
743-
value = p.value
744-
value = ''.join(['|%s|' % hex(ord(x)) if x < ' ' else x for x in value])
745-
error(p.lineno, "syntax error. Unexpected token '%s' [%s]" %
746-
(value, p.type))
743+
if p.type == 'NEWLINE':
744+
error(p.lineno, "Syntax error. Unexpected end of line", output.CURRENT_FILE[-1])
745+
elif p.type == '_ENDFILE_':
746+
error(p.lineno, "Syntax error. Unexpected end of file", output.CURRENT_FILE[-1])
747+
else:
748+
value = p.value
749+
value = ''.join(['|%s|' % hex(ord(x)) if x < ' ' else x for x in value])
750+
error(p.lineno, "Syntax error. Unexpected token '%s' [%s]" % (value, p.type), output.CURRENT_FILE[-1])
747751
else:
748752
OPTIONS.stderr.write("General syntax error at preprocessor "
749753
"(unexpected End of File?)")
@@ -804,7 +808,7 @@ def main(argv):
804808
return global_.has_errors
805809

806810

807-
parser = src.api.utils.get_or_create('zxbppparse', lambda: yacc.yacc())
811+
parser = yacc.yacc()
808812
parser.defaulted_states = {}
809813
ID_TABLE = DefinesTable()
810814

src/libzxbpp/zxbpplex.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,8 @@ def include(self, filename):
422422
def include_end(self):
423423
""" Performs and end of include.
424424
"""
425+
old_lineno = self.lex.lineno
426+
old_lexpos = self.lex.lexpos
425427
self.lex = self.filestack[-1][2]
426428
self.input_data = self.filestack[-1][3]
427429
self.filestack.pop()
@@ -434,8 +436,8 @@ def include_end(self):
434436
result = lex.LexToken()
435437
result.value = self.put_current_line(suffix='\n')
436438
result.type = '_ENDFILE_'
437-
result.lineno = self.lex.lineno
438-
result.lexpos = self.lex.lexpos
439+
result.lineno = old_lineno
440+
result.lexpos = old_lexpos
439441

440442
return result
441443

tests/functional/due_inc_main.bas

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
' Main.bas
2+
3+
#include "due_par.bas"
4+
5+
Test(5)
6+
7+
pause 0
8+

tests/functional/due_par.bas

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
SUB test(code as Ubyte)
2+
k=in(12234
3+
END SUB
4+

tests/functional/test_errmsg.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,8 @@ llb.bas:3: error: Undeclared function "f$"
163163
>>> process_file('opt2_include_unused.bas')
164164
func0.bas:5: warning: Using default implicit type 'float' for 'f'
165165
func0.bas:4: warning: Function 'f' is never called and has been ignored
166-
166+
>>> process_file('due_par.bas')
167+
due_par.bas:2: error: Syntax error. Unexpected end of line
168+
>>> process_file('due_inc_main.bas')
169+
due_par.bas:2: error: Syntax error. Unexpected end of line
170+
due_par.bas:5: error: Syntax error. Unexpected end of file

0 commit comments

Comments
 (0)