Permalink
Browse files

Better documentation for that last example

  • Loading branch information...
1 parent 25ecbf6 commit fc66cfd455e1a70d54021a12df556e6aeaf0b5dd @kschiess committed Dec 27, 2012
Showing with 11 additions and 1 deletion.
  1. +11 −1 example/capture.rb
View
12 example/capture.rb
@@ -13,14 +13,24 @@
class CapturingParser < Parslet::Parser
root :document
+ # Introduce a scope for each document. This ensures that documents can be
+ # nested.
rule(:document) { scope { doc_start >> text >> doc_end } }
+
+ # Start of a document is a heredoc marker. This is captured in :marker
rule(:doc_start) { str('<') >> marker >> newline }
+ rule(:marker) { match['A-Z'].repeat(1).capture(:marker) }
+
+ # The content of a document can be either lines of text or another
+ # document, introduced by <HERE, where HERE is the doc marker.
rule(:text) { (document.as(:doc) | text_line.as(:line)).repeat(1) }
rule(:text_line) { captured_marker.absent? >> any >>
(newline.absent? >> any).repeat >> newline }
+
+ # The end of the document is marked by the marker that was at the beginning
+ # of the document, by itself on a line.
rule(:doc_end) { captured_marker }
- rule(:marker) { match['A-Z'].repeat(1).capture(:marker) }
rule(:newline) { match["\n"] }
rule(:captured_marker) {
dynamic { |source, context|

0 comments on commit fc66cfd

Please sign in to comment.