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

World: r3wp

[Parse] Discussion of PARSE dialect

Ladislav
18-Sep-2010
[5211x2]
http://en.wikibooks.org/wiki/REBOL_Programming/Language_Features/Parse#Local_variables_in_parse_rules
, a new subsection in the REBOL Programming wikibook.
http://en.wikibooks.org/wiki/REBOL_Programming/Language_Features/Parse#Local_variables_in_parse_rules

a new section in the REBOL Programming wikibook
Janko
18-Sep-2010
[5213]
wowo, most awesome Ladislav!
Ladislav
18-Sep-2010
[5214x2]
Pekr, knowing that you were interested in the subject as well, I 
sincerely hope, that you read the wikibook subsection, run the examples, 
and let me know what you are missing in the doc and in the functionality.
thanks, Janko
Claude
20-Sep-2010
[5216x4]
hi, i would you to parse a string like this {number: 123456 name: 
abcd message: i like rebol}
i would like to use parse to do it
something like this way parse mystring ["number:" number: "name:" 
name: "message:" msg:]]
a get number, name and msg . is that possible ? and how ?
Maxim
20-Sep-2010
[5220]
is the data guaranteed to be rebol compatible?  (you can use 'LOAD 
on it and it will never return an erro)
Claude
20-Sep-2010
[5221x3]
but mystring is like this {Number : 10017
Name       : Disable Message Partner
Application: MXS
Severity   : Info
Type       : Alarm Event
Date-Time  : 20/09/10 12:39:43
GMT        : Mon Sep 20 10:39:43 2010}
i start to use => parse mystring [thru "Number  :"  number: to end]
i would like to do => parse mystring [thru "Number :" number: to 
"Name    :" ] but this don't work !!!!]
Maxim
20-Sep-2010
[5224x4]
using 'TO or THRU rules is garanteed to make you life a nightmare 
for arbitrary string parsing, unless its very linear in content.
isn't there a rebol string parser on rebol.org which has the basic 
parsing rules to filter out rebol data?
with a few search I didn't zero-in on it, but I seem to recall seeing 
that before.
otherwise, you can just do a line by line parsing, filtering out 
"keywords" at the start and then loading the data after the ":"
Claude
20-Sep-2010
[5228x2]
if i can't use parse i will do this
but it is very strange. i heard that parse is very powerfull. and 
now i can't use it because if to difficult !!!!
Maxim
20-Sep-2010
[5230]
parse is very powerfull and easy to *use*  but *mastering* it isn't.
Janko
20-Sep-2010
[5231]
[thru "Number  :"  copy number [ to newline | to end ] ] maybe?
Maxim
20-Sep-2010
[5232x2]
although it is much more approachable than regexp in certain ways.
here is an overblown example, with lots of extra prints, so you can 
see it running... it parses your above example text.
--------------------------------
rebol []

data: {Number : 10017
Name       : Disable Message Partner
Application: MXS
Severity   : Info
Type       : Alarm Event
Date-Time  : 20/09/10 12:39:43
GMT        : Mon Sep 20 10:39:43 2010}


; rules

token: ["Number" | "Name" | "Severity" | "Type" | "Date-Time" | "GMT" 
| "Application"]
space: charset " ^-"
spaces: [any space]

content: complement charset "^/" ; all but newlines.
contents: [some content]


; used as variables in the rules
v-token: none
v-content: none
parse/all data [
	some [
		(
			print "^/0>"
		)
		copy v-token token 
		(
			print "1>"
			probe v-token
		)
		spaces ; ignore
		(
			print "2>"
		)
		":" ; end of token
		(
			print "3>"
		)
		spaces ; ignore
		(
			print "4>"
		)
		copy v-content contents ;grab all but newline
		(
			print "5>"
			probe v-content
		)
		[
			; this is important, it ensures that you've got a whole line.
			[some "^/"] | end
		]
		(
			print "6>"
		)
		(
			; do something with your data here
			?? v-token
			?? v-content
		)
	]

]


probe "."

