help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Spaces please!


From: Pascal J. Bourguignon
Subject: Re: Spaces please!
Date: Tue, 01 Dec 2009 18:06:57 +0100
User-agent: Gnus/5.1008 (Gnus v5.10.8) Emacs/22.3 (darwin)

andrea <address@hidden> writes:

> I get sick sometimes to read something like
> x=y+1*(2-3*a^2)
> but I get even more sick to add spaces manually every time.

Funny.  I get sick reading something like 

  x = y+1 * ( 2-3 * a   ^2)

;-)

Perhaps the best form would be:

  (= x
    (+ y
       (* 1
          (- 2
             (* 3
                (^ a 2))))))


but that would be a different syntax.  Notice that it could be the
work of an editor to read a source in whatever "syntax" it may happen
to be, to present it to the user in whatever form the user prefers,
allowing it to do structural editing instead of textual edition, and
saving it back to whatever "syntax" is required. 

A little like it is done for character encodings, where you can load a
file in iso-2022, edit it in emacs unicode, and save it in utf-8, only
for language syntaxes.

But that'd be a whole project.



> I ask help to you elisp gurus, should I use a regexp 

No, you cannot parse arithmetic expressions with regular expressions,
they are too weak!  You need a parser.


> or a macro-like
> function (go-here, do-this)?
>
> I think I should define some rules like:
> - which operators want spaces (and if before or after)
> - what is the arity of them
>
> One problem for example could arise from things like
>
> (-2)^10
> but we could have some operators that could be binary or unary and check
> that somehow.
>
> Someone has done it already maybe?

Well, you could hack something half working yourself, or perhaps you
could use a predefined parser for the language in question.

http://cedet.sourceforget.net  has a parser generator (the
"bovinator", and parsers predefined for a number of programming
languages, that you could use to implement your "formula beautifier".
Once you have the parse tree, it is indeed trivial to generate the
expression with the number of spaces you want.


-- 
__Pascal Bourguignon__


reply via email to

[Prev in Thread] Current Thread [Next in Thread]