From nobody Tue Jul 1 09:14:24 2025 Delivered-To: importer2@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+importer2=patchew.org@nongnu.org; dmarc=fail(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1621451883; cv=none; d=zohomail.com; s=zohoarc; b=Of7SKtKjP860u/gwV4xuk/8167J7yEZln9/HiiR3AhD7un35KYecqffMcf5NDQwZeBAO27qUo3fV6YB/NTIT4WTZurZhPmxykre5/Jm2YJhO844/5cRL+tom2/2IeDYxHyxsiv2+b+9Z8B3X/Y222Oh6+qG6GgMJShQPQZNxlHc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1621451883; 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=jp8Frl7879ztIXD/N9WVnUKG+uz2E5Qwvhv8zguDcGA=; b=bCJtRYNlNlf3wJH2WXvDGrIYPuFPUyv2QKz1H3vT773QZUuJ4AxySA2MRXbm8CvDeVvk9ShppgYlBLp9ePHoTon6tFHAbEgin+H56066a6xD5xLiNwGtYRMpC/NbrNZk1jCZ6WqNqp13IlUvrsTDUVTpn65LnnRtvZsnbsLfbRs= 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+importer2=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 1621451883443411.9002218636134; Wed, 19 May 2021 12:18:03 -0700 (PDT) Received: from localhost ([::1]:36844 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ljRhi-0000xE-AX for importer2@patchew.org; Wed, 19 May 2021 15:18:02 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:55244) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljR71-000612-O3 for qemu-devel@nongnu.org; Wed, 19 May 2021 14:40:09 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:23600) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ljR6w-0001zN-TP for qemu-devel@nongnu.org; Wed, 19 May 2021 14:40:07 -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-162-1nkSOxKNNSChjWtj7_h0nw-1; Wed, 19 May 2021 14:39:58 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 0DEE5100747E; Wed, 19 May 2021 18:39:57 +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 5378B59443; Wed, 19 May 2021 18:39:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1621449600; 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=jp8Frl7879ztIXD/N9WVnUKG+uz2E5Qwvhv8zguDcGA=; b=OGcYhtqtJMweNzLOvpqeYnXxgJT5hpqcCKS0+qpw+EiswLuk2pus4MCwRGAFDmGHAvDFFO SpOgxKLfhplkS7H3QTv86leKuMdZVgPHS5B0EPl7vGp79PALQchfB88BTAM72I+0I20LKp spSdw0CtsF3BKf2rIWZ7aUwYEoj7a9Q= X-MC-Unique: 1nkSOxKNNSChjWtj7_h0nw-1 From: John Snow To: qemu-devel@nongnu.org Subject: [PATCH v3 04/15] qapi/parser: factor parsing routine into method Date: Wed, 19 May 2021 14:39:40 -0400 Message-Id: <20210519183951.3946870-5-jsnow@redhat.com> In-Reply-To: <20210519183951.3946870-1-jsnow@redhat.com> References: <20210519183951.3946870-1-jsnow@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 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+importer2=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: -31 X-Spam_score: -3.2 X-Spam_bar: --- X-Spam_report: (-3.2 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.39, 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 , Cleber Rosa , John Snow , Eduardo Habkost , Markus Armbruster Errors-To: qemu-devel-bounces+importer2=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