Changeset trunk,13 for trunk


Ignore:
Timestamp:
06/23/2007 04:30:57 AM (19 years ago)
Author:
dsowen
revision id:
svn-v3-trunk0:2948df59-2b31-0410-8e06-c40c0b09d5b6:trunk:13
Message:

Added more documentation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/parse3.lisp

    r12 r13  
    1 (in-package #:dso-parse)
     1;(in-package #:dso-parse)
    22
    33
     
    2929  `(if (and (string/= "" ,input) (char= ,char (char ,input 0)))
    3030    (let ((,next (substring ,input :start 1))
    31           (,match '(,char)))
     31          (,match ,char))
    3232      ,then)
    3333    ,else))
     
    4242          (string= ,string (substring ,input :length ,len)))
    4343      (let ((,next (substring ,input :start ,len))
    44             (,match '(,string)))
     44            (,match ,string))
    4545        ,then)
    4646      ,else)))
     
    5858        (if ,start
    5959            (let ((,next (substring ,input :start ,end))
    60                   (,match (list (substring ,input :end ,end))))
     60                  (,match (substring ,input :end ,end)))
    6161              ,then)
    6262            ,else)))))
     
    8686          `(if-matches ,input ,head (,head-next ,head-match)
    8787            (if-matches-sequence ,head-next ,tail (,next ,tail-match)
    88               (let ((,match (cons ,head-match ,tail-match)))
     88              (let ((,match (if ,head-match (cons ,head-match ,tail-match) ,tail-match)))
    8989                ,then)
    9090              ,else)
     
    9292        (with-gensyms (tail-match)
    9393          `(if-matches ,input ,head (,next ,tail-match)
    94             (let ((,match (list ,tail-match)))
     94            (let ((,match (if ,tail-match (list ,tail-match) nil)))
    9595              ,then)
    9696            ,else)))))
     
    110110          (if-matches ,next ,rule (,next2 ,match2)
    111111            (setf ,next ,next2
    112                   ,match (cons ,match2 ,match))
     112                  ,match (if ,match2 (cons ,match2 ,match) ,match))
    113113            (return-from ,r ,else)))
    114114        (setf ,match (nreverse ,match))
     
    131131                      (progn
    132132                        (psetf ,next ,next2
    133                                ,match (cons ,match2 ,match)
     133                               ,match (if ,match2 (cons ,match2 ,match) ,match)
    134134                               ,i (1+ ,i))
    135135                        t)))))
     
    152152      ,else)))
    153153
    154 ;;; The required and forbidden rules don't quite work right, as using
    155 ;;; a rule like (#\a (& #\b)) will put an extaneous NIL on the end of
    156 ;;; the match tree.
    157 
    158154(defmacro if-matches-required (input rule (next match)
    159155                               &body (&optional (then t) else))
     156  "Matches if RULE matches against INPUT, but neither consumes input
     157nor contributes to the parse tree."
    160158  `(if-matches ,input ,rule (,(gensym) ,(gensym))
    161159    (let ((,next ,input)
     
    166164(defmacro if-matches-forbidden (input rule (next match)
    167165                                &body (&optional (then t) else))
     166  "Matches if RULE doesn't match against INPUT, but neither consumes
     167input nor contributes to the parse tree."
    168168  `(if-matches ,input ,rule (,(gensym) ,(gensym))
    169169    ,else
     
    174174(defmacro if-matches-only (input rule (next match)
    175175                           &body (&optional (then t) else))
    176   (with-gensyms (match2)
    177     `(if-matches ,input ,rule (,next ,match2)
    178       (let ((,match nil))
    179         ,then)
    180       ,else)))
     176  "Matches RULE against INPUT and consumes input, but doesn't
     177contribute to the parse tree."
     178  `(if-matches ,input ,rule (,next ,match)
     179    (let ((,match nil))
     180      ,then)
     181    ,else))
    181182
    182183(defmacro if-matches (input rule (next match) &body (&optional (then t) else))
     184  "Matches RULE against INPUT.  If the rule is matched, the THEN form
     185is executed with NEXT bound to the remainder of the input after
     186consumption (if any), and MATCH to the parse tree.
     187
     188Rules may be defined in a short-hand using this macro, and nested:
     189
     190* Characters and strings match themselves.
     191
     192* A regular expression is denoted as (^ regex).
     193
     194* A sequence is a list of rules (rule1 rule2) that doesn't begin with
     195  a reserved mark.
     196
     197* An ordered choice is (/ rule1 rule2).
     198
     199* A Kleene-closure is (* rule).  + and ? may be used as well, and all
     200  act as in regular expressions.
     201
     202* Range matching is ({} min max rule).  If MIN is NIL, it is treated
     203  as 0.  If MAX is NIL, no limit is placed on matches.
     204
     205* Requiring a match without consuming or tree-building is done as (&
     206  rule).  Forbidding a match likewise as (! rule).
     207
     208* Matching and consuming without tree-building is (= rule).
     209
     210If RULE is a symbol, that symbol will be used as the name of a parsing
     211function.  The function will be given the input as its only argument,
     212and is expected to return (VALUES T NEXT MATCH) (where NEXT refers to
     213the remaining, unsonsumed input, and MATCH is the generated tree), or
     214NIL otherwise."
    183215  (etypecase rule
    184216    (symbol `(if-matches-parser ,input ,rule (,next ,match) ,then ,else))
Note: See TracChangeset for help on using the changeset viewer.