eclipse-xtext / eclipse-xtext/xtext

[formatting2] Serialization-based formatting leads to insertion of tokens

Open
#2,392 20 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
831
Forks
330
Avg merge
3d 7h
Merged PRs (30d)
12

Description

I'm experiencing a strange issue where formatting my DSL leads to insertion of parentheses. I suspect there is something going wrong with serialization.

Minimal repro: https://github.com/REGnosys/rosetta-dsl/tree/formatting-bug2

The language in the repro consists of an "expression" language with only a single operation (<Expression> exists) and parentheses. The operand of the exists operation is optional.

Problem: formatting the following code with the default(!) formatter

exists exists

results in

( exists ) exists

This does not happen if useSerializer of the FormatterTestRequest is set to false.


Grammar:

grammar com.regnosys.rosetta.Rosetta with org.eclipse.xtext.common.Terminals
import "http://www.rosetta-model.com/RosettaExpression"

RosettaExistsExpression:
	RosettaCalcPrimary
	(
		=>(
			({RosettaExistsExpression.argument=current} 'exists')
		)
	)*
	| // Without left parameter:
	(
		(
			 ({RosettaExistsExpression} 'exists')
		)
	)
	(
		=>(
			({RosettaExistsExpression.argument=current} 'exists')
		)
	)*
;

RosettaCalcPrimary returns RosettaExistsExpression:
	'(' RosettaExistsExpression ')'
;

Formatter:

class RosettaFormatter extends AbstractFormatter2 {
	override format(Object obj, IFormattableDocument document) {
		
	}
}

Failing unit test:

@Inject
extension FormatterTestHelper

@Test
def void parenthesesBug() {
        assertFormatted[
	        it.useNodeModel = false // test succeeds if replaced with `it.useSerializer = false`
	        it.expectation = '''
	        exists exists
	        '''
	        it.toBeFormatted = '''
	        exists exists
	        '''
        ]
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the FormatterTestHelper test and the FormatterTestRequest setting that switches between useNodeModel and useSerializer. Compare the two formatting paths using the supplied grammar and minimal reproduction. Done means the default formatter preserves exists exists without inserting parentheses.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.