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

toAST incorrectly makes some nodes intermediate nodes #463

Open
rrthomas opened this issue Nov 6, 2023 · 1 comment
Open

toAST incorrectly makes some nodes intermediate nodes #463

rrthomas opened this issue Nov 6, 2023 · 1 comment

Comments

@rrthomas
Copy link

rrthomas commented Nov 6, 2023

Using Ohm 17.1.0, and the following grammar:

Demo {
  Sequence = ListOf<Exp, ";">

  PrimaryExp = number

  UnaryExp
    = "~" UnaryExp    -- bitwise_not
    | PrimaryExp

  Exp = UnaryExp

  number
    = digit* "." digit+  -- fract
    | digit+             -- whole
}

I run the following code:

import {toAST} from 'ohm-js/extras'

// eslint-disable-next-line import/extensions
import grammar from './demo.ohm-bundle.js'

const matchResult = grammar.match('2')
console.dir(toAST(matchResult))

const matchResult2 = grammar.match('~2')
console.dir(toAST(matchResult2))

and the output is:

[ '2' ]
[ '2' ]

In other words, there's no difference between the ASTs for ~2 and for 2.

It looks to me as though Ohm is first discarding the node for the literal string "~", and then incorrectly deciding that the rule UnaryExp_bitwise_not has only one node, and hence considering it an intermediate node.

@cristiano-belloni
Copy link

cristiano-belloni commented Aug 7, 2024

I'm seeing a similar behaviour, where something like this (simplified):

ComparisonExp = 
  "a" "IN" aValues
 |"b" "IN" bValues

Gives you only the value of aValues or bValues when toAST is called, while I expect to see if the node is called on "a" or "b", and with which operator. Using mapping doesn't work at all, because ComparisonExp only maps to the 0-th place, if I try to access the 1st or 2nd I get an exception.

To be fair, why can't .toAST() generate a complete AST, without discarding literals? Apart from being buggy, it's really hard to read.

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