1 /* 2 * Copyright (c) 2015 PLUMgrid, Inc. 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 #pragma once 18 19 #include <fstream> // NOLINT 20 #include "node.h" 21 #include "lexer.h" 22 #include "scope.h" 23 24 namespace ebpf { 25 namespace cc { 26 27 using std::pair; 28 using std::string; 29 using std::vector; 30 31 class Parser { 32 public: Parser(const string & infile)33 explicit Parser(const string& infile) 34 : root_node_(NULL), scopes_(new Scopes), in_(infile), lexer(&in_), parser(lexer, *this) { 35 // parser.set_debug_level(1); 36 } ~Parser()37 ~Parser() { delete root_node_; } parse()38 int parse() { 39 return parser.parse(); 40 } 41 42 VariableDeclStmtNode * variable_add(vector<int> *types, VariableDeclStmtNode *decl); 43 VariableDeclStmtNode * variable_add(vector<int> *types, VariableDeclStmtNode *decl, ExprNode *init_expr); 44 StructVariableDeclStmtNode * variable_add(StructVariableDeclStmtNode *decl, ExprNodeList *args, bool is_kv); 45 StmtNode * state_add(Scopes::StateScope *scope, IdentExprNode *id1, BlockStmtNode *body); 46 StmtNode * state_add(Scopes::StateScope *scope, IdentExprNode *id1, IdentExprNode *id2, BlockStmtNode *body); 47 StmtNode * func_add(std::vector<int> *types, Scopes::StateScope *scope, 48 IdentExprNode *id, FormalList *formals, BlockStmtNode *body); 49 StmtNode * table_add(IdentExprNode *type, IdentExprNodeList *templates, IdentExprNode *id, string *size); 50 StmtNode * struct_add(IdentExprNode *type, FormalList *formals); 51 StmtNode * result_add(int token, IdentExprNode *id, FormalList *formals, BlockStmtNode *body); 52 bool variable_exists(VariableDeclStmtNode *decl) const; 53 bool table_exists(TableDeclStmtNode *decl, bool search_local = true); add_pragma(const std::string & pr,const std::string & v)54 void add_pragma(const std::string& pr, const std::string& v) { pragmas_[pr] = v; } 55 void set_loc(Node *n, const BisonParser::location_type &loc) const; 56 std::string pragma(const std::string &name) const; 57 58 Node *root_node_; 59 Scopes::Ptr scopes_; 60 std::map<std::string, std::string> pragmas_; 61 private: 62 std::ifstream in_; 63 Lexer lexer; 64 BisonParser parser; 65 }; 66 67 } // namespace cc 68 } // namespace ebpf 69