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

Fragments do not work as expected #3016

Closed
monrealis opened this issue Dec 27, 2020 · 8 comments
Closed

Fragments do not work as expected #3016

monrealis opened this issue Dec 27, 2020 · 8 comments

Comments

@monrealis
Copy link

I'm working on a plantuml grammar and fragments do not work as expected:

This works well:

relationType
:
	(
		'|o'
		| '||'
		| '}o'
		| '}|'
		| '}'
	)
	(
		(
			'-'+
			(
				'left'
				| 'right'
				| 'top'
				| 'down'
			) '-'+
		)
		| '-'+
	)
	(
		'{'
		| 'o{'
		| 'o|'
		| '|{'
		| '||'
	)
	| '-->'
;

This gives line 34:16 no viable alternative at input '-------down' :

relationType
:
	(
		'|o'
		| '||'
		| '}o'
		| '}|'
		| '}'
	)
	(
		(
			'-'+ DIRECTION '-'+
		)
		| '-'+
	)
	(
		'{'
		| 'o{'
		| 'o|'
		| '|{'
		| '||'
	)
	| '-->'
;

fragment
DIRECTION
:
	'left'
	| 'right'
	| 'top'
	| 'down'
;

Line:
A7 }o-------down----------{ B7

@monrealis
Copy link
Author

4.9 gives " implicit definition of token DIRECTION in parser", while 4.4 is silent.

@monrealis
Copy link
Author

monrealis commented Dec 27, 2020

This works too:

RELATION_TYPE
:
	(
		'|o'
		| '||'
		| '}o'
		| '}|'
		| '}'
	)
	(
		(
			'-'+ (DIRECTION) '-'+
		)
		| '-'+
	)
	(
		'{'
		| 'o{'
		| 'o|'
		| '|{'
		| '||'
	)
	| '-->'
;
fragment
DIRECTION
:
	'left'
	| 'right'
	| 'top'
	| 'down'
;

@monrealis
Copy link
Author

image

@KvanTTT
Copy link
Member

KvanTTT commented Dec 27, 2020

4.9 gives " implicit definition of token DIRECTION in parser", while 4.4 is silent.

That's just correct behavior because you can't use fragment token inside parse rule. They are only allowed inside lexer rules. Probably the error message a bit confusing.

@monrealis
Copy link
Author

I see two minor issues here:

  • The behavior changes when you inline fragment inside parser rule. That's not how inlining should work, as understood by most IT people. Is there a reason for that?
  • Google says that fragment will never be counted as a token, but the tool says "implicit definition of token DIRECTION in parser"

@KvanTTT
Copy link
Member

KvanTTT commented Dec 28, 2020

The behavior changes when you inline fragment inside parser rule. That's not how inlining should work, as understood by most IT people. Is there a reason for that?

There is no common understanding of how inline lexer fragments work in IT because grammars are a specific topic. In ANTLR fragment lexer rules don't exist themselves, they are used within other real tokens for lexer simplification.

Google says that fragment will never be counted as a token, but the tool says "implicit definition of token DIRECTION in parser"

Yes, DIRECTION token does not exist that's why it's being created in an implicit way. But I agree, this error is quite confusing and should be renamed to something like unable to use fragment rule in parser.

@KvanTTT
Copy link
Member

KvanTTT commented Dec 29, 2020

Also, it's a duplicate of #2886

@KvanTTT
Copy link
Member

KvanTTT commented Nov 7, 2021

@parrt @ericvergnaud please close since it's duplicate.

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

Successfully merging a pull request may close this issue.

3 participants