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

[Feature Request] Skip all "codeExpr" while looking ahead #149

Open
fy0 opened this issue May 8, 2024 · 0 comments
Open

[Feature Request] Skip all "codeExpr" while looking ahead #149

fy0 opened this issue May 8, 2024 · 0 comments

Comments

@fy0
Copy link

fy0 commented May 8, 2024

For example:

aExpr <- &('-' number) stateChange '-' number { c.globalState["code"].push(vmCode(typeUnaryMinus)) }

stateChange <- #{ c.globalState['minus'] += 1 }

number <- [0-9]+ { c.globalState["code"].push(newIntVal(c.text)); }

Many script languages are not generate ast, instead of write code directly.

In this example, i want to check if - number is matched, then make state change.

But c.globalState["code"].push(newIntVal(c.text)) is invoked while looking ahead of "- number". I think it could use some improvement.

There's a simple patch:

// edit parseAndExpr:

func (p *parser) parseAndExpr(and *andExpr) (any, bool) {
	pt := p.pt
	state := p.cloneState()
	p.pushV()
	p.cur.state["skipCode"] = true
	_, ok := p.parseExprWrap(and.expr)
	delete(p.cur.state, "skipCode")
	p.popV()
	p.restoreState(state)
	p.restore(pt)

	return nil, ok
}

// edit parseActionExpr:
func (p *parser) parseActionExpr(act *actionExpr) (any, bool) {
	if p.cur.state["skipCode"] == true {
		return nil, true
	}
	start := p.pt
	val, ok := p.parseExprWrap(act.expr)
	if ok {
        .....
}

Do same thing to parseOrExpr and other codeExpr.

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