all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Helmut Eller <eller.helmut@gmail.com>
To: help-gnu-emacs@gnu.org
Subject: Re: how to use parsing expressing grammar
Date: Sun, 21 Dec 2008 12:24:41 +0100	[thread overview]
Message-ID: <m2bpv5ojnq.fsf@gmail.com> (raw)
In-Reply-To: deeda909-1f3a-4cd5-8e63-bf00144b92a4@g1g2000pra.googlegroups.com

* Xah Lee [2008-12-20 23:27+0100] writes:

> Hi Helmut,
>
> another question i have is, how do i capture text as in regex's “\(...
> \)”, “\1”?
>
> I know in your elisp header file it mentions
>
> ;;  (action FORM)          ; evaluate FORM
> ;;  `(VAR... -- FORM...)   ; stack action
>
> how do i capture match using that?

Basically there are 2 phases.  The first phase parses, i.e., decides
which rules match, and the second phase executes actions.  Actions are
executed at the point of match.  The action can do whatever is needed.

The peg stack can be use to pass values between actions.  You don't have
to use the stack, you could also use global variables or whatever is
convenient.

[...]
> suppose i want to swap the src and alt text and have
>
> <img src="pretty" alt="some.png" width="33" height="33" >

That's a bit complicated, but I think it's also a bit complicated to swap
to submatches with regexps.  Let's assume that we have a helper function
to swap two regions:

(defun swap-regions (start1 end1 start2 end2)
  (if (< start2 start1) 
      (swap-regions start2 end2 start1 end1)
    (let ((string1 (buffer-substring start1 end1))
	  (string2 (buffer-substring start2 end2)))
      (delete-region start2 end2)
      (goto-char start2)
      (insert string1)
      (delete-region start1 end1)
      (goto-char start1)
      (insert string2))))

Then this should work:

(defun doMyReplace ()
  (interactive)
  (let (src-start src-end alt-start alt-end)
    (peg-parse
     (imgTag "<img" _ (+ attributes _) ">")
     (attributes (or src alt width height))
     (src "src" = 
	  (action (setq src-start (point)))
	  "\"" filePath "\""
	  (action (setq src-end (point))))
     (filePath (+ [A-Z a-z "./_-"]))
     (alt "alt" = 
	  (action (setq alt-start (point)))
	  "\"" altStr "\"" 
	  (action (setq alt-end (point))))
     (altStr (* [A-Z a-z "./ '_"]))
     (width "width" =  "\"" digits "\"")
     (height "height" = "\"" digits "\"")
     (= _* "=" _*)
     (_* (* ["\n \t"])) ; 0 or more white space
     (_ (+ ["\n \t"])) ; 1 or more white space
     (digits (+ [0-9])))
    (swap-regions alt-start alt-end src-start src-end)))


I guess a mechanism to give matched regions a name would be useful.
E.g.  (capture E NAME) would match E and store the matched region with
NAME in a hashtable.  Actions could than easily refer to that region by
looking in the hashtable.  Auxiliary functions like (capture-string NAME)
or (capture-beginning NAME) to lookup the matched string resp. start
position in the hypothetical capture table could be implemented on top
of that.  Also something like replace-capture would then be easy to add.

Helmut.


  reply	other threads:[~2008-12-21 11:24 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <6b8a1070-1a89-48b0-9287-343b673b5758@a29g2000pra.googlegroups.com>
     [not found] ` <m27i5yygi5.fsf@gmail.com>
     [not found]   ` <m2k59ywtj2.fsf@gmail.com>
     [not found]     ` <b3203a8b-324f-440f-98a9-653c8d582c7c@y1g2000pra.googlegroups.com>
2008-12-20  8:42       ` how to use parsing expressing grammar Xah Lee
2008-12-20  9:34         ` Helmut Eller
2008-12-20 21:41           ` Xah Lee
2008-12-21  9:49             ` Helmut Eller
2009-03-03 17:34               ` Leo
2009-03-03 17:59                 ` Mike Mattie
     [not found]                 ` <mailman.2299.1236115586.31690.help-gnu-emacs@gnu.org>
2009-03-03 22:05                   ` Xah Lee
2009-03-03 23:52                     ` W Dan Meyer
2009-03-04  0:35                       ` Miles Bader
2009-03-05  6:55                         ` Mike Mattie
2009-03-05  6:18                       ` Mike Mattie
2009-03-05 16:38                     ` Mike Mattie
2009-03-06  8:53                     ` Helmut Eller
2008-12-20 22:27           ` Xah Lee
2008-12-21 11:24             ` Helmut Eller [this message]
2008-12-23 23:21         ` ashishnkadakia
2008-12-17 11:53 Xah Lee
2008-12-18  3:43 ` Kevin Rodgers
     [not found] ` <mailman.3007.1229571828.26697.help-gnu-emacs@gnu.org>
2008-12-18  9:24   ` Xah Lee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=m2bpv5ojnq.fsf@gmail.com \
    --to=eller.helmut@gmail.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.