Cadabra
Computer algebra system for field theory problems
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
PreProcessor.hh
Go to the documentation of this file.
1 /*
2 
3  Cadabra: a field-theory motivated computer algebra system.
4  Copyright (C) 2001-2014 Kasper Peeters <kasper.peeters@phi-sci.com>
5 
6  This program is free software: you can redistribute it and/or
7  modify it under the terms of the GNU General Public License as
8  published by the Free Software Foundation, either version 3 of the
9  License, or (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18 
19 */
20 
21 #pragma once
22 
23 #include <iostream>
24 #include <string>
25 #include <vector>
26 
27 enum { tok_arrow=0xac, tok_unequals=0xad, tok_wedge=0xae, tok_pow=0xaf, tok_set_option=0xb0, tok_declare=0xb1, tok_sequence=0xb2, tok_siblings=0xb3 };
28 
35 
36 class preprocessor {
37  public:
38  preprocessor();
39  friend std::istream& operator>>(std::istream&, preprocessor&);
40  friend std::ostream& operator<<(std::ostream&, const preprocessor&);
41 
42  void erase();
43  void strip_outer_brackets() const;
44 
45  const static unsigned char orders[];
46 
65  // FIXME: we really need a way to associate multiple characters to a single operator,
66  // since that would allow for ".." (sequence), ":=" (define), ">=" and so on. The current
67  // '.' is a hack and is treated as such: when it occurs there is an additional check for
68  // a followup '.'.
69  const static char *const order_names[];
70  private:
71  void parse_(const std::string&);
72  void parse_internal_();
73  bool verbatim_;
76  bool unwind_(unsigned int tolevel, unsigned int bracketgoal=0, bool usebracket=true) const;
77  unsigned char get_token_(unsigned char prev_token);
78  void show_and_throw_(const std::string& str) const;
79 
80  void bracket_strings_(unsigned int cb, std::string& obrack, std::string& cbrack) const;
81  bool is_infix_operator_(unsigned char c) const;
82  bool is_link_(unsigned char c) const;
83  unsigned int is_opening_bracket_(unsigned char c) const;
84  unsigned int is_closing_bracket_(unsigned char c) const;
85  unsigned int is_bracket_(unsigned char c) const;
86  bool is_already_bracketed_(const std::string& str) const;
87  bool is_digits_(const std::string& str) const;
88  unsigned int current_bracket_(bool deep=false) const;
89  void print_stack() const; // for debuggging purposes
90 
91  bool default_is_product_() const;
92  unsigned int cur_pos;
93  std::string cur_str;
94 
95  // A backslash followed by a bracket is also a bracket (gets code
96  // of the bracket plus 128).
97  const static unsigned char open_brackets[];
98  const static unsigned char close_brackets[];
99 
100  class accu_t {
101  public:
102  accu_t();
103  void erase();
104 
105  bool head_is_generated; // when infix -> postfix has occurred
106  std::string accu;
107  unsigned int order;
108  std::vector<std::string> parts;
109  unsigned int bracket;
110  bool is_index; // whether the bracket was prefixed with ^ or _
111  };
112  mutable accu_t cur;
113  mutable std::vector<accu_t> accus;
114 };
115 
116 std::ostream& operator<<(std::ostream&, const preprocessor&);
117 std::istream& operator>>(std::istream&, preprocessor&);
118 
119 
bool is_index
Definition: PreProcessor.hh:110
Definition: PreProcessor.hh:54
Definition: PreProcessor.hh:57
bool verbatim_
Definition: PreProcessor.hh:73
static const char *const order_names[]
Definition: PreProcessor.hh:69
Definition: PreProcessor.hh:62
void show_and_throw_(const std::string &str) const
Definition: PreProcessor.cc:514
Definition: PreProcessor.hh:49
friend std::ostream & operator<<(std::ostream &, const preprocessor &)
Definition: PreProcessor.cc:61
void erase()
Definition: PreProcessor.cc:499
Definition: PreProcessor.hh:59
Definition: PreProcessor.hh:61
bool unwind_(unsigned int tolevel, unsigned int bracketgoal=0, bool usebracket=true) const
Definition: PreProcessor.cc:331
std::ostream & operator<<(std::ostream &, const preprocessor &)
Definition: PreProcessor.cc:61
void print_stack() const
Definition: PreProcessor.cc:88
bool head_is_generated
Definition: PreProcessor.hh:105
unsigned int is_closing_bracket_(unsigned char c) const
Definition: PreProcessor.cc:140
bool is_already_bracketed_(const std::string &str) const
Definition: PreProcessor.cc:165
bool eat_initial_whitespace_
Definition: PreProcessor.hh:75
Definition: PreProcessor.hh:27
unsigned int is_bracket_(unsigned char c) const
Definition: PreProcessor.cc:158
Definition: PreProcessor.hh:51
Definition: PreProcessor.hh:50
Definition: PreProcessor.hh:27
std::string accu
Definition: PreProcessor.hh:106
static const unsigned char open_brackets[]
Definition: PreProcessor.hh:97
void bracket_strings_(unsigned int cb, std::string &obrack, std::string &cbrack) const
Definition: PreProcessor.cc:315
preprocessor()
Definition: PreProcessor.cc:71
unsigned int cur_pos
Definition: PreProcessor.hh:92
Definition: PreProcessor.hh:52
std::istream & operator>>(std::istream &, preprocessor &)
Definition: PreProcessor.cc:52
std::vector< accu_t > accus
Definition: PreProcessor.hh:113
unsigned int order
Definition: PreProcessor.hh:107
Definition: PreProcessor.hh:48
void strip_outer_brackets() const
Definition: PreProcessor.cc:493
void parse_(const std::string &)
Definition: PreProcessor.cc:508
void erase()
Definition: PreProcessor.cc:484
Definition: PreProcessor.hh:55
bool default_is_product_() const
Definition: PreProcessor.cc:122
bool next_is_product_
Definition: PreProcessor.hh:74
accu_t()
Definition: PreProcessor.cc:479
Definition: PreProcessor.hh:27
Definition: PreProcessor.hh:63
bool is_digits_(const std::string &str) const
Definition: PreProcessor.cc:132
std::vector< std::string > parts
Definition: PreProcessor.hh:108
bool is_link_(unsigned char c) const
Definition: PreProcessor.cc:76
Definition: PreProcessor.hh:27
std::string cur_str
Definition: PreProcessor.hh:93
Definition: PreProcessor.hh:64
Definition: PreProcessor.hh:27
Definition: PreProcessor.hh:53
Definition: PreProcessor.hh:60
unsigned int current_bracket_(bool deep=false) const
Definition: PreProcessor.cc:110
unsigned int bracket
Definition: PreProcessor.hh:109
unsigned int is_opening_bracket_(unsigned char c) const
Definition: PreProcessor.cc:149
Definition: PreProcessor.hh:27
Definition: PreProcessor.hh:58
void parse_internal_()
Definition: PreProcessor.cc:527
Preprocessing class which takes infix mathematical notation with all sorts of maths shortcuts and tra...
Definition: PreProcessor.hh:36
static const unsigned char orders[]
Definition: PreProcessor.hh:45
Definition: PreProcessor.hh:47
friend std::istream & operator>>(std::istream &, preprocessor &)
Definition: PreProcessor.cc:52
Definition: PreProcessor.hh:27
bool is_infix_operator_(unsigned char c) const
Definition: PreProcessor.cc:81
accu_t cur
Definition: PreProcessor.hh:112
Definition: PreProcessor.hh:27
static const unsigned char close_brackets[]
Definition: PreProcessor.hh:98
unsigned char get_token_(unsigned char prev_token)
Definition: PreProcessor.cc:174
Definition: PreProcessor.hh:100
order_labels
Definition: PreProcessor.hh:47
Definition: PreProcessor.hh:56