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

World: r3wp

[Parse] Discussion of PARSE dialect

[unknown: 5]
4-Mar-2008
[2422x4]
I'm gonna have to work on this a bit - still seems like a cumbersome 
way
But it helps to know that as it gives me more options
I think I have a way around that.
Thanks Henrik.
Henrik
4-Mar-2008
[2426]
no problem
BrianH
4-Mar-2008
[2427]
Keep in mind that you can parse for 'string! (the lit-word version 
of the word string!) and that will match without reducing.
[unknown: 5]
4-Mar-2008
[2428x3]
Yes, thanks Brian.
In my situation the lit-word and the mold/all methods are not ideal.
I can see where we need to add this capability to REBOL.
Henrik
5-Mar-2008
[2431x2]
Pail, what exactly does the user input in the field?
Paul :-)
[unknown: 5]
5-Mar-2008
[2433x2]
They input a string followed by a datatype.  For example:    ["age" 
integer! "location" string!]
The datatype entered will vary.
Henrik
5-Mar-2008
[2435]
one way to deal with that would be to make your form so that it would 
not be possible to enter the data in a different way than the intended 
format, but that requires more form design of course.
btiffin
5-Mar-2008
[2436]
Henrik's original hint and any-type! should catch them.


parse ["age" integer! "loc" string!] [string!  set the-type #[datatype! 
any-type!] (print ["got an " the-type]) string! #[datatype! any-type!]]
[unknown: 5]
5-Mar-2008
[2437x2]
Henrik that is ugly way to approach it.  I feel were lacking a "better 
means" to handle this problem.
Brian here is a problem with your stradegy:

>> parse [1][set n #[datatype! any-type!]]
== true


Notice it returns true for the actual values that meet the datatype. 
 Which is what I don't want.  I need to know SPECIFICALLY if what 
was passed was integer! or string! or whatever.
Henrik
5-Mar-2008
[2439]
well, how do you want it to automatically recognize the input as 
datatypes? the only other way around it is to keep them as words 
and make a datatype rule to detect words that look like a datatype.
[unknown: 5]
5-Mar-2008
[2440]
Henrik that was the problem I almost had to resort to.  But hopefully 
you see the problem.  We should come up with ANOTHER method for handling 
this problem that is more seemless.
Henrik
5-Mar-2008
[2441]
you can also simply parse each single word like that. you can be 
as exact as you want in the parser.
[unknown: 5]
5-Mar-2008
[2442]
Yes, I understand that but it defeats Carl's famous philosophy which 
is that simple things should be simple to do.
Henrik
5-Mar-2008
[2443]
parse data ['string! (do-this) | 'integer (do-that)]
[unknown: 5]
5-Mar-2008
[2444x4]
I currently have resorted to a different approach.
Currently, I do something similiar to this:

user passes dynamic data captures in a block:
data: ["fname" string! "age" integer!]

Then I do the following:
        data: next data

        forskip data 2 [poke data 1 load mold/all attempt [to-datatype data/1]]
then this:

        data: head data

        unless parse data [some [string! datatype!]][return "Syntax Error!"]
Uses the mold/all method you provided yesterday.
Henrik
5-Mar-2008
[2448]
well, if that's all you do, you don't even have to convert it to 
a datatype, IMHO. it's enough to collect a list of rebol's datatypes 
as words, so they be used to trigger on the input word which looks 
like a datatype. I can see you are going for correctness, i.e. wanting 
the input to be a real datatype, but if you only use that input as 
a trigger to do something, you don't need to convert it to a real 
datatype. just operate using words.
[unknown: 5]
5-Mar-2008
[2449]
Yeah I even looked into going thru system/words and collecting all 
the datatype but the method I deployed was smaller and more effective.
Henrik
5-Mar-2008
[2450x3]
put weight in that dialects are words and if you take advantage of 
that, your dialect will become simpler
all datatype words are stored in the 'datatypes word
woah, that was nonsense: "dialects are words" I meant "dialects consists 
of words and a few other things"
[unknown: 5]
5-Mar-2008
[2453]
lol
Henrik
5-Mar-2008
[2454]
that means: if your datatype requires some level of serialization 
syntax to work, just consider them words.
[unknown: 5]
5-Mar-2008
[2455x2]
Would be nice to have something that simply says lit-type?
except that this isn't exactly what I think of as being "lit" as 
we know it.
Henrik
5-Mar-2008
[2457x2]
dialects are separate language domains where the normal rules of 
REBOL syntax don't necessarily apply.... about lit-type, then you 
need lit-object, lit-none, lit-whatever :-)
the serialized syntax _is_ the solution to that problem. yes the 
syntax is a bit more cumbersome.
[unknown: 5]
5-Mar-2008
[2459x2]
Maybe something like this is best solution:
dlt-type?: func [w [word!]][

    foreach item datatypes [if equal? to-word item w [return true]]
    false
]
Henrik
5-Mar-2008
[2461]
or perhaps:

dlt-type?: func [w [word!]] [any [attempt [to-datatype w] false]]
[unknown: 5]
5-Mar-2008
[2462x6]
Yeah which is what I use now.
similiar anyway
More like this for my needs:
dlt-type?: func [w] [found? any [attempt [to-datatype w] false]]
drop the [word!] requirement from the argument and report true or 
false.
But that doesn't fill a rule block to be passed to parse which is 
my original intention but is still very useful.
Henrik
5-Mar-2008
[2468]
I'd still not bother with it :-) how many datatypes will you support?
btiffin
5-Mar-2008
[2469x3]
Sorry, try  [#[datatype! datatype!]  that should restrict the match 
to only datatype values.
Or not.  :)
Or yes, if the source is reduced.

parse reduce ["age" integer!] [string!  set type #[datatype! datatype!] 
(print ['got type 'type? type? type])]