[parse] Special characters in block parsing
[1/8] from: apwing::zonnet::nl at: 3-Apr-2005 15:25
Hi all,
while experimenting with a small dialect for calculations, I stumbled
into a problem.
I've done simple things like:
>> parse [6 * 3] [set num1 number! '* set num2 number! (print num1 *
num2) ]
18
== true
However, when I try:
>> parse [6 / 3] [set num1 number! '/ set num2 number! (print num1 /
num2) ]
** Syntax Error: Invalid word-lit -- '
** Near: (line 1) parse [6 / 3] [set num1 number! '/ set num2 number!
(print num1 / num2
) ]
REBOL complains, because (of course) '/ is not a proper lit-word.
How can I still parse the / ? I already tried a charset, but I suppose
that's only possible with string-parsing, or not?
TIA
Arie
[2/8] from: antonr:lexicon at: 4-Apr-2005 0:39
Hi Arie,
>> parse [/] compose [set word (to-lit-word first [/])]
== true
>> word
== /
Anton.
[3/8] from: volker::nitsch::gmail::com at: 3-Apr-2005 16:48
Composing was my first thought too.
But this looks cleaner (with complex rules):
divide-word: to-lit-word "/"
probe parse [/][ set word divide-word ]
probe word
On Apr 3, 2005 4:39 PM, Anton Rolls <[antonr--lexicon--net]> wrote:
> Hi Arie,
> >> parse [/] compose [set word (to-lit-word first [/])]
<<quoted lines omitted: 33>>
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
-Volker
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem.
David
Wheeler
[4/8] from: apwing:zonnet:nl at: 3-Apr-2005 17:12
Volker Nitsch wrote:
>Composing was my first thought too.
>But this looks cleaner (with complex rules):
<<quoted lines omitted: 58>>
>>
>>
Hi Anton / Volker,
many thanks for these useful solutions. In my case Volker's solution is
somewhat simpler to use, but I can imagine situations where Anton's
solution might be more appropriate!
I tried both and indeed both do work fine!
What I find strange however is that specifying '/ is an invalid lit-word
but to-lit-word "/" is not.
This looks like a contradiction to me...
Any ideas why this works that way (and I am glad it does ;-) ?
Thanks again!
Arie
[5/8] from: volker:nitsch:g:mail at: 3-Apr-2005 19:43
Actually both methods do the same. Only Antons method inlines the "/"
in the main rule:
!>> compose [set word (to-lit-word first [/])]
== [set word '/]
while mine use a sub-rule and references it by name. so i have only to
create that sub-rule in a special way. you could write
divide-word: compose [(to-lit-word first [/])]
to make that rule, thats not the main point ;)
sub-rules are a good trick with parse and compose, because parens have
special meanings in both. in parse it means "process", in compose
expand
. and if you feed compose a parse-rule with parens, it gives
chaos.
;this works:
divide-word: to-lit-word "/"
parse[/] [set word divide-word (?? word)]
;this not
parse[/] compose[set word (to-lit-word "/") (?? word)]
;because compose replaces the (?? word) too.
about: strange '/ is an invalid lit-word, but to-lit-word "/" not:
the rebol parser has a complicated job. here it has to decide what "/"
shall be. it could be the start of /refinement too. so it prefers to
say "hey, invalid!". actually it is invalid by parsers rules, not the
implementation. the parser can not sort it out. but to-lit-word does
not go through the parser, it just takes the string and makes it a
word. and that string is clearly defined: the stuff between "". so
that works. only if you mold it, mold simply join ' and the word-text
and outputs "'/", which is not loadable again. so dont save "handmade"
words!
On Apr 3, 2005 5:12 PM, Arie van Wingerden <[apwing--zonnet--nl]> wrote:
> Volker Nitsch wrote:
> >Composing was my first thought too.
<<quoted lines omitted: 81>>
> To unsubscribe from the list, just send an email to
> lists at rebol.com with unsubscribe as the subject.
--
-Volker
Any problem in computer science can be solved with another layer of
indirection. But that usually will create another problem.
David
Wheeler
[6/8] from: rotenca:telvia:it at: 3-Apr-2005 19:44
Arie van Wingerden wrote:
>What I find strange however is that specifying '/ is an invalid lit-word
>but to-lit-word "/" is not.
>This looks like a contradiction to me...
>Any ideas why this works that way (and I am glad it does ;-) ?
>
The problem is that the loader (load) understands only a subset of Rebol
value. This is a need, because load must assign a different datatype to
the value when the string start with % or < or " or # or is in the form
2-2-2005 o [aa--sdf] and so on.
A word in rebol by itself can have ANY syntax, examples of valid rebol
words:
to-word "a%"
to-word "3^""
But load does not understand them. Other examples:
to-word "2-2-1004"
to-word "31"
to-word "3.5e4"
to-word "a/b/c"
and also:
to-word ":a"
to-word "a:"
to-word "'a"
to-word "/a"
Also a 0 char can be in a word:
mold to-word "^@b" ;== "^@b"
A solution to the problem could be the implementation of the mold construct:
#[lit-word! "/"]
--
Ciao
Romano Paolo Tenca
[7/8] from: apwing::zonnet::nl at: 3-Apr-2005 20:03
Hi Volker,
good explanation!
I discover more power under the REBOL hood every time I dig in ;-)
Many thanks,
Arie
Volker Nitsch wrote:
[8/8] from: apwing::zonnet::nl at: 3-Apr-2005 20:05
Hi Romano,
thanks for your help! With Volker's and your help I now understand why
these things work this way.
Kind regards,
Arie
Romano Paolo Tenca wrote:
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted