blob: dcab33a1401e882e75d3fd09c60c625a07c5b8f6 [file] [log] [blame]
Gaurav Agrawal88897632016-02-12 18:37:50 +05301/*
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
17package org.onosproject.yangutils.parser.impl;
18
19import org.antlr.v4.runtime.ANTLRFileStream;
20import org.antlr.v4.runtime.ANTLRInputStream;
21import org.antlr.v4.runtime.CommonTokenStream;
22import org.antlr.v4.runtime.tree.ParseTree;
23import org.antlr.v4.runtime.tree.ParseTreeWalker;
24import org.onosproject.yangutils.datamodel.YangNode;
25import org.onosproject.yangutils.parser.YangUtilsParser;
26import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangLexer;
27import org.onosproject.yangutils.parser.antlrgencode.GeneratedYangParser;
28import org.onosproject.yangutils.parser.exceptions.ParserException;
29import org.onosproject.yangutils.parser.impl.parserutils.ParseTreeErrorListener;
30
31import java.io.IOException;
32
33/**
34 * Manages file parsing, parse tree creation and data model tree creation
35 * 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 /**
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 */
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 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 /**
89 * Walk parse tree, provide call backs to methods in listener and
90 * build 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}