r3wp [groups: 83 posts: 189283]
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

World: r3wp

[Parse] Discussion of PARSE dialect

Gordon
29-Jun-2006
[1103]
I'm pretty sure that you are right in that I have to loop throught 
the "Data".  That was my big stumbling block and the rest is just 
logic to figure out.  Thanks a bunch.
Izkata
29-Jun-2006
[1104]
No problem  (I'm glad I could actually help  '^^ )
Gordon
29-Jun-2006
[1105x2]
In the phrase.  "Print index :x", what does putting a colon before 
a variable do again?
Oops I meant "Print index? :x"
Izkata
29-Jun-2006
[1107]
Not sure - I remember seeing it in others' parse rules, so I just 
put it there and it worked  '^^
Take it out and see what happens lol
Gordon
29-Jun-2006
[1108]
:)
Izkata
29-Jun-2006
[1109]
I think it was like get-word or something
BrianH
29-Jun-2006
[1110x3]
; Did you try this?
data: read/lines to-file Readfile
fields: [note category flag]
foreach x data [
    set fields parse x ","
    ; do something
]
In particular, remember not to use parse/all
>> parse {"Hello, World", "Blah"} ","
== ["Hello, World" "Blah"]
Gordon
29-Jun-2006
[1113]
Hi BrianH;

  Yes I did try that and the problem was that even though I specified 
  the "," as the delimiter, it came across an embedded quote #"^"" 
  and split the input at the quote.  Rebol Shouldn't have split it 
  up that way, to my understanding.  I will post some simple data to 
  test.
BrianH
29-Jun-2006
[1114x2]
Embedded quotes should be escaped somehow.
Please include some troublesome data if you could.
Gordon
29-Jun-2006
[1116]
This data was exported by PalmOS.  I like the Palm desktop for keeping 
track on notes/,memos addresses but the  search engine sucks badly. 
 Therefore I wanted to export the data to allow a nice Rebol search 
on it..  Therefore, the PalmOS export function does "escape" an embedded 
quote by quoting it again.  Ex:
Press the "Home" button
becomes
Press the 
Home
 button.
