Skip to content

Commit 922b532

Browse files
authored
Merge pull request #447 from boriel/bugfix/msg_line_numbers_reporting
Put line after macro
2 parents a1c22a9 + 1491d8b commit 922b532

59 files changed

Lines changed: 149 additions & 120 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/libzxbc/zxblex.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,14 @@ def t_BNOT(t):
225225
return t
226226

227227

228+
def t_initial_PREPROC_LINE(t):
229+
r'\#[ \t]*[Ll][Ii][Nn][Ee][ \t]+([0-9]+)(?:[ \t]+"((?:[^"]|"")*)")?[ \t]*\r?\n'
230+
231+
match = re.match('#[ \t]*[Ll][Ii][Nn][Ee][ \t]+([0-9]+)(?:[ \t]+"((?:[^"]|"")*)")?[ \t]*\r?\n', t.value)
232+
t.lexer.lineno = int(match.groups()[0])
233+
global_.FILENAME = match.groups()[1] or global_.FILENAME
234+
235+
228236
def t_INITIAL_SHARP(t):
229237
r'\#'
230238

src/libzxbc/zxbparser.py

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,24 +3158,17 @@ def p_type(p):
31583158
p[0] = p[1].lower()
31593159

31603160

3161-
# Some preprocessor directives
3162-
def p_preprocessor_line(p):
3163-
""" preproc_line : preproc_line_line
3164-
"""
3165-
3166-
3167-
def p_preprocessor_line_line(p):
3168-
""" preproc_line_line : _LINE INTEGER
3169-
"""
3170-
p.lexer.lineno = int(p[2]) + p.lexer.lineno - p.lineno(2)
3171-
3172-
3173-
def p_preprocessor_line_line_file(p):
3174-
""" preproc_line_line : _LINE INTEGER STRING
3175-
"""
3176-
p.lexer.lineno = int(p[2]) + p.lexer.lineno - p.lineno(3) - 1
3177-
gl.FILENAME = p[3]
3161+
# region PREPROC
31783162

3163+
# ----------------------------------------
3164+
# PREPROCESSOR lines starting with:
3165+
#
3166+
# #pragma
3167+
# #init
3168+
# #require
3169+
#
3170+
# are processed here and not in the lexer
3171+
# ----------------------------------------
31793172

31803173
def p_preproc_line_init(p):
31813174
""" preproc_line : _INIT ID
@@ -3510,7 +3503,7 @@ def p_error(p):
35103503
msg = "Syntax Error. Unexpected token '%s' <%s>" % (p.value, p.type)
35113504
else:
35123505
msg = "Unexpected end of line"
3513-
error(p.lexer.lineno, msg)
3506+
error(p.lineno, msg)
35143507
else:
35153508
msg = "Unexpected end of file"
35163509
error(zxblex.lexer.lineno, msg)

src/libzxbpp/prepro/macrocall.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,6 @@ def __call__(self, symbolTable: 'prepro.DefinesTable' = None) -> str:
9999
table.set(id_.args[i].name, self.lineno, args[i])
100100

101101
tmp = id_(table)
102-
if '\n' in tmp:
103-
tmp += '\n#line %i\n' % self.lineno
104-
105102
return tmp
106103

107104
def is_defined(self, symbolTable: 'prepro.DefinesTable' = None) -> bool:

src/libzxbpp/zxbpp.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@
1414
import sys
1515
import os
1616
import argparse
17-
from typing import NamedTuple, List
17+
18+
from typing import Any
19+
from typing import List
20+
from typing import NamedTuple
21+
from typing import Optional
1822

1923
from .zxbpplex import tokens # noqa
2024
from src.libzxbpp import zxbpplex
@@ -47,7 +51,7 @@
4751
CURRENT_DIR = None
4852

4953
# Default include path
50-
INCLUDEPATH = ['library', 'library-asm']
54+
INCLUDEPATH: List[str] = ['library', 'library-asm']
5155

5256
# Enabled to FALSE if IFDEF failed
5357
ENABLED: bool = True
@@ -100,7 +104,7 @@ def init():
100104
del output.CURRENT_FILE[:]
101105

102106

103-
def get_include_path():
107+
def get_include_path() -> str:
104108
""" Default include path using a tricky sys calls.
105109
"""
106110
return os.path.realpath(os.path.join(
@@ -117,7 +121,7 @@ def set_include_path():
117121
INCLUDEPATH = [os.path.join(pwd, 'library'), os.path.join(pwd, 'library-asm')]
118122

119123

120-
def setMode(mode):
124+
def setMode(mode: str) -> None:
121125
global LEXER
122126

123127
mode = mode.upper()
@@ -130,14 +134,17 @@ def setMode(mode):
130134
LEXER = zxbpplex.Lexer()
131135

132136

133-
def search_filename(fname, lineno, local_first):
137+
def search_filename(fname: str, lineno: int, local_first: bool) -> str:
134138
""" Search a filename into the list of the include path.
135139
If local_first is true, it will try first in the current directory of
136140
the file being analyzed.
137141
"""
138142
fname = src.api.utils.sanitize_filename(fname)
139-
i_path = [CURRENT_DIR] + INCLUDEPATH if local_first else list(INCLUDEPATH)
143+
144+
assert CURRENT_DIR is not None
145+
i_path: List[str] = [CURRENT_DIR] + INCLUDEPATH if local_first else list(INCLUDEPATH)
140146
i_path.extend(OPTIONS.include_path.split(':') if OPTIONS.include_path else [])
147+
141148
if os.path.isabs(fname):
142149
if os.path.isfile(fname):
143150
return fname
@@ -151,7 +158,7 @@ def search_filename(fname, lineno, local_first):
151158
return ''
152159

153160

154-
def include_file(filename, lineno, local_first):
161+
def include_file(filename: str, lineno: int, local_first: bool) -> str:
155162
""" Performs a file inclusion (#include) in the preprocessor.
156163
Writes down that "filename" was included at the current file,
157164
at line <lineno>.
@@ -174,7 +181,7 @@ def include_file(filename, lineno, local_first):
174181
return LEXER.include(filename)
175182

176183

177-
def include_once(filename, lineno, local_first):
184+
def include_once(filename: str, lineno: int, local_first: bool) -> str:
178185
""" Performs a file inclusion (#include) in the preprocessor.
179186
Writes down that "filename" was included at the current file,
180187
at line <lineno>.
@@ -201,6 +208,20 @@ def include_once(filename, lineno, local_first):
201208
return ''
202209

203210

211+
def expand_macros(macros: List[Any], lineno: int) -> Optional[str]:
212+
try:
213+
tmp = ''.join(remove_spaces(str(x())) if isinstance(x, MacroCall) else x for x in macros)
214+
except PreprocError as v:
215+
error(v.lineno, v.message)
216+
return None
217+
218+
if '\n' in tmp:
219+
tmp += f'\n#line {lineno + 1}'
220+
tmp += '\n'
221+
222+
return tmp
223+
224+
204225
# -------- GRAMMAR RULES for the preprocessor ---------
205226
def p_start(p):
206227
""" start : program
@@ -227,15 +248,12 @@ def p_program(p):
227248
def p_program_tokenstring(p):
228249
""" program : defs NEWLINE
229250
"""
230-
try:
231-
tmp = [remove_spaces(str(x())) if isinstance(x, MacroCall) else x for x in p[1]]
232-
except PreprocError as v:
233-
error(v.lineno, v.message)
251+
tmp = expand_macros(p[1], p.lineno(2))
252+
if tmp is None:
234253
p[0] = []
235254
return
236255

237-
tmp.append(p[2])
238-
p[0] = tmp
256+
p[0] = [tmp]
239257

240258

241259
def p_program_tokenstring_2(p):
@@ -261,22 +279,19 @@ def p_program_char(p):
261279
def p_program_newline(p):
262280
""" program : program defs NEWLINE
263281
"""
264-
try:
265-
tmp = [remove_spaces(str(x())) if isinstance(x, MacroCall) else x for x in p[2]]
266-
except PreprocError as v:
267-
error(v.lineno, v.message)
282+
tmp = expand_macros(p[2], p.lineno(3))
283+
if tmp is None:
268284
p[0] = []
269285
return
270286

271287
p[0] = p[1]
272-
p[0].extend(tmp)
273-
p[0].append(p[3])
288+
p[0].append(tmp)
274289

275290

276291
def p_program_newline_2(p):
277292
""" program : program define NEWLINE
278293
"""
279-
p[0] = p[1] + p[2] + [p[3]]
294+
p[0] = p[1] + [f'#line {p.lineno(3) + 1} "{output.CURRENT_FILE[-1]}"\n']
280295

281296

282297
def p_token(p):

src/parsetab/tabs.dbm.bak

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'zxbpp', (0, 72571)
1+
'zxbpp', (0, 72580)
22
'asmparse', (72704, 254311)
33
'zxnext_asmparse', (327168, 285299)
4-
'zxbparser', (612864, 715296)
4+
'zxbparser', (612864, 711440)

src/parsetab/tabs.dbm.dat

-3.77 KB
Binary file not shown.

src/parsetab/tabs.dbm.dir

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
'zxbpp', (0, 72571)
1+
'zxbpp', (0, 72580)
22
'asmparse', (72704, 254311)
33
'zxnext_asmparse', (327168, 285299)
4-
'zxbparser', (612864, 715296)
4+
'zxbparser', (612864, 711440)

tests/functional/emook0.out

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@
44
ASM
55

66
LD A,A
7-
LD BC,254
8-
#line 6
9-
7+
LD BC,254
8+
#line 7
109
end Asm
1110

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
#define BORDE(x) \
3+
asm \
4+
ld a,x \
5+
out (254),a \
6+
end asm
7+
8+
DIM a
9+
10+
BORDE(5): a = 1
11+
a+
12+

tests/functional/prepro00.out

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212

1313

14-
14+
#line 13 "/zxbasic/src/arch/zx48k/library/attr.bas"
1515

1616
#pragma push(case_insensitive)
1717
#pragma case_insensitive = TRUE

0 commit comments

Comments
 (0)