Skip to content

Commit b7b1030

Browse files
authored
Merge pull request #428 from boriel/feature/add_prepro_concatenation
Feature/add prepro concatenation
2 parents 30aef4f + 76e8b17 commit b7b1030

6 files changed

Lines changed: 34 additions & 21 deletions

File tree

src/libzxbpp/zxbpplex.py

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@
1010
# This is the Lexer for the ZXBpp (ZXBasic Preprocessor)
1111
# ----------------------------------------------------------------------
1212

13+
import re
1314
import sys
15+
1416
from src.ply import lex
17+
from src.api import global_
1518
from src.libzxbpp.base_pplex import BaseLexer
1619

1720

@@ -102,24 +105,13 @@ def t_asm_CONTINUE(self, t):
102105
return t
103106

104107
def t_asm_COMMENT(self, t):
105-
r';'
106-
t.lexer.push_state('asmcomment')
107-
108-
def t_asmcomment_skip(self, t):
109-
r'.'
108+
r';.*'
110109

111-
def t_asmcomment_NEWLINE(self, t):
112-
r'\r?\n'
113-
# New line => remove whatever state in top of the stack and replace it with INITIAL
114-
t.lexer.lineno += 1
115-
t.lexer.pop_state()
116-
return t
117-
118-
def t_asm_NEWLINE(self, t):
119-
r'\r?\n'
120-
# New line => remove whatever state in top of the stack and replace it with INITIAL
121-
t.lexer.lineno += 1
122-
return t
110+
def t_asm_PREPROCLINE(self, t):
111+
r'\#[ \t]*[Ll][Ii][Nn][Ee][ \t]+([0-9]+)(?:[ \t]+"((?:[^"]|"")*)")?[ \t]*\r?\n'
112+
match = re.match('#[ \t]*[Ll][Ii][Nn][Ee][ \t]+([0-9]+)(?:[ \t]+"((?:[^"]|"")*)")?[ \t]*\r?\n', t.value)
113+
t.lexer.lineno = int(match.groups()[0])
114+
global_.FILENAME = match.groups()[1] or global_.FILENAME
123115

124116
def t_asm_ID(self, t):
125117
r'[_A-Za-z][_A-Za-z0-9]*'
@@ -149,7 +141,7 @@ def t_prepro_define_defargs_defargsopt_defexpr_pragma_if_NEWLINE(self, t):
149141
t.lexer.pop_state()
150142
return t
151143

152-
def t_INITIAL_NEWLINE(self, t):
144+
def t_INITIAL_asm_NEWLINE(self, t):
153145
r'\r?\n'
154146
t.lexer.lineno += 1
155147
return t

tests/functional/line_asm.bi

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
ASM
2+
NOP
3+
#line 25
4+
END ASM
5+
#warning this should be line 26
6+

tests/functional/line_asm.out

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#line 1 "line_asm.bi"
2+
ASM
3+
NOP
4+
END ASM
5+

tests/functional/line_err.bas

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
DIM q as UByte = 1
2+
ASM
3+
NOP
4+
#line 25
5+
END ASM
6+
DIM q = 1: REM this error should show at line 26, not line 6!
7+

tests/functional/test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ def updateTest(tfname: str, pattern_, strip_blanks: bool = True):
240240
lines = get_file_lines(tfname, replace_regexp=pattern_, replace_what=ZXBASIC_ROOT,
241241
replace_with=_original_root, strip_blanks=strip_blanks)
242242
with src.api.utils.open_file(tfname, 'wt', encoding='utf-8') as f:
243-
f.write('\n'.join(lines))
243+
f.write('\n'.join(lines) + '\n')
244244

245245

246246
@src.api.utils.timeout(_timeout)
@@ -506,7 +506,7 @@ def normalizeDiff(diff: List[str]) -> List[str]:
506506
lines = get_file_lines(tfname, replace_regexp=FILTER, replace_what=ZXBASIC_ROOT,
507507
replace_with=_original_root)
508508
with src.api.utils.open_file(fname1, 'wt', encoding='utf-8') as f:
509-
f.write('\n'.join(lines))
509+
f.write('\n'.join(lines) + '\n')
510510

511511
os.unlink(tfname)
512512
_msg("\rTest: %s (%s) updated\n" % (fname, fname1))

tests/functional/test_errmsg.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,4 +172,7 @@ due_par.bas:5: error: Syntax error. Unexpected end of file
172172
error_array.bas:3: error: Invalid assignment. Variable z$() is an array
173173
>>> process_file('prepro76.bi')
174174
prepro76.bi:2: error: this is an intended error
175-
175+
>>> process_file('line_asm.bi')
176+
line_asm.bi:26: warning: this should be line 26
177+
>>> process_file('line_err.bas')
178+
line_err.bas:5: error: Variable 'q' already declared at line_err.bas:1

0 commit comments

Comments
 (0)