Project

General

Profile

Statistics
| Branch: | Revision:

root / src / nedxml / nedparser.h @ 79bb12dc

History | View | Annotate | Download (4.71 KB)

1 01873262 Georg Kunz
//==========================================================================
2
//  NEDPARSER.H - part of
3
//
4
//                     OMNeT++/OMNEST
5
//            Discrete System Simulation in C++
6
//
7
//==========================================================================
8
9
/*--------------------------------------------------------------*
10
  Copyright (C) 2002-2008 Andras Varga
11
  Copyright (C) 2006-2008 OpenSim Ltd.
12

13
  This file is distributed WITHOUT ANY WARRANTY. See the file
14
  `license' for details on this and other legal matters.
15
*--------------------------------------------------------------*/
16
17
#ifndef __NEDPARSER_H
18
#define __NEDPARSER_H
19
20
#include <stdio.h>
21
#include "nedelement.h"
22
#include "nedyydefs.h"
23
#include "nederror.h"
24
25
NAMESPACE_BEGIN
26
27
class NEDFileBuffer;
28
class NEDParser;
29
30
extern NEDParser *np;
31
32
/* to EXPR_TYPE: */
33
#define TYPE_NUMERIC   'N'
34
#define TYPE_CONST_NUM 'C'
35
#define TYPE_STRING    'S'
36
#define TYPE_BOOL      'B'
37
#define TYPE_XML       'X'
38
#define TYPE_ANYTYPE   'A'
39
40
41
/**
42
 * Parses NED files into a NED object tree.
43
 * Elements of the tree are subclassed from NEDElement;
44
 * NEDElementFactory is used to actually create the objects.
45
 * Internally this class uses a bison/flex grammar and NEDFileBuffer.
46
 *
47
 * @ingroup NEDParser
48
 */
49
class NEDXML_API NEDParser
50
{
51
  public:
52
    // INTERNAL: error and debug handling, called from grammar file
53
    void error(const char *msg, int line);
54
55
    NEDErrorStore *getErrors() {return errors;}
56
    NEDFileBuffer *getSource() {return nedsource;}
57
58
  protected:
59
    bool parseexpr;            // whether to parse expressions or not
60
    bool storesrc;             // whether to fill in sourceCode attributes
61
    const char *filename;      // name of file being parsed
62
    NEDErrorStore *errors;     // accumulates error messages
63
    NEDFileBuffer *nedsource;  // represents the source file
64
65
    bool loadFile(const char *osfname, const char *fname);
66
    bool loadText(const char *nedtext, const char *fname);
67
    NEDElement *parseNED();
68
    NEDElement *parseMSG();
69
    static bool guessIsNEDInNewSyntax(const char *txt);
70
71
  public:
72
    /**
73
     * Constructor.
74
     */
75
    NEDParser(NEDErrorStore *e);
76
77
    /**
78
     * Destructor.
79
     */
80
    ~NEDParser();
81
82
    /**
83
     * Returns a NED source which contains declarations of
84
     * built-in NED types.
85
     */
86
    static const char *getBuiltInDeclarations();
87
88
    /**
89
     * Affects operation of parseFile() and parseText(), specifies whether
90
     * expressions should be parsed or not.
91
     * Default is true.
92
     */
93
    void setParseExpressions(bool b) {parseexpr = b;}
94
95
    /**
96
     * Affects operation of parseFile() and parseText(), specifies whether
97
     * sourceCode attributes in NEDElements should be filled out.
98
     * Default is false.
99
     */
100
    void setStoreSource(bool b) {storesrc = b;}
101
102
    /**
103
     * Returns the "parse expressions" flag; see setParseExpressions().
104
     */
105
    bool getParseExpressionsFlag() {return parseexpr;}
106
107
    /**
108
     * Returns the "store source code" flag; see setStoreSource().
109
     */
110
    bool getStoreSourceFlag() {return storesrc;}
111
112
    /**
113
     * May only be called during parsing. It returns the name of the source file being parsed.
114
     */
115
    const char *getFileName() {return filename;}
116
117
    /**
118
     * Parses the given NED file (osfname), and returns the result tree.
119
     * Returns NULL or partial tree if there was an error.
120
     * The fname parameter will be used to fill in the source location
121
     * attributes; it defaults to osfname.
122
     */
123
    NEDElement *parseNEDFile(const char *osfname, const char *fname=NULL);
124
125
    /**
126
     * Parse the given NED source and return the result tree.
127
     * Returns NULL or partial tree if there was an error.
128
     * The fname parameter will be used to fill in the source location
129
     * attributes; it defaults to "buffer".
130
     */
131
    NEDElement *parseNEDText(const char *nedtext, const char *fname=NULL);
132
133
    /**
134
     * Parse the given text as a NED expression, and return the result tree.
135
     * Returns NULL or partial tree if there was an error.
136
     */
137
    NEDElement *parseNEDExpression(const char *nedexpression);
138
139
    /**
140
     * Parse the given MSG (.msg) file and return the result tree.
141
     * Returns NULL or partial tree if there was an error.
142
     * The fname parameter will be used to fill in the source location
143
     * attributes; it defaults to osfname.
144
     */
145
    NEDElement *parseMSGFile(const char *osfname, const char *fname=NULL);
146
147
    /**
148
     * Parse the given MSG source and return the result tree.
149
     * Returns NULL or partial tree if there was an error.
150
     * The fname parameter will be used to fill in the source location
151
     * attributes; it defaults to osfname.
152
     */
153
    NEDElement *parseMSGText(const char *nedtext, const char *fname=NULL);
154
};
155
156
NAMESPACE_END
157
158
159
#endif