parserImport XOCL; /****************************************************************************** * * * Extensions to Classifier * * ------------------------ * * * * Classifiers may have grammars attached to them. The idea is that the * * classifier provides program support for the language construct defined by * * the grammar. In particular, a class + a grammar defines a syntax construct* * where the class often plays the part of an abstract syntax tree component.* * * * Usually, a new construct is defined as @C ... end where the grammar of * * class C defines how to process the syntax between C and end. The stats of * * class C defines how to represent the language construct and the operations* * of C define how to process the new construct. In particular C is likely * * to define how to compile and evaluate the new construct. * * * * This file extends Classifier with the machinery to add grammars. * * * ******************************************************************************/ import Parser::BNF; Classifier.shadowOperation( // The grammar of a classifier can be set by adding the // grammar to the classifier object via 'add'. If this // file is loaded then the default 'add' is overridden // to filter out the grammars... @Operation add(namedElement:NamedElement):Classifier if namedElement.isKindOf(Grammar) then self.addGrammar(namedElement); super(namedElement) else super(namedElement) end end); context Classifier // Add an operation to add grammars to a classifier... @Operation addGrammar(grammar:Grammar) grammar.owner := self; grammar.startingSymbol := name.name; self.grammar := grammar end context Classifier // Find the grammar for a classifier... @Operation grammar():Grammar // This should add in the grammars for all the parent // classifiers. Don't override any grammars that are // already a parent... grammar end