ask "done"
Claude
20-Sep-2010
[5234]
thank you i will test it and try to understand too ;-)
Maxim
20-Sep-2010
[5235]
I made it easy for you to figure out (at least as easy as can be 
 ;-)
Claude
20-Sep-2010
[5236x2]
Maxim here the result
0>
1>
Number
2>
.
done
Maxim
20-Sep-2010
[5238]
using which rebol?  I did run it through and it worked ok on 2.7.7
Claude
20-Sep-2010
[5239x2]
it stop at ":" !!!
2.7.7.4.2
Steeve
20-Sep-2010
[5241]
Much easier with R3.
assuming, 
>> src: {Number : 10017
Name       : Disable Message Partner
Application: MXS
Severity   : Info
Type       : Alarm Event
Date-Time  : 20/09/10 12:39:43
GMT        : Mon Sep 20 10:39:43 2010}

then, 


>> construct parse copy src [return while[and[some #" " #":"] remove[some 
#" "]| skip]]
== make object! [
    Number: "10017"
    Name: "Disable Message Partner"
    Application: "MXS"
    Severity: "Info"
    Type: "Alarm Event"
    Date-Time: "20/09/10 12:39:43"
    GMT: "Mon Sep 20 10:39:43 2010"
]
Claude
20-Sep-2010
[5242x2]
thank you steeve but i need it for a production server  to parse 
log file for nagios
i would prefer to use rebol 2
Maxim
20-Sep-2010
[5244x4]
claude it might be because of copy/pasting... you are on linux.. 
did you just copy the code and paste it into the console?
the best is to copy it to a file and run it.
I just (re)tried it and it does work on my system...
(windows xp box)
Claude
20-Sep-2010
[5248]
well i on linux and on linux altme do work well with ctrl-c !!!!!
Maxim
20-Sep-2010
[5249x2]
but here we are parsing and any character code has to be loaded properly.. 
in theory it should work.. but it seems not...

add this just before the ":"

		here:
		(probe copy/part here 20)


it will print what is exactly at that point ... may provide clues 
as to why the ":" isn't matching...
note: I just tried to copy/paste the above in my R2 console .. it 
worked ok..
Claude
20-Sep-2010
[5251]
0>
1>
Number
2>
 : 10017^/Name       
.
done
Maxim
20-Sep-2010
[5252]
strange... it didn't match the spaces!
Steeve
20-Sep-2010
[5253]
parse/all ?
Maxim
20-Sep-2010
[5254x2]
already is using parse/all
claude... so, did you try to run it as a script?


one thing I would do... since this is a strange error is to retype 
this:

 " ^-"


in your editor... to make sure its not using the wrong ascii character 
on your os... it should not be a problem... but there is something 
weird going on here.
Claude
20-Sep-2010
[5256]
i do a copy/paste http://host4.altme.com/altweb/rebol3/chat210.html
into a file.r  and i execut it . i have got the same error
Maxim
20-Sep-2010
[5257x3]
steeve does it work for you?
oh.. he's gone... 

anyone else care to test it?  this is a strange bug...
this is what I get in my console....

0>
1>
Number
2>
3>
4>
5>
10017
6>
v-token: "Number"
v-content: "10017"

0>
1>
Name
2>
3>
4>
5>
Disable Message Partner
6>
v-token: "Name"
v-content: "Disable Message Partner"

0>
1>
Application
2>
3>
4>
5>
MXS
6>
v-token: "Application"
v-content: "MXS"

0>
1>
Severity
2>
3>
4>
5>
Info
6>
v-token: "Severity"
v-content: "Info"

0>
1>
Type
2>
3>
4>
5>
Alarm Event
6>
v-token: "Type"
v-content: "Alarm Event"

0>
1>
Date-Time
2>
3>
4>
5>
20/09/10 12:39:43
6>
v-token: "Date-Time"
v-content: "20/09/10 12:39:43"

0>
1>
GMT
2>
3>
4>
5>
Mon Sep 20 10:39:43 2010
6>
v-token: "GMT"
v-content: "Mon Sep 20 10:39:43 2010"
Claude
20-Sep-2010
[5260]
i will try it on the server tomorrow morning (it is a windows one 
;-) ) i will keep you informed