4 ms·
Unlike a regex string, using incorrect syntax with a parser combinator generally results in a syntax error. Regex parsers generally treat "unknown" characters
by colanderman 10y ago
Unlike a regex string, using incorrect syntax with a parser combinator generally results in a syntax error. Regex parsers generally treat "unknown" characters as characters to match, hiding bugs.
- 1ris 10y agoAnd usually not at compile time.
- deleted 10y ago[deleted]
- junke 10y agoHere is an example of an "unusual" case where the compiler can detect errors in statically known regexes. This fails at compilation with Opening paren has no matching closing paren. at position 0 in string "(ab" (defun scan-ab (s) (ppcre:scan "(ab" s)) By the way, here is the alternative syntax: (defun scan-ab (s) (ppcre:scan '(:register "ab") s)) The alternative syntax allows to embed string regexes: (defun wrap-regex (regex) (typecase regex (string `(:regex ,regex)) (t regex))) This is useful for combining regexes: (defun exactly-some (&rest choices) (let ((choices (mapcar #'wrap-regex choices))) `(:sequence :start-anchor ,@(if (rest choices) `((:alternation ,@choices)) choices) :end-anchor))) (exactly-some "t.*") (:SEQUENCE :START-ANCHOR (:REGEX "t.*") :END-ANCHOR) (exactly-some "t.*" "a.a" ':whitespace-char-class) (:SEQUENCE :START-ANCHOR (:ALTERNATION (:REGEX "t.*") (:REGEX "a.a") :WHITESPACE-CHAR-CLASS) :END-ANCHOR)
- deleted 10y ago[deleted]
- kazinator 10y agoA regex language could fix it by some clear prefix convention or whatever for all operators. Though more verbose, it would still be less verbose than alternatives. Suppose all regex operator characters have to be backslashed (and \\ stands for a single \). Then it's clear. No backslash means it's literal; otherwise it's an operator (and a backslash on a nonexistent operator is a parse error). The ambiguities exist because regex aficionados want common operators to be just one character long.
- kazinator 10y agoA really annoying "feature" of regexes in POSIX is that known regex meta-characters are used as ordinary, when they are in a context that doesn't recognize them. Some examples are a superfluous closing parenthesis or a [ bracket in a character class. A sane regex syntax treats these characters as a separate lexical category from literal characters, in all contexts, and only produces literals out of them when they are escaped.
- raiph 10y agoPerl 6 regexes are parsed at compile-time (i.e. they're code just like any other code) so using incorrect regex syntax, eg unknown characters, results in a compile-time syntax error.