added init blocks to antlr code generation

This commit is contained in:
overflowerror 2021-11-23 17:32:48 +01:00
parent 69b5babb91
commit ad5a6b33f5
3 changed files with 39 additions and 0 deletions

View file

@ -87,6 +87,13 @@ class AntlrGrammarGenerator extends AbstractAntlrGrammarWithActionsGenerator {
protected «grammarAccess.simpleName» getGrammarAccess() {
return grammarAccess;
}
«IF it.initBlock === null»
// no init block
«ELSE»
// init block
«JavaCodeUtils.getSource(it.initBlock.code)»
«ENDIF»
}
'''

View file

@ -25,6 +25,7 @@ import org.eclipse.xtext.EnumLiteralDeclaration;
import org.eclipse.xtext.EnumRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.InitBlock;
import org.eclipse.xtext.Keyword;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.RuleCall;
@ -39,6 +40,7 @@ import org.eclipse.xtext.xtext.generator.parser.antlr.AbstractAntlrGrammarWithAc
import org.eclipse.xtext.xtext.generator.parser.antlr.AntlrGrammarGenUtil;
import org.eclipse.xtext.xtext.generator.parser.antlr.AntlrOptions;
import org.eclipse.xtext.xtext.generator.parser.antlr.GrammarNaming;
import org.eclipse.xtext.xtext.generator.parser.antlr.JavaCodeUtils;
@Singleton
@SuppressWarnings("all")
@ -192,6 +194,25 @@ public class AntlrGrammarGenerator extends AbstractAntlrGrammarWithActionsGenera
_builder.append(" \t");
_builder.append("}");
_builder.newLine();
_builder.append(" \t");
_builder.newLine();
{
InitBlock _initBlock = it.getInitBlock();
boolean _tripleEquals = (_initBlock == null);
if (_tripleEquals) {
_builder.append(" \t");
_builder.append("// no init block");
_builder.newLine();
} else {
_builder.append(" \t");
_builder.append("// init block");
_builder.newLine();
_builder.append(" \t");
String _source = JavaCodeUtils.getSource(it.getInitBlock().getCode());
_builder.append(_source, " \t");
_builder.newLineIfNotEmpty();
}
}
_builder.newLine();
_builder.append("}");
_builder.newLine();

View file

@ -35,6 +35,8 @@ import org.eclipse.xtext.EnumRule;
import org.eclipse.xtext.Grammar;
import org.eclipse.xtext.GrammarUtil;
import org.eclipse.xtext.Group;
import org.eclipse.xtext.InitBlock;
import org.eclipse.xtext.JavaCode;
import org.eclipse.xtext.Parameter;
import org.eclipse.xtext.ParserRule;
import org.eclipse.xtext.RuleCall;
@ -59,6 +61,15 @@ public class FlattenedGrammarAccess {
Grammar grammar = names.getContextGrammar();
Grammar flattenedGrammar = copy(grammar);
flattenedGrammar.setName(grammar.getName());
if (grammar.getInitBlock() != null) {
InitBlock copiedBlock = copy(grammar.getInitBlock());
JavaCode copiedCode = copy(grammar.getInitBlock().getCode());
copiedCode.setSource(grammar.getInitBlock().getCode().getSource());
copiedBlock.setCode(copiedCode);
flattenedGrammar.setInitBlock(copiedBlock);
}
Map<RuleWithParameterValues, AbstractRule> origToCopy = new LinkedHashMap<>();
List<AbstractRule> copies = copyRuleStubs(names, origToCopy, filter.getRules(grammar),
filter.isDiscardRuleTypeRef());