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

World: r3wp

[Parse] Discussion of PARSE dialect

DideC
30-Jun-2006
[1142]
;***** 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.
Tomc
1-Jul-2006
[1153x2]
in that case sorting them from long to short to begin with will foil 
recursive macro rxpansion
at  the macros and expansons single tokens
BrianH
1-Jul-2006
[1155]
HTML/XML entities begin with & and end with ; for just this reason. 
What kind of text? can you give us an example?
Graham
1-Jul-2006
[1156x2]
well, the macros wiill end up being delimited by whitespace
Heart: Heart regular rate and rhythm, no rubs, murmurs, or gallops 
noted. 

A: Abdomen:  soft, nontender, no mass, no hernia, no guarding, no 
rebound tenderness, no ascites, non obese 
Hbp Hypertension (high blood pressure) #401.9.  
Ii Type II Diabetes #250.00
BrianH
1-Jul-2006
[1158]
Are macros the only thing delimited by whitespace?
Graham
1-Jul-2006
[1159x2]
So, someone might type

heart:
A: with striae
macros are typed inside normal text.
BrianH
1-Jul-2006
[1161]
That A: isn't delimited by whitespace.
Graham
1-Jul-2006
[1162x2]
it is .. it's preceded by a newline character
from the line above ...
BrianH
1-Jul-2006
[1164]
Not it it were on the first line. Plus, is the : part of the macro?
Graham
1-Jul-2006
[1165x4]
True .. not if it were on the first line.  Some macros contain punctuation.
so, a: is a macro, whereas "a" is not.
the macros are user defined.
so, in the examples given above, the first word in each line is the 
macro.
BrianH
1-Jul-2006
[1169]
Do they always contain the same punctuation, is the punctuation per-macro, 
of does it need to be factored out?
Tomc
1-Jul-2006
[1170]
isn't there a controled vocabulary for this sort of thing
Graham
1-Jul-2006
[1171]
per macro.
BrianH
1-Jul-2006
[1172]
Is a macro always the first word in a line?
Graham
1-Jul-2006
[1173x2]
what do you mean controlled vocabulary?
no, it can be anywhere in a line.
Tomc
1-Jul-2006
[1175]
an ontology
BrianH
1-Jul-2006
[1176]
Is there a seperate syntax for defining macros?
Graham
1-Jul-2006
[1177x2]
no, it's just a text file which is read in at start up.
the scenario is this guy uses these macros as he types up his notes. 
 his software currently expands them inline as he types.
BrianH
1-Jul-2006
[1179]
So in use, a macro a: will always be a: in the text. Will it be A: 
sometimes, or "a:"?
Graham
1-Jul-2006
[1180x2]
I would have to intercept the keyboard handler to do this .. so I 
want to try and just do the replacement after he's finished typing.
macros won't be case sensitive.
Tomc
1-Jul-2006
[1182]
but groups of people doing the sane thing dont use the same nacros? 
  they should
Graham
1-Jul-2006
[1183x2]
no, they don't use the same macros.
so, it's user specific.
Tomc
1-Jul-2006
[1185]
personal shorthand
Graham
1-Jul-2006
[1186]
yes
Tomc
1-Jul-2006
[1187]
hmm I am in the bussiness of sharing biological information and I 
got to say  please strongly consider  creating  an ontology if one 
does not exist already
Graham
1-Jul-2006
[1188x4]
Oh .. sure there's ontologies ....
but people don't use them when typing.
SNOMED CT is the one being currently promulgated
and there's the proprietary MEDCIN