blob: 9d78d130371173352945c6374d506ee61606fbd6 [file] [log] [blame]
Gaurav Agrawal88897632016-02-12 18:37:50 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2016-present Open Networking Laboratory
Gaurav Agrawal88897632016-02-12 18:37:50 +05303 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.yangutils.parser.impl;
18
Vinod Kumar S71cba682016-02-25 15:52:16 +053019import java.io.IOException;
Gaurav Agrawal88897632016-02-12 18:37:50 +053020import org.antlr.v4.runtime.ANTLRFileStream;
21import org.antlr.v4.runtime.ANTLRInputStream;
22import org.antlr.v4.runtime.CommonTokenStream;
23import org.antlr.v4.runtime.tree.ParseTree;
24import org.antlr.v4.runtime.tree.ParseTreeWalker;
25import org.onosproject.yangutils.datamodel.YangNode;
26import org.onosproject.yangutils.parser.YangUtilsParser;
27import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer;
28import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
29import org.onosproject.yangutils.parser.exceptions.ParserException;
30import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener;
31
Gaurav Agrawal88897632016-02-12 18:37:50 +053032/**
Bharat saraswald9822e92016-04-05 15:13:44 +053033 * Represents file parsing, parse tree creation and data model tree creation
Gaurav Agrawal88897632016-02-12 18:37:50 +053034 * corresponding to an input YANG file.
35 */
36public class YangUtilsParserManager implements YangUtilsParser {
37
38 @Override
39 public YangNode getDataModel(String yangFile) throws IOException, ParserException {
40
41 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053042 * Create a char stream that reads from YANG file. Throws an exception
43 * in case input YANG file is either null or non existent.
44 */
Vidyashree Rama74453712016-04-18 12:29:39 +053045 ANTLRInputStream input;
Gaurav Agrawal88897632016-02-12 18:37:50 +053046 try {
47 input = new ANTLRFileStream(yangFile);
48 } catch (IOException e) {
Vidyashree Rama74453712016-04-18 12:29:39 +053049 throw new ParserException("YANG file error : YANG file does not exist.");
Gaurav Agrawal88897632016-02-12 18:37:50 +053050 }
51
52 // Create a lexer that feeds off of input char stream.
53 GeneratedYangLexer lexer = new GeneratedYangLexer(input);
54
55 // Create a buffer of tokens pulled from the lexer.
56 CommonTokenStream tokens = new CommonTokenStream(lexer);
57
58 // Create a parser that feeds off the tokens buffer.
59 GeneratedYangParser parser = new GeneratedYangParser(tokens);
60
61 // Remove console error listener.
62 parser.removeErrorListeners();
63
64 // Create instance of customized error listener.
65 ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
66
67 // Add customized error listener to catch errors during parsing.
68 parser.addErrorListener(parseTreeErrorListener);
69
Gaurav Agrawala04483c2016-02-13 14:23:40 +053070 ParseTree tree;
Gaurav Agrawal88897632016-02-12 18:37:50 +053071
Gaurav Agrawala04483c2016-02-13 14:23:40 +053072 try {
73 // Begin parsing YANG file and generate parse tree.
74 tree = parser.yangfile();
75 } catch (ParserException parserException) {
Gaurav Agrawal88897632016-02-12 18:37:50 +053076 parserException.setFileName(yangFile);
77 throw parserException;
78 }
79
80 // Create a walker to walk the parse tree.
81 ParseTreeWalker walker = new ParseTreeWalker();
82
83 // Create a listener implementation class object.
84 TreeWalkListener treeWalker = new TreeWalkListener();
85
86 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053087 * Walk parse tree, provide call backs to methods in listener and build
88 * data model tree.
89 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053090 try {
91 walker.walk(treeWalker, tree);
92 } catch (ParserException listenerException) {
93 // TODO free incomplete data model tree.
Gaurav Agrawal88897632016-02-12 18:37:50 +053094 listenerException.setFileName(yangFile);
95 throw listenerException;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053096 } finally {
97 // TODO free parsable stack
Gaurav Agrawal88897632016-02-12 18:37:50 +053098 }
99
100 // Returns the Root Node of the constructed data model tree.
101 return treeWalker.getRootNode();
102 }
Gaurav Agrawal8a5af142016-06-15 13:58:01 +0530103}