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

World: r3wp

[Core] Discuss core issues

Anton
24-Jan-2005
[311]
Can you give an example of your input and final desired output ?
Geomol
24-Jan-2005
[312x3]
Input:
Input: "^"^/{^""

That's a string with 4 bytes: A ", then a newline, a { and another 
".
Operation: to-block
Desired output: ["^/{"]

That's a block with a string, that consists of 2 bytes: a newline 
and a {.

If this can be solved, then I can solve my other problems, I think. 
(Same goes with } btw.)
Maybe it's easier to see, if I use binary:
>> b: #{220A7B22}	; That's my input string as binary.
Try these:
>> to-string b
>> to-block b
The last one will fail, but it should give me:
== ["^/{"]
sqlab
24-Jan-2005
[315x3]
>> s: input
^/{
== {"^^/^{"}
>> to-block s
== ["^/{"]
>> print s
^/{
>> print to-block s

{
>>
Is that what you want?
I guess as soon as you have a newline in your string you should start 
the string with { and not with "
Geomol
24-Jan-2005
[318x3]
Interesting!
I would just think, that I should write the string like this then:
>> s: {"^/^{"}
but that's not a string for some reason!?
Thanks sqlab. I'll see, if your way can solve my problem, when I 
get home later today.
sqlab
24-Jan-2005
[321]
Why do you think, that this is no string?
eFishAnt
24-Jan-2005
[322]
>> s: "^/{"
== "^/{"
>> type? s
== string!
sqlab
24-Jan-2005
[323]
It's the molded representation of an internal string
Geomol
24-Jan-2005
[324x2]
Try it!
You get a new line with { in the beginning. That's REBOL way to tell 
you, you haven't finished your string. Like this:
>> s: {"^/^{"}
{

if I put in a } to finish the string, I get:
{    }
** Syntax Error: Invalid string -- }
** Near: (line 2) }
sqlab
24-Jan-2005
[326]
I see, the problem arises when you type it.
Geomol
24-Jan-2005
[327]
I would initially think, that {"^/^{"} would be a valid string with 
4 chars inside. 2 ", 1 newline and one {, but it isn't.
sqlab
24-Jan-2005
[328]
Did you check if it is the same behaviour when loading from a file?
Geomol
24-Jan-2005
[329]
yes
sqlab
24-Jan-2005
[330]
I have seen different behaviour from typing in the console and loading 
from a file

for example
>> func: [;  []]
** Syntax Error: Missing ] at end-of-script
** Near: (line 1) func: [;  []]
>>
Geomol
24-Jan-2005
[331]
eFishAnt, yes that's a string. I'm trying to build REBOL content 
within a string, so I have to figure out how to type strings within 
strings.
sqlab
24-Jan-2005
[332x2]
maybe you can use 
inp:  ["^/{"]
append your-string   inp
Seems not to work
Geomol
24-Jan-2005
[334x2]
Not really. I start with a string:
output: make string! 10000

then I go into a parse, where I build REBOL content within my output 
string. Sometimes I have to append a string (as a string) to output, 
and it fails, when I have newlines and { like characters.
I can't start with a block, because I can't append the start of a 
block to a block. I have to append a whole block to a block, and 
I don't know the full content of the block. So I start with a string.
sqlab
24-Jan-2005
[336]
What do you mean with appending the start of a block to a block?
Geomol
24-Jan-2005
[337]
appending a [
sqlab
24-Jan-2005
[338]
You can with '[
Geomol
24-Jan-2005
[339]
like:
>> blk: []
== []
>> append blk '[

No, I can't. Try it!
sqlab
24-Jan-2005
[340x4]
I have to check in a script I did short before and I thought I did 
something similar
>> append [] to-lit-word "["
== ['[]
append [] to-word "["
== [[]
t: append [] to-lit-word "["
== ['[]
>> reduce t
== [[]
>> or compose etc
Geomol
24-Jan-2005
[344]
Oki doki! :-)

I then just save my result to disk (because I can't use it directly, 
as those [ and ] are words (and not a real block). After reload of 
the result from disk, it should be real REBOL, right? Would be great, 
if I could build in a block and not a string.
sqlab
24-Jan-2005
[345x2]
Yes, that's how I try to compose some rules.
load mold works too
>> t: rejoin [ [] to-word "[" to-word "]"]
== [[ ]]
>>  load mold  t
== [[]]
>> type? first load mold t
== block!
>>
Geomol
24-Jan-2005
[347x2]
Ok, got it. Thanks!
@sqlab I've now implemented the block method instead of the string 
method, when I'm building REBOL syntax, and it works very well. Now 
I should be able to finish the first pass of my document format very 
soon, so I'm happy! :-)
sqlab
25-Jan-2005
[349]
Just one comment

You say, you do not know the content of the block you want to insert.

If you keep the reference to the block, you can always insert later 
into the block.

>> outer: append/only  [] internal: []
== [[]]
>> insert internal "test"
== []
>> outer
== [["test"]]
Geomol
25-Jan-2005
[350]
Yes, I'm aware of that REBOL trick. :-) It's because, what I'm parsing 
might be blocks within blocks in a recursive way.

XML is an example of such a structure. If I see a start-tag, I insert 
the beginning of a block in my result, then parse further in the 
document finding content and other start-tags and so on. The best 
way is to produce the output in a seriel manner from beginning to 
end, like I parse the input.
DideC
25-Jan-2005
[351]
You can use another block as a stack of  blocks references :

- When you meet a new tag, push the current block reference on the 
stack (insert tail) and make current ref to a new block.

- When you meet a close tag, pop the last reference from the stack 
in the current ref  (pick last, and remove back tail)
Sunanda
25-Jan-2005
[352]
Just a word of warning -- use the latest betas for any significantly 
nested block structure.

As I found out the hard way recently, the production releases (at 
least under windows) behave erratically when stressed with a few 
hundred nested blocks. Some problem with garbage collection, apparently.
Geomol
25-Jan-2005
[353]
@DideC

Fine suggestion! I use a similar method to stack the names of the 
tags, so I can produce the correct end-tag (like </tag>), when I'm 
at that point in the parsing. But I've found that appending >>to-word 
"["<< and >>to-word "]"<< works very well, so I've solved my problem.
Terry
25-Jan-2005
[354x2]
What's up with this?.. 

>> read http://127.0.0.1:83
connecting to: 127.0.0.1
** Script Error: Invalid argument: /
** Where: to-integer
** Near: to integer! :value
(nevermind, it was the script)
Robert
26-Jan-2005
[356]
stack: If you are interested, I have implemented a stack! object.
JaimeVargas
26-Jan-2005
[357]
Robert what does the stack! object do?
Geomol
26-Jan-2005
[358]
For a stack, I just do:
stack: []
append stack <something>
remove back tail stack
Robert
26-Jan-2005
[359]
It provides functions for pop, top, push etc. It implements a stack 
datastructure.
Volker
26-Jan-2005
[360]
stack: []

push: func[ var-block ][ insert/only stack reduce['set var-block 
reduce var-block] ]
pop: func[][ do first stack remove stack ]
a: 1 b: 2 push[a b] a: 11 b: 22 ? a ? b ? stack pop ? a ? b
; cannot push functions :(