Changeset 30703 in project


Ignore:
Timestamp:
04/14/14 12:07:54 (7 years ago)
Author:
Moritz Heidkamp
Message:

comparse: Remove mentions of lazy-seq from introduction, use parser-input type instead

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/comparse

    r30702 r30703  
    2525Comparse provides a library of ''parsers'' which can be applied to
    2626''inputs'' in order to produce ''parse results''. It also provides
    27 ''parser combinators'' to create new parsers. In Comparse the term
    28 ''input'' refers to a [[/egg/lazy-seq|lazy sequence]] of ''items'' of
    29 any kind (often characters). For example:
    30 
    31 <enscript language="scheme">
    32 (use lazy-seq)
     27''parser combinators'' to create new parsers. In Comparse inputs are
     28represented by a {{parser-input}} type. It can hold a sequence of any
     29kinds of ''items'' (often characters). For example:
     30
     31<enscript language="scheme">
     32(use comparse)
    3333
    3434(define some-input
    35   (list->lazy-seq (string->list "hello world")))
     35  (->parser-input "hello world"))
    3636</enscript>
    3737
    3838A ''parser'' then is a function which takes an ''input'' as its
    39 argument and, in case the parse succeeds, returns a pair of the
    40 ''parse result'' (which can be a value of any kind) and the
    41 ''remainder'' of the input (which is a lazy sequence again). In case
    42 the parse fails it returns {{#f}}.
     39argument and, in case the parse succeeds, returns a ''parse result''
     40(which can be a value of any kind) and the ''remainder'' of the input
     41(which is a {{parser-input}} again). In case the parse fails it
     42returns {{#f}}.
    4343
    4444A very basic parser provided by Comparse is the {{item}} parser which
    4545consumes one item from the given input and returns it as its parse
    46 result. The remainder is the input's tail. If the input is empty,
    47 {{item}} fails. Applying it to the input we defined above:
    48 
    49 <enscript language="scheme">
    50 (item some-input)
    51 </enscript>
    52 
    53 Gives us this result:
    54 
    55 <enscript language="scheme">
    56 (#\h . #<lazy-seq #\e #\l #\l #\o #\space #\w #\o #\r #\l #\d>)
     46result. If the input is empty, {{item}} fails. We can apply it to the
     47input we defined above using the {{parse}} procedure
     48
     49<enscript language="scheme">
     50(parse item some-input)
     51</enscript>
     52
     53Which gives us these results:
     54
     55<enscript language="scheme">
     56#\h
     57#<parser-input #\e #\l #\l #\o #\space #\w #\o #\r #\l #\d ...>
    5758</enscript>
    5859
     
    7374
    7475<enscript language="scheme">
    75 (use comparse)
    76 
    7776(define double-item
    7877  (bind item (lambda (x)
     
    8079                 (cons (list x x) remainder)))))
    8180
    82 (double-item some-input)
     81(parse double-item some-input)
    8382</enscript>
    8483
     
    8685
    8786<enscript language="scheme">
    88 ((#\h #\h) . #<lazy-seq #\e #\l #\l #\o #\space #\w #\o #\r #\l #\d>)
     87(#\h #\h)
     88#<parser-input #\e #\l #\l #\o #\space #\w #\o #\r #\l #\d ...>
    8989</enscript>
    9090
     
    107107[[#examples|Examples section]] on how to use them to create parsers
    108108for practical tasks. Also note that you would usually not manually
    109 convert inputs (like strings or input ports) into a lazy sequence like
    110 we did above. The main interface to applying a parser to an input is
    111 the {{parse}} procedure which turns its input argument into a lazy
    112 sequence for you.
     109convert raw inputs (like strings or input ports) into a
     110{{parser-input}} object like we did above. Instead you can pass the
     111raw input object directly to the {{parse}} procedure which will
     112automatically turn it into a {{parser-input}} for you.
    113113
    114114
Note: See TracChangeset for help on using the changeset viewer.