Skip to content

Commit aeb7e07

Browse files
committed
refact: changes grammar
From now on, labels can only be declared in the 1st position of the row (they can be indented though).
1 parent 742692b commit aeb7e07

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

src/zxbc/zxblex.py

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,8 @@ def t_ID(t):
617617
entry = api.global_.SYMBOL_TABLE.get_entry(t.value) if api.global_.SYMBOL_TABLE is not None else None
618618
if entry:
619619
t.type = callables.get(entry.class_, t.type)
620+
elif is_label(t):
621+
t.type = "LABEL"
620622

621623
if t.type == "BIN":
622624
t.lexer.begin("bin")
@@ -683,7 +685,7 @@ def find_column(token):
683685
return column
684686

685687

686-
def is_label(token):
688+
def is_label(token) -> bool:
687689
"""Return whether the token is a label (an integer number or id
688690
at the beginning of a line.
689691
@@ -706,11 +708,23 @@ def is_label(token):
706708
i -= 1
707709

708710
column = c - i
711+
if column != 0:
712+
return False
713+
714+
if token.type == "NUMBER":
715+
return True
716+
717+
i = token.lexpos + len(token.value)
718+
while i < len(input):
719+
if input[i] in {"\n", ":"}:
720+
return True
721+
722+
if input[i] not in {" ", "\t"}:
723+
break
709724

710-
if column == 0:
711-
column += 1
725+
i += 1
712726

713-
return column == 1
727+
return False
714728

715729

716730
lexer = lex.lex()

src/zxbc/zxbparser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1074,7 +1074,7 @@ def p_statement_call(p):
10741074
p[0] = None
10751075
elif len(p) == 2:
10761076
entry = SYMBOL_TABLE.get_entry(p[1])
1077-
if not entry or entry.class_ in (CLASS.label, CLASS.unknown):
1077+
if entry is not None and entry.class_ in (CLASS.label, CLASS.unknown):
10781078
p[0] = make_label(p[1], p.lineno(1))
10791079
else:
10801080
p[0] = make_sub_call(p[1], p.lineno(1), make_arg_list(None))

0 commit comments

Comments
 (0)