use of org.eclipse.xtext.xtext.FlattenedGrammarAccess in project xtext-core by eclipse.
the class GrammarPDAProvider method getGrammarPDAs.
@Override
public SerializationContextMap<Pda<ISerState, RuleCall>> getGrammarPDAs(Grammar grammar) {
RuleNames names = RuleNames.getRuleNames(grammar, true);
RuleFilter filter = new RuleFilter();
filter.setDiscardTerminalRules(true);
filter.setDiscardUnreachableRules(false);
filter.setDiscardRuleTypeRef(false);
Grammar flattened = new FlattenedGrammarAccess(names, filter).getFlattenedGrammar();
Builder<Pda<ISerState, RuleCall>> result = SerializationContextMap.<Pda<ISerState, RuleCall>>builder();
for (ParserRule rule : GrammarUtil.allParserRules(flattened)) {
RuleWithParameterValues withParams = RuleWithParameterValues.findInEmfObject(rule);
AbstractRule original = withParams.getOriginal();
if (original instanceof ParserRule && isValidRule((ParserRule) original)) {
ISerializationContext context = createContext((ParserRule) original, withParams.getParamValues());
try {
Pda<ISerState, RuleCall> pda = createPDA(grammar, rule);
result.put(context, pda);
} catch (Exception e) {
LOG.error("Error creating PDA for context '" + context + "': " + e.getMessage(), e);
}
}
}
return result.create();
}
use of org.eclipse.xtext.xtext.FlattenedGrammarAccess in project xtext-core by eclipse.
the class XtextAntlrGeneratorFragment2 method initNameMappings.
/**
* @since 2.14
*/
protected StringConcatenationClient initNameMappings(final Grammar it) {
StringConcatenationClient _xblockexpression = null;
{
final RuleFilter filter = new RuleFilter();
filter.setDiscardUnreachableRules(this.getOptions().isSkipUnusedRules());
final RuleNames ruleNames = RuleNames.getRuleNames(it, true);
final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
final Set<AbstractElement> seenElements = CollectionLiterals.<AbstractElement>newHashSet();
Collection<? extends AbstractElement> _allAlternatives = GrammarUtil.getAllAlternatives(flattened);
Collection<? extends AbstractElement> _allGroups = GrammarUtil.getAllGroups(flattened);
Iterable<AbstractElement> _plus = Iterables.<AbstractElement>concat(_allAlternatives, _allGroups);
Collection<? extends AbstractElement> _allAssignments = GrammarUtil.getAllAssignments(flattened);
Iterable<AbstractElement> _plus_1 = Iterables.<AbstractElement>concat(_plus, _allAssignments);
Collection<? extends AbstractElement> _allUnorderedGroups = GrammarUtil.getAllUnorderedGroups(flattened);
final Function1<AbstractElement, Boolean> _function = (AbstractElement it_1) -> {
return Boolean.valueOf(seenElements.add(AntlrGrammarGenUtil.<AbstractElement>getOriginalElement(it_1)));
};
final List<AbstractElement> elements = IterableExtensions.<AbstractElement>toList(IterableExtensions.<AbstractElement>filter(Iterables.<AbstractElement>filter(Iterables.<AbstractElement>concat(_plus_1, _allUnorderedGroups), AbstractElement.class), _function));
final List<List<AbstractElement>> partitions = Lists.<AbstractElement>partition(elements, 2500);
StringConcatenationClient _client = new StringConcatenationClient() {
@Override
protected void appendTo(StringConcatenationClient.TargetStringConcatenation _builder) {
_builder.append("@");
_builder.append(Singleton.class);
_builder.newLineIfNotEmpty();
_builder.append("public static final class NameMappings {");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
{
int _size = partitions.size();
boolean _greaterThan = (_size > 1);
if (_greaterThan) {
{
Iterable<Pair<Integer, List<AbstractElement>>> _indexed = IterableExtensions.<List<AbstractElement>>indexed(partitions);
for (final Pair<Integer, List<AbstractElement>> partition : _indexed) {
_builder.append("\t");
_builder.append("private static final class Init");
Integer _key = partition.getKey();
_builder.append(_key, "\t");
_builder.append(" {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("private static void doInit(");
_builder.append(ImmutableMap.class, "\t\t");
_builder.append(".Builder<");
_builder.append(AbstractElement.class, "\t\t");
_builder.append(", ");
_builder.append(String.class, "\t\t");
_builder.append("> builder, ");
TypeReference _grammarAccess = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_grammarAccess, "\t\t");
_builder.append(" grammarAccess) {");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t\t");
StringConcatenationClient _initNameMappings = XtextAntlrGeneratorFragment2.this.initNameMappings(partition.getValue());
_builder.append(_initNameMappings, "\t\t\t");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
}
}
}
}
_builder.append("\t");
_builder.append("private final ");
_builder.append(Map.class, "\t");
_builder.append("<");
_builder.append(AbstractElement.class, "\t");
_builder.append(", ");
_builder.append(String.class, "\t");
_builder.append("> mappings;");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("@");
_builder.append(Inject.class, "\t");
_builder.newLineIfNotEmpty();
_builder.append("\t");
_builder.append("public NameMappings(");
TypeReference _grammarAccess_1 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_grammarAccess_1, "\t");
_builder.append(" grammarAccess) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append(ImmutableMap.class, "\t\t");
_builder.append(".Builder<");
_builder.append(AbstractElement.class, "\t\t");
_builder.append(", ");
_builder.append(String.class, "\t\t");
_builder.append("> builder = ");
_builder.append(ImmutableMap.class, "\t\t");
_builder.append(".builder();");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("init(builder, grammarAccess);");
_builder.newLine();
_builder.append("\t\t");
_builder.append("this.mappings = builder.build();");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("public ");
_builder.append(String.class, "\t");
_builder.append(" getRuleName(");
_builder.append(AbstractElement.class, "\t");
_builder.append(" element) {");
_builder.newLineIfNotEmpty();
_builder.append("\t\t");
_builder.append("return mappings.get(element);");
_builder.newLine();
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("\t");
_builder.newLine();
_builder.append("\t");
_builder.append("private static void init(");
_builder.append(ImmutableMap.class, "\t");
_builder.append(".Builder<");
_builder.append(AbstractElement.class, "\t");
_builder.append(", ");
_builder.append(String.class, "\t");
_builder.append("> builder, ");
TypeReference _grammarAccess_2 = XtextAntlrGeneratorFragment2.this.grammarUtil.getGrammarAccess(XtextAntlrGeneratorFragment2.this.getGrammar());
_builder.append(_grammarAccess_2, "\t");
_builder.append(" grammarAccess) {");
_builder.newLineIfNotEmpty();
{
int _size_1 = partitions.size();
boolean _greaterThan_1 = (_size_1 > 1);
if (_greaterThan_1) {
{
Iterable<Pair<Integer, List<AbstractElement>>> _indexed_1 = IterableExtensions.<List<AbstractElement>>indexed(partitions);
for (final Pair<Integer, List<AbstractElement>> partition_1 : _indexed_1) {
_builder.append("\t\t");
_builder.append("Init");
Integer _key_1 = partition_1.getKey();
_builder.append(_key_1, "\t\t");
_builder.append(".doInit(builder, grammarAccess);");
_builder.newLineIfNotEmpty();
}
}
} else {
{
for (final List<AbstractElement> partition_2 : partitions) {
_builder.append("\t\t");
StringConcatenationClient _initNameMappings_1 = XtextAntlrGeneratorFragment2.this.initNameMappings(partition_2);
_builder.append(_initNameMappings_1, "\t\t");
_builder.newLineIfNotEmpty();
}
}
}
}
_builder.append("\t");
_builder.append("}");
_builder.newLine();
_builder.append("}");
_builder.newLine();
_builder.newLine();
_builder.append("@");
_builder.append(Inject.class);
_builder.newLineIfNotEmpty();
_builder.append("private NameMappings nameMappings;");
_builder.newLine();
}
};
_xblockexpression = _client;
}
return _xblockexpression;
}
use of org.eclipse.xtext.xtext.FlattenedGrammarAccess in project xtext-core by eclipse.
the class AbstractAntlrGrammarGenerator method generate.
public void generate(final Grammar it, final AntlrOptions options, final IXtextGeneratorFileSystemAccess fsa) {
this.keywordHelper = KeywordHelper.getHelper(it);
this.originalGrammar = it;
final RuleFilter filter = new RuleFilter();
filter.setDiscardUnreachableRules(options.isSkipUnusedRules());
final RuleNames ruleNames = RuleNames.getRuleNames(it, true);
final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
boolean _isCombinedGrammar = this.isCombinedGrammar();
new CombinedGrammarMarker(_isCombinedGrammar).attachToEmfObject(flattened);
fsa.generateFile(this.getGrammarNaming().getParserGrammar(it).getGrammarFileName(), this.compileParser(flattened, options));
boolean _isCombinedGrammar_1 = this.isCombinedGrammar();
boolean _not = (!_isCombinedGrammar_1);
if (_not) {
fsa.generateFile(this.getGrammarNaming().getLexerGrammar(it).getGrammarFileName(), this.compileLexer(flattened, options));
}
}
use of org.eclipse.xtext.xtext.FlattenedGrammarAccess in project xtext-core by eclipse.
the class GrammarFlatteningTest method getModel.
public Grammar getModel(final String model, final boolean dropUnreachable) throws Exception {
EObject _model = super.getModel(model);
Grammar grammar = ((Grammar) _model);
RuleNames ruleNames = RuleNames.getRuleNames(grammar, false);
RuleFilter filter = new RuleFilter();
filter.setDiscardUnreachableRules(dropUnreachable);
Grammar result = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
XtextResource resource = this.<XtextResource>get(XtextResource.class);
resource.getContents().add(result);
resource.setURI(URI.createURI("synthetic://flattened.xtext"));
return result;
}
Aggregations