ohm icon indicating copy to clipboard operation
ohm copied to clipboard

Indentation-sensitive parsing: dedent not working?

Open poke1024 opened this issue 4 months ago • 3 comments

Here is an example modified from https://github.com/ohmjs/ohm/blob/main/examples/indentation-sensitive/index.js, that I would expect to work. Note that Block has been changed to takeExpr+ instead of a single Expr.

import {grammar, ExperimentalIndentationSensitive as IndentationSensitive} from 'ohm-js';

const g = grammar(
	String.raw`
G <: IndentationSensitive {
  IfExpr = "if" Expr ":" Block
  Block = indent Expr+ dedent
  Expr = IfExpr
	   | "True"
	   | "False"
	   | number

  number = digit+
}
`,
	{IndentationSensitive}
);

const examples = [
`
if True:
  3
`,
`
if True:
  if True:
    3
`,
`
if True:
  if True:
    3
  if True:
    3
`
];

for (const s of examples) {
	const r = g.match(s.trim());
	if (!r.succeeded()) {
		console.log("failed on:", s);
		console.log(r.message);
	}
}

The first two examples in examples work, however the third (rather trivial) example gives this error:

Line 4, col 3:
  3 |     3
> 4 |   if True:
        ^
  5 |     3
Expected a dedent

Similarly, any actual dedent onto a previous level seems to issue this problem.

poke1024 avatar Feb 19 '24 13:02 poke1024