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

Indentation-sensitive parsing: dedent not working? #467

Open
poke1024 opened this issue Feb 19, 2024 · 3 comments
Open

Indentation-sensitive parsing: dedent not working? #467

poke1024 opened this issue Feb 19, 2024 · 3 comments

Comments

@poke1024
Copy link

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
Copy link
Author

By the way, running findIndentation on the code gives very reasonable results, namely { '11': 1, '24': 1, '28': -1, '41': 1, '42': -2 }, which are exactly the expected indents and dedents.

@pdubroy
Copy link
Contributor

pdubroy commented Feb 25, 2024

Interesting...thanks for reporting. Might be a problem with space skipping, but I'll investigate.

@simonwo
Copy link

simonwo commented Mar 5, 2024

FYI this looks superficially similar to my issue in #465.

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

3 participants