Skip to content

Commit

Permalink
Merge pull request #8 from mvc-works/json-demo
Browse files Browse the repository at this point in the history
add other-than+ and provide a JSON demo
  • Loading branch information
Tmono authored May 4, 2020
2 parents f388905 + fac980a commit 2e69007
Show file tree
Hide file tree
Showing 7 changed files with 655 additions and 34 deletions.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,15 @@ Demo of `(def a (add 1 2))` http://repo.mvc-works.org/lilac-parser/

### Usage

_TODO_
[![Clojars Project](https://img.shields.io/clojars/v/mvc-works/lilac-parser.svg)](https://clojars.org/mvc-works/lilac-parser)

```edn
[mvc-works/lilac-parser "0.0.2-a1"]
```

```clojure
(require '[lilac-parser.core :refer [parse-lilac defparser is+ many+ one-of+ some+ combine+]])
(require '[lilac-parser.core :refer
[parse-lilac defparser is+ many+ one-of+ some+ combine+ interleave+ other-than+]])

(parse-lilac "aaaa" (many+ (is+ "a")))
```
Expand All @@ -37,6 +42,8 @@ Demo of a stupid S-expression parser:
(parse-lilac (string/split "(def a (add 1 2))" "") (s-expr-parser+))
```

`defparser` is a macro for defining parser that can be used recursively. Notice that `s-expr-parser+` is different from a normal `number-parser`, it's a function so it need to be called before passing as a rule.

### Workflow

Workflow https://github.com/mvc-works/calcit-workflow
Expand Down
577 changes: 554 additions & 23 deletions calcit.cirru

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@mvc-works/lilac-parser",
"version": "0.1.0",
"version": "0.0.2-a1",
"description": "Toy parser in cljs",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion release.edn
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{:version "0.0.1"
{:version "0.0.2-a1"
:group-id "mvc-works"
:artifact-id "lilac-parser"
:skip-tag true
Expand Down
8 changes: 5 additions & 3 deletions src/lilac_parser/comp/container.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
[cirru-edn.core :as cirru-edn]
[feather.core :refer [comp-icon]]
[lilac-parser.demo.s-expr :refer [s-expr-parser+]]
[lilac-parser.demo.json :refer [demo-parser]]))
[lilac-parser.demo.json
:refer
[demo-parser number-parser string-parser array-parser+ value-parser+]]))

(def style-label
{:font-family ui/font-code,
Expand Down Expand Up @@ -111,8 +113,8 @@
:inner-text "Parse",
:on-click (fn [e d!]
(let [result (parse-lilac (string/split (:code state) "") (s-expr-parser+))
r1 (parse-lilac (string/split (:code state) "") demo-parser)]
(d! cursor (assoc state :result result))))})
r1 (parse-lilac (string/split (:code state) "") (value-parser+))]
(d! cursor (assoc state :result r1))))})
(=< 16 nil)
(span
{:inner-text "GUI",
Expand Down
20 changes: 20 additions & 0 deletions src/lilac_parser/core.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@
(when (and dev? (not (sequential? xs))) (println "Expected argument passed to or+ :" xs))
{:parser-node :or, :items xs})

(defn other-than+ [items]
(when (and dev? (not (or (string? items) (set? items))))
(println "Unexpected parameter passed to other-than+ :" items))
{:parser-node :other-than, :items items})

(defn seq-strip-beginning [xs ys]
(cond
(empty? ys) {:ok? true, :rest xs}
Expand All @@ -64,6 +69,20 @@
{:ok? true, :value (first xs), :rest (rest xs), :parser-node :one-of}
{:ok? false, :message "not in list", :parser-node :one-of, :rest xs})))

(defn parse-other-than [xs rule]
(if (empty? xs)
{:ok? false,
:message "Unexpected EOF in other-than+ rule",
:parser-node :other-than,
:rest xs}
(let [items (:items rule), x0 (first xs)]
(if (if (string? items) (string/includes? items x0) (contains? items x0))
{:ok? false,
:message (str (pr-str x0) "is in not expected item in other-than+"),
:parser-node :other-than,
:rest xs}
{:ok? true, :value x0, :rest (rest xs), :parser-node :other-than}))))

(defn parse-some [xs0 rule]
(let [item (:item rule)]
(loop [acc [], xs xs0]
Expand Down Expand Up @@ -136,6 +155,7 @@
:combine (parse-combine xs rule)
:one-of (parse-one-of xs rule)
:interleave (parse-interleave xs rule)
:other-than (parse-other-than xs rule)
(do (js/console.warn "Unknown node" rule) nil)))

(defn parse-interleave [xs0 rule]
Expand Down
69 changes: 65 additions & 4 deletions src/lilac_parser/demo/json.cljs
Original file line number Diff line number Diff line change
@@ -1,8 +1,69 @@

(ns lilac-parser.demo.json (:require [lilac-parser.core :refer [interleave+ is+]]))
(ns lilac-parser.demo.json
(:require [lilac-parser.core
:refer
[interleave+
is+
other-than+
many+
combine+
optional+
one-of+
some+
or+
defparser]]))

(def demo-parser (interleave+ (is+ "aaa") (is+ "b")))
(declare value-parser+)

(defn number-parser [] )
(declare array-parser+)

(defn string-parser [] )
(declare object-parser+)

(def boolean-parser (or+ [(is+ "true") (is+ "false")]))

(def space-parser (some+ (is+ " ")))

(def comma-parser (combine+ [space-parser (is+ ",") space-parser]))

(def digit-parser (one-of+ "1234567890"))

(def nil-parser (or+ [(is+ "null") (is+ "undefined")]))

(def number-parser
(combine+
[(optional+ (is+ "-"))
(many+ digit-parser)
(optional+ (combine+ [(is+ ".") (many+ digit-parser)]))]))

(def string-parser
(combine+
[(is+ "\"")
(some+ (or+ [(other-than+ "\"\\") (is+ "\\\"") (is+ "\\\\") (is+ "\\n")]))
(is+ "\"")]))

(defparser
value-parser+
()
identity
(or+
[number-parser string-parser nil-parser boolean-parser (array-parser+) (object-parser+)]))

(defparser
object-parser+
()
identity
(combine+
[(is+ "{")
(some+
(interleave+
(combine+ [string-parser space-parser (is+ ":") space-parser (value-parser+)])
comma-parser))
(is+ "}")]))

(defparser
array-parser+
()
identity
(combine+ [(is+ "[") (some+ (interleave+ (value-parser+) comma-parser)) (is+ "]")]))

(def demo-parser (many+ (other-than+ "abc")))

0 comments on commit 2e69007

Please sign in to comment.