AltME: Parse

Messages

Geomol
welcome
Endo
I'll go with regular expression, ^((?!four).)*$
gives the lines does not contain "four".

sqlab
not-four: [[(not-four/2: [])  "four" (not-four/2:  [thru end skip] ) | to " "]  []]
>> parse/all "one two three" ["one " not-four  " three"  ]
== true
>> parse/all "one four three" ["one " not-four  " three"  ]
== false
Gabriele
A variation of sqlab's approach:
>> space: [some #" "]
== [some #" "]
>> parse/all "one two three" [(fail?: none) "one" space ["four" space (fail?: [end skip]) | to #" " space] fail? "three"]                                                == true
>> parse/all "one four three" [(fail?: none) "one" space ["four" space (fail?: [end skip]) | to #" " space] fail? "three"]                                               == false
>> parse/all "one five three" [(fail?: none) "one" space ["four" space (fail?: [end skip]) | to #" " space] fail? "three"]                                               == true
Gabriele
If your case is more specific maybe it can be done in a different way, like checking for the condition after parsing, or filtering out the input you don't want in advance, and so on.
In Topaz you could do something like:
>> parse [one two three] ['one either 'four [(false)] [skip 'three (true)]]
== true
>> parse [one four three] ['one either 'four [(false)] [skip 'three (true)]]  
== false
(no string parsing yet so I used a block to illustrate)

Ladislav
Endo, you can try my parse enhancements at
http://www.rebol.org/view-script.r?script=parseen.r&sid=ypnz89xc

Arnold
I have an SQL text with arguments in the form ":argument_1" How do I get a list of the used arguments used in this SQL using parse?

Endo
Something like this?
>> digit: charset [#"0" - #"9"]
>> alpha: charset [#"a" - #"z" #"A" - #"Z"]
>> alphanum: union alpha digit
>> validchars: union alphanum charser [#"_"] ;put any other valid chars here
>> sql: {select * from table where a = :param1 and b=:param2     or x=3}
>> parse/all sql [some [thru ":" copy p some avlidchars (print p) | skip]]
param1
param2
You may need to cleanup comments, You can use something like:
remove-sql-comments: has [m n] [
    parse/all read clipboard:// [
        some [
            m: "--" to newline n: (remove/part m n) :m
        |
            m: "/*" thru "*/" n: (remove/part m n) :m
        |
            skip
        ]
    ]
]

Arnold
Thank you Endo. This is very useful.
Endo
It is not complete, it doesn't care about comments in strings, but you got the idea.

szeng
Can anybody help me to replace all of "on-init" in the parttern of "space on-init non-word" with "abcd" in a string?
I've tried
space: charset [#" " #"^-"]
word: charset [#"a" - #"z" #"A" - #"Z" #"-"]
non-word: complement word
on-init-rule: [
    space mark: "on-init" non-word (
            remove/part mark 7 ;remove on-init
            insert mark "abcd"
            )
]
parse/all inp: {abcasdfasdf on-init
a on-init
b
} [
    any [
        thru on-init-rule
    ]
]
it failed with:
** Script error: PARSE - invalid rule or usage of rule: make bitset! #{0040000080}
** Where: parse do either either either -apply-
** Near: parse/all inp: {abcasdfasdf on-init
a on-init
b
} [
    any ...
>> q
DocKimbel
Here is a working version:
space: charset [#" " #"^-"]
word: charset [#"a" - #"z" #"A" - #"Z" #"-"]
non-word: complement word
on-init-rule: [
    space mark: "on-init" non-word (
        remove/part mark 7 ;remove on-init
        mark: insert mark "abcd"
    ) :mark
]
parse/all inp: {abcasdfasdf on-init
a on-init
b
} [some [on-init-rule | skip]]
szeng
Thanks Doc, I'''' give it a try
Yes, it works. Thanks!
DocKimbel
You're welcome.

Last message posted 54 weeks ago.