Changeset 15036 in project


Ignore:
Timestamp:
06/19/09 04:16:55 (10 years ago)
Author:
Ivan Raikov
Message:

added abnf example

File:
1 edited

Legend:

Unmodified
Added
Removed
  • wiki/eggref/4/abnf

    r14971 r15036  
    159159Given a rule {{P}}, returns a matcher that always returns an empty
    160160list of consumed tokens when {{P}} succeeds.
     161
     162=== Examples
     163
     164<enscript highlight="scheme">
     165
     166(use abnf)
     167
     168(define (between-fws p)
     169  (abnf:concatenation
     170   (abnf:drop-consumed (abnf:optional-sequence fws)) p
     171   (abnf:drop-consumed (abnf:optional-sequence fws))))
     172
     173;; Date and Time Specification from RFC 5322 (Internet Message Format)
     174
     175;; The following abnf parser combinators parse a date and time
     176;; specification of the form
     177;;
     178;;   Thu, 19 Dec 2002 20:35:46 +0200
     179;;
     180; where the weekday specification is optional.
     181                             
     182;; Match the abbreviated weekday names
     183
     184(define day-name
     185  (abnf:alternatives
     186   (abnf:lit "Mon")
     187   (abnf:lit "Tue")
     188   (abnf:lit "Wed")
     189   (abnf:lit "Thu")
     190   (abnf:lit "Fri")
     191   (abnf:lit "Sat")
     192   (abnf:lit "Sun")))
     193
     194;; Match a day-name, optionally wrapped in folding whitespace
     195
     196(define day-of-week (between-fws day-name))
     197
     198
     199;; Match a four digit decimal number
     200
     201(define year (between-fws (abnf:repetition-n 4 abnf:decimal)))
     202
     203;; Match the abbreviated month names
     204
     205(define month-name (abnf:alternatives
     206                    (abnf:lit "Jan")
     207                    (abnf:lit "Feb")
     208                    (abnf:lit "Mar")
     209                    (abnf:lit "Apr")
     210                    (abnf:lit "May")
     211                    (abnf:lit "Jun")
     212                    (abnf:lit "Jul")
     213                    (abnf:lit "Aug")
     214                    (abnf:lit "Sep")
     215                    (abnf:lit "Oct")
     216                    (abnf:lit "Nov")
     217                    (abnf:lit "Dec")))
     218
     219;; Match a month-name, optionally wrapped in folding whitespace
     220
     221(define month (between-fws month-name))
     222
     223
     224;; Match a one or two digit number
     225
     226(define day (abnf:concatenation
     227             (abnf:drop-consumed (abnf:optional-sequence fws))
     228             (abnf:alternatives
     229              (abnf:repetition-n 2 abnf:decimal)
     230              (abnf:drop-consumed fws))))
     231
     232;; Match a date of the form dd:mm:yyyy
     233(define date (abnf:concatenation day month year))
     234
     235;; Match a two-digit number
     236
     237(define hour      (abnf:repetition-n 2 abnf:decimal))
     238(define minute    (abnf:repetition-n 2 abnf:decimal))
     239(define isecond   (abnf:repetition-n 2 abnf:decimal))
     240
     241;; Match a time-of-day specification of hh:mm or hh:mm:ss.
     242
     243(define time-of-day (abnf:concatenation
     244                     hour (abnf:drop-consumed (abnf:char #\:))
     245                     minute (abnf:optional-sequence
     246                             (abnf:concatenation (abnf:drop-consumed (abnf:char #\:))
     247                                                 isecond))))
     248
     249;; Match a timezone specification of the form
     250;; +hhmm or -hhmm
     251
     252(define zone (abnf:concatenation
     253              (abnf:drop-consumed fws)
     254              (abnf:alternatives (abnf:char #\-) (abnf:char #\+))
     255              hour minute))
     256
     257;; Match a time-of-day specification followed by a zone.
     258
     259(define itime (abnf:concatenation time-of-day zone))
     260
     261(define date-time (abnf:concatenation
     262                   (abnf:optional-sequence
     263                    (abnf:concatenation
     264                     day-of-week
     265                     (abnf:drop-consumed (abnf:char #\,))))
     266                   date
     267                   itime
     268                   (abnf:drop-consumed (abnf:optional-sequence cfws))))
     269
     270</enscript>
    161271
    162272
Note: See TracChangeset for help on using the changeset viewer.