Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SyntaxError: unterminated parenthetical #223

Open
jehna opened this issue Dec 11, 2020 · 1 comment
Open

SyntaxError: unterminated parenthetical #223

jehna opened this issue Dec 11, 2020 · 1 comment

Comments

@jehna
Copy link
Contributor

jehna commented Dec 11, 2020

From using:

VerEx().startOfLine().then("aaa").or("bbb").endOfLine()

I'd expect it to compile to something like:

/^(aaa|bbb)$/

...but since endOfLine discards existing suffixes (where there is a capture group close parenthesis from .or), it leaves the capture group open, resulting in:

/^(aaa|bbb$/
@joseignaciorm
Copy link

The same error here: SyntaxError: Invalid regular expression: /^(?:(?:aaa))|(?:(?:bbb)$/: Unterminated group

The .or() function generates the capture group's open parenthesis in _prefixes: '^(?:', the corresponding capture group's close parenthesis in source: _source: '(?:aaa))' before the .or() function, but the capture group's close parenthesis of the .or function is generated by the suffixes variable => _suffixes: ')$'. So then, the problem is when we use endOfLine, it generates an empty prefixes _prefixes: '' and the suffixes _suffixes: '$' which deletes the previous suffixes which includes the capture group's close parenthesis generated by the .or function.

My solution for now was the following below:
In the endOfLine function I've added the addition assignment operator += to the _suffixes variable instead of the assignment operator = so that we add instead of rewrite the _suffixes variable. example:

Before adding addition assignment operator to the _suffixes variable:

{
    key: "endOfLine",
    value: function endOfLine() {
      var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
      this._suffixes = enable ? '$' : '';
      return this.add(); 
}

After adding addition assignment operator to the _suffixes variable:

{
    key: "endOfLine",
    value: function endOfLine() {
      var enable = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : true;
      this._suffixes += enable ? '$' : '';
      return this.add(); 
}

Also you can test it out with this examples to see the three different outputs:

const VerEx = require('../dist/verbalexpressions.js');

// or and endOfLine
const expr1 = VerEx().startOfLine().then("aaa").or("bbb").endOfLine()
console.log(expr1); 

// endOfLine
const expr2 = VerEx().find('aaa').endOfLine();
console.log(expr2); 

// or
const expr3 = VerEx().find('aaa').or('bbb');
console.log(expr3); 

I hope this can help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants