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

World: r3wp

[Parse] Discussion of PARSE dialect

BrianH
1-Jul-2006
[1223x3]
expand-macros: func [data [string!] macros [block!]
	/local whitespace macro-rule macro here there
] compose [
	whitespace: (charset " ^/")
    macro-rule: make block! length? macros
	foreach [macro expansion] macros [
        macro-rule: insert insert macro-rule macro '|
    ]
    macro-rule: head remove back macro-rule
    parse/all data [some [
        here: copy macro macro-rule there: [whitespace | end] (

            there: change/part here select/skip macros macro 2 there
        ) :there |
        skip
    ]]
    macro-rule: none
    data
]
Sorry, need to change the settings in my editor.
expand-macros: func [data [string!] macros [block!]
    /local whitespace macro-rule macro here there
] compose [
    whitespace: (charset " ^/")
    macro-rule: make block! length? macros
    foreach [macro expansion] macros [
        macro-rule: insert insert macro-rule macro '|
    ]
    macro-rule: head remove back macro-rule
    parse/all data [some [
        here: copy macro macro-rule there: [whitespace | end] (

            there: change/part here select/skip macros macro 2 there
        ) :there |
        skip
    ]]
    macro-rule: none
    data
]
Graham
1-Jul-2006
[1226]
my first effort makes n passes ...
Tomc
1-Jul-2006
[1227]
that the macro-expansoion fioe needs to self check for incidental 
occurances of  a "macro" in an "expansion" and protect against
BrianH
1-Jul-2006
[1228x2]
This will make one pass.
And it won't have the problem you mention Tomc.
Tomc
1-Jul-2006
[1230]
in one pass you can jump over the expansion
BrianH
1-Jul-2006
[1231]
My code does that.
Graham
1-Jul-2006
[1232x2]
Pretty neat ...
Parse is somewhat beyond my skills!
Tomc
1-Jul-2006
[1234]
I wouls still sort the macros by longest to shortest so cant glob 
on to part of  a macro  ..
Graham
1-Jul-2006
[1235]
so, basically you created a single parse rule from the macro list 
and then parsed the text in one go.
Tomc
1-Jul-2006
[1236]
but if you include the trailing white space that should be avoided
BrianH
1-Jul-2006
[1237]
Tomc, that is a good point - I'll fix it. Graham, that's right.
Graham
1-Jul-2006
[1238x2]
Did you compose that without any testing? :)
We need a masterclass in parse ....
BrianH
1-Jul-2006
[1240x5]
Graham, yes I did. I know parse well enough to run it in my head.
expand-macros: func [data [string!] macros [block!]
    /local whitespace macro-rule macro expansion here there
] compose [
    whitespace: (charset " ^/")
    macro-rule: make block! 2.5 * length? macros
    foreach [macro expansion] macros [
        macro-rule: insert macro-rule
            compose [here: (macro) there: [whitespace | end] '|]
    ]
    macro-rule: head remove back macro-rule
    parse/all data [some [
        macro-rule (
            macro: copy/part here there

            there: change/part here select/skip macros macro 2 there
        ) :there |
        skip
    ]]
    macro-rule: none
    data
]
Whoops, small fix...
expand-macros: func [data [string!] macros [block!]
    /local whitespace macro-rule macro expansion here there
] compose [
    whitespace: (charset " ^/")
    macro-rule: make block! 2.5 * length? macros
    foreach [macro expansion] macros [
        macro-rule: insert macro-rule
            compose [here: (macro) there: [whitespace | end] |]
    ]
    macro-rule: head remove back macro-rule
    parse/all data [some [
        macro-rule (
            macro: copy/part here there

            there: change/part here select/skip macros macro 2 there
        ) :there |
        skip
    ]]
    macro-rule: none
    data
]
Wait, there is another error.
Tomc
1-Jul-2006
[1245]
I am glad to see someone else using here and there ;)
BrianH
1-Jul-2006
[1246x2]
expand-macros: func [data [string!] macros [block!]
    /local whitespace macro-rule macro expansion here there
] compose [
    whitespace: (charset " ^/")
    macro-rule: make block! 2.5 * length? macros
    foreach [macro expansion] macros [
        macro-rule: insert macro-rule
            compose [here: (macro) there: [whitespace | end] |]
    ]
    macro-rule: head remove back macro-rule
    parse/all data [some [
        macro-rule (
            macro: copy/part here there

            there: change/part here select/skip macros macro 2 there
        ) :there |
        thru whitespace
    ] to end]
    macro-rule: none
    data
]
That should do it.
Graham
1-Jul-2006
[1248]
I don't feel so bad that my rule wasn't working !
BrianH
1-Jul-2006
[1249x2]
; Now whitespace is dealt with
expand-macros: func [data [string!] macros [block!]
    /local whitespace macro-rule macro expansion here there
] compose [
    whitespace: (charset " ^/")
    macro-rule: make block! 2.5 * length? macros
    foreach [macro expansion] macros [
        macro-rule: insert macro-rule
            compose [here: (macro) there: [whitespace | end] |]
    ]
    macro-rule: head remove back macro-rule
    parse/all data [some [any whitespace [
        macro-rule (
            macro: copy/part here there

            there: change/part here select/skip macros macro 2 there
        ) :there |
        to whitespace
    ]] to end]
    macro-rule: none
    data
]
You might want to add tabs to the whitespace charset.
Graham
1-Jul-2006
[1251x2]
why do you need to reset macro-rule to none?
it's a local so memory will be released anyway ..
BrianH
1-Jul-2006
[1253]
You don't need to, but if you don't it will sit in memory until the 
next time you run the function.
Graham
1-Jul-2006
[1254]
Oh ... ok.
BrianH
1-Jul-2006
[1255]
Memory for locals is not released.
Graham
1-Jul-2006
[1256]
when the function is exited ?
BrianH
1-Jul-2006
[1257]
Nope.
Graham
1-Jul-2006
[1258]
Hmm.
BrianH
1-Jul-2006
[1259]
Only on recursed functions.
Graham
1-Jul-2006
[1260]
I guess it can't be otherwise we wouldn't need to use 'copy on series
BrianH
1-Jul-2006
[1261]
It's a speed optimization. This might change with REBOL 3.
Graham
1-Jul-2006
[1262]
memory use is a large with Rebol.
BrianH
1-Jul-2006
[1263]
The series copy thing is something different.
Graham
1-Jul-2006
[1264]
speed or memory ..
BrianH
1-Jul-2006
[1265x2]
Most of the excessive memory overhead of REBOL is just sloppy (no 
offense Carl). It's not much of a problem for most, but I have run 
into memory limits when running on embedded or handheld platforms, 
or running hundreds of instances on servers.
You have to design your platform for memory efficiency or it won't 
be.
Graham
1-Jul-2006
[1267]
Little problem :(

** Script Error: Invalid argument: make bitset! #{
0004000001000000000000000000000000000000000000000000000000000000
}
** Where: expand-macros
** Near: parse/all data [some [any whitespace [
            macro-rule (
                macro: copy/part here there
           ...
Tomc
1-Jul-2006
[1268x3]
whi the parens on   whitespace: rule?
nevermind it is in compose
but that can just be a static rule outside of compose
BrianH
1-Jul-2006
[1271]
More efficiency. I build my charsets once, rather than on every call.
Graham
1-Jul-2006
[1272]
should probably build the rule outside the function as well since 
it only should be rebuilt when the macro list changes