From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Re: Make peg.el a built-in library? Date: Wed, 16 Nov 2022 06:24:36 +0000 Message-ID: <87bkp7ct7f.fsf@localhost> References: <875yvtbbn3.fsf@ericabrahamsen.net> <877d07a16u.fsf@localhost> <87tu3asg2r.fsf@ericabrahamsen.net> <87edud25ov.fsf@localhost> <87a6511ku0.fsf@ericabrahamsen.net> <87wn85z0zl.fsf@ericabrahamsen.net> <87leobplpv.fsf_-_@ericabrahamsen.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3816"; mail-complaints-to="usenet@ciao.gmane.io" Cc: emacs-devel@gnu.org To: Eric Abrahamsen Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Nov 16 07:25:31 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ovBrW-0000gc-Co for ged-emacs-devel@m.gmane-mx.org; Wed, 16 Nov 2022 07:25:30 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1ovBqE-0008AV-5T; Wed, 16 Nov 2022 01:24:10 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ovBq9-0008A5-Mb for emacs-devel@gnu.org; Wed, 16 Nov 2022 01:24:05 -0500 Original-Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1ovBq3-00070R-F2 for emacs-devel@gnu.org; Wed, 16 Nov 2022 01:24:02 -0500 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id E8042240026 for ; Wed, 16 Nov 2022 07:23:56 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1668579836; bh=FS2/1cW7jPwp50mYFL5bYOF7shAINI3gnUxrTwWsMFs=; h=From:To:Cc:Subject:Date:From; b=HI3ph/hnC3pEXU39V1Oo1w9EQ5kuWmro7UeQRqCDh94L1Z6R2cjUNCJZNLYkFXmHf LbMRCrMf+AuuxfWjngHQxnYQoi+/438KDKAU1PZhsP7zFEAZX7u2+NBpLuRRGmmJWY xks9RTIPNpCzFD8JSgSNCZbMzoDhmJERK++mMob14ye0gfgb+kd2z7GCCpxoGxMP1g kS81dsiXzJFtT2DRRn2n32bzRmBsdyG7Ij+vuFkT/kee9T5Dypa8L20Ja8v/c4MpvD 6Z1EGR/yJpy6HrZfJWUiLqPKdMt2EwRFXeeScUHebTqMfvJel93ACQou9GqgJ9KSgU pYSQEL3IV9Gig== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4NBtKl3tkQz6tnl; Wed, 16 Nov 2022 07:23:55 +0100 (CET) In-Reply-To: <87leobplpv.fsf_-_@ericabrahamsen.net> Received-SPF: pass client-ip=185.67.36.65; envelope-from=yantar92@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:299900 Archived-At: Eric Abrahamsen writes: > Okay, here's a first stab. I read the paper, and understood about half > of it, which seemed like enough. It was interesting to see that the > paper explicitly calls out the exact greedy-matching behavior I'd > encountered. Thanks! > + Emacs Lisp provide several tools for parsing and matching text, from provides > +regular expressions (@pxref{Regular Expressions}) to full @acronym{LL} > +grammar parsers (@pxref{Top,, Bovine parser development, bovine}). > +@dfn{Parsing Expression Grammars} (@acronym{PEG}) are another approach > +to text parsing that offer more structure and composibility than > +regular expressions, but less complexity than context-free grammars. > + > +A @acronym{PEG} parser is defined as a list of named rules, each of > +which match text patterns, and/or contain references to other rules. > +Parsing is initiated with the function @code{peg-run} or the macro > +@code{peg-parse}, and parses text after point in the current buffer, > +using a given set of rules. > + > +The definition of each rule is referred to as a @dfn{parsing > +expression} (@acronym{PEX}), and can consist of a literal string, a > +regexp-like character range or set, a peg-specific construct > +resembling an elisp function call, a reference to another rule, or a > +combination of any of these. A grammar is expressed as a set of rules > +in which one rule is typically treated as a ``top-level'' or > +``entry-point'' rule. For instance: > + > +@example > +@group > +((number sign digit (* digit)) > + (sign (or "+" "-" "")) > + (digit [0-9])) > +@end group > +@end example > + > +The above grammar could be used directly in a call to > +@code{peg-parse}, in which the first rule is considered the > +``entry-point'' rule: > + > +@example > +(peg-parse > + ((number sign digit (* digit)) > + (sign (or "+" "-" "")) > + (digit [0-9]))) > +@end example > + > +Or set as the value of a variable, and the variable used in a > +combination of calls to @code{with-peg-rules} and @code{peg-run}, > +where the ``entry-point'' rule is given explicitly: > + > +@example > +(defvar number-grammar > + '((number sign digit (* digit)) > + (sign (or "+" "-" "")) > + (digit [0-9]))) > + > +(with-peg-rules number-grammar > + (peg-run (peg number))) > +@end example > + > +By default, calls to @code{peg-run} or @code{peg-parse} produce no > +output: parsing simply moves point. In order to return or otherwise > +act upon parsed strings, rules can include @dfn{actions}, see > +@xref{Parsing Actions} for more information. > + > +Individual rules can also be defined using a more @code{defun}-like > +syntax, using the macro @code{define-peg-rule}: > + > +@example > +(define-peg-rule digit () > + [0-9]) > +@end example > + > +This allows the rule to be referred to by name within calls to > +@code{peg-run} or @code{peg-parse} elsewhere, and also allows the use > +of function arguments in the rule body. > + > +@node PEX Definitions > +@section PEX Definitions > + > +Parsing expressions can be defined using the following syntax: > + > +@table @code > +@item (and E1 E2 ...) > +A sequence of PEXs that must all be matched. The @code{and} form is > +optional and implicit. > + > +@item (or E1 E2 ...) > +Prioritized choices, meaning that, as in Elisp, the choices are tried > +in order, and the first successful match is used. It is worth highlighting that it is different from CFGs. > +@item (* E) > +Zero or more of an expression, as the regexp ``*''. > + > +@item (+ E) > +One or more of an expression, as the regexp ``+''. It is worth highlighting the greedy part here and referring to &A and !A. > +@item SYMBOL > +A symbol representing a previously-define PEG rule. defined > +By default the process of parsing simply moves point in the current > +buffer, ultimately returning @code{t} if the parsing succeeds, and > +@code{nil} if it doesn't. It's also possible to define ``actions'' > +that can run arbitrary Elisp at certain points during parsing. These > +actions can affect something called the @dfn{parsing stack}: a list of > +values built up during the course of parsing. If the stack is > +non-@code{nil} at the end of parsing, it is returned as the final > +value of the parsing process. Actions are only run when the expression matches; with point moved after the match, right? What about &A and !A? > +There must be values on the stack before they can be popped and > +returned. What if there is just one value in the stack while the action required two? > +@item (list E) > +Match E, collect all values produced by E (and its sub-expressions) > +into a list, and push that list to the stack. > +@end table This one is not very clear. Does it imply that E is recursively wrapped into substring? > +It is up to the grammar author to keep track of which rules and > +sub-rules push values to the stack, and the state of the stack at any > +given point in the parsing. If an action pops values from an empty > +stack, the symbols will be bound to @code{nil}. The part about popping out of empty stack looks out of scope. Maybe move it to earlier discussion of variable bindings in actions? -- Ihor Radchenko // yantar92, Org mode contributor, Learn more about Org mode at . Support Org development at , or support my work at