All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.textmapper.tool.templates.ast_text.ltp Maven / Gradle / Ivy

There is a newer version: 0.10.0
Show newest version
${template unit-}

// Ast

${foreach nterm in parser.symbols.select(i|!i.isTerm())-}
${self->nonterm(nterm)-}
${end-}
${foreach cl in ast.classifiers-}
${self->ast_classifier(cl)-}
${end-}
${end}


${template nonterm(nterm)-}
${foreach cl in nterm->nontermClasses()-}
${self->ast_classifier(cl)-}
${end-}
${nterm.name} ${if nterm.type is LiRawAstType}(${nterm.type})${else} : ${nterm.type.toString()}${end-}
${if nterm.definition is LiRootRhsChoice} ::=
	${foreach rule in nterm.definition.parts separator '\n  | '}${rule->rhsPart(100)}${end}
;
${else}
	${nterm.definition->rhsPart(100)} ;
${end}
${end}

${query nontermClasses() =
	result = self.type is LiAstClass || self.type is LiAstEnum ? [self.type] : [],
	rhsRes = self.definition->rhsClasses(),
	[result, rhsRes].collect(it|it) }

${query rhsClasses() =
	self is LiRootRhsChoice || self is LiRhsChoice || self is LiRhsUnordered ? self.parts.collect(p|p->rhsClasses()) :
	self is LiRhsList ? self.element->rhsClasses() :
	self is LiRhsSequence && self.mapping && (self.type is LiAstClass || self.type is LiAstEnum) ? [[self.type], self.parts.collect(p|p->rhsClasses())].collect(it|it) :
	[]
}


${query isSequenceWOMapping() = !self.mapping || !self.mapping->hasValue() && !self.mapping.field && !self.type && !self.mapping.isAddition()}

${query rhsPart(prio) =
	self is LiRhsSequence && !self->isSequenceWOMapping()
		? self.mapping->rhsMapping((self.type ? 'new.' +self.type.toString() + (prio<100?'':': ') : '')
				+ self.parts.collect(it|it->rhsPart(90))->util.join(' ')->paren(prio < 100)) :
	self is LiRhsChoice
		? self.parts.collect(it|it->rhsPart(100))->util.join(' | ')->paren(true) :
	self is LiRhsSequence
		? self.parts.collect(it|it->rhsPart(90))->util.join(' ')->paren(prio < 90) :
	self is LiRhsUnordered
		? self.parts.collect(it|it->rhsPart(80))->util.join(' & ')->paren(prio < 80) :
	self is LiRhsAssignment
		? self.part->rhsPart(50)->paren(prio < 50) :
	self is LiRhsOptional
		? self.part->rhsPart(40)->paren(prio < 40) + '?' :
	self is LiRhsCast
		? self.part->rhsPart(30)->paren(prio < 30) :
	self is LiRhsList
		? self->rhsList() :
	self is LiRhsSymbol
		? (self.mapping ? self.mapping->rhsMapping(self.target.nameText) : self.target.nameText) :
	'/* ERROR */'
}

${query hasValue() = self.value || self.value is Boolean || self.value is String}

${query rhsMapping(defaultText) =
	self.field && self.field.name == defaultText && !self.isAddition() && !self->hasValue() ? '{' + defaultText + '}' :
	(self.field ? self.field.name  : 'this') +
	(self.isAddition() ? '+=' : '=') +
	(self->hasValue() ? defaultText + ' as ' + (self.value is LiAstEnumMember ? value.containingEnum.name + '.' + value.name : value) : defaultText)
}

${query paren(condition) = condition ? '(' + self + ')' : self}

${query rhsList() =
	self.customInitialElement ? (
		nonEmptyInitial = customInitialElement.parts.size() != 0,
        (self.isRightRecursive() ? (
            '(' + self.element->rhsPart(90) + (self.getSeparator() ? '\n\t\t' + self.getSeparator()->rhsPart(90) : '') + ' /rr)*' +
            (nonEmptyInitial ? '\n\t\t' + self.customInitialElement->rhsPart(90) : '')
        ) : (
            (nonEmptyInitial ? self.customInitialElement->rhsPart(90) + '\n\t\t' : '') +
            '(' + (self.getSeparator() ? self.getSeparator()->rhsPart(90) + '\n\t\t' : '') + self.element->rhsPart(90) + ')*'
        ))
	) : (
		'(' + self.element->rhsPart(90) + (self.getSeparator() ? '\n\t\tseparator ' + self.getSeparator()->rhsPart(90) : '')
				+ (self.isRightRecursive() ? '/rr' : '') + ')' + (self.isNonEmpty() ? '+' : '*')
	) }

${cached query innerName() =
	(self.containingClass ? self.containingClass->innerName() + '.' : '' ) + self.name}

${query ast_classifier(cl) =
	cl->innerName() != util.uniqueId(cl->innerName(), '__asttext__') ? '' :
	cl is LiAstEnum ? self->ast_enum(cl) : self->ast_class(cl)
}

${template baseClasses(cl)-}
${if cl.super && cl.super.length > 0-}
 : ${foreach impl in cl.super separator ', '}${impl.name}${end-}
${end-}
${end}

${template ast_enum(en)-}
enum ${en.name} {
	${foreach member in en.members separator ',\n\t'}${member.name}${end}
}

${end}

${template ast_class(cl)-}
${cl.isInterface() ?'interface':'class'} ${cl.name}${call baseClasses(cl)} {
${foreach prop in cl.fields-}
	${prop.type} ${prop.name};
${end-}
${foreach cl in cl.inner-}
${util.shiftRight(self->ast_classifier(cl), 1)-}
${end-}
}

${end}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy