From f68f1889fa7cf11c7dc85c82f9a28befd6302c93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ph=E1=BA=A1m=20Ng=E1=BB=8Dc=20Quang=20Nam?= Date: Thu, 4 Mar 2021 15:15:44 +0100 Subject: [PATCH] Update regex grammar for the special character Update regex grammar for the special character --- grammar.pegjs | 36 ++++++++++++++++++++++++++++++++++-- 1 file changed, 34 insertions(+), 2 deletions(-) diff --git a/grammar.pegjs b/grammar.pegjs index c08955b..0d9cd8a 100644 --- a/grammar.pegjs +++ b/grammar.pegjs @@ -86,8 +86,8 @@ attr path = i:identifierName { return { type: 'literal', value: i }; } type = "type(" _ t:[^ )]+ _ ")" { return { type: 'type', value: t.join('') }; } flags = [imsu]+ - regex = "/" d:[^/]+ "/" flgs:flags? { return { - type: 'regexp', value: new RegExp(d.join(''), flgs ? flgs.join('') : '') }; + regex = "/" pattern:$RegularExpressionBody "/" flgs:flags? { return { + type: 'regexp', value: new RegExp(pattern, flgs ? flgs.join('') : '') }; } field = "." i:identifierName is:("." identifierName)* { @@ -107,3 +107,35 @@ nthLastChild = ":nth-last-child(" _ n:[0-9]+ _ ")" { return nthLast(parseInt(n.j class = ":" c:("statement"i / "expression"i / "declaration"i / "function"i / "pattern"i) { return { type: 'class', name: c }; } + +RegularExpressionBody + = RegularExpressionFirstChar RegularExpressionChar* + +RegularExpressionFirstChar + = ![*\\/[] RegularExpressionNonTerminator + / RegularExpressionBackslashSequence + / RegularExpressionClass + +RegularExpressionChar + = ![\\/[] RegularExpressionNonTerminator + / RegularExpressionBackslashSequence + / RegularExpressionClass + +RegularExpressionBackslashSequence + = "\\" RegularExpressionNonTerminator + +RegularExpressionNonTerminator + = !LineTerminator SourceCharacter + +RegularExpressionClass + = "[" RegularExpressionClassChar* "]" + +RegularExpressionClassChar + = ![\]\\] RegularExpressionNonTerminator + / RegularExpressionBackslashSequence + +LineTerminator + = [\n\r\u2028\u2029] + +SourceCharacter + = .