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

jcon对expr的解析如 1+2*3+4/5 #11

Open
acodercc opened this issue May 19, 2015 · 1 comment
Open

jcon对expr的解析如 1+2*3+4/5 #11

acodercc opened this issue May 19, 2015 · 1 comment

Comments

@acodercc
Copy link
Owner

    var jcon = require('jcon');
    var epsilon = jcon.string('');
    var num = jcon.regex(/\d+/).setAst('num');
    var sum_operator = jcon.regex(/[\+\-]/).setAst('operator');
    var product_operator = jcon.regex(/[\*\/]/).setAst('operator');
    var skips = jcon.regex(/\s*/).skip();
    var product = jcon.lazy(function(){
        return jcon.or(
            num,
            jcon.seq(num, product_rest)
        );
    }).setAst('product');
    var product_rest = jcon.or(epsilon, jcon.seq(skips, product_operator, skips, product));

    var sum = jcon.lazy(function(){
        return jcon.or(product,
            jcon.seq(product, sum_rest)
        );
    }).setAst('sum');
    var sum_rest = jcon.or(epsilon, jcon.seq(skips, sum_operator, skips, sum));

    var s = sum.parse('1+2*3+4/5').ast();
    console.log(JSON.stringify(s, null, '  '));
@acodercc
Copy link
Owner Author

sum和product的ast设置错误,应该设置在seq rest的节点上,lazy只是为了在rest定义前使用rest定义sum或product

@acodercc acodercc reopened this May 21, 2015
@acodercc acodercc changed the title jcon对expr的解析如1+2*3+4/5 jcon对expr的解析如 1+2*3+4/5 May 27, 2015
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

1 participant