Do not instantiate new maps in LeafRefContextBuilder
[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.ANTLRInputStream;
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      public 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, LeafRefYangSyntaxErrorException {
32
33         final Path_argContext pathCtx = parseLeafRefPathSource(stream);
34
35         final ParseTreeWalker walker = new ParseTreeWalker();
36         final LeafRefPathParserListenerImpl leafRefPathParserListenerImpl = new LeafRefPathParserListenerImpl(schemaContext, module, node);
37         walker.walk(leafRefPathParserListenerImpl,pathCtx);
38
39         final LeafRefPath leafRefPath = leafRefPathParserListenerImpl.getLeafRefPath();
40
41         return leafRefPath;
42     }
43
44
45     private Path_argContext parseLeafRefPathSource(final InputStream stream) throws IOException, LeafRefYangSyntaxErrorException {
46         final LeafRefPathLexer lexer = new LeafRefPathLexer(new ANTLRInputStream(stream));
47         final CommonTokenStream tokens = new CommonTokenStream(lexer);
48         final LeafRefPathParser parser = new LeafRefPathParser(tokens);
49         parser.removeErrorListeners();
50
51         final LeafRefPathErrorListener errorListener = new LeafRefPathErrorListener(module);
52         parser.addErrorListener(errorListener);
53
54         final Path_argContext result = parser.path_arg();
55         errorListener.validate();
56
57         return result;
58     }
59
60 }