Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 0f7908b | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 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; |
Bharat saraswal | 8beac34 | 2016-08-04 02:00:03 +0530 | [diff] [blame] | 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 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 34 | * Represents file parsing, parse tree creation and data model tree creation |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 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 | */ |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 46 | ANTLRInputStream input; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 47 | try { |
| 48 | input = new ANTLRFileStream(yangFile); |
| 49 | } catch (IOException e) { |
Bharat saraswal | e3175d3 | 2016-08-31 17:50:11 +0530 | [diff] [blame] | 50 | throw new ParserException("YANG file error : YANG file does not exist. " + yangFile); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | // Create a lexer that feeds off of input char stream. |
| 54 | GeneratedYangLexer lexer = new GeneratedYangLexer(input); |
| 55 | |
| 56 | // Create a buffer of tokens pulled from the lexer. |
| 57 | CommonTokenStream tokens = new CommonTokenStream(lexer); |
| 58 | |
| 59 | // Create a parser that feeds off the tokens buffer. |
| 60 | GeneratedYangParser parser = new GeneratedYangParser(tokens); |
| 61 | |
| 62 | // Remove console error listener. |
| 63 | parser.removeErrorListeners(); |
| 64 | |
| 65 | // Create instance of customized error listener. |
| 66 | ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener(); |
| 67 | |
| 68 | // Add customized error listener to catch errors during parsing. |
| 69 | parser.addErrorListener(parseTreeErrorListener); |
| 70 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 71 | ParseTree tree; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 72 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 73 | try { |
| 74 | // Begin parsing YANG file and generate parse tree. |
| 75 | tree = parser.yangfile(); |
| 76 | } catch (ParserException parserException) { |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 77 | parserException.setFileName(yangFile); |
| 78 | throw parserException; |
| 79 | } |
| 80 | |
| 81 | // Create a walker to walk the parse tree. |
| 82 | ParseTreeWalker walker = new ParseTreeWalker(); |
| 83 | |
| 84 | // Create a listener implementation class object. |
| 85 | TreeWalkListener treeWalker = new TreeWalkListener(); |
Bharat saraswal | e304c25 | 2016-08-16 20:56:20 +0530 | [diff] [blame] | 86 | treeWalker.setFileName(yangFile); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 87 | /** |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 88 | * Walk parse tree, provide call backs to methods in listener and build |
| 89 | * data model tree. |
| 90 | */ |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 91 | try { |
| 92 | walker.walk(treeWalker, tree); |
| 93 | } catch (ParserException listenerException) { |
| 94 | // TODO free incomplete data model tree. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 95 | listenerException.setFileName(yangFile); |
| 96 | throw listenerException; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 97 | } finally { |
| 98 | // TODO free parsable stack |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | // Returns the Root Node of the constructed data model tree. |
| 102 | return treeWalker.getRootNode(); |
| 103 | } |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 104 | } |