f7f316b752536a8c9c5f97921f86e87efcfd03ec
[yangtools.git] / yang / yang-data-impl / src / main / java / org / opendaylight / yangtools / yang / data / impl / leafref / LeafRefPathParserImpl.java
1 /**
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.data.impl.leafref;
9
10 import java.io.IOException;
11 import java.io.InputStream;
12 import org.antlr.v4.runtime.CharStreams;
13 import org.antlr.v4.runtime.CommonTokenStream;
14 import org.antlr.v4.runtime.tree.ParseTreeWalker;
15 import org.opendaylight.yangtools.yang.data.impl.leafref.LeafRefPathParser.Path_argContext;
16 import org.opendaylight.yangtools.yang.model.api.Module;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import org.opendaylight.yangtools.yang.model.api.SchemaNode;
19
20 final class LeafRefPathParserImpl {
21     private final SchemaContext schemaContext;
22     private final Module module;
23     private final SchemaNode node;
24
25     LeafRefPathParserImpl(final SchemaContext schemaContext, final Module currentModule, final SchemaNode currentNode) {
26         this.schemaContext = schemaContext;
27         this.module = currentModule;
28         this.node = currentNode;
29     }
30
31     public LeafRefPath parseLeafRefPathSourceToSchemaPath(final InputStream stream) throws IOException,
32             LeafRefYangSyntaxErrorException {
33         final Path_argContext pathCtx = parseLeafRefPathSource(stream);
34
35         final ParseTreeWalker walker = new ParseTreeWalker();
36         final LeafRefPathParserListenerImpl leafRefPathParserListenerImpl = new LeafRefPathParserListenerImpl(
37             schemaContext, module, node);
38         walker.walk(leafRefPathParserListenerImpl, pathCtx);
39
40         return leafRefPathParserListenerImpl.getLeafRefPath();
41     }
42
43     private Path_argContext parseLeafRefPathSource(final InputStream stream) throws IOException,
44             LeafRefYangSyntaxErrorException {
45         final LeafRefPathLexer lexer = new LeafRefPathLexer(CharStreams.fromStream(stream));
46         final CommonTokenStream tokens = new CommonTokenStream(lexer);
47         final LeafRefPathParser parser = new LeafRefPathParser(tokens);
48         parser.removeErrorListeners();
49
50         final LeafRefPathErrorListener errorListener = new LeafRefPathErrorListener(module);
51         parser.addErrorListener(errorListener);
52
53         final Path_argContext result = parser.path_arg();
54         errorListener.validate();
55         return result;
56     }
57 }