Define a feature-parent
[yangtools.git] / parser / yang-parser-rfc7950 / src / main / java / org / opendaylight / yangtools / yang / parser / rfc7950 / repo / TextToIRTransformer.java
1 /*
2  * Copyright (c) 2020 PANTHEON.tech, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.parser.rfc7950.repo;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.util.concurrent.Futures;
12 import java.io.IOException;
13 import org.eclipse.jdt.annotation.NonNull;
14 import org.opendaylight.yangtools.yang.model.api.source.YangTextSource;
15 import org.opendaylight.yangtools.yang.model.repo.api.SchemaRepository;
16 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceRegistry;
17 import org.opendaylight.yangtools.yang.model.repo.spi.SchemaSourceTransformer;
18 import org.opendaylight.yangtools.yang.model.spi.source.YangIRSource;
19 import org.opendaylight.yangtools.yang.parser.api.YangSyntaxErrorException;
20 import org.opendaylight.yangtools.yang.parser.rfc7950.antlr.IRSupport;
21
22 @Beta
23 public final class TextToIRTransformer extends SchemaSourceTransformer<YangTextSource, YangIRSource> {
24     private TextToIRTransformer(final SchemaRepository provider, final SchemaSourceRegistry consumer) {
25         super(provider, YangTextSource.class, consumer, YangIRSource.class,
26             input -> Futures.immediateFuture(transformText(input)));
27     }
28
29     public static @NonNull TextToIRTransformer create(final SchemaRepository provider,
30             final SchemaSourceRegistry consumer) {
31         return new TextToIRTransformer(provider, consumer);
32     }
33
34     public static @NonNull YangIRSource transformText(final YangTextSource text)
35             throws YangSyntaxErrorException, IOException {
36         final var rootStatement = IRSupport.createStatement(YangStatementStreamSource.parseYangSource(text));
37         final var info = YangIRSourceInfoExtractor.forIR(rootStatement, text.sourceId());
38         return new YangIRSource(info.sourceId(), rootStatement, text.symbolicName());
39     }
40 }