gcc config
[prop.git] / prop-src / parser.ph
blob38bf9a3c493bd707177e238e8fb9e77eab74db1d
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 //  Definition of the Prop parser.
4 //
5 ///////////////////////////////////////////////////////////////////////////////
6 #ifndef prop_parser_interface_h
7 #define prop_parser_interface_h
9 #include <iostream>
10 #include <AD/automata/iolexerstack.h>
11 #include "ir.h"
12 #include "ast.h"
13 #include "patenv.h"
14 #include "textbuf.h"
16 class Compiler;
17 class GraphTypeDef;
19 ///////////////////////////////////////////////////////////////////////////////
21 //  Lexical contexts
23 ///////////////////////////////////////////////////////////////////////////////
24 datatype LexicalContext = NONE | C | PROP | COMMENT | QUOTE | ANTIQUOTE;
26 ///////////////////////////////////////////////////////////////////////////////
28 //  The syntax class PropParser describe the interface to the
29 //  parsing/lexing front end.
31 ///////////////////////////////////////////////////////////////////////////////
32 syntax class PropParser
33 {  PropParser(const PropParser&);
34    void operator = (const PropParser&);
35 private:
36    enum { MAX_LEXICAL_DEPTH = 256,
37           MAX_ARITY         = 256,
38           MAX_INCLUDE_FILES = 256
39         };
41    ////////////////////////////////////////////////////////////////////////////
42    //  Lexical buffer
43    ////////////////////////////////////////////////////////////////////////////
44    IOLexerStack lexbuf;  
46    ////////////////////////////////////////////////////////////////////////////
47    //  Lexical context stack.  This is used to keep track of the
48    //  current lexical context.
49    ////////////////////////////////////////////////////////////////////////////
50    LexicalContext SCs[MAX_LEXICAL_DEPTH];
51    int            SCs_top;
52    int            levels[MAX_LEXICAL_DEPTH];
53    int            levels_top;
54    Decls          code_stack[MAX_LEXICAL_DEPTH];
55    int            code_top;
56    void start_sc (LexicalContext); // enter a new lexical context
57    void end_sc   ();               // leave the current lexical context
59    ////////////////////////////////////////////////////////////////////////////
60    //  Quoting stack
61    ////////////////////////////////////////////////////////////////////////////
62    struct Quote {
63       const char * file_name;
64       int          line_number;
65       char         open, close;
66       Quote() {}
67       Quote(const char * file, int line, char a, char b)
68          : file_name(file), line_number(line), open(a), close(b) {}
69    };
70    Quote quote_stack[MAX_LEXICAL_DEPTH];
71    int   quote_top;
72    void  start_quote(char, char);
73    char  end_quote  (char);
74    void  start_statement ();
75    void  end_statement ();
77    ////////////////////////////////////////////////////////////////////////////
78    //  Internal code and documentation buffer.
79    ////////////////////////////////////////////////////////////////////////////
80    Bool       emit_code;
81    TextBuffer scan;
82    TextBuffer doc;
83    TextBuffer meta;
84    void emit ();
85    void emit (char);
86    void emit (const char *, long = -1);
87    void emit (Exp);
88    void emit_cons (Id cons_name);
89    void emit_doc  ();
90    void emit_meta ();
91    void emit_header ();
92    void count_lines ();
94    ////////////////////////////////////////////////////////////////////////////
95    //  Pattern variable environment and match expression stack
96    ////////////////////////////////////////////////////////////////////////////
97    PatternVarEnv pv_env;                        // pattern variable environment
98    MatchExps     me_stack[MAX_LEXICAL_DEPTH];   // match expression stack
99    MatchOptions  match_kind[MAX_LEXICAL_DEPTH]; // kind of match construct
100    int           me_top;                        // top of me stack
102    ////////////////////////////////////////////////////////////////////////////
103    //  Variable binding stack
104    ////////////////////////////////////////////////////////////////////////////
105    Id   var_stack[MAX_ARITY];
106    Pat  pat_stack[MAX_ARITY];
107    int  var_top;
109    ////////////////////////////////////////////////////////////////////////////
110    //  Include file stack
111    ////////////////////////////////////////////////////////////////////////////
112    struct IncludeFile
113    {  int          line;
114       int          first_line;
115       const char * file;
116       std::istream    * file_stream;
117       TextBuffer   scan;
118       TextBuffer   doc;
119       TextBuffer   meta;
120    };
121    IncludeFile  includes[MAX_INCLUDE_FILES];
122    int          includes_top;
123    const char * included_files[MAX_INCLUDE_FILES];
124    int          included_count;
126    ////////////////////////////////////////////////////////////////////////////
127    //  Miscellaneous flags and counters
128    ////////////////////////////////////////////////////////////////////////////
129    int  antecedent_count;  // number of antecedents in an inference rule
130    Bool in_rewrite;        // are we in rewriting?
131    int  rule_count;        // number of rules
132    int  is_view_def;       // are we in a view definition?
133    int  symbol_count;      // number of symbols in a production
134    int  item_count;        // number of items in a production
135    int  nonterm_count;     // number of non-terminals in a production
136    int  match_rule;
137    int  constraint_rule;
139    struct rewrite_info
140    {  MatchRuleInfo::RewritingMode mode; // current rewrite mode
141       MatchRuleInfo::RewritingOption option;
142       TyQual qual;
143    }  rw_stack[MAX_LEXICAL_DEPTH]; // kind of rewriting mode
144    int rw_top;                     // top of rw stack
145    void push_rw_stack();
146    void pop_rw_stack();
148    ////////////////////////////////////////////////////////////////////////////
149    //  The program being constructed.
150    ////////////////////////////////////////////////////////////////////////////
151    Cons  my_cons;
152    Exp   my_exp;
153    Compiler * compiler;          // the current compiler
154    GraphTypeDef * graphtype_def; // the current graph type definition
156 public:
157    ////////////////////////////////////////////////////////////////////////////
158    //  The public interface
159    ////////////////////////////////////////////////////////////////////////////
160    virtual ~PropParser();
162    int get_token ();             // method to retrieve a token
163    int get_token2 ();             // method to retrieve a token
164    ErrorAction error_report (const char *); // error reporting.
165    void initialize (Compiler&);
166    void initialize_lexer (const char * file_name);
167    void cleanup_lexer();
168    void print_header(Compiler&);
169    Decls program;                 
171 private:
172    void open_include_file (const char *);   // open a new include file
173    void close_include_file ();              // close include file
175    Decls mkcode        (Decls);
176    Pat   mkconspat     (Cons);
177    Exp   lookup_exp    (Id);
178    Ty    lookup_tyvar  (Id);
179    Pat   lookup_patvar (Id);
180    void  add_parse_stack_binding (int,int,int);
182    // For error explanation
183    void  print_user_symbol (std::ostream&, Symbol);
184    void  explain_error();
187 #endif