Skip to content

Commit

Permalink
#15 and #16: tentative Ecore support and initial implementation of tr…
Browse files Browse the repository at this point in the history
…anspilation traces.
  • Loading branch information
alessiostalla committed Sep 12, 2022
1 parent 76352ca commit 4429c19
Show file tree
Hide file tree
Showing 5 changed files with 417 additions and 0 deletions.
1 change: 1 addition & 0 deletions Requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
antlr4-python3-runtime
pyecore; extra == 'ecore'
304 changes: 304 additions & 0 deletions pylasu/StrumentaLanguageSupport/StrumentaLanguageSupport.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,304 @@
"""Definition of meta model 'StrumentaLanguageSupport'."""
from functools import partial
import pyecore.ecore as Ecore
from pyecore.ecore import *


name = 'StrumentaLanguageSupport'
nsURI = 'https://strumenta.com/kolasu/v2'
nsPrefix = ''

eClass = EPackage(name=name, nsURI=nsURI, nsPrefix=nsPrefix)

eClassifiers = {}
getEClassifier = partial(Ecore.getEClassifier, searchspace=eClassifiers)
IssueType = EEnum('IssueType', literals=['LEXICAL', 'SYNTACTIC', 'SEMANTIC'])

IssueSeverity = EEnum('IssueSeverity', literals=['ERROR', 'WARNING', 'INFO'])


BigDecimal = EDataType('BigDecimal', instanceClassName='java.math.BigDecimal')

BigInteger = EDataType('BigInteger', instanceClassName='java.math.BigInteger')


class LocalDate(EObject, metaclass=MetaEClass):

year = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)
month = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)
dayOfMonth = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)

def __init__(self, *, year=None, month=None, dayOfMonth=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if year is not None:
self.year = year

if month is not None:
self.month = month

if dayOfMonth is not None:
self.dayOfMonth = dayOfMonth


class LocalTime(EObject, metaclass=MetaEClass):

hour = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)
minute = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)
second = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)
nanosecond = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)

