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; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 20 | import org.antlr.v4.runtime.ANTLRFileStream; |
| 21 | import org.antlr.v4.runtime.ANTLRInputStream; |
| 22 | import org.antlr.v4.runtime.CommonTokenStream; |
| 23 | import org.antlr.v4.runtime.tree.ParseTree; |
| 24 | import org.antlr.v4.runtime.tree.ParseTreeWalker; |
| 25 | import org.onosproject.yangutils.datamodel.YangNode; |
| 26 | import org.onosproject.yangutils.parser.YangUtilsParser; |
| 27 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer; |
| 28 | import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser; |
| 29 | import org.onosproject.yangutils.parser.exceptions.ParserException; |
| 30 | import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener; |
| 31 | |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 32 | /** |
Bharat saraswal | 63f26fb | 2016-04-05 15:13:44 +0530 | [diff] [blame] | 33 | * Represents file parsing, parse tree creation and data model tree creation |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 34 | * corresponding to an input YANG file. |
| 35 | */ |
| 36 | public class YangUtilsParserManager implements YangUtilsParser { |
| 37 | |
| 38 | @Override |
| 39 | public YangNode getDataModel(String yangFile) throws IOException, ParserException { |
| 40 | |
| 41 | /** |
Vinod Kumar S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 42 | * 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 Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 45 | ANTLRInputStream input; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 46 | try { |
| 47 | input = new ANTLRFileStream(yangFile); |
| 48 | } catch (IOException e) { |
Vidyashree Rama | 02f115f | 2016-04-18 12:29:39 +0530 | [diff] [blame] | 49 | throw new ParserException("YANG file error : YANG file does not exist."); |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 50 | } |
| 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 Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 70 | ParseTree tree; |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 71 | |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 72 | try { |
| 73 | // Begin parsing YANG file and generate parse tree. |
| 74 | tree = parser.yangfile(); |
| 75 | } catch (ParserException parserException) { |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 76 | 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 S | 48848f7 | 2016-02-25 15:52:16 +0530 | [diff] [blame] | 87 | * Walk parse tree, provide call backs to methods in listener and build |
| 88 | * data model tree. |
| 89 | */ |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 90 | try { |
| 91 | walker.walk(treeWalker, tree); |
| 92 | } catch (ParserException listenerException) { |
| 93 | // TODO free incomplete data model tree. |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 94 | listenerException.setFileName(yangFile); |
| 95 | throw listenerException; |
Gaurav Agrawal | 2737d2a | 2016-02-13 14:23:40 +0530 | [diff] [blame] | 96 | } finally { |
| 97 | // TODO free parsable stack |
Gaurav Agrawal | 91c6961 | 2016-02-12 18:37:50 +0530 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | // Returns the Root Node of the constructed data model tree. |
| 101 | return treeWalker.getRootNode(); |
| 102 | } |
Gaurav Agrawal | bfce934 | 2016-06-15 13:58:01 +0530 | [diff] [blame] | 103 | } |