|
Revision 1, 4.2 kB
(checked in by krobillard, 3 years ago)
|
|
Import orca & thune.
|
| Line | |
|---|
| 1 | REBOL [ |
|---|
| 2 | Notes: { |
|---|
| 3 | * 'throw & 'break do not work inside 'parse parens. |
|---|
| 4 | } |
|---|
| 5 | ] |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | ;pmsg: func [msg] [print [" PF" msg]] |
|---|
| 9 | pmsg: :comment |
|---|
| 10 | |
|---|
| 11 | dpos: none |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | next-rule: func [it] [ |
|---|
| 15 | if it: find it '| [next it] |
|---|
| 16 | ] |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | eval-pstr: func [ |
|---|
| 20 | ; Returns true/false if matching rule found/not found. |
|---|
| 21 | it |
|---|
| 22 | /local fit res it2 |
|---|
| 23 | ][ |
|---|
| 24 | pmsg "eval" |
|---|
| 25 | while [not tail? it] |
|---|
| 26 | [ |
|---|
| 27 | fit: first it |
|---|
| 28 | switch/default fit [ |
|---|
| 29 | opt [ |
|---|
| 30 | eval-pstr second it |
|---|
| 31 | it: skip it 2 |
|---|
| 32 | ] |
|---|
| 33 | |
|---|
| 34 | any [ |
|---|
| 35 | until [not eval-pstr second it] |
|---|
| 36 | it: skip it 2 |
|---|
| 37 | ] |
|---|
| 38 | |
|---|
| 39 | some [ |
|---|
| 40 | either not eval-pstr second it [ |
|---|
| 41 | pmsg "some fail" |
|---|
| 42 | it: next-rule it |
|---|
| 43 | ][ |
|---|
| 44 | pmsg "some ok" |
|---|
| 45 | until [not eval-pstr second it] |
|---|
| 46 | it: skip it 2 |
|---|
| 47 | ] |
|---|
| 48 | ] |
|---|
| 49 | |
|---|
| 50 | break [ |
|---|
| 51 | return true |
|---|
| 52 | ] |
|---|
| 53 | |
|---|
| 54 | | [ |
|---|
| 55 | pmsg "|" |
|---|
| 56 | return true |
|---|
| 57 | ] |
|---|
| 58 | |
|---|
| 59 | to [ |
|---|
| 60 | pmsg it |
|---|
| 61 | res: find dpos second it |
|---|
| 62 | it: either res |
|---|
| 63 | [ |
|---|
| 64 | pmsg "to -> TRUE" |
|---|
| 65 | dpos: res |
|---|
| 66 | skip it 2 |
|---|
| 67 | ][ |
|---|
| 68 | pmsg "to -> false" |
|---|
| 69 | next-rule it |
|---|
| 70 | ] |
|---|
| 71 | ] |
|---|
| 72 | ][ |
|---|
| 73 | switch/default type?/word fit [ |
|---|
| 74 | paren! [ |
|---|
| 75 | pmsg "()" |
|---|
| 76 | do fit |
|---|
| 77 | it: next it |
|---|
| 78 | ] |
|---|
| 79 | block! [ |
|---|
| 80 | pmsg "[]" |
|---|
| 81 | res: do fit |
|---|
| 82 | it: next it |
|---|
| 83 | ] |
|---|
| 84 | char! [ |
|---|
| 85 | pmsg "char" |
|---|
| 86 | res: first dpos = fit |
|---|
| 87 | if not res [ |
|---|
| 88 | it: next-rule it |
|---|
| 89 | ] |
|---|
| 90 | ] |
|---|
| 91 | string! [ |
|---|
| 92 | ;probe dpos |
|---|
| 93 | res: find/match dpos fit |
|---|
| 94 | it: either res [ |
|---|
| 95 | pmsg "string -> TRUE" |
|---|
| 96 | dpos: res |
|---|
| 97 | next it |
|---|
| 98 | ][ |
|---|
| 99 | pmsg "string -> false" |
|---|
| 100 | next-rule it |
|---|
| 101 | ] |
|---|
| 102 | ] |
|---|
| 103 | ][ |
|---|
| 104 | print rejoin ["Bad value (" type? fit "): {" fit "}"] |
|---|
| 105 | halt |
|---|
| 106 | ] |
|---|
| 107 | ] |
|---|
| 108 | if not it [ return false ] |
|---|
| 109 | ] |
|---|
| 110 | true |
|---|
| 111 | ] |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | parsef: func [ |
|---|
| 115 | "A function which implements 'parse" |
|---|
| 116 | data [string! block!] |
|---|
| 117 | rules [block! none!] |
|---|
| 118 | /all |
|---|
| 119 | ][ |
|---|
| 120 | dpos: data |
|---|
| 121 | if string? data |
|---|
| 122 | [ |
|---|
| 123 | if block? rules |
|---|
| 124 | [ |
|---|
| 125 | if eval-pstr rules [ |
|---|
| 126 | return tail? dpos |
|---|
| 127 | ] |
|---|
| 128 | ] |
|---|
| 129 | ] |
|---|
| 130 | false |
|---|
| 131 | ] |
|---|
| 132 | |
|---|
| 133 | |
|---|
| 134 | data1: { |
|---|
| 135 | asf lsdkjf ldkf |
|---|
| 136 | [X] asdfjk fdlk |
|---|
| 137 | [ ] aslkfjaslkjf |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | test1: does [ |
|---|
| 141 | ; Found '_' |
|---|
| 142 | ; false |
|---|
| 143 | print parsex/all data1 [ |
|---|
| 144 | to "[ ]" (print "Found '_'") |
|---|
| 145 | | to "[X]" (print "Found 'X'") |
|---|
| 146 | ] |
|---|
| 147 | ] |
|---|
| 148 | |
|---|
| 149 | test2: does [ |
|---|
| 150 | ; Infinine loop "Found _" |
|---|
| 151 | print parsex/all data1 [some[ |
|---|
| 152 | to "[ ]" (print "Found '_'") |
|---|
| 153 | | to "[X]" (print "Found 'X'") |
|---|
| 154 | ]] |
|---|
| 155 | ] |
|---|
| 156 | |
|---|
| 157 | test3: does [ |
|---|
| 158 | ; Found 'X' |
|---|
| 159 | ; Found '_' |
|---|
| 160 | ; false |
|---|
| 161 | print parsex/all data1 [some[ |
|---|
| 162 | "[ ]" (print "Found '_'") |
|---|
| 163 | | "[X]" (print "Found 'X'") |
|---|
| 164 | | to "[" |
|---|
| 165 | ]] |
|---|
| 166 | ] |
|---|
| 167 | |
|---|
| 168 | test4: does [ |
|---|
| 169 | print parsex/all data1 [ |
|---|
| 170 | to "[!]" (print "Found '!'") |
|---|
| 171 | | to "[ ]" (print "Found '_'") |
|---|
| 172 | ] |
|---|
| 173 | ] |
|---|
| 174 | |
|---|
| 175 | tests: [ |
|---|
| 176 | test1 |
|---|
| 177 | ;test2 |
|---|
| 178 | test3 |
|---|
| 179 | test4 |
|---|
| 180 | ] |
|---|
| 181 | |
|---|
| 182 | print "N ---------" |
|---|
| 183 | parsex: :parse |
|---|
| 184 | do tests |
|---|
| 185 | |
|---|
| 186 | print "F ---------" |
|---|
| 187 | parsex: :parsef |
|---|
| 188 | do tests |
|---|
| 189 | |
|---|
| 190 | |
|---|
| 191 | comment { |
|---|
| 192 | Match |
|---|
| 193 | string! |
|---|
| 194 | char! |
|---|
| 195 | 'tag |
|---|
| 196 | ;end |
|---|
| 197 | bitset! |
|---|
| 198 | datatype! match type (block parse only) |
|---|
| 199 | |
|---|
| 200 | Forms |
|---|
| 201 | | alternate rule |
|---|
| 202 | [] sub-rule |
|---|
| 203 | () evaluate |
|---|
| 204 | |
|---|
| 205 | Quantity |
|---|
| 206 | none nothing |
|---|
| 207 | opt 0 or 1 times |
|---|
| 208 | some 1 or more times |
|---|
| 209 | any 0 or more times |
|---|
| 210 | integer! repeat |
|---|
| 211 | integer! integer! repeat |
|---|
| 212 | |
|---|
| 213 | Skip |
|---|
| 214 | skip |
|---|
| 215 | to |
|---|
| 216 | thru |
|---|
| 217 | |
|---|
| 218 | Values |
|---|
| 219 | set set variable to next value |
|---|
| 220 | copy set variable to next match sequence |
|---|
| 221 | |
|---|
| 222 | Words |
|---|
| 223 | word evaluate word |
|---|
| 224 | word: mark position |
|---|
| 225 | :word set position |
|---|
| 226 | 'word match literal (block parse only) |
|---|
| 227 | } |
|---|