• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/ucode/tests/custom/00_syntax/21_regex_literals

  1 Regex literals are enclosed in forward slashes and may contain zero
  2 or more trailing flag characters. The pattern is kept as written and is
  3 preprocessed when the literal is loaded, so converting a regexp back to
  4 a string yields the pattern as it was written, while its interpretation
  5 is subject to the underlying regular expression engine.
  6 
  7 -- Expect stdout --
  8 [ "/Hello world/", "/test/gis", "/test/g", "/test1 \\/ test2/", "/\\x31\\n\\.\\a\\b\\c\\u2600\\\\/" ]
  9 -- End --
 10 
 11 -- Testcase --
 12 {%
 13         print([
 14                 /Hello world/,                          // A very simple expression
 15                 /test/gsi,                                      // Regular expression flags
 16                 /test/gg,                                       // Repeated flags
 17                 /test1 \/ test2/,                       // Escaped forward slash
 18                 /\x31\n\.\a\b\c\u2600\\/        // Ensure that escape sequences are passed as-is
 19         ], "\n");
 20 %}
 21 -- End --
 22 
 23 
 24 Testing regular expression type.
 25 
 26 -- Expect stdout --
 27 regexp
 28 -- End --
 29 
 30 -- Testcase --
 31 {{ type(/foo/) }}
 32 -- End --
 33 
 34 
 35 Testing invalid flag characters.
 36 
 37 -- Expect stderr --
 38 Syntax error: Unexpected token
 39 Expecting ';'
 40 In line 2, byte 8:
 41 
 42  `    /test/x`
 43             ^-- Near here
 44 
 45 
 46 -- End --
 47 
 48 -- Testcase --
 49 {%
 50         /test/x
 51 %}
 52 -- End --
 53 
 54 
 55 Testing unclosed regular expression.
 56 
 57 -- Expect stderr --
 58 Syntax error: Unterminated regexp pattern
 59 In line 2, byte 2:
 60 
 61  `    /foo \/`
 62       ^-- Near here
 63 
 64 
 65 -- End --
 66 
 67 -- Testcase --
 68 {%
 69         /foo \/
 70 %}
 71 -- End --
 72 
 73 
 74 Testing regex compilation errors.
 75 
 76 -- Expect stderr --
 77 Catched syntax error
 78 In line 7, byte 30:
 79 
 80  `            die("Catched syntax error");`
 81               ~~~~~~~~~~~~~~~~~~~~~~~~~~^~
 82 
 83 
 84 -- End --
 85 
 86 -- Testcase --
 87 {%
 88         try {
 89                 /foo (/
 90         }
 91         catch (e) {
 92                 if (e.type == "Syntax error")
 93                         die("Catched syntax error");
 94         }
 95 %}
 96 -- End --
 97 
 98 
 99 Testing that slashes within character classes are not treated as regex
100 literal delimitters.
101 
102 -- Expect stdout --
103 [
104         "/[/]/",
105         "/[[./.]/]/",
106         "/[[:alpha:]/]/",
107         "/[[=/=]/]/"
108 ]
109 -- End --
110 
111 -- Testcase --
112 {%
113         printf("%.J\n", [
114                 /[/]/,
115                 /[[./.]/]/,
116                 /[[:alpha:]/]/,
117                 /[[=/=]/]/
118         ]);
119 %}
120 -- End --
121 
122 
123 Testing that regex extension macros are substituted only outside of
124 bracket set expressions, where a backslash there merely quotes the
125 following character.
126 
127 -- Expect stdout --
128 [
129         "/ \\d \\D [\\d \\D] /",
130         "/ \\s \\S [\\s \\S] /",
131         "/ \\w \\W [\\w \\W] /",
132         [
133                 "5"
134         ],
135         [
136                 "d"
137         ],
138         null,
139         [
140                 " "
141         ],
142         [
143                 "s"
144         ],
145         [
146                 "a"
147         ],
148         [
149                 "w"
150         ]
151 ]
152 -- End --
153 
154 -- Testcase --
155 {%
156         printf("%.J\n", [
157                 / \d \D [\d \D] /,
158                 / \s \S [\s \S] /,
159                 / \w \W [\w \W] /,
160                 match("5", /\d/),
161                 match("d", /[\d]/),
162                 match("5", /[\d]/),
163                 match(" ", /\s/),
164                 match("s", /[\s]/),
165                 match("a", /\w/),
166                 match("w", /[\w]/)
167         ]);
168 %}
169 -- End --
170 
171 
172 Testing that only the byte right behind the opening bracket of a character
173 list negates it, and that a right bracket in leading position - and in the
174 position behind a negation - is an ordinary member. A second caret therefore
175 denotes itself, which makes `[^^]` the list of everything but a caret.
176 
177 -- Expect stdout --
178 [
179         "/[^^]/",
180         [
181                 "a"
182         ],
183         null,
184         "/[]]/",
185         [
186                 "]"
187         ],
188         "/[^]]/",
189         null,
190         [
191                 "a"
192         ],
193         "/[a^]/",
194         [
195                 "^"
196         ]
197 ]
198 -- End --
199 
200 -- Testcase --
201 {%
202         printf("%.J\n", [
203                 /[^^]/,
204                 match("a", /[^^]/),
205                 match("^", /[^^]/),
206                 /[]]/,
207                 match("]", /[]]/),
208                 /[^]]/,
209                 match("]", /[^]]/),
210                 match("a", /[^]]/),
211                 /[a^]/,
212                 match("^", /[a^]/)
213         ]);
214 %}
215 -- End --
216 
217 
218 Testing that a character list whose right bracket is quoted is not closed,
219 since the backslash only quotes the bracket and does not end the list.
220 
221 -- Expect stderr --
222 Syntax error: Unterminated regexp pattern
223 In line 2, byte 2:
224 
225  `    /[a\]/`
226       ^-- Near here
227 
228 
229 -- End --
230 
231 -- Testcase --
232 {%
233         /[a\]/
234 %}
235 -- End --

This page was automatically generated by LXR 0.3.1.  •  OpenWrt