Repository: DmitrySoshnikov/regexp-tree (js)

Issue:
`/\e/u` should throw
`/\e/u` is an invalid regular expression: https://mathiasbynens.be/notes/es6-unicode-regex#impact-syntax

So instead of parsing, I’d expect this to throw:

```
$ regexp-tree -e '/\e/u'
{
  "type": "RegExp",
  "body": {
    "type": "Char",
    "value": "e",
    "kind": "simple",
    "escaped": true
  },
  "flags": [
    "u"
  ]
}
```


Pull request description:
Parser: When u flag is set, throw error on invalid unicode escape
This should fix #8 

Patch:
diff --git a/src/parser/generated/regexp-tree.js b/src/parser/generated/regexp-tree.js
index 8ddf1a3..29fd404 100644
--- a/src/parser/generated/regexp-tree.js
+++ b/src/parser/generated/regexp-tree.js
@@ -360,7 +360,13 @@ const lexRules = [[/^#[^\n]+/, function() { /* skip comments */ }],
 [/^\\x[0-9a-fA-F]{2}/, function() { return 'HEX_CODE' }],
 [/^\\[tnrdDsSwWvf]/, function() { return 'META_CHAR' }],
 [/^\\\//, function() { return 'ESC_CHAR' }],
-[/^\\[^*?+\[()]/, function() { return 'ESC_CHAR' }],
+[/^\\[^*?+\[()]/, function() { 
+                                        const s = this.getCurrentState();
+                                        if (s === 'u' || s === 'xu' || s === 'u_class') {
+                                            throw new SyntaxError(`invalid Unicode escape ${yytext}`);
+                                        }
+                                        return 'ESC_CHAR';
+                                     }],
 [/^\\[*?+\[()]/, function() { return 'ESC_CHAR' }],
 [/^\(/, function() { return 'CHAR' }],
 [/^\)/, function() { return 'CHAR' }],
diff --git a/src/parser/regexp.bnf b/src/parser/regexp.bnf
index 89b8b2c..133cf3b 100644
--- a/src/parser/regexp.bnf
+++ b/src/parser/regexp.bnf
@@ -103,7 +103,14 @@ GROUP_NAME              ([\w$]|\\'u'[0-9a-fA-F]{4}|\\'u{'[0-9a-fA-F]{1,}'}')+
 
 {ESC}'/'                            return 'ESC_CHAR'
 
-{ESC}{CHAR}                         return 'ESC_CHAR'
+{ESC}{CHAR}                         {
+                                        const s = this.getCurrentState();
+                                        if (s === 'u' || s === 'xu' || s === 'u_class') {
+                                            throw new SyntaxError(`invalid Unicode escape ${yytext}`);
+                                        }
+                                        return 'ESC_CHAR';
+                                    }
+
 {ESC}[*?+\[()]                      return 'ESC_CHAR'
 
 <class,u_class>'('                  return 'CHAR'


Test patch:
diff --git a/src/parser/__tests__/parser-basic-test.js b/src/parser/__tests__/parser-basic-test.js
index 4a4b4e1..32733a5 100644
--- a/src/parser/__tests__/parser-basic-test.js
+++ b/src/parser/__tests__/parser-basic-test.js
@@ -1068,4 +1068,10 @@ describe('basic', () => {
     });
   });
 
+  it('throws error on invalid Unicode escape', () => {
+    expect(() => parser.parse('/\\p/u')).toThrowError(SyntaxError);
+    expect(() => parser.parse('/\\e/u')).toThrowError(SyntaxError);
+    expect(() => parser.parse('/\\g/u')).toThrowError(SyntaxError);
+  });
+
 });
\ No newline at end of file