2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.impl.leafref;
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;
20 final class LeafRefPathParserImpl {
21 private final SchemaContext schemaContext;
22 private final Module module;
23 private final SchemaNode node;
25 public LeafRefPathParserImpl(final SchemaContext schemaContext, final Module currentModule, final SchemaNode currentNode) {
26 this.schemaContext = schemaContext;
27 this.module = currentModule;
28 this.node = currentNode;
31 public LeafRefPath parseLeafRefPathSourceToSchemaPath(final InputStream stream) throws IOException, LeafRefYangSyntaxErrorException {
33 final Path_argContext pathCtx = parseLeafRefPathSource(stream);
35 final ParseTreeWalker walker = new ParseTreeWalker();
36 final LeafRefPathParserListenerImpl leafRefPathParserListenerImpl = new LeafRefPathParserListenerImpl(schemaContext, module, node);
37 walker.walk(leafRefPathParserListenerImpl,pathCtx);
39 final LeafRefPath leafRefPath = leafRefPathParserListenerImpl.getLeafRefPath();
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();
51 final LeafRefPathErrorListener errorListener = new LeafRefPathErrorListener(module);
52 parser.addErrorListener(errorListener);
54 final Path_argContext result = parser.path_arg();
55 errorListener.validate();