Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search

[REBOL] Re: Parse versus Regular Expressions

From: brett:codeconscious at: 5-Apr-2003 17:19

I noted in my last email that I didn't think I was getting much value add from PARSE in that solution so I thought I would accept that idea and see what a less PARSE reliant solution might look like. I know that this is tangential to the request but it could be useful to the discussion so here it is. I'm interested to see if there is an elegant PARSE solution for this problem but I'm not sure. In solving the problem, it might have been useful to invoke PARSE from inside a PARSE rule but I cannot remember if this is safe or not. REBOL [ Title: "Joel's PARSE problem." Author: "Brett Handley" Comments: {} ] data: decompress #{ 789CA5D25D0AC3200C00E0ABE46DD087E24FF7738FED02B6CD9820DA55A5D75F EA2C8C958DD9459F8C7C2644C628385C6EDA036D051E6D40DB610D45211893B4 F8CC31910FCFAE0C597B2279F2254145FEED356FC940DD6FEBF7E9EDAB44C0A4 8D815E7B350CA8C62D9E4CDEA18236866C3ABB0BD062A7A24770571AD03DCE03 0237F6F8F191E4F13C8F23CD17C1281FCA9A5CD7D724EFF4FDEE2F5F68F11EAA 35717B80020000 } emit: :print parse-file: function [data] [ digit out seq1 seq2 v_lineseq1 v_lineseq2 v_linebody ] [ digit: charset {0123456789} out: copy [] forskip data 80 [ if not parse/all data [ copy v_lineseq1 6 digit copy v_linebody 66 skip (trim v_linebody) copy v_lineseq2 8 digit to end ] [make error! "Bad file format - sequence numbers must be digits."] either all [ any [none? seq1 v_lineseq1 >= seq1] any [none? seq2 v_lineseq2 >= seq2] ] [ seq1: v_lineseq1 seq2: v_lineseq2 if data/7 <> #"*" [ insert tail out v_linebody if #"." = last v_linebody [ emit reform out clear out ] ] ] [emit join "ERROR: " copy/part data 80] ] if not empty? out [emit "ERROR: Last sentence is incomplete."] ] ; Wrap the invocation in some error handling. if error? set/any 'result try [parse-file data] [ error: disarm result if block? msg: get in get in system/error error/type error/id [ msg: bind/copy msg in error 'self ] reform msg emit join "ERROR: " reform msg ] HALT