1 //  $Id$
2 // Copyright (c) 2001,2002                        RIPE NCC
3 //
4 // All Rights Reserved
5 //
6 // Permission to use, copy, modify, and distribute this software and its
7 // documentation for any purpose and without fee is hereby granted,
8 // provided that the above copyright notice appear in all copies and that
9 // both that copyright notice and this permission notice appear in
10 // supporting documentation, and that the name of the author not be
11 // used in advertising or publicity pertaining to distribution of the
12 // software without specific, written prior permission.
13 //
14 // THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
15 // ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS; IN NO EVENT SHALL
16 // AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
17 // DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN
18 // AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 // OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20 //
21 //
22 //  Copyright (c) 1994 by the University of Southern California
23 //  All rights reserved.
24 //
25 //    Permission is hereby granted, free of charge, to any person obtaining a copy
26 //    of this software and associated documentation files (the "Software"), to deal
27 //    in the Software without restriction, including without limitation the rights
28 //    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 //    copies of the Software, and to permit persons to whom the Software is
30 //    furnished to do so, subject to the following conditions:
31 //
32 //    The above copyright notice and this permission notice shall be included in
33 //    all copies or substantial portions of the Software.
34 //
35 //    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 //    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 //    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
38 //    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 //    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 //    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41 //    THE SOFTWARE.
42 //
43 //  Questions concerning this software should be directed to
44 //  irrtoolset@cs.usc.edu.
45 //
46 //  Author(s): Cengiz Alaettinoglu <cengiz@ISI.EDU>
47 
48 #ifndef SCHEMA_HH
49 #define SCHEMA_HH
50 
51 #include "config.h"
52 #include <vector>
53 #include <string>
54 #include "rptype.hh"
55 #include "rpsl_attr.hh"
56 
57 class RPSLKeyword {
58    friend class Schema;
59 private:
60    const char *_name;		    // token in ASCII, e.g. "accept"
61    int  token_id;	    // token as integer, e.g. TKN_ACCEPT
62    char is_reserved;        // a reserved word returns token_id to the parser
63 public:
RPSLKeyword(const char * n,int id,int is_res)64    RPSLKeyword(const char *n, int id, int is_res) : _name(n), token_id(id),
65       is_reserved(is_res) {}
66 };
67 
68 class Object;
69 
70 class Schema {
71 private:
72    static char dictionary_text[];
73 
74    int           lastClass;
75    AttrClass     *classes[64];
76    RPSLKeyword   *keywords;
77    RPSLKeyword   *attrSyntax;
78 
79    Object  *dictionary;
80 
81    bool forgiving;		// if true mandatory/single valued mismatches
82 				// can be ignored,
83                                 // if *very* true (not yet implemented),
84                                 // syntax errors can also be ignored
85 				// key attributes are always non-forgiving
86 
87    char is_context_active;  // parsers context may be active
88 			    // in this context rp_attributes are active
89 			    // when active, all words return token_id's
90 			    // to the parser, when non active words return
91 			    // TKN_WORD to the parser
92 			    // active context is usually filters and actions
93 public:
Schema(RPSLKeyword * _keywords,RPSLKeyword * _attrSyntax)94    Schema(RPSLKeyword *_keywords, RPSLKeyword *_attrSyntax) :
95       keywords(_keywords), attrSyntax(_attrSyntax),
96       dictionary((Object *) NULL), forgiving(true) {
97       lastClass = 0;
98    }
99    ~Schema();
100    void initialize();
101    Object *initializeFromString(char *class_text, const char *lookFor=(const char *) NULL);
102 
103    // following searches return NULL/0 on failure
104    AttrClass               *searchClass(const char *name);
105    const class  AttrRPAttr *searchRPAttr(const char *name);
106    int                      searchKeyword(const char *word);
107    RPType                  *searchTypedef(const char *name);
108    const AttrProtocol      *searchProtocol(const char *name);
109    int                      searchAttrSyntax(const char *name);
110    void                     printClassStats();
111 
getLastClass(void) const112    int getLastClass(void) const {
113      return lastClass;
114    }
getClass(int i) const115    AttrClass *getClass(int i) const {
116      return classes[i];
117    }
118 
beForgiving(bool _forgiving=true)119    void beForgiving(bool _forgiving = true) {
120       forgiving = _forgiving;
121    }
beHarsh()122    void beHarsh() {
123       forgiving = false;
124    }
isForgiving()125    bool isForgiving() {
126       return forgiving;
127    }
isVeryForgiving()128    bool isVeryForgiving() {
129       return false;
130    }
131 
132 private:
133    void initializeBase();
134    void addClass(AttrClass *clss);
135 };
136 
137 extern Schema schema;
138 
139 #endif   // SCHEMA_HH
140