Parse to Object Mapping question.
[1/2] from: steve:shireman:semaxwireless at: 2-Jul-2001 16:58
I expect there might be a simple document to explain this, so here is my
question:
I want to parse a message, and stuff it into an object if the parsing is
valid.
Ideally, the structure of the object could be used to create the parsing
rule.
Here is a simple example, soas not to give away my project:
Message-in-ASCII: "RECTANGLE 5.0 CM by 8.0 CM"
Geometric-shape: make object![
shape-type: " "
size-x: 1.0
size-y: 1.0
units: "inches"
]
a manually created parse rule might be--
shape-type: [some char]
size: [some digits "." some digits]
units ["cm" | "in"]
parse-shape-rule [ shape-type spacer size units "by" size units ]
so that:
parse Message-in-ASCII parse-shape-rule ;should give true if no mistakes
instead of the above, it would be nice to have something like:
parse message-in-ASCII [ fit Geometric-shape ] ;so object provides
parsing rules automatically
I hope this is clear enough.
Is there already a parse mechanism to fill in an object like this?
Thanks,
Steve Shireman
[2/2] from: ingo:2b1 at: 3-Jul-2001 12:31
Hi Steve,
this is a little hack that does a little of what you want ...
Once upon a time Steve Shireman spoketh thus:
<...>
> I want to parse a message, and stuff it into an object if the parsing is
> valid.
>
> Ideally, the structure of the object could be used to create the parsing
> rule.
<...>
-- Attached file included as plaintext by Listar --
REBOL [
Title: "Object parse-setter"
Author: "Ingo Hohmann"
Date: 2001-07-03
EMail: [ingo--2b1--de]
purpose: {
Demonstrate an awful hack, to create an object setting
parse rule ... just try to do it, and you'll see ...
}
]
template-object: make object! [
length: integer!
unit: word!
]
data-string: "1 cm"
parse-object-rule: func [
data [string!] template [object!]
/local rule object-info new-obj
][
rule: copy []
object-info: next head third template
forskip object-info 2 [
append rule compose [ set tmp (first object-info) ]
append/only rule to-paren compose [ change at object-info (index? object-info) tmp]
]
object-info: head object-info
parse load data rule
object-info: next head object-info
forskip object-info 2 [
if word? first object-info [
change object-info to-lit-word first object-info
]
]
make object! object-info
]
print "^/My string is:"
probe data-string
print "^/My template looks like this:"
probe template-object
print "^/ ... and I get"
probe parse-object-rule data-string template-object