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

Parsing the text failed, and the error message said that an opening bracket is required, but I already have an opening bracket. #639

Open
qinjunhang opened this issue Jul 11, 2023 · 1 comment

Comments

@qinjunhang
Copy link

image

The content of wql.ne is:

# http://www.json.org/
# http://www.asciitable.com/
@{%

const moo = require('moo')

let lexer = moo.compile({
    space: {match: /\s+/, lineBreaks: true},
    number: /-?(?:[0-9]|[1-9][0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/,
    string: /"(?:\\["bfnrt\/\\]|\\u[a-fA-F0-9]{4}|[^"\\])*"/,
    key: /[a-zA-Z-_]+/,
    comparator: /=|<|<=|>=|>|!=/,
})

%}

@lexer lexer


start -> condition {% d => { return d[0]}  %}

operator -> "AND"  {% d => {   return d[0].value}  %}
    | "OR" {% d => {   return d[0].value}  %}
    
comparator -> %comparator {% d => { return d[0].value}  %} 

condition ->  _ expression  (_  operator _ expression ):* _  {% extractCondition %}

expression -> IDENTIFIER _ comparator _ LITERAL  {% function(d) { return [d[0], d[2], d[4]]; } %}  
    | "(" _ condition _ ")" {% function(d) { return [d[0], d[2], d[4]]; } %}

key -> %key {% function(d) { return d[0].value } %}
IDENTIFIER -> key   {% id %}

number -> %number {% function(d) { return parseFloat(d[0].value) } %}
string -> %string {% function(d) { return d[0].value } %}
LITERAL -> number  {% id %}
    | string   {% id %}
    | [a-zA-Z-_] {% function(d) { return d[0].value } %}


_ -> null | %space {% function(d) { return null; } %}

@{%
function extractCondition(d){
    console.log("@@@@@@@@@@@@",d)
    let output = [d[1]];

    for (let i in d[2]) {
        output.push(d[2][i][1]);
        output.push(d[2][i][3]);
    }


    return output;
}
%}
@esdmr
Copy link

esdmr commented Jul 15, 2023

The lexer seems to not have a token for open/close parenthesis. Adding that would solve it.

let lexer = moo.compile({
    space: {match: /\s+/, lineBreaks: true},
    number: /-?(?:[0-9]|[1-9][0-9]+)(?:\.[0-9]+)?(?:[eE][-+]?[0-9]+)?\b/,
    string: /"(?:\\["bfnrt\/\\]|\\u[a-fA-F0-9]{4}|[^"\\])*"/,
    key: /[a-zA-Z-_]+/,
    comparator: /=|<|<=|>=|>|!=/,
    lparen: '(',
    rparen: ')',
})

(Also, wql.ne seems to have some ambiguities. Notably: in expression, there is a _ both inside and outside condition. Also, since there is a lexer, character classes like [a-zA-Z-_] behave incorrectly. In this case, both [a-zA-Z-_] and %string can match at the same time.)

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