Skip to content

Commit a5ad716

Browse files
committed
refact: allow numbers and strings in zxbpp #if exprs
1 parent 84d404f commit a5ad716

4 files changed

Lines changed: 45 additions & 36 deletions

File tree

src/zxbpp/zxbasmpplex.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@
3434
("defargsopt", "exclusive"),
3535
("defargs", "exclusive"),
3636
("defexpr", "exclusive"),
37+
("msg", "exclusive"),
3738
("pragma", "exclusive"),
3839
("singlecomment", "exclusive"),
3940
("asmcomment", "exclusive"),
4041
)
4142

4243
_tokens = (
4344
"STRING",
45+
"TEXT",
4446
"TOKEN",
4547
"NEWLINE",
4648
"_ENDFILE_",
@@ -169,6 +171,15 @@ def t_prepro_ID(self, t):
169171
t.lexer.begin("pragma")
170172
elif t.type == "LINE":
171173
t.lexer.begin("line")
174+
elif t.type in ("ERROR", "WARNING"):
175+
t.lexer.begin("msg")
176+
return t
177+
178+
def t_msg_TEXT(self, t):
179+
r".*\n"
180+
t.lexer.lineno += 1
181+
t.lexer.begin("INITIAL")
182+
t.value = t.value.strip() # remove newline and spaces
172183
return t
173184

174185
def t_pragma_LP(self, t):
@@ -245,12 +256,7 @@ def t_prepro_pragma_INTEGER(self, t):
245256
r"[0-9]+" # an integer number
246257
return t
247258

248-
def t_prepro_pragma_STRING(self, t):
249-
r'"([^"]|"")*"' # a doubled quoted string
250-
t.value = t.value[1:-1] # Remove quotes
251-
return t
252-
253-
def t_defexpr_INITIAL_STRING(self, t):
259+
def t_INITIAL_prepro_pragma_defexpr_STRING(self, t):
254260
r'"([^"]|"")*"' # a doubled quoted string
255261
return t
256262

@@ -317,7 +323,7 @@ def t_line_defargs_defargsopt_prepro_define_defexpr_pragma_singlecomment_INITIAL
317323
r"."
318324
self.error("illegal preprocessor character '%s'" % t.value[0])
319325

320-
def t_line_defargs_defargsopt_prepro_define_defexpr_pragma_singlecomment_INITIAL_asmcomment_error(self, t):
326+
def t_line_msg_defargs_defargsopt_prepro_define_defexpr_pragma_singlecomment_INITIAL_asmcomment_error(self, t):
321327
"""error handling rule. This should never happens!"""
322328
pass # The lexer will raise an exception here. This is intended
323329

src/zxbpp/zxbpp.py

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -364,15 +364,6 @@ def p_include_once_ok(p):
364364
CURRENT_DIR = os.path.dirname(output.CURRENT_FILE[-1])
365365

366366

367-
def p_include(p):
368-
"""include : INCLUDE STRING"""
369-
if ENABLED:
370-
p[0] = include_file(p[2], p.lineno(2), local_first=True)
371-
else:
372-
p[0] = []
373-
p.lexer.next_token = "_ENDFILE_"
374-
375-
376367
def p_include_fname(p):
377368
"""include : INCLUDE FILENAME"""
378369
if ENABLED:
@@ -401,7 +392,7 @@ def p_include_macro(p):
401392
def p_include_once(p):
402393
"""include_once : INCLUDE ONCE STRING"""
403394
if ENABLED:
404-
p[0] = include_once(p[3], p.lineno(3), local_first=True)
395+
p[0] = include_once(p[3][1:-1], p.lineno(3), local_first=True)
405396
else:
406397
p[0] = []
407398

@@ -440,16 +431,19 @@ def p_line_file(p):
440431

441432
def p_require_file(p):
442433
"""require : REQUIRE STRING NEWLINE"""
443-
p[0] = ['#%s "%s"\n' % (p[1], utils.sanitize_filename(p[2]))]
434+
p[0] = ["#%s %s\n" % (p[1], utils.sanitize_filename(p[2]))]
444435

445436

446437
def p_init(p):
447-
"""init : INIT ID NEWLINE
448-
| INIT STRING NEWLINE
449-
"""
438+
"""init : INIT ID NEWLINE"""
450439
p[0] = ['#%s "%s"\n' % (p[1], p[2])]
451440

452441

442+
def p_init_str(p):
443+
"""init : INIT STRING NEWLINE"""
444+
p[0] = ["#%s %s\n" % (p[1], p[2])]
445+
446+
453447
def p_undef(p):
454448
"""undef : UNDEF ID"""
455449
if ENABLED:
@@ -459,14 +453,14 @@ def p_undef(p):
459453

460454

461455
def p_errormsg(p):
462-
"""errormsg : ERROR STRING"""
456+
"""errormsg : ERROR TEXT"""
463457
if ENABLED:
464458
error(p.lineno(1), p[2])
465459
p[0] = []
466460

467461

468462
def p_warningmsg(p):
469-
"""warningmsg : WARNING STRING"""
463+
"""warningmsg : WARNING TEXT"""
470464
if ENABLED:
471465
warning(p.lineno(1), p[2])
472466
p[0] = []
@@ -535,12 +529,16 @@ def p_pragma_id(p):
535529

536530
def p_pragma_id_expr(p):
537531
"""pragma : PRAGMA ID EQ ID
538-
| PRAGMA ID EQ STRING
539532
| PRAGMA ID EQ INTEGER
540533
"""
541534
p[0] = ["#%s %s %s %s" % (p[1], p[2], p[3], p[4])]
542535

543536

537+
def p_pragma_id_string(p):
538+
"""pragma : PRAGMA ID EQ STRING"""
539+
p[0] = ["#%s %s %s %s" % (p[1], p[2], p[3], p[4][1:-1])]
540+
541+
544542
def p_pragma_push(p):
545543
"""pragma : PRAGMA PUSH LP ID RP
546544
| PRAGMA POP LP ID RP
@@ -644,6 +642,16 @@ def p_expr(p):
644642
p[0] = str(p[1]()).strip()
645643

646644

645+
def p_expr_val(p):
646+
"""expr : NUMBER"""
647+
p[0] = p[1]
648+
649+
650+
def p_expr_str(p):
651+
"""expr : STRING"""
652+
p[0] = p[1]
653+
654+
647655
def p_exprne(p):
648656
"""expr : expr NE expr"""
649657
p[0] = "1" if p[1] != p[3] else "0"
@@ -858,7 +866,7 @@ def main(argv):
858866
return global_.has_errors
859867

860868

861-
parser = utils.get_or_create("zxbpp", lambda: yacc.yacc(debug=True))
869+
parser = yacc.yacc(debug=True)
862870
parser.defaulted_states = {}
863871

864872

src/zxbpp/zxbpplex.py

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
_tokens = (
4949
"STRING",
50+
"TEXT",
5051
"TOKEN",
5152
"NEWLINE",
5253
"_ENDFILE_",
@@ -196,11 +197,11 @@ def t_comment_endBlock(self, t):
196197
if not self.__COMMENT_LEVEL:
197198
t.lexer.begin("INITIAL")
198199

199-
def t_msg_STRING(self, t):
200+
def t_msg_TEXT(self, t):
200201
r".*\n"
201202
t.lexer.lineno += 1
202203
t.lexer.begin("INITIAL")
203-
t.value = t.value.strip() # remove newline an spaces
204+
t.value = t.value.strip() # remove newline and spaces
204205
return t
205206

206207
# Any other character is ignored until EOL
@@ -323,12 +324,7 @@ def t_prepro_pragma_INTEGER(self, t):
323324
r"[0-9]+" # an integer number
324325
return t
325326

326-
def t_prepro_pragma_STRING(self, t):
327-
r'"([^"\n]|"")*"' # a doubled quoted string
328-
t.value = t.value[1:-1] # Remove quotes
329-
return t
330-
331-
def t_INITIAL_defexpr_asm_STRING(self, t):
327+
def t_INITIAL_pragma_prepro_defexpr_asm_if_STRING(self, t):
332328
r'"([^"\n]|"")*"' # a doubled quoted string
333329
return t
334330

tests/functional/error_macro.asm

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
#warning "this is a warning"
2-
#error "this is an error"
1+
#warning this is a warning
2+
#error this is an error
33
nop
4-

0 commit comments

Comments
 (0)