Arc Forumnew | comments | leaders | submitlogin
2 points by almkglor 5974 days ago | link | parent

treeparse is very generalized, so expect to write a lot of code.

As for files: the lib/scanner.arc library allows you to treat input streams (e.g. input files) as if they were a list of characters, which is what treeparse accepts (treeparse can work with "real" lists and scanners).

Treeparse usually returns a list of characters, but this behavior can be modified by using a 'filt parser.

Just off the top of my head (untested!):

  (require "lib/scanner.arc")
  (require "lib/treeparse.arc") ; could use cps_treeparse.arc
  (let (list-str newline line oneline lastline allparse) nil
    ; converts a list of chars to a string
    (= list-str
       [string _])
    ; support \r\n, \n\r, \n, and \r
    (= newline
       ; alt & seq's semantics should be obvious
       (alt
         (seq #\return #\newline)
         (seq #\newline #\return)
         #\newline
         #\return))
    (= line
       ; lots of characters that could be anything...
       ; but *not* newlines!
       (many (anything-but newline)))
    (= oneline
       (seq line
            newline)))
    ; in case the last line doesn't end in newline
    (= lastline
       line)
    (= allparse
       (seq
         ; many is greedy, but will still pass
         ; if there are none
         (many
           ; the filter function has to return a list
           (filt [list (list-str _)]
             oneline))
         ; might not exist; will still pass whether
         ; it exists or not
         (maybe
           (filt [list (list-str _)]
             lastline))))
    (def lines-from-scanner (s)
      (parse allparse s)))
  (def lines-from-file (f)
    (w/infile s f
      (lines-from-scanner (scanner-input s))))
p.s. performance may be slow. cps_treeparse.arc is a more optimized version (maybe 25% faster on average, which most of the speedup in many and seq IIRC, which could be twice the speed), but does not support the "semantics" feature.


1 point by almkglor 5974 days ago | link

More on using scanners and treeparse:

http://arclanguage.com/item?id=5227

As an aside, Arkani (wiki-arc.arc, not to be confused with Anarki, arc-wiki.git) is now called Arki

-----

1 point by akkartik 5974 days ago | link

Thanks a bunch, Alan. I've been reading up. I'll be around :)

-----

3 points by almkglor 5974 days ago | link

If you're curious about the CPS variant of treeparse:

http://arclanguage.com/item?id=5321

Achtung! The CPS variant is written in, of all things, continuation passing style, which is hard on the eyes. It is thus expected to be much harder to read than the straightforward treeparse.

Achtung! In a single Arc session, it is safe to use only treeparse or cps_treeparse. Never load both in the same session.

Achtung! Some of treeparse's features are not supported by cps_treeparse.

-----