World: r3wp
[Parse] Discussion of PARSE dialect
older newer | first last |
Steeve 30-Jun-2007 [2151] | REBOL is cool, and free |
Tomc 1-Jul-2007 [2152x2] | Steeve do you have any case where the self modifying recursive parse rule above fails? |
>> ; insufficent pattern >> data: "first rule second rule third " == "first rule second rule third " >> erode: [a | b | c] == [a | b | c] >> parse/all data rule == false >> >> ;;; unused rule before pattern is consumed >> data: "first rule second rule third rule " == "first rule second rule third rule " >> erode: [a | b | c | a] == [a | b | c | a] >> parse/all data rule == false >> >> ;;; patterns and rules allowed in any order >> data: "first rule second rule third rule " == "first rule second rule third rule " >> erode: [c | b | a] == [c | b | a] >> parse/all data rule == true >> >> ;;; multiple (duplicate) rules allowed >> data: "first rule first rule second rule third rule " == "first rule first rule second rule third rule " >> erode: [a | b | c | a] == [a | b | c | a] >> parse/all data rule == true >> | |
Steeve 1-Jul-2007 [2154x2] | no i don't, it's an intersting alternative to my own code |
a crazy simple alternative from DocKimbel (non repetitive pattern and limited to rules with single char) >> rule: [(c: charset "ABC") 3 [copy v c (remove/part c v)]] >> parse "ABC" rule == true >> parse "BAC" rule == true >> parse "CBA" rule == true >> parse "ABA" rule == false >> parse "ABCA" rule == false | |
btiffin 6-Jul-2007 [2156] | How do you build parse rules? rule: copy [] word: "abc" ;; Want to compose the block to look like this ["abc" (print ["found word len" 3])] insert tail rule compose [(word) (print "found word len (length? word))] no go - obvious I've tried compose/deep [(word) (to paren! [print ["found word len" (length? word)])] but length? word doesn't get composed, it gets included in the to paren! expression compose/only/deep same thing I guess the question is what is the technique to compose something that is to include paren! expressions with data inside that needs to be composed? **With binding of course** :) |
Chris 7-Jul-2007 [2157x2] | reduce [word to paren! compose/deep [print ["Found Word of Length" (length? word)]]] |
compose [(word) (to paren! compose/deep [print ["found word len" (length? word)]])] | |
Tomc 7-Jul-2007 [2159] | was almost there |
btiffin 7-Jul-2007 [2160x2] | Thanks gentlemen. I've struggled with that one on and off for...well forever. |
compose/deep before the to paren! block, makes so much sense...ummm, now. Now that I've seen it. :) | |
Tomc 7-Jul-2007 [2162] | after |
btiffin 7-Jul-2007 [2163] | well after to paren! before the block ...in between. :) This trick is one of those V8 'tok' to the head moments. |
Geomol 17-Jul-2007 [2164] | How should string parsing work? I see something peculiar: >> parse "1 2" [copy a1 integer! copy a2 integer!] == true >> a1 == "1" >> a2 == " 2" >> to integer! a2 ** Script Error: Invalid argument: 2 Doing it this way make a2 different: >> parse "1 2" [copy a1 integer! opt [copy a2 integer!]] == true >> a1 == "1" >> a2 == "2" >> to integer! a2 == 2 |
Gabriele 17-Jul-2007 [2165x3] | when using parse (not parse/all) you can skip spaces putting something like "" in the rule |
eg [copy a1 integer! "" copy a2 integer!] | |
i always use /all though. | |
Geomol 17-Jul-2007 [2168x2] | I ended up doing something similar. I just speculate, if parse can be made easier to use, when doing string parsing without /all. Also string parsing can deal with the integer! datatype. Why not other datatypes? Are there deeper reasons for this? |
Gabriele, do you know, if there are changes to parse in R3? Maybe string parsing without /all should be changed. | |
BrianH 17-Jul-2007 [2170] | No changes yet. I agree that parse needs some improvements - we've written whole papers on that. |
Dockimbel 17-Jul-2007 [2171] | AFAIK, datatype matching is reserved for block! parsing. IMHO it should report a dialect error! in your example. Is string! matching supposed to support datatypes in latest core releases, maybe I missed an evolution ? |
BrianH 17-Jul-2007 [2172] | One of those proposals, brought up in the comments of one of Carl's blogs, is to unify the block and string parsing dialects. |
Gabriele 17-Jul-2007 [2173] | doc, integer! works for strings for some reason. it's the only one that works, and seems to match a sequence of digits (not sure if it does anything more than that). it's been there since a long time (probably from the beginning ;) but not documented. |
Dockimbel 17-Jul-2007 [2174] | It looks to me more as a bug than as an intended feature (maybe a side effect of the block! parsing addition?). Anyway matching REBOL datatypes in string! parsing mode could be a useful feature and would make a lot of parsing rules shorter. |
Tomc 19-Jul-2007 [2175] | I asked Carl at the first devcon for the rest of the datatypes when string parsing and he agreed at the time that if integer! was in there others should be as well. |
Rebolek 26-Jul-2007 [2176] | Is it possible to parse exact integer! value? something like parse [1.0][1.0] but with integer!. And something more "elegant" than parse [1][set val integer! (equal? val 1)] :) |
btiffin 26-Jul-2007 [2177] | parse [1 2 3] [1 1 1 1 1 2 1 1 3] You have to trick the repeat ranges |
Geomol 26-Jul-2007 [2178x2] | Alternative: parse form [1 2 3] ["1" "2" "3"] |
Yet another alternative: >> parse [1 2 3] [1.0 2.0 3.0] == true | |
Rebolek 26-Jul-2007 [2180] | thanks for suggestions |
Geomol 26-Jul-2007 [2181] | Think, the last alternative only works, because both integer and decimal are categories of number. It doesn't seem to work with other 'number'-like datatypes, like money or tuple. |
Rebolek 26-Jul-2007 [2182] | I was just curious how to do pattern-matching in REBOL with parse |
[unknown: 5] 5-Aug-2007 [2183x2] | Anyone know what "^-" is called? |
is thta a tab? | |
Henrik 5-Aug-2007 [2185] | >> tab == #"^-" yes :-) |
[unknown: 5] 5-Aug-2007 [2186x2] | ok |
I'm trying to parse data using that but seem to get it parsing some spaces also | |
Henrik 5-Aug-2007 [2188] | parse/all perhaps |
[unknown: 5] 5-Aug-2007 [2189x5] | I'll try it |
no that parses every letter | |
I'm beginning to wonder if it is a bug in parse | |
Anyone know how to parse a string such that the newlines and tabs are parsed? I'm not getting the results I'm expecting. | |
Hoping to see someone elses command in case I'm just brain farting something. | |
PeterWood 5-Aug-2007 [2194] | >> a: join "line1" [newline "line2"] == "line1^/line2" >> parse a [any [newline (print "newline found") | skip]] newline found == true |
Steeve 5-Aug-2007 [2195] | yeah, never had problems when parsing tabs and newlines |
Geomol 6-Aug-2007 [2196] | When parsing strings without the /all refinement, words are separated by space. Example that work: >> parse "word1 word2^-word3^/word4" ["word1" "word2" "word3" newline "word4"] == true You can also explicit specify the tab: >> parse "word1 word2^-word3^/word4" ["word1" "word2" #(tab) "word3" newline "word4"] == true Actually the #(tab) seems to be ignored, because you can specify it anywhere: >> parse "word1 word2^-word3^/word4" ["word1" "wo" #(tab) "rd2" "word3" newline "word4"] == true But you get false, if specifying the space (which may be a strange thing): >> parse "word1 word2^-word3^/word4" ["word1" #" " "word2" "word3" newline "word4"] == false Also you need to specify newlines, they are not seen as space: >> parse "word1 word2^-word3^/word4" ["word1" "word2" "word3" "word4"] == false If you need to parse for tabs at certain places, use: parse/all I hope, it helps! |
Gabriele 6-Aug-2007 [2197x2] | >> b: [#(tab)] == [# (tab)] >> length? b == 2 >> first b == # >> type? first b == issue! >> second b == (tab) >> type? second b == paren! |
so your "tab" is ignored because it's not a tab at all. it's the same as doing "" (tab) in the rule, ie empty string (always matches) followed by code that basically does nothing. | |
Geomol 6-Aug-2007 [2199x2] | Aha! :-) |
How did I get #(tab) in my head!? So to explicit specitying the tab, it must be: >> parse "word1 word2^-word3^/word4" ["word1" "word2" "^(tab)" "word3" newline "word4"] == false So that doesn't parse, meaning tabs are seen as spaces. | |
older newer | first last |