0

I am writing a peg js grammar to parse logical query literals into a json format but facing an issue with selecting a multi value word in a condition value. Like (city = 'a' OR city = 'b') works properly but (city = 'a b' OR city = 'b') is not working because it has a multi word value for first condition. I have a regular expression '((?:''|[^'])*)' which can match strings within a single quote value, but PEG js parser is failing to parse it.

Can anyone please help me with a way to achieve this ? Below is my grammar: Online Compiler: https://pegjs.org/online Input: (city = 'a b' OR city = 'b') Grammar:

Set = col: Column right: (_ 'in'
    i_ '('
    Series ')') {
    return {
        column: col,
        operator: right[1],
        value: right[4].split(','),
        conditions: []
    }
}
Series = left: Column right: (_ ','
    _(Series / Column)) ? {
    if (right) {
        var leftValue = left.slice(1).substring(0, left.length - 2);
        var rightValue = right[3];
        return leftValue + ',' + rightValue;
    } else {
        var leftValue = left.slice(1).substring(0, left.length - 2);
        return leftValue;
    }
}
LogicalCondition = left: LogicalSeparator right: (_('AND'
    i / 'OR') _ LogicalSeparator) * {
    var conditions = right.reduce(function (result, element) {
        var condition = element[3];
        if (condition.groupCondition) {
            condition.groupOperator = element[1];
        }
        result.push(condition);
        return result;
    }, [left]);
    if (right.length > 0) {
        return {
            "groupCondition": true,
            "conditions": conditions,
            "groupOperator": right[0][1]
        }
    } else if (left.groupCondition === true) {
        return left;
    } else {
        return {
            "groupCondition": true,
            "conditions": [left]
        }
    }
}
/ Set
LogicalSeparator = Set /
    "("
_ expr: LogicalCondition _ ")" {
    return expr;
}
/ Condition
Condition = left: Column right: (_('customequalto'
    i / 'customnotequalto'
    i / '='
    i / '!='
    i / 'contains'
    i / 'not contains'
    i / 'in'
    i / 'matches'
    i / 'not matches'
    i / 'is empty'
    i / 'is not empty'
    i / '<>') _ ConditionValue) * {
    var validColumns = Array.of('employee_name', 'city', 'disabled', 'birthDate', 'age');
    if (!validColumns.includes(left)) {
        error('invalid column name ' + left);
    }
    return right.reduce(function (result, element) {
        var valueText = right[0][3];
        return {
            conditions: [],
            column: left,
            operator: right[0][1],
            value: valueText.slice(1).substring(0, valueText.length - 2),
        };
    }, left);
}
ConditionValue "string" = _['/A-Za-z0-9-_'] + {
    return text();
}
Column "string" = _['/A-Za-z0-9-_'] + {
    return text();
}
_ "whitespace" = [\t\ n\ r] *```

It would be a great help if you can help!.
Thank you in advance.
Harshad Sindhav
  • 171
  • 2
  • 7
  • The grammar has some issue: - line 2 you use i but did not define it - line 87 white space definition is not correct can you write what is your expected output? – Digital Alpha Oct 22 '20 at 12:07
  • 1
    It's been awhile but with simple strings without escapes it's `string = "'" [^']* "'" { return text(); }` – jcubic Mar 25 '21 at 22:00

0 Answers0