/
parse.h
135 lines (102 loc) · 3.2 KB
/
parse.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/*
IL : Intlog Language
Object Oriented Prolog Project
Copyright (C) 1992-2021 - Ing. Capelli Carlo
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
#ifndef PARSE_H_
#define PARSE_H_
#include "iafx.h"
#include "term.h"
#include "scanner.h"
class ReduceStack;
class SrcPosTree;
//-------------------------------------------------
// top-down recursive parser
// fetch next clause from stream (0 at EOF or error)
// use a precedence algorithm to handle operators
//
class IAFX_API IntlogParser {
public:
IntlogParser(OperTable*);
~IntlogParser();
// set current scanner for current stream
void SetSource(istream *pStream, kstring kFileid, kstr_list *varList);
void PopSource(istream *pStream);
istream *GetSourceStream() const;
kstring GetSourceName() const;
// type of input found
enum objType {
NewClause,
RedoCommand,
EndFile,
Error
};
objType getinput();
// valorized if getinput() returned NewClause
Term parsed;
SrcPosTree& GetSrcPosTree();
// return operator table
OperTable* get_ops() const;
// display message on parsing error with infos on source location
void err_msg(int, const char * = nullptr) const;
private:
static const int MaxOpPrec;
static const int MaxOpCdr;
// keep active scanner stream
IntlogScanner* scan;
kstr_list* var_ids;
struct iDesc : public e_slist {
~iDesc() override;
istream *pStream;
kstring kFileid;
kstr_list *pVars;
int nRow, nCol, lhChar;
};
slist activeScan;
// hold operators
OperTable* OpTbl;
// term reduction stack
ReduceStack* rs;
// hold last token code read
IntlogScanner::Codes curr_tok;
// read next token
void advance();
// return next term read (0 at EOF or error)
Term term();
// Term Expr1 | '(' Expr ')' Expr1 | Opre Expr Expr1
int Expr(int maxLev = MaxOpPrec);
// Oinf Expr Expr1 | Opos Expr1
int Expr1(int maxLev = MaxOpPrec);
// atom '(' Expr ')' | int | var | '[' Expr ']'
int Termp();
// check backtracking command
int checkCmdRedo();
// parse list data
int Listd();
// parse a symbol
int Symbol();
// scan operators definition
Operator* lastOp;
Operator* GetOp(Operator* = nullptr);
};
inline OperTable* IntlogParser::get_ops() const {
return OpTbl;
}
inline istream *IntlogParser::GetSourceStream() const {
return scan->i;
}
inline kstring IntlogParser::GetSourceName() const {
return scan->id;
}
#endif