From nobody Tue Feb 10 00:58:26 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1620770882; cv=none; d=zohomail.com; s=zohoarc; b=WJyh0ZBHhCasBB9vN1hAqL2EHCBG9sg0uO568ya/J9vra6EqBiHUXCkqbj74IhS8Yt4BWg4US/8gmx9UZIV9JEO9GHUWVC95i8U+eL9GZRLvSJNZcYfYssAvqQePJZcJOuYeuiO7hnlslsUBtlJvMLQDj1GvuSTt7kOsLETizs0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1620770882; h=Content-Type:Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=nAUt9ep2U9+NRGrcfgncJ14WoGsv9dOEug/5liuMNFs=; b=JRwIwF3TnZ19MLFI9aC1A8fbsmwM/+JRhzIAvcHkNBlw0Iv70+oCyeG7eW/2FotWo2SzHMm0O06rFHBJeVDa9EuTWbvwpSkaYL+2ONGKnDpzTdLFcGbr5bbd7Pu6WWfhQPtOBGw4J0p0QZmZVU04h6NS5KUvg7xUhLqmucSkITY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=fail header.from= (p=none dis=none) header.from= Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1620770882245939.1224494439688; Tue, 11 May 2021 15:08:02 -0700 (PDT) Received: from localhost ([::1]:45046 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lgaXp-0001ux-6h for importer@patchew.org; Tue, 11 May 2021 18:08:01 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55632) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lgaW4-0007M6-QG for qemu-devel@nongnu.org; Tue, 11 May 2021 18:06:12 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:54166) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lgaW2-0006O3-5f for qemu-devel@nongnu.org; Tue, 11 May 2021 18:06:12 -0400 Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-372-PpaUFrZ2M62VCTA3t9dhIw-1; Tue, 11 May 2021 18:06:06 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BC626107ACE3; Tue, 11 May 2021 22:06:05 +0000 (UTC) Received: from scv.redhat.com (ovpn-117-64.rdu2.redhat.com [10.10.117.64]) by smtp.corp.redhat.com (Postfix) with ESMTP id 20FDB60CC9; Tue, 11 May 2021 22:06:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1620770769; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nAUt9ep2U9+NRGrcfgncJ14WoGsv9dOEug/5liuMNFs=; b=NIe1lizNChlWw37Zc26FBL4zetaM/tCoX0OkKIoBK1+1iDVh0/vgvQdNovV15OhMbFqtZn lRCXpDk4onziZmdTvviPPq4f9dfQH77WZMNIGB0x62J5ZJReLfBj+O36qPF/qqwnKXaG8D i7YhxvwLNQk1IIyLFOWhpPitweQuNBE= X-MC-Unique: PpaUFrZ2M62VCTA3t9dhIw-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v2 04/21] qapi/parser: factor parsing routine into method Date: Tue, 11 May 2021 18:05:44 -0400 Message-Id: <20210511220601.2110055-5-jsnow@redhat.com> In-Reply-To: <20210511220601.2110055-1-jsnow@redhat.com> References: <20210511220601.2110055-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=jsnow@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=170.10.133.124; envelope-from=jsnow@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -34 X-Spam_score: -3.5 X-Spam_bar: --- X-Spam_report: (-3.5 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.699, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Michael Roth , John Snow , Eduardo Habkost , Markus Armbruster , Cleber Rosa Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: "Qemu-devel" X-ZohoMail-DKIM: fail (Header signature does not verify) Content-Type: text/plain; charset="utf-8" For the sake of keeping __init__ smaller (and treating it more like a gallery of what state variables we can expect to see), put the actual parsing action into a parse method. It remains invoked from the init method to reduce churn. To accomplish this, 'previously_included' becomes the private data member '_included', and the filename is stashed as _fname. Add any missing declarations to the init method, and group them by function so they can be understood quickly at a glance. Signed-off-by: John Snow --- scripts/qapi/parser.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/scripts/qapi/parser.py b/scripts/qapi/parser.py index 39dbcc4eacc..d620706fffb 100644 --- a/scripts/qapi/parser.py +++ b/scripts/qapi/parser.py @@ -37,23 +37,39 @@ def __init__(self, parser, msg): class QAPISchemaParser: =20 def __init__(self, fname, previously_included=3DNone, incl_info=3DNone= ): - previously_included =3D previously_included or set() - previously_included.add(os.path.abspath(fname)) + self._fname =3D fname + self._included =3D previously_included or set() + self._included.add(os.path.abspath(self._fname)) + self.src =3D '' =20 - # May raise OSError; allow the caller to handle it. - with open(fname, 'r', encoding=3D'utf-8') as fp: - self.src =3D fp.read() - - if self.src =3D=3D '' or self.src[-1] !=3D '\n': - self.src +=3D '\n' + # Lexer state (see `accept` for details): + self.info =3D QAPISourceInfo(self._fname, incl_info) + self.tok =3D None + self.pos =3D 0 self.cursor =3D 0 - self.info =3D QAPISourceInfo(fname, incl_info) + self.val =3D None self.line_pos =3D 0 + + # Parser output: self.exprs =3D [] self.docs =3D [] - self.accept() + + # Showtime! + self._parse() + + def _parse(self): cur_doc =3D None =20 + # May raise OSError; allow the caller to handle it. + with open(self._fname, 'r', encoding=3D'utf-8') as fp: + self.src =3D fp.read() + if self.src =3D=3D '' or self.src[-1] !=3D '\n': + self.src +=3D '\n' + + # Prime the lexer: + self.accept() + + # Parse until done: while self.tok is not None: info =3D self.info if self.tok =3D=3D '#': @@ -71,12 +87,12 @@ def __init__(self, fname, previously_included=3DNone, i= ncl_info=3DNone): if not isinstance(include, str): raise QAPISemError(info, "value of 'include' must be a strin= g") - incl_fname =3D os.path.join(os.path.dirname(fname), + incl_fname =3D os.path.join(os.path.dirname(self._fname), include) self.exprs.append({'expr': {'include': incl_fname}, 'info': info}) exprs_include =3D self._include(include, info, incl_fname, - previously_included) + self._included) if exprs_include: self.exprs.extend(exprs_include.exprs) self.docs.extend(exprs_include.docs) --=20 2.30.2