From abc40bf770e2bff2ea1d3867b8853c1f8ee5c1d7 Mon Sep 17 00:00:00 2001 From: Aadhavan Srinivasan Date: Mon, 27 Jan 2025 16:07:23 -0500 Subject: [PATCH] Return an error if a POSIX charclass is specified outside of brackets --- compile.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/compile.go b/compile.go index 91b098c..93dc54d 100644 --- a/compile.go +++ b/compile.go @@ -481,6 +481,16 @@ func shuntingYard(re string, flags ...ReFlag) ([]postfixNode, error) { if i >= len(re_postfix) { return nil, fmt.Errorf("Opening bracket with no closing bracket.") } + + // Check if a POSIX character class was specified ouside a bracket. This is an error. + // Eg. [:digit:] should lead to an error, telling the user that the right syntax is [[:digit:]] + if re_postfix[i] == ':' { + posixClassPresent, _ := getPOSIXClass(re_postfix[i+1:]) + if posixClassPresent { + return nil, fmt.Errorf("The syntax for POSIX character classes is [[:digit:]], not [:digit:]") + } + } + var invertMatch bool if re_postfix[i] == '^' { invertMatch = true