Big parse rules
[1/7] from: AJMartin:orcon at: 19-Jul-2003 20:15
Anyone know how to turn a parse rule into a function?
Here's my situation. I've got a pile of 'parse rules in my eText function
(I'm rewriting it to make it smaller, faster, and more versatile), and I
find I need some sort of function-like ability, to keep the rules for a
section together, apart from the rest, and able to be reused or nested.
Table_Printable^: exclude Printable charset #"|"
Table_Cell^: [copy Text any Table_Printable^ (insert tail Table_Row compose
[td (Text)])]
Table_Row^: [
(Table_Row: copy [])
#"|" some [Table_Cell^ #"|"] opt #"|" LWS? Line_End (
insert tail Table compose [tr [(Table_Row)]]
Table_Row: none
)
]
Table^: [
(Table: copy [])
some Table_Row^ (
Accrete [table [tbody [(Table)]]]
)
]
As can be seen, I'd really like to make "Table_" into a sort of object or
function, rather than "fake" it with the arrangement of names.
Any ideas?
Andrew J Martin
ICQ: 26227169 http://www.rebol.it/Valley/ http://Valley.150m.com/
[2/7] from: nitsch-lists:netcologne at: 19-Jul-2003 10:39
Am Samstag, 19. Juli 2003 10:15 schrieb A J Martin:
> Anyone know how to turn a parse rule into a function?
> Here's my situation. I've got a pile of 'parse rules in my eText function
<<quoted lines omitted: 20>>
> function, rather than "fake" it with the arrangement of names.
> Any ideas?
would like pathes in parserules.
without it, you could put everything in a context and export only 'Table^ with
'set.
or do
table^: ctx-table/table^
parse string [..]
context[
Table_Printable^: exclude Printable charset #"|"
=2E.
set 'Table^[
..
]
]
[3/7] from: AJMartin:orcon at: 19-Jul-2003 22:02
Volker wrote:
> would like pathes in parserules.
So would I! :)
> without it, you could put everything in a context and export only 'Table^
with 'set. or do
> table^: ctx-table/table^
> parse string [..]
<<quoted lines omitted: 5>>
> ]
> ]
Thanks, Volker! That would work, except that I wanted it to eventually be
recursive. With an object, I can't yet see how this could be done (perhaps
copying to a stack/block?).
Here's what I eventually ended up with:
set 'Table^ function [
Markup [block!] eText [string!]
] [Printable^ Cell! Cell^ Cell Row^ Row Header Rows Index] [
Printable^: exclude Printable charset #"|"
Cell!: 'th
Cell^: [
#"|" copy Cell any Printable^
(Cell: reduce [Cell! any [Cell #" "]])
]
Row^: [
(Row: copy [])
some [Cell^ (insert tail Row Cell)] opt #"|" LWS? Line_End
(Row: reduce ['tr Row])
]
all [
parse/all eText [
Row^ (
Header: Row
Cell!: 'td
Rows: copy []
)
some [Row^ (insert tail Rows Row)] (
insert tail Markup compose/deep [
table [
thead [(Header)]
tbody [(Rows)]
]
]
) Index: to end
end
]
Index
]
]
And in a separate function:
Fail^: [to end skip] ; Always fails rule.
Succeed^: [] ; Always succeeds rule.
Switch^: none
parse/all Dialect [
some [
Blank_Lines [
List
| Block_Quote
| [
Mark: (
Switch^: Fail^
if Result: Table^ Markup Mark [
Mark: Result
Switch^: Succeed^
]
) Switch^ :Mark
]
| Divider
| Bullet_Divider
| Centre
| Heading
]
| Paragraph
| Initial_Paragraph
| skip
]
end
]
Though it's still only about 90% I feel.
Andrew J Martin
ICQ: 26227169 http://www.rebol.it/Valley/ http://Valley.150m.com/
[4/7] from: lmecir::mbox::vol::cz at: 19-Jul-2003 17:50
Hi Andrew,
> Volker wrote:
> > would like pathes in parserules.
>
> So would I! :)
...
> I wanted it to eventually be
> recursive.
it is possible to make a recursive parse rule (see
http://www.fm.vslib.cz/~ladislav/rebol/parseen.r, where the rules are
recursive). Nevertheless, it is difficult in general, because there is a GC
bug "messing" things. (see
http://www.compkarori.com/vanilla/display/Garbage+Collection+Crash+and+the+P
ARSE+function )
-L
[5/7] from: brett:codeconscious at: 20-Jul-2003 18:40
Hi Martin,
I'm not entirely sure how you want to partition your function's logic,
however I do have a couple of suggestions but no idea
if they will be useful for you.
It does seem though that you need a way to "contextualise" your sectional
rules.
1) Volker mentioned objects. One program I made recently had all the rules
together in a file call it eg. %lexical.r
Some of these rules had variable names to set and names of functions to call
from inside parens. Ie the variables
are state variables and the functions are equivalent to scanner events.
I then "late bound" the lexcial rules to the scanner like this...
scanner: copy [
tkType: tkStart: tkEnd: tkBeginLine: tkEndLine: tkEmit: none
tkResult: copy []
tkEmit: does [do-body tkType tkStart subtract index? tkEnd index?
tkStart]
]
scanner: make context scanner load %lexical.r
...where tkType, tkStart etc are words embedded in the rules stored in
%lexical. The scanner was was created inside a function to keep things tidy.
In this way I could possibly use different generated scanners to achieve
different purposes while reusing the same lexical rules.
2) functions
I'm interested in the way code/data can be exploited by converting it into a
function
eg this block stores a tree
sample-tree: [node 1 [node 2 [] node 3 []] node 4 []]
it can be transformed into a function body and each tree node evaluated by
doing something like
eval-tree: func [node] sample-tree
eval-tree func [label subtrees] [probe label reduce subtrees]
Hope one of these ideas helps!
Regards,
Brett.
[6/7] from: brett:codeconscious at: 20-Jul-2003 20:26
> Hi Martin,
Oops sorry - Hi Andrew! :^)
Brett.
[7/7] from: nitsch-lists:netcologne at: 23-Jul-2003 21:13
Hi Andrew
Am Samstag, 19. Juli 2003 12:02 schrieb A J Martin:
> Volker wrote:
[snip]
> > without it, you could put everything in a context and export only 'Table^
> with 'set. or do
<<quoted lines omitted: 11>>
> recursive. With an object, I can't yet see how this could be done (perhaps
> copying to a stack/block?).
Played a bit with the stack-idea. Actually i played with it a while before,
coming up with a stack which took a block of words and saved the words and the
contents. so one could do
rule: [
(push [a b c])
rules rules rules
(pop)
]
lost it somewhere.
Started rewriting it, and wrote a bit more.
Here is the story:
First, a stack like described above.
stack: copy []
push: func [word-block] [
insert stack reduce ['set word-block reduce word-block]
]
pop: func [] [do/next stack remove/part stack 3]
then i realized i don't like typing, so the push-pop should be automatic,
and creating real locals to.
stack: copy []
rule-with-locals: func [locals rule] [
insert/only rule to-paren compose/only [insert stack reduce (locals)]
append/only rule to-paren compose/only [
set (locals) first stack remove stack]
;cute hack to create new context and bind block to it:
use locals reduce [ 'set locals none rule]
]
;specially constructed test, see console-session:
rule: [
here: copy part any ["(" rule | ")" break | skip] (? here)
]
string: "very-left ( left ( middle ) right ) very-right )"
{console:
>> parse string rule ;subrule overwrites superrules 'here
HERE is a string of value: "middle ) right ) very-right )"
HERE is a string of value: "middle ) right ) very-right )"
HERE is a string of value: "middle ) right ) very-right )"
== true
>> parse string rule-with-locals [here] rule ; this works :) <<<<<<<<<
HERE is a string of value: "middle ) right ) very-right )"
HERE is a string of value: "left ( middle ) right ) very-right )"
HERE is a string of value: "very-left ( left ( middle ) right ) very-right )"
== true
}
Then i thought, eventually the locals should be in a context, instead of in a
hidden use, played with contexts and discovered:
>> ctx: context [val: #original]
>> old: third ctx
== [val: #original]
>> ctx/val: #modified
== #modified
>> ctx/val
== #modified
>> do old
== #original
>> ctx/val
== #original
cute :) but does not work with
>> ctx: context [val: 'original]
>> ctx/val
== original
because it dequotes 'original
>> third ctx
== [val: original]
>> do third ctx
** Script Error: original has no value
** Near: val: original
End of part 1 / parse-with-locals.
Will the great coders extend this further?
Login when the great coders say: "Errrmmmm..."
So, what exactly do you want? ;)
[snip]
> Andrew J Martin
> ICQ: 26227169 http://www.rebol.it/Valley/ http://Valley.150m.com/
> -><-
Cheers,
-Volker
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted