blob: a1fffb825357ffe81a4dda23bbe0832de73750b5 [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;
20
Gaurav Agrawal88897632016-02-12 18:37:50 +053021import org.antlr.v4.runtime.ANTLRFileStream;
22import org.antlr.v4.runtime.ANTLRInputStream;
23import org.antlr.v4.runtime.CommonTokenStream;
24import org.antlr.v4.runtime.tree.ParseTree;
25import org.antlr.v4.runtime.tree.ParseTreeWalker;
26import org.onosproject.yangutils.datamodel.YangNode;
27import org.onosproject.yangutils.parser.YangUtilsParser;
28import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer;
29import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
30import org.onosproject.yangutils.parser.exceptions.ParserException;
31import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener;
32
Gaurav Agrawal88897632016-02-12 18:37:50 +053033/**
Bharat saraswald9822e92016-04-05 15:13:44 +053034 * Represents file parsing, parse tree creation and data model tree creation
Gaurav Agrawal88897632016-02-12 18:37:50 +053035 * corresponding to an input YANG file.
36 */
37public class YangUtilsParserManager implements YangUtilsParser {
38
39 @Override
40 public YangNode getDataModel(String yangFile) throws IOException, ParserException {
41
42 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053043 * Create a char stream that reads from YANG file. Throws an exception
44 * in case input YANG file is either null or non existent.
45 */
Gaurav Agrawal88897632016-02-12 18:37:50 +053046 ANTLRInputStream input = null;
47 try {
48 input = new ANTLRFileStream(yangFile);
49 } catch (IOException e) {
50 e.printStackTrace();
51 throw e;
52 }
53
54 // Create a lexer that feeds off of input char stream.
55 GeneratedYangLexer lexer = new GeneratedYangLexer(input);
56
57 // Create a buffer of tokens pulled from the lexer.
58 CommonTokenStream tokens = new CommonTokenStream(lexer);
59
60 // Create a parser that feeds off the tokens buffer.
61 GeneratedYangParser parser = new GeneratedYangParser(tokens);
62
63 // Remove console error listener.
64 parser.removeErrorListeners();
65
66 // Create instance of customized error listener.
67 ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
68
69 // Add customized error listener to catch errors during parsing.
70 parser.addErrorListener(parseTreeErrorListener);
71
Gaurav Agrawala04483c2016-02-13 14:23:40 +053072 ParseTree tree;
Gaurav Agrawal88897632016-02-12 18:37:50 +053073
Gaurav Agrawala04483c2016-02-13 14:23:40 +053074 try {
75 // Begin parsing YANG file and generate parse tree.
76 tree = parser.yangfile();
77 } catch (ParserException parserException) {
Gaurav Agrawal88897632016-02-12 18:37:50 +053078 parserException.setFileName(yangFile);
79 throw parserException;
80 }
81
82 // Create a walker to walk the parse tree.
83 ParseTreeWalker walker = new ParseTreeWalker();
84
85 // Create a listener implementation class object.
86 TreeWalkListener treeWalker = new TreeWalkListener();
87
88 /**
Vinod Kumar S71cba682016-02-25 15:52:16 +053089 * Walk parse tree, provide call backs to methods in listener and build
90 * data model tree.
91 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053092 try {
93 walker.walk(treeWalker, tree);
94 } catch (ParserException listenerException) {
95 // TODO free incomplete data model tree.
Gaurav Agrawal88897632016-02-12 18:37:50 +053096 listenerException.setFileName(yangFile);
97 throw listenerException;
Gaurav Agrawala04483c2016-02-13 14:23:40 +053098 } finally {
99 // TODO free parsable stack
Gaurav Agrawal88897632016-02-12 18:37:50 +0530100 }
101
102 // Returns the Root Node of the constructed data model tree.
103 return treeWalker.getRootNode();
104 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530105}