@samitouri / QOSamiQemu / commits / 8c5388bc73

minikconf: simplify self.tok

Do not ever store a string in self.tok, only a finished token. Reviewed-by: Marc-André Lureau <marcandre.lureau@redhat.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>

Paolo Bonzini committed Apr 24, 2026 at 10:24 UTC 8c5388bc736520c9efec6d49777579d81e01126f
1 file changed +30 -29
scripts/minikconf.py
+30 -29
@@ -330,9 +330,9 @@ TOK_EOF = 18; TOKENS[TOK_EOF] = 'end of file';
330 class KconfigParserError(Exception):
331 def __init__(self, parser, msg, tok=None):
332 self.loc = parser.location()
333 - tok = tok or parser.tok
333 + tok = tok if tok is not None else parser.tok
334 if tok != TOK_NONE:
335 - location = TOKENS.get(tok, None) or ('"%s"' % tok)
335 + location = TOKENS[tok] if isinstance(tok, int) else '"%s"' % tok
336 msg = '%s before %s' % (msg, location)
337 self.msg = msg
338
@@ -573,13 +573,14 @@ class KconfigParser:
573
574 def get_token(self):
575 while True:
576 - self.tok = self.src[self.cursor]
576 + ch = self.src[self.cursor]
577 self.pos = self.cursor
578 self.cursor += 1
579
580 self.val = None
581 - self.tok = self.scan_token()
582 - if self.tok is not None:
581 + tok = self.scan_token(ch)
582 + if tok is not None:
583 + self.tok = tok
584 return
585
586 def check_keyword(self, rest):
@@ -591,46 +592,46 @@ class KconfigParser:
592 self.cursor += length
593 return True
594
594 - def scan_token(self):
595 - if self.tok == '#':
595 + def scan_token(self, ch):
596 + if ch == '#':
597 self.cursor = self.src.find('\n', self.cursor)
598 return None
598 - elif self.tok == '=':
599 + elif ch == '=':
600 return TOK_EQUAL
600 - elif self.tok == '(':
601 + elif ch == '(':
602 return TOK_LPAREN
602 - elif self.tok == ')':
603 + elif ch == ')':
604 return TOK_RPAREN
604 - elif self.tok == '&' and self.src[self.pos+1] == '&':
605 + elif ch == '&' and self.src[self.pos+1] == '&':
606 self.cursor += 1
607 return TOK_AND
607 - elif self.tok == '|' and self.src[self.pos+1] == '|':
608 + elif ch == '|' and self.src[self.pos+1] == '|':
609 self.cursor += 1
610 return TOK_OR
610 - elif self.tok == '!':
611 + elif ch == '!':
612 return TOK_NOT
612 - elif self.tok == 'd' and self.check_keyword("epends"):
613 + elif ch == 'd' and self.check_keyword("epends"):
614 return TOK_DEPENDS
614 - elif self.tok == 'o' and self.check_keyword("n"):
615 + elif ch == 'o' and self.check_keyword("n"):
616 return TOK_ON
616 - elif self.tok == 's' and self.check_keyword("elect"):
617 + elif ch == 's' and self.check_keyword("elect"):
618 return TOK_SELECT
618 - elif self.tok == 'i' and self.check_keyword("mply"):
619 + elif ch == 'i' and self.check_keyword("mply"):
620 return TOK_IMPLY
620 - elif self.tok == 'c' and self.check_keyword("onfig"):
621 + elif ch == 'c' and self.check_keyword("onfig"):
622 return TOK_CONFIG
622 - elif self.tok == 'd' and self.check_keyword("efault"):
623 + elif ch == 'd' and self.check_keyword("efault"):
624 return TOK_DEFAULT
624 - elif self.tok == 'b' and self.check_keyword("ool"):
625 + elif ch == 'b' and self.check_keyword("ool"):
626 return TOK_BOOL
626 - elif self.tok == 'i' and self.check_keyword("f"):
627 + elif ch == 'i' and self.check_keyword("f"):
628 return TOK_IF
628 - elif self.tok == 'y' and self.check_keyword(""):
629 + elif ch == 'y' and self.check_keyword(""):
630 return TOK_Y
630 - elif self.tok == 'n' and self.check_keyword(""):
631 + elif ch == 'n' and self.check_keyword(""):
632 return TOK_N
632 - elif (self.tok == 's' and self.check_keyword("ource")) or \
633 - self.tok == 'i' and self.check_keyword("nclude"):
633 + elif (ch == 's' and self.check_keyword("ource")) or \
634 + ch == 'i' and self.check_keyword("nclude"):
635 # source FILENAME
636 # include FILENAME
637 while self.src[self.cursor].isspace():
@@ -639,19 +640,19 @@ class KconfigParser:
640 self.cursor = self.src.find('\n', self.cursor)
641 self.val = self.src[start:self.cursor]
642 return TOK_SOURCE
642 - elif self.tok.isalnum():
643 + elif ch.isalnum():
644 # identifier
645 while self.src[self.cursor].isalnum() or self.src[self.cursor] == '_':
646 self.cursor += 1
647 self.val = self.src[self.pos:self.cursor]
648 return TOK_ID
648 - elif self.tok == '\n':
649 + elif ch == '\n':
650 if self.cursor == len(self.src):
651 return TOK_EOF
652 self.line += 1
653 self.line_pos = self.cursor
653 - elif not self.tok.isspace():
654 - raise KconfigParserError(self, 'invalid input')
654 + elif not ch.isspace():
655 + raise KconfigParserError(self, 'invalid input', ch)
656
657 return None
658