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

Update CTO grammar for Import Aliasing #853

Closed
3 tasks
ekarademir opened this issue May 29, 2024 · 2 comments · Fixed by #855
Closed
3 tasks

Update CTO grammar for Import Aliasing #853

ekarademir opened this issue May 29, 2024 · 2 comments · Fixed by #855
Assignees
Milestone

Comments

@ekarademir
Copy link
Contributor

ekarademir commented May 29, 2024

  • Update Parsing Expression Grammar to generate aliased import types within the AST
  • Generate the new parser
  • Add tests for import aliasing to the parser test suite

Specification

Imported terms can ONLY be aliased using curly braces, i.e. multiple import syntax

Correct

Import with version

import com.example.foo@1.0.4.{baz as bar}

Import with version with pre-release identifier

import com.example.foo@1.0.4-X.y.z-01.{baz as bar}

Import with version with pre-release identifier from an external source

import com.example.foo@1.0.4.{baz as bar} from https://com.example.co.uk/models/com.example.foo.cto

Import without version (strict: false)

import com.example.foo.{baz as bar}

Incorrect

import com.example.foo@1.0.4.baz as bar
import com.example.foo.baz as bar

Aliased imports can be mixed with non-aliased imports

import com.example.foo@1.0.4.{tee, baz as bar, lorem, ipsum, dolor as freq}

as can be used as an identifier

import com.example.foo@1.0.4.{as as as}
import com.example.foo@1.0.4.{as as As}
import com.example.foo@1.0.4.{As as as}
import com.example.foo@1.0.4.{one, as as as, two as three}

AST changes are backwards compatible

  • types list will still have the list of names that are being imported, even though it's aliased (tentative, depending on not-doing this is breaking change or not)
import com.example.foo@1.0.4.{baz as bar}

will produce

"imports": [
    {
      "$class": "concerto.metamodel@1.0.0.ImportTypes",
      "namespace": "com.example.foo@1.0.4",
      "types": [
        "baz"
      ],
     "aliasedTypes":[
         {
            "$class": "concerto.metamodel@1.0.0.AliasedType",
            "name": "baz",
            "aliasedName": "bar"
         }
     ]
    }
  ]
  • If no aliased types are defined, aliasedTypes will not be present.
import com.example.foo@1.0.4.{baz}

will produce

"imports": [
    {
      "$class": "concerto.metamodel@1.0.0.ImportTypes",
      "namespace": "com.example.foo@1.0.4",
      "types": [
        "baz"
      ]
    }
  ]

Cannot alias to primitive types

Incorrect

import com.example.foo@1.0.0.{AString as String}

Cannot alias to keywords (for Concerto v4)

Incorrect

import com.example.foo@1.0.4.{bar as map}
@ekarademir ekarademir added this to the Import Alias milestone May 29, 2024
@salujajaskeerat
Copy link
Contributor

salujajaskeerat commented May 29, 2024

I have prepared a rough design of grammar if we are going to follow the syntax in issue, also considering the metamodel changes, i.e. a separate concept for importAliasTypes.

The grammar is close to the existing grammar for the ImportTypes

AliasedIdentifier = name:$Identifier _ ('as' _ aliasedName:$Identifier _)? 
 {
	const result = {
				name,
				aliasedName
				};
	return result;
 }

commaSeparatedAliasedIdentifiers  = head:$AliasedIdentifier _ tail:(',' _ @$AliasedIdentifier _ )*
  {
    return [head, ...tail];
  }

ImportAliasTypes
  = ImportToken __ ns:QualifiedNamespaceDeclaration ".{" _ types:commaSeparatedAliasedIdentifiers _ "}" __ u:FromUri? {
    const result = {
          $class: "concerto.metamodel@1.0.0.ImportAliasTypes",
          namespace: ns,
          types,
      };
      u && (result.uri = u);
      return result;
  }

After parsing using the above, we will get a list of objects (object of type aliased imports) that contain the optional field aliasedName.

This could be one approach to the grammar design.

@salujajaskeerat
Copy link
Contributor

salujajaskeerat commented Jun 5, 2024

We have dropped the concept for the AliasedTypes in the metamodel and integrated it as an optional array in the ImportTypes concept.

Thus, we have dropped the grammar rules for the ImportAliasTypes and parsed aliased types in the ImportTypes under the new field aliasedTypes, while the non-aliased types fall under the types.

So, we now have the following structure.

imports:[
{
 '$class': 'concerto.metamodel@1.0.0.ImportTypes',
      namespace: '',
      types: [],
      aliasedTypes: []
 }
]

In Technology-wt, @mttrbrts discussed the backward compatibility of the ast-generated; when we split the types based on which are aliased and which are not, we are changing the definition of the old array types; thus, the ast-generated won't be backwards compatible if any type was aliased.

So the suggestion is to keep all the types (aliased or not) in the types array if possible to prevent breaking change in the ast-generated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants