/parser-combinators

Simple Parser Combinators solution in Clojure

Primary LanguageCirruEclipse Public License 1.0EPL-1.0

Parser Combinators in Clojure

Clojure library to provide simple parser combinators solution.

Usage

Clojars Project

[mvc-works/parser-combinators "0.0.5-a1"]

Examples:

Exposed APIs

The structure of the whole state(:x, :y not in use yet):

(def initial-state
 {:y 0,
  :value nil,
  :msg "initial",
  :code "",
  :indentation 0,
  :x 0,
  :tab "",
  :failed false})

Some combinators to build larger parser:

combine-some
combine-asterisk
combine-chain
combine-interleave
combine-or
combine-opposite
combine-peek
combine-times
combine-optional

Special function to handle values:

handle-value
transform-value

Generators of some very simple parser:

generate-char
generate-char-in
generate-chars
generate-char-match

Some parsers defined in parsing Cirru, you may use them. However they may be not very suitable to many of your needs:

parse-eof
parse-open-paren
parse-close-paren
parse-double-quote
parse-whitespace
parse-backslash
parse-line-break
parse-escaped-char
parse-blanks
parse-newlines
parse-token-special
parse-string-special
parse-token-end
parse-in-string-char
parse-in-token-char
parse-string
parse-token
parse-empty-line
parse-line-breaks
parse-two-blanks
parse-indentation
parse-indent
parse-unindent
parse-align

nil in recursion

There's an edge case in combing the parser that when you call:

(def parse-item
  (pc/combine-or
   parse-null
   parse-number
   pc/parse-string
   parse-array
   (fn [x] (parse-object x))))

since combination rules are defined recursively but not in functions, parse-object can be nil when used on parse-item since parse-object is actually using parse-item, which is like:

# arrow means depends on...
parse-json -> parse-object -> parse-item -> parse-object -> parse-item -> ...

and nil is definitely not a valid parser. So in order to treat the combinator, use a function instead.

(def parse-item
  (pc/combine-or
   parse-null
   parse-number
   pc/parse-string
   parse-array
   (fn [x] (parse-object x))))

Where's Clojure code?

It's generated from Sepal.clj , try this command:

lein cirru-sepal

Bugs

Or, to be improved:

  • error messages look very bad

License

Copyright © 2015 jiyinyiyong

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.