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

World: r3wp

[Parse] Discussion of PARSE dialect

Chris
15-Nov-2008
[3202]
The point is though that I'd want it to fail.  The set-word! could 
be used as a delimiter:

	[link-one: %file-one "File One" link-two: %file-two "File Two"]

Would be matched by:

	some [set-word! of link-spec]	

Or in VID:

	some [opt set-word! word! face-spec]
Steeve
15-Nov-2008
[3203x2]
Hmm Chris, what is your request actually ?
i wonder if delect is not more usefull in your case
Chris
15-Nov-2008
[3205x3]
Perhaps, but I thought incorporating 'delect was part of the point 
of 'of
Steeve, two requests -- matching from a block! and a slightly more 
nuanced 'of
Both based on situations I've come upon.
Steeve
15-Nov-2008
[3208x2]
matching from a block! .... isn't it already the case ?
i mean in the wiki definition
Chris
15-Nov-2008
[3210]
No, as above (you asked me to summarize).
BrianH
17-Nov-2008
[3211x7]
Chris, re: your more nuanced OF, that is covered in the existing 
proposal (including Steeve's alternate and Carl's possible future 
extensions). Carl will have to determine how flexible OF can be implemented, 
without having diminishing returns on increased complexity.
About your matching from a block proposal, if the CHECK proposal 
gets accepted then I doubt this will - the usage scenarios where 
you can't just use alternates would be too rare, especially given 
how easy CHECK (FIND ...) could do the job in those cases.
Your example with alternates (and bug fixes, still ignoring leap 
years):


 m31: ["Jan" | "Mar" | "May" | "Jul" | "Aug" | "Oct" | "Dec"]  ; joins 
 were in wrong direction
	m30: join m31 [| "Apr" | "Jun" | "Sep" | "Nov"]
	m28: join m30 [| "Feb"]

 b28: next repeat x 28 [repend [] ['| form x]]  ; next to skip leading 
 |, numbers don't work in string parsing
	b30: ["29" | "30"]  ; optimization based on above reversed joins
	b31: ["31"]
	parse date-str [
		b28 "-" m28
		| b30 "-" m30
		| b31 "-" m31
	]

The above with CHECK instead:

	m31: ["Jan" "Mar" "May" "Jul" "Aug" "Oct" "Dec"]
	m30: join m31 ["Apr" "Jun" "Sep" "Nov"]
	m28: join m30 ["Feb"]
	b28: repeat x 28 [append [] form x]  ; not assuming 
	b30: ["29" "30"]  ; optimization based on above reversed joins
	b31: ["31"]
	parse date-str [
		copy d some digit "-" copy m some alpha
		check (	any [
			all [find b31 d  find m31 m]
			all [find b30 d  find m30 m]
			all [find b28 d  find m28 m]
		])
	]

Which would be faster would depend on the data and scenario.
(the comments on the second example can be ignored)
Your proposal seems like a slightly faster but more limited version 
of alternates, and not as flexible or optimizable as check. Does 
this situation come up so often that you need direct support for 
it?
Here's a simpler date checker with CHECK:


parse date-str [copy d [1 2 digit "-" 3 alpha "-" 4 digit] check 
(attempt [to-date d])]
That requires years too, but at least it gets leap year 29-Feb.
Gabriele
17-Nov-2008
[3218]
Brian, JOIN does a REDUCE on the second block.
BrianH
17-Nov-2008
[3219]
Right you are, whoops. It's been a while since I used it with blocks.
Chris
18-Nov-2008
[3220x3]
'append would do it...

numbers don't work in string parsing

 - I thought about this when I developed the example, thought it might 
 be possible as the numbers appear outside the dialect.  But 'check 
 seems like the better option.  

joins were in the wrong direction
 - d'oh!

simpler date checker

 - that's only useful if to-date recognizes the date format : )  (and 
 using dates was illustrative - there are other situations with similar 
 needs).  Though on dates, what would be the most succinct way with 
 the proposals on the table to do the following?

	ameridate: "2/15/2008"
	parse ameridate ...rule...
	newdate = 15-Feb-2008

One attempt:

	parse ameridate [
		use [d m][
			change [copy m 1 2 digit "/" copy d 1 2 digit]
			(rejoin [d "/" m])
		]
		"/" 4 digit end check (newdate: to-date ameridate)
	]
(making the assumption it is a valid date)
(and that it's ok tomodify the original string)
eFishAnt
22-Nov-2008
[3223x4]
If I am parsing something like javascript that has { and } in it 
like C, how can I put that into a string to parse without using {}
my test cases for testing a dialect, I usually use this form:
print parse/all {test case that 
can't have {} inside } parse-rule
There must be some dead-simple guru trick for this...
Sunanda
22-Nov-2008
[3227]
I think I raised the same question on the ML years ago, and got  
a disappointing answer. Maybe things have changed since. Or, if not, 
it may not be too late to add to the R3 parse wishlist:
    http://www.rebol.org/ml-display-thread.r?m=rmlSQHQ
eFishAnt
22-Nov-2008
[3228x3]
My first inclination is to "go binary" on it...;-) but that is inelegant, 
and the MSB of binary gets wonky sometimes.
(MSB bit is a sign bit oftentimes)
I was thinking of taking away the special meaning of { but not yet 
sure how to unset it...it initially seems hardcoded in there....not 
like I would expect.
Oldes
22-Nov-2008
[3231]
I don't understand what do you mean.
eFishAnt
22-Nov-2008
[3232x2]
In the console if you type a {  and then hit Enter, it continues 
on the next line.

:{   and }:   don't seem to work, either.
The problem is if you have a string of Javascript which uses {} inside, 
then it is hard to get REBOL to make a string of Javascript that 
has {} inside.
Sunanda
22-Nov-2008
[3234]
Replace "{" with to-char 0 then put it back afterwards? (Assuming 
to-char 0 does not occur in your string)?

I've done that sort of thing before to get around parse limitations 
(whether the limitation is in 'parse or my understanding of it)
Oldes
22-Nov-2008
[3235]
what about escaping?
eFishAnt
22-Nov-2008
[3236]
so ^{ is what you mean by escaping?
Oldes
22-Nov-2008
[3237]
yes
eFishAnt
22-Nov-2008
[3238]
that's the old XON/XOFF sw handshaking trick.


So you mean to add ^ in the Javascript, and then wrapping it with 
{} won't cause a script error?
Oldes
22-Nov-2008
[3239x2]
You want to write JS in Rebol console?
>> s: {a^/{b}c^{d^}"e"}
== {a
{b}c{d}"e"}
eFishAnt
22-Nov-2008
[3241x4]
no, I am parsing it automatically...the console I was just testing, 
but that makes the problem worse, methinks
I think you meant this (or at least I would have wanted you to mean 
this):
>> s: {a^{b^}c^{d^}"e"}
== {a{b}c{d}"e"}
that still doesn't cut the mustard for me...but can be useful for 
part of it.


to-string 4838f{gmgmg{    ;this doesn't work because REBOL doesn't 
know what datatype 4838f{gmgmg{ is, but I don't want REBOL to know 
that.  I want REBOL to make it into a string.
to-string/raw or to-string/force could be a useful refinement so 
that I could write parse like this:


print parse/all to-string/force ;alksjdf;alsjdflk;{""""}}}}}    parse-rules 
  ;I think this would be a VERY useful thing.
Oldes
22-Nov-2008
[3245]
I don't understand how it could work like
eFishAnt
22-Nov-2008
[3246]
it would need a terminator pattern to know when to stop it


print parse/all to-string/force ;alksjdf;alsjdflk;{""""}}}}}terminate 
parse-rules   ;I think this would be a VERY useful thing.
Oldes
22-Nov-2008
[3247x3]
you mean something like that http://en.wikipedia.org/wiki/HEREDOC
? That could be useful.
but that's not parse related, the Rebol lexer should be improved 
to do that.
we can try to ask Carl
eFishAnt
22-Nov-2008
[3250x2]
It is strongly related...for sure.  One of my requirements in a good 
OS which is not done yet is to NEVER lose anything I type, EVER.
That page of Wiki is very good to beg my case, thanks!