blob: 1fcf0b8aed750f460734bfbf276f40f927931bf7 [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
Vidyashree Rama4f1f08b2016-02-13 21:47:58 +053039 public static final int SUB_STATEMENT_CARDINALITY = 1;
40
Gaurav Agrawal88897632016-02-12 18:37:50 +053041 @Override
42 public YangNode getDataModel(String yangFile) throws IOException, ParserException {
43
44 /**
45 * Create a char stream that reads from YANG file. Throws an exception
46 * in case input YANG file is either null or non existent.
47 */
48 ANTLRInputStream input = null;
49 try {
50 input = new ANTLRFileStream(yangFile);
51 } catch (IOException e) {
52 e.printStackTrace();
53 throw e;
54 }
55
56 // Create a lexer that feeds off of input char stream.
57 GeneratedYangLexer lexer = new GeneratedYangLexer(input);
58
59 // Create a buffer of tokens pulled from the lexer.
60 CommonTokenStream tokens = new CommonTokenStream(lexer);
61
62 // Create a parser that feeds off the tokens buffer.
63 GeneratedYangParser parser = new GeneratedYangParser(tokens);
64
65 // Remove console error listener.
66 parser.removeErrorListeners();
67
68 // Create instance of customized error listener.
69 ParseTreeErrorListener parseTreeErrorListener = new ParseTreeErrorListener();
70
71 // Add customized error listener to catch errors during parsing.
72 parser.addErrorListener(parseTreeErrorListener);
73
Gaurav Agrawala04483c2016-02-13 14:23:40 +053074 ParseTree tree;
Gaurav Agrawal88897632016-02-12 18:37:50 +053075
Gaurav Agrawala04483c2016-02-13 14:23:40 +053076 try {
77 // Begin parsing YANG file and generate parse tree.
78 tree = parser.yangfile();
79 } catch (ParserException parserException) {
Gaurav Agrawal88897632016-02-12 18:37:50 +053080 parserException.setFileName(yangFile);
81 throw parserException;
82 }
83
84 // Create a walker to walk the parse tree.
85 ParseTreeWalker walker = new ParseTreeWalker();
86
87 // Create a listener implementation class object.
88 TreeWalkListener treeWalker = new TreeWalkListener();
89
90 /**
91 * Walk parse tree, provide call backs to methods in listener and
92 * build data model tree.
93 */
Gaurav Agrawala04483c2016-02-13 14:23:40 +053094 try {
95 walker.walk(treeWalker, tree);
96 } catch (ParserException listenerException) {
97 // TODO free incomplete data model tree.
Gaurav Agrawal88897632016-02-12 18:37:50 +053098 listenerException.setFileName(yangFile);
99 throw listenerException;
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530100 } finally {
101 // TODO free parsable stack
Gaurav Agrawal88897632016-02-12 18:37:50 +0530102 }
103
104 // Returns the Root Node of the constructed data model tree.
105 return treeWalker.getRootNode();
106 }
Gaurav Agrawala04483c2016-02-13 14:23:40 +0530107}