Skip to content

Support default value for Reference #1864

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

Open
ydaveluy opened this issue Mar 21, 2025 · 1 comment
Open

Support default value for Reference #1864

ydaveluy opened this issue Mar 21, 2025 · 1 comment

Comments

@ydaveluy
Copy link
Contributor

It is currently possible to assign a default value to properties of type string and number.

It would be very useful to be also able to provide a default value for a reference by providing a string for the $refText attribute.

The typical use case is to provide by default a reference to an element defined in a builtin library.

E.g:
grammar:

entry Model:
    elements+=(Primitve|Type) *;

interface Primitive {
    name:string
}
interface Type {
    name:string
    primitive:@Primitive = 'int32' /* default reference text */
}
Primitve returns Primitive:
    'primitive' name=ID;

Type returns Type:
    'type' name=ID ('extends' primitive=[Primitive:ID])?;

hidden terminal WS: /\s+/;
terminal ID: /[_a-zA-Z][\w_]*/;

builtin library:

primitive int8
primitive int16
primitive int32
primitive int64

model:

type T1 /* extends int32 by default */
type T2 extends int8

I can provide a PR for this request.

@msujew
Copy link
Member

msujew commented Mar 21, 2025

I generally like the idea, but I have two misgivings:

  1. Where do we represent a linking error on the missing reference? (i.e. how can the user be informed that the reference resolving failed; We don't have a corresponding CST node for the reference)
  2. I don't really like the way this dilutes the type system of the grammar language (as it would kind of allow to assign strings to references). This is just a minor issue.

I feel like (1) in particular is rather problematic in the general case. I think this should be rather handled in the adopter code as something like primitive.ref ?? int32Primitive, since it is an adopter concern that the variable is available for referencing in the first place.

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