Skip to content

Commit

Permalink
incremental example
Browse files Browse the repository at this point in the history
  • Loading branch information
cgrand committed Aug 4, 2011
1 parent 0a2dad6 commit 926fbb4
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions README.asciidoc
Expand Up @@ -115,6 +115,8 @@ A parser is created using the `parser` or `make-parser` functions.

== Creating buffers ==

Creating a buffer, editing it and getting its resulting parse-tree:

----
(-> p p/incremental-buffer (p/edit 0 0 "(") (p/edit 1 0 "(x)") p/parse-tree pprint)
Expand All @@ -126,6 +128,25 @@ A parser is created using the `parser` or `make-parser` functions.
{:tag :expr, :content ["(" {:tag :expr, :content ["x"]} ")"]}]}]}
----

Incremental parsing at work:

----
=> (def p (p/parser :expr #{"x" "\n" ["(" :expr* ")"]}))
#'net.cgrand.parsley/p
=> (let [line (apply str "\n" (repeat 10 "((x))"))
input (str "(" (apply str (repeat 1000 line)) ")")
buf (p/incremental-buffer p)
buf (p/edit buf 0 0 input)]
(time (p/parse-tree buf))
(time (p/parse-tree (-> buf (p/edit 2 0 "(") (p/edit 51002 0 ")"))))
nil)
"Elapsed time: 508.834 msecs"
"Elapsed time: 86.038 msecs"
nil
----

Hence, *reparsing the buffer only took a fraction of the original time* despite
the buffer having been modified at the start and at the end.

== Options ==

Expand Down

0 comments on commit 926fbb4

Please sign in to comment.