Giter Site home page Giter Site logo

emacs-pl's Introduction

This is a parsing library in the spirit of Haskell's parsec. For example:

(pl-parse
  (delete-region (pl-str "<xml>" :beg)
                 (pl-until
                   (pl-str "</xml>" :end))))

There are a few parsers, whose job is to inspect whatever is at the current buffer position, and return zero or more details regarding what was found:

pl-ch                Match a single character
pl-str               Match a string
pl-re                Match a regular expression
pl-num               Match an integer or floating-point number

Other possibilities include: inspecting text properties, overlays, etc.

If the parser succeeds, it returns the object matched (a string by default), and advances point to the next position after the match. Keywords may be given to return other details:

:beg                 Beginning of the match
:end                 End of the match
:group N             A particular regexp group
:props               All properties within the matched region
:nil                 Return `nil` (same as using `ignore`)

If a parser fails, it throws the exception failed. This is caught by the macro pl-try, which returns nil upon encountering the exception. This makes it possible to build certain combinators out of these few parts:

pl-or                Return result from first successful parser
pl-and               Return last result, if all parsers succeed
pl-until             If the parse fails, advance cursor position by
                     one character and try again.  Keywords can
                     change the advance amount.

For other constructs, such as returning the result of every parser as a list, just combine parsers with regular Lisp forms (pl-parse is just a synonym for pl-try):

(pl-parse
  (list (pl-str "Hello") (pl-str "World")))

Note that even though a parse may fail, and thus return no value, any side-effects that occur during the course of the parse will of course be retained. This can be used to good effect, by continuing an action for as long as a parse succeeds:

(pl-parse
  (while t
     (delete-region (pl-str "<xml>" :beg)
                    (pl-until
                      (pl-str "</xml>" :end)))))

This will delete blocks demarcated by <xml> and </xml>, for as long as such blocks continue to occur contiguously to one another.

emacs-pl's People

Contributors

jwiegley avatar tzz avatar

Watchers

Tadeusz Kurpiel avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.