1 /* lex.h
2 
3    Written by Don Maszle
4    13 October 1991
5 
6    Copyright (c) 1991-2017 Free Software Foundation, Inc.
7 
8    This file is part of GNU MCSim.
9 
10    GNU MCSim is free software; you can redistribute it and/or
11    modify it under the terms of the GNU General Public License
12    as published by the Free Software Foundation; either version 3
13    of the License, or (at your option) any later version.
14 
15    GNU MCSim is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19 
20    You should have received a copy of the GNU General Public License
21    along with GNU MCSim; if not, see <http://www.gnu.org/licenses/>
22 
23    Header file for Lexical parsing routines.
24 */
25 
26 #ifndef LEX_H_DEFINED
27 
28 /* ----------------------------------------------------------------------------
29    Inclusions  */
30 
31 #include "hungtype.h"
32 
33 /* ----------------------------------------------------------------------------
34    Constants  */
35 
36 #define BUFFER_SIZE    0x10000    // Size of input data buffer
37 #define MAX_LEX        255        // Max size of Lexical Element
38 #define MAX_EQN        0x03FF     // Max size of a string eqn
39 
40 /* ----------------------------------------------------------------------------
41    Lexical types */
42 
43 #define LX_NULL       0x0000
44 #define LX_IDENTIFIER 0x0001
45 #define LX_INTEGER    0x0002
46 #define LX_FLOAT      0x0004
47 #define LX_NUMBER     (LX_INTEGER | LX_FLOAT)
48 #define LX_PUNCT      0x0008
49 #define LX_STRING     0x0010
50 
51 /* To avoid unmatched delimeters confusions in editor */
52 
53 #define CH_LPAREN       ('(')
54 #define CH_RPAREN       (')')
55 #define CH_LBRACKET     ('[')
56 #define CH_RBRACKET     (']')
57 #define CH_LBRACE       ('{')
58 #define CH_RBRACE       ('}')
59 
60 /* Character constants for convenience */
61 
62 #define CH_EOLN        ('\n')   // End of line character
63 #define CH_COMMENT     ('#')    // One line Comment Char
64 #define CH_STRDELIM    ('\"')   // String delimiter
65 #define CH_STMTTERM    (';')    // Statement terminator
66 
67 /* Report Error constants -- Lex errors */
68 
69 #define MAX_ERRORS 0
70 
71 #define RE_FATAL            0x8000 // Can be ORd to iCode to cause exit(1)
72 #define RE_WARNING          0x4000 // can be ORd to issue Warning instead
73 
74 #define RE_UNKNOWN          0x0000 /* Unspecified error */
75 #define RE_INIT             0x0001 // Error during initialization
76 #define RE_FILENOTFOUND     0x0002 // Error opening file for I/O
77 #define RE_CANNOTOPEN       0x0003 // Cannot open file
78 #define RE_OUTOFMEM         0x0004 // Error allocating memory
79 #define RE_READERROR        0x0005 // General read file error
80 
81 #define RE_UNEXPECTED       0x0011 // Unexpected char in input
82 #define RE_UNEXPNUMBER      0x0012 // Unexpected number in input
83 #define RE_EXPECTED         0x0013 // Expected character szMsg[0]
84 #define RE_LEXEXPECTED      0x0014 // Expected szMsg lexical element
85 #define RE_SYNTAXERR        0x0015 // Let's make syntax errors fatal
86 
87 #define RE_BADCONTEXT       0x0101 // Invalid context for identifier
88 #define RE_EQNTOOLONG       0x0104 // Eq. too long for buffer
89 #define RE_UNDEFINED        0x0106 // Undefined identifier
90 #define RE_TOOMANYLEVELS    0x0110 // Too many dependency levels
91 #define RE_TOOMANYINST      0x0111 // Too many instances in level
92 #define RE_OPENLEVEL        0x0112 // Unclosed level or simulation
93 #define RE_LEVINEXPT        0x0113 // Level enclosed in simulation
94 #define RE_TYPENOTMCMC      0x0116 // Level statement outside MCMC
95 #define RE_TOOMANYPVARS     0x0117 // Too many variables in Print statement
96 #define RE_DUPVARINEXPRT    0x0121 // Same var appears twice or more
97 #define RE_POSITIVE         0x0122 // Positive number expected
98 
99 #define RE_ERRORSINEXP      0x0201 // Errors reported, skipping exp
100 #define RE_NOOUTPUTS        0x0202 // No outputs specified
101 #define RE_SPECERR          0x0205 // Errors in specification
102 #define RE_INSUF_POINTS     0x0208 // Insufficient forced points
103 #define RE_MAXMIN_RANGE     0x0209 // Max < min
104 #define RE_OUTISRESTART     0x0210 // Output and restart files have same name
105 
106 /* Run-time Errors */
107 
108 #define RE_BADNORMALSD      0x0301
109 #define RE_BADLOGNORMALSD   0x0302
110 #define RE_BADLOGNORMALMEAN 0x0303
111 #define RE_BADUNIFORMDIST   0x0304
112 #define RE_UNKNOWNDIST      0x0305
113 #define RE_BADMODEL         0x0307
114 
115 /* ----------------------------------------------------------------------------
116    Typedefs */
117 
118 /* The INPUTBUF structure which is used for file I/O buffering */
119 
120 typedef PSTR PBUF;
121 
122 
123 typedef struct tagINPUTBUF {
124   PFILE pfileIn;    /* DOS file pointer */
125   PBUF  pbufOrg;    /* Pointers for buffer Origin */
126   PBUF  pbufCur;    /* ... Current point */
127   int   iLineNum;   /* Line number in file */
128   int   iLNPrev;    /* Prev line num.  For formatting Dynamics eqns */
129   int   cErrors;    /* Count of Errors */
130 
131   PVOID    pInfo; /* Pointer to private user information */
132 
133 } INPUTBUF, * PINPUTBUF;
134 
135 
136 typedef char PSTRLEX[MAX_LEX]; /* String of a lexical element */
137 typedef char PSTREQN[MAX_EQN]; /* String of an equation */
138 
139 
140 /* ----------------------------------------------------------------------------
141    Macros */
142 
143 #define EOB(pib) (!(pib)\
144                   || ((!(pib)->pbufCur || !*(pib)->pbufCur)\
145               && (!(pib)->pfileIn || feof((pib)->pfileIn))))
146 
147 #define IsUnderscore(c)    ((c) == '_')
148 #define IsSign(c)    ((c) == '+' || (c) == '-')
149 #define IsString(szLex) ((szLex) ? (*(szLex) == CH_STRDELIM) : (0) )
150 
151 #define ErrorsReported(pib) ((pib)->cErrors)
152 #define ClearErrors(pib) ((pib) ? (pib)->cErrors = 0 : 0)
153 
154 
155 /* ----------------------------------------------------------------------------
156    Prototypes */
157 
158 void EatStatement (PINPUTBUF pib);
159 int  EGetPunct (PINPUTBUF pibIn, PSTR szLex, char chPunct);
160 int  ENextLex (PINPUTBUF, PSTRLEX, int);
161 
162 int  FillBuffer (PINPUTBUF pibIn);
163 void FlushBuffer (PINPUTBUF pibIn);
164 
165 void GetArrayBounds (PINPUTBUF pibIn, PLONG piLB, PLONG piUB);
166 BOOL GetFuncArgs (PINPUTBUF, int, PINT, PSTR);
167 void GetIdentifier (PINPUTBUF pibIn, PSTR szLex);
168 void GetNumber (PINPUTBUF pibIn, PSTR szLex, PINT piLexType);
169 int  GetOptPunct (PINPUTBUF, PSTR, char);
170 int  GetPunct (PINPUTBUF pibIn, PSTR szLex, char chPunct);
171 void GetStatement (PINPUTBUF pibIn, PSTR szStmt);
172 void GetaString (PINPUTBUF pibIn, PSTR szLex);
173 
174 BOOL InitBuffer (PINPUTBUF pibIn, PSTR szFullPathname);
175 
176 void MakeStringBuffer (PINPUTBUF pBuf, PINPUTBUF pStrBuf, PSTR sz);
177 
178 char NextChar (PINPUTBUF pibIn);
179 void NextLex    (PINPUTBUF, PSTRLEX, PINT);
180 int  NextListItem (PINPUTBUF, PSTR, int, int, char);
181 
182 void PreventLexSplit (PINPUTBUF pibIn, int iOffset);
183 
184 void SkipComment (PINPUTBUF);
185 int  SkipWhitespace (PINPUTBUF pibIn);
186 
187 void UnrollEquation (PINPUTBUF pibIn, long index, PSTR szEqn, PSTR szEqnU);
188 
189 #define LEX_H_DEFINED
190 #endif
191 
192 /* End */
193 
194 
195