use of org.eclipse.xtext.xtext.RuleFilter in project xtext-eclipse by eclipse.
the class DefaultAntlrGrammarGenerator method generate.
public void generate(final Grammar it, final AntlrOptions options, final Xtend2ExecutionContext ctx) {
final RuleFilter filter = new RuleFilter();
final RuleNames ruleNames = RuleNames.getRuleNames(GrammarUtil.getGrammar(it), true);
final Grammar flattened = new FlattenedGrammarAccess(ruleNames, filter).getFlattenedGrammar();
String _name = this._xtextIDEAGeneratorExtensions.getSrcGenOutlet(ctx).getName();
String _asPath = this._naming.asPath(this.getGrammarFileName(it));
String _plus = (_asPath + ".g");
ctx.writeFile(_name, _plus, this.compile(flattened, options));
}
use of org.eclipse.xtext.xtext.RuleFilter in project xtext-eclipse by eclipse.
the class ParametersContentAssistParserSanityTest method methodNames.
/**
* Returns test data.
*/
@Parameters(name = "{0}")
public static Collection<Object[]> methodNames() {
try {
TwoParametersTestLanguageInjectorProvider injectorProvider = new TwoParametersTestLanguageInjectorProvider();
try {
injectorProvider.setupRegistry();
Injector injector = injectorProvider.getInjector();
DummyParser dummyParser = new DummyParser();
TwoParametersTestLanguageGrammarAccess grammarAccess = injector.getInstance(TwoParametersTestLanguageGrammarAccess.class);
RuleNames ruleNames = RuleNames.getRuleNames(grammarAccess.getGrammar(), false);
RuleFilter ruleFilter = new RuleFilter();
ruleFilter.setDiscardUnreachableRules(true);
FlattenedGrammarAccess flattened = new FlattenedGrammarAccess(ruleNames, ruleFilter);
Grammar flattenedGrammar = flattened.getFlattenedGrammar();
dummyParser.setGrammarAccess(grammarAccess);
dummyParser.setNameMappings(new TwoParametersTestLanguageParser.NameMappings(grammarAccess));
dummyParser.getRuleName(null);
Map<AbstractElement, String> nameMappings = new ReflectExtensions().get(dummyParser.getNameMappings(), "mappings");
List<String> methodNames = Lists.newArrayList(nameMappings.values());
Collections.sort(methodNames);
Set<String> flattenedRuleNames = Sets.newHashSet(Lists.transform(flattenedGrammar.getRules(), r -> r.getName()));
return Lists.transform(methodNames, (s) -> new Object[] { s, flattenedRuleNames });
} finally {
injectorProvider.restoreRegistry();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.eclipse.xtext.xtext.RuleFilter in project n4js by eclipse.
the class ContentAssistParserSanityTest method methodNames.
/**
* Returns test data.
*/
@Parameters(name = "{0}")
public static Collection<Object[]> methodNames() {
try {
N4JSInjectorProvider injectorProvider = new N4JSInjectorProvider();
Injector injector = injectorProvider.getInjector();
DummyParser dummyParser = new DummyParser();
N4JSGrammarAccess grammarAccess = injector.getInstance(N4JSGrammarAccess.class);
RuleNames ruleNames = RuleNames.getRuleNames(grammarAccess.getGrammar(), false);
RuleFilter ruleFilter = new RuleFilter();
ruleFilter.setDiscardUnreachableRules(true);
FlattenedGrammarAccess flattened = new FlattenedGrammarAccess(ruleNames, ruleFilter);
Grammar flattenedGrammar = flattened.getFlattenedGrammar();
dummyParser.setGrammarAccess(grammarAccess);
injectorProvider.restoreRegistry();
dummyParser.getRuleName(null);
Map<AbstractElement, String> nameMappings = new ReflectExtensions().get(dummyParser, "nameMappings");
List<String> methodNames = Lists.newArrayList(nameMappings.values());
Collections.sort(methodNames);
Set<String> flattenedRuleNames = Sets.newHashSet(Lists.transform(flattenedGrammar.getRules(), r -> r.getName()));
return Lists.transform(methodNames, (s) -> new Object[] { s, flattenedRuleNames });
} catch (Exception e) {
throw new RuntimeException(e);
}
}
use of org.eclipse.xtext.xtext.RuleFilter 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.RuleFilter 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();
}
Aggregations