Tomc
29-Jun-2006
[1117]
truth (as far as i know) is:   word  is is a shortcut for :word  
but there are a few places such as inside parse where the shortcut 
does not work so you need to make it explicit
Gordon
29-Jun-2006
[1118x4]
I will get some troubleshooting data posted in a minute.
Tomc:  Do I understand that :word would be like "get word" except 
in a parse sentence?
Wait I said that wrong
Tomc:  Do I understand that :word would be like "get word" and needed 
in a parse sentence but you can just use the shortcut 'word' most 
everywhere else?
BrianH
29-Jun-2006
[1122]
The colon before the word prevents the interpreter from evaluating 
active values like functions and parens. It's a safety thing.
Tomc
29-Jun-2006
[1123x3]
works for me
you can use :word everywhere you would use word
as far as i know
BrianH
29-Jun-2006
[1126]
Except when you want an active value assigned to the word to be evaluated, 
like when you are calling a function.
Tomc
29-Jun-2006
[1127]
and that would be get 'word  not get word
BrianH
29-Jun-2006
[1128]
In parse rules the :word means something different (not in the code 
blocks in parens).
Gordon
29-Jun-2006
[1129]
Thanks Tomc and BrianH.  I'll chew on it for a while.  Meanwhile 
I'm working on building some test data for the first problem.
Tomc
29-Jun-2006
[1130]
in the pars rume (not the paren) it means  "be here now"
Gordon
29-Jun-2006
[1131x2]
okay so in the parse rules (except in a parenthesized code block) 
it means "be here now"?
but what does 'be here now' mean?
Tomc
29-Jun-2006
[1133]
to change to point you are in what you are parsing   backtracking 
or jumping ahead
Gordon
29-Jun-2006
[1134]
Thanks
BrianH
29-Jun-2006
[1135]
Setting the parse position.
Tomc
29-Jun-2006
[1136]
inside the parens it means what is in what I am parsing at the point 
pointed to by :word
Anton
30-Jun-2006
[1137]
BrianH, correct except not, I think, parens:
>> foreach word [(1 + 2)][print mold word]
(1 + 2)
BrianH
30-Jun-2006
[1138]
That's interesting. Parens and paths used to be active - oh yeah, 
that was changeda while ago. Still, there are some value types that 
are active (function types, lit-path, lit-word) and if you think 
you will get one of these you should disable their evaluation by 
referencing them with a get-word, unless you intend them to be evaluated.
Anton
30-Jun-2006
[1139x2]
parens changed at around View 1.2.10
both parens and paths changed between  View 1.2.1 and 1.2.5, actually.
DideC
30-Jun-2006
[1141x2]
Gordon: I did not read this thread in a whole but as for converting 
CSV string to/from Rebol blocks, here is some fully functionnal functions 
:
;***** Conversion function from/to CSV format
csv-to-block: func [

 "Convert a string of CSV formated data to a Rebol block. First line 
 is header."
	csv-data [string!] "CSV data."

 /separator separ [char!] "Separator to use if different of comma 
 (,)."
	/without-header "Do not include header in the result."

 /local out line start end this-string header record value data chars 
 spaces chars-but-space

 ; CSV format information http://www.creativyst.com/Doc/Articles/CSV/CSV01.htm
] [
	out: copy []
	separ: any [separ #","]
	

 ; This function handle replacement of dual double-quote by quote 
 while copying substring
	this-string: func [s e] [replace/all copy/part s e {""} {"}]
	; CSV parsing rules

 header: [(line: copy []) value any [separ value] (if not without-header 
 [append/only out line])]

 record: [(line: copy []) value any [separ value] (append/only out 
 line)]

 value: [any spaces data any spaces (append line this-string start 
 end)]

 data: [start: some chars-but-space end: | #"^"" start: any [some 
 chars | {""} | #"," | newline] end: #"^""]
	chars: complement charset rejoin [ {"} separ newline]
	spaces: charset exclude { ^-} form separ
	chars-but-space: exclude chars spaces
	
	parse/all csv-data [header any [newline record] any newline end]
	out
]

block-to-csv: func [
	"Convert a block of blocks to a CSV formated string." 
	blk-data [block!] "block of data to convert"
	/separator separ "Separator to use if different of comma (,)."
	/local out csv-string record value v
] [
	out: copy ""
	separ: any [separ #","]
	; This function convert a string to a CSV formated one

 csv-string: func [val] [head insert next copy {""} replace/all copy 
 val {"} {""} ]
	record: [into [some [value (append out #",")]]]

 value: [set v string! (append out csv-string v) | set v any-type! 
 (append out form v)]
	

 parse/all blk-data [any [record (remove back tail out append out 
 newline)]]
	out
]
Graham
30-Jun-2006
[1143]
there's also the sql csv thingy in the library
Gordon
30-Jun-2006
[1144]
DideC: Thanks.  I've copied and pasted it for review and added it 
to my local public library.  This script should be useful especially 
with the html help page.   Documentation on a script is very rare 
and much appreciated.


Graham: Did a search using  "librarian" and search term of  "sql 
cvs" and didn't come up with anything.  Although, I think we've got 
it covered now anyhow.
Graham
1-Jul-2006
[1145]
Trying to do some macro expansion in text ...

This is not working :(

expand-macros: func [tmp [string!] macros [block!]
	/local white-rule rule len lexp
] [
	white-rule: charset [#" " #"^/"]
	foreach [macro expansion] macros [
        len: length? macro
		lexp: length? expansion
		rule:  compose/deep copy [

            [ to here: white-rule (macro) white-rule ( change/part here expansion 
            len  ?? macro) lexp skip ]
            to end
        ]
		parse/all tmp [some [rule]]
	]
	tmp
]
BrianH
1-Jul-2006
[1146x2]
Are macros marked by some starting character? That would make this 
easier to implement.
Also, it would help to know whether macro expansions can contain 
further macros.
Graham
1-Jul-2006
[1148]
no, they can be any characters (alphanumeric), but should not contain 
further macros.
Tomc
1-Jul-2006
[1149]
ise if the macros are delimited you should be able to do it in one 
pass
Graham
1-Jul-2006
[1150x3]
What I was trying to do above is to look for the macro text preceded 
by a space or newline, and ending in a space or newline.
and then replace in situ.
leaving the whitespace unaffected.