def __init__(self, *, hour=None, minute=None, second=None, nanosecond=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if hour is not None:
self.hour = hour

if minute is not None:
self.minute = minute

if second is not None:
self.second = second

if nanosecond is not None:
self.nanosecond = nanosecond


class LocalDateTime(EObject, metaclass=MetaEClass):

date = EReference(ordered=True, unique=True, containment=True, derived=False)
time = EReference(ordered=True, unique=True, containment=True, derived=False)

def __init__(self, *, date=None, time=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if date is not None:
self.date = date

if time is not None:
self.time = time


class Point(EObject, metaclass=MetaEClass):

line = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)
column = EAttribute(eType=EInt, unique=True, derived=False, changeable=True)

def __init__(self, *, line=None, column=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if line is not None:
self.line = line

if column is not None:
self.column = column


class Position(EObject, metaclass=MetaEClass):

start = EReference(ordered=True, unique=True, containment=True, derived=False)
end = EReference(ordered=True, unique=True, containment=True, derived=False)

def __init__(self, *, start=None, end=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if start is not None:
self.start = start

if end is not None:
self.end = end


@abstract
class Origin(EObject, metaclass=MetaEClass):

def __init__(self):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()


@abstract
class Destination(EObject, metaclass=MetaEClass):

def __init__(self):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()


class Statement(EObject, metaclass=MetaEClass):

def __init__(self):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()


class Expression(EObject, metaclass=MetaEClass):

def __init__(self):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()


class EntityDeclaration(EObject, metaclass=MetaEClass):

def __init__(self):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()


class Issue(EObject, metaclass=MetaEClass):

type = EAttribute(eType=IssueType, unique=True, derived=False, changeable=True)
message = EAttribute(eType=EString, unique=True, derived=False, changeable=True)
severity = EAttribute(eType=IssueSeverity, unique=True, derived=False, changeable=True)
position = EReference(ordered=True, unique=True, containment=True, derived=False)

def __init__(self, *, type=None, message=None, severity=None, position=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if type is not None:
self.type = type

if message is not None:
self.message = message

if severity is not None:
self.severity = severity

if position is not None:
self.position = position


class PossiblyNamed(EObject, metaclass=MetaEClass):

name = EAttribute(eType=EString, unique=True, derived=False, changeable=True)

def __init__(self, *, name=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if name is not None:
self.name = name


class ReferenceByName(EObject, metaclass=MetaEClass):

name = EAttribute(eType=EString, unique=True, derived=False, changeable=True)
referenced = EReference(ordered=True, unique=True, containment=False, derived=False)

def __init__(self, *, name=None, referenced=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if name is not None:
self.name = name

if referenced is not None:
self.referenced = referenced


class Result(EObject, metaclass=MetaEClass):

root = EReference(ordered=True, unique=True, containment=True, derived=False)
issues = EReference(ordered=True, unique=True, containment=True, derived=False, upper=-1)

def __init__(self, *, root=None, issues=None):
# if kwargs:
# raise AttributeError('unexpected arguments: {}'.format(kwargs))

super().__init__()

if root is not None:
self.root = root

if issues:
self.issues.extend(issues)


class NodeDestination(Destination):

node = EReference(ordered=True, unique=True, containment=False, derived=False)

def __init__(self, *, node=None, **kwargs):

super().__init__(**kwargs)

if node is not None:
self.node = node


class TextFileDestination(Destination):

position = EReference(ordered=True, unique=True, containment=True, derived=False)

def __init__(self, *, position=None, **kwargs):

super().__init__(**kwargs)

if position is not None:
self.position = position


@abstract
class ASTNode(Origin):

position = EReference(ordered=True, unique=True, containment=True, derived=False)
origin = EReference(ordered=True, unique=True, containment=False, derived=False)
destination = EReference(ordered=True, unique=True, containment=True, derived=False)

def __init__(self, *, position=None, origin=None, destination=None, **kwargs):

super().__init__(**kwargs)

if position is not None:
self.position = position

if origin is not None:
self.origin = origin

if destination is not None:
self.destination = destination


class Named(PossiblyNamed):

name = EAttribute(eType=EString, unique=True, derived=False, changeable=True)

def __init__(self, *, name=None, **kwargs):

super().__init__(**kwargs)

if name is not None:
self.name = name
42 changes: 42 additions & 0 deletions pylasu/StrumentaLanguageSupport/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@

from .StrumentaLanguageSupport import getEClassifier, eClassifiers
from .StrumentaLanguageSupport import name, nsURI, nsPrefix, eClass
from .StrumentaLanguageSupport import BigDecimal, BigInteger, LocalDate, LocalTime, LocalDateTime, Point, Position, Origin, Destination, NodeDestination, TextFileDestination, ASTNode, Statement, Expression, EntityDeclaration, IssueType, IssueSeverity, Issue, PossiblyNamed, Named, ReferenceByName, Result


from . import StrumentaLanguageSupport

__all__ = ['BigDecimal', 'BigInteger', 'LocalDate', 'LocalTime', 'LocalDateTime', 'Point', 'Position', 'Origin', 'Destination', 'NodeDestination', 'TextFileDestination',
'ASTNode', 'Statement', 'Expression', 'EntityDeclaration', 'IssueType', 'IssueSeverity', 'Issue', 'PossiblyNamed', 'Named', 'ReferenceByName', 'Result']

eSubpackages = []
eSuperPackage = None
StrumentaLanguageSupport.eSubpackages = eSubpackages
StrumentaLanguageSupport.eSuperPackage = eSuperPackage

LocalDateTime.date.eType = LocalDate
LocalDateTime.time.eType = LocalTime
Position.start.eType = Point
Position.end.eType = Point
NodeDestination.node.eType = ASTNode
TextFileDestination.position.eType = Position
ASTNode.position.eType = Position
ASTNode.origin.eType = Origin
ASTNode.destination.eType = Destination
Issue.position.eType = Position
# TODO eGenericType not supported ReferenceByName.referenced.eType =
# TODO eGenericType not supported
Result.root.eType = ASTNode
Result.issues.eType = Issue

otherClassifiers = [BigDecimal, BigInteger, IssueType, IssueSeverity]

for classif in otherClassifiers:
eClassifiers[classif.name] = classif
classif.ePackage = eClass

for classif in eClassifiers.values():
eClass.eClassifiers.append(classif.eClass)

for subpack in eSubpackages:
eClass.eSubpackages.append(subpack.eClass)
30 changes: 30 additions & 0 deletions pylasu/playground/transpilation_trace.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from pyecore.ecore import *

from pylasu.StrumentaLanguageSupport import Result, Issue

TRANSPILATION_METAMODEL = EPackage(
name="StrumentaLanguageSupportTranspilation", nsURI="https://strumenta.com/kolasu/transpilation/v1")


class TranspilationTrace(EObject, metaclass=MetaEClass):
original_code: EAttribute(name="originalCode", eType=EString)
source_result = EReference(name="sourceResult", containment=True, eType=Result)
target_result = EReference(name="targetResult", containment=True, eType=Result)
generated_code: EAttribute(name="generatedCode", eType=EString)
issues = EReference(containment=True, eType=Issue, upper=-1)

def __init__(self, *, original_code=None, source_result=None, target_result=None, generated_code=None, issues=None):
super().__init__()
if original_code is not None:
self.original_code = original_code
if source_result is not None:
self.source_result = source_result
if target_result is not None:
self.target_result = target_result
if generated_code is not None:
self.generated_code = generated_code
if issues:
self.issues.extend(issues)


TRANSPILATION_METAMODEL.eContents.append(TranspilationTrace)
Loading

0 comments on commit 4429c19

Please sign in to comment.