Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 Open Networking Laboratory |
| 3 | * |
| 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 | |
| 17 | package org.onosproject.yangutils.parser.impl; |
| 18 | |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 19 | import java.io.IOException; |
| 20 | |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 21 | import org.antlr.v4.runtime.ANTLRFileStream; |
| 22 | import org.antlr.v4.runtime.ANTLRInputStream; |
| 23 | import org.antlr.v4.runtime.CommonTokenStream; |
| 24 | import org.antlr.v4.runtime.tree.ParseTree; |
| 25 | import org.antlr.v4.runtime.tree.ParseTreeWalker; |
| 26 | import org.onosproject.yangutils.datamodel.YangNode; |
| 27 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 28 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer; |
| 29 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 30 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 31 | import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener; |
| 32 | |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 33 | /** |
| 34 | * Manages file parsing, parse tree creation and data model tree creation |
| 35 | * corresponding to an input YANG file. |
| 36 | */ |
| 37 | public class YangUtilsParserManager implements YangUtilsParser { |
| 38 | |
| 39 | @Override |
| 40 | public YangNode getDataModel(String yangFile) throws IOException, ParserException { |
| 41 | |
| 42 | /** |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 43 | * 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 Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 46 | 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 Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 72 | ParseTree tree; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 73 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 74 | try { |
| 75 | // Begin parsing YANG file and generate parse tree. |
| 76 | tree = parser.yangfile(); |
| 77 | } catch (ParserException parserException) { |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 78 | 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 S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 89 | * Walk parse tree, provide call backs to methods in listener and build |
| 90 | * data model tree. |
| 91 | */ |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 92 | try { |
| 93 | walker.walk(treeWalker, tree); |
| 94 | } catch (ParserException listenerException) { |
| 95 | // TODO free incomplete data model tree. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 96 | listenerException.setFileName(yangFile); |
| 97 | throw listenerException; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 98 | } finally { |
| 99 | // TODO free parsable stack |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // Returns the Root Node of the constructed data model tree. |
| 103 | return treeWalker.getRootNode(); |
| 104 | } |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 105 | } |