Cadabra
Computer algebra system for field theory problems
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 char32_t orders[];
46 
65  };
66  // FIXME: we really need a way to associate multiple characters to a single operator,
67  // since that would allow for ".." (sequence), ":=" (define), ">=" and so on. The current
68  // '.' is a hack and is treated as such: when it occurs there is an additional check for
69  // a followup '.'.
70  const static char32_t *const order_names[];
71  private:
72  void parse_(const std::u32string&);
73  void parse_internal_();
74  bool verbatim_;
77  bool unwind_(unsigned int tolevel, unsigned int bracketgoal=0, bool usebracket=true) const;
78  char32_t get_token_(char32_t prev_token);
79  void show_and_throw_(const std::string& str) const;
80 
81  void bracket_strings_(char32_t cb, std::u32string& obrack, std::u32string& cbrack) const;
82  bool is_infix_operator_(char32_t c) const;
83  bool is_link_(char32_t c) const;
84  unsigned int is_opening_bracket_(char32_t c) const;
85  unsigned int is_closing_bracket_(char32_t c) const;
86  unsigned int is_bracket_(char32_t c) const;
87  bool is_already_bracketed_(const std::u32string& str) const;
88  bool is_digits_(const std::u32string& str) const;
89  unsigned int current_bracket_(bool deep=false) const;
90  void print_stack() const; // for debuggging purposes
91 
92  bool default_is_product_() const;
93  unsigned int cur_pos;
94  std::u32string cur_str;
95 
96  // A backslash followed by a bracket is also a bracket (gets code
97  // of the bracket plus 128).
98  const static char32_t open_brackets[];
99  const static char32_t close_brackets[];
100 
101  class accu_t {
102  public:
103  accu_t();
104  void erase();
105 
106  bool head_is_generated; // when infix -> postfix has occurred
107  std::u32string accu;
108  unsigned int order;
109  std::vector<std::u32string> parts;
110  unsigned int bracket;
111  bool is_index; // whether the bracket was prefixed with ^ or _
112  };
113  mutable accu_t cur;
114  mutable std::vector<accu_t> accus;
115  };
116 
117 std::ostream& operator<<(std::ostream&, const preprocessor&);
118 std::istream& operator>>(std::istream&, preprocessor&);
119 
120 
operator>>
std::istream & operator>>(std::istream &, preprocessor &)
Definition: PreProcessor.cc:55
preprocessor::erase
void erase()
Definition: PreProcessor.cc:511
preprocessor::get_token_
char32_t get_token_(char32_t prev_token)
Definition: PreProcessor.cc:183
preprocessor::cur
accu_t cur
Definition: PreProcessor.hh:113
preprocessor::open_brackets
const static char32_t open_brackets[]
Definition: PreProcessor.hh:98
preprocessor::accu_t::head_is_generated
bool head_is_generated
Definition: PreProcessor.hh:106
preprocessor::order_unequals
@ order_unequals
Definition: PreProcessor.hh:56
preprocessor::order_conditions
@ order_conditions
Definition: PreProcessor.hh:59
preprocessor::order_tilde
@ order_tilde
Definition: PreProcessor.hh:64
preprocessor::preprocessor
preprocessor()
Definition: PreProcessor.cc:79
preprocessor::accu_t::is_index
bool is_index
Definition: PreProcessor.hh:111
preprocessor::strip_outer_brackets
void strip_outer_brackets() const
Definition: PreProcessor.cc:505
tok_set_option
@ tok_set_option
Definition: PreProcessor.hh:27
preprocessor::close_brackets
const static char32_t close_brackets[]
Definition: PreProcessor.hh:99
preprocessor::order_names
const static char32_t *const order_names[]
Definition: PreProcessor.hh:70
preprocessor::accus
std::vector< accu_t > accus
Definition: PreProcessor.hh:114
preprocessor::current_bracket_
unsigned int current_bracket_(bool deep=false) const
Definition: PreProcessor.cc:118
preprocessor::verbatim_
bool verbatim_
Definition: PreProcessor.hh:74
preprocessor::order_pow
@ order_pow
Definition: PreProcessor.hh:48
tok_pow
@ tok_pow
Definition: PreProcessor.hh:27
preprocessor::is_link_
bool is_link_(char32_t c) const
Definition: PreProcessor.cc:84
preprocessor::is_already_bracketed_
bool is_already_bracketed_(const std::u32string &str) const
Definition: PreProcessor.cc:174
preprocessor::default_is_product_
bool default_is_product_() const
Definition: PreProcessor.cc:131
preprocessor::parse_
void parse_(const std::u32string &)
Definition: PreProcessor.cc:520
preprocessor::show_and_throw_
void show_and_throw_(const std::string &str) const
Definition: PreProcessor.cc:526
preprocessor::order_greater_than
@ order_greater_than
Definition: PreProcessor.hh:58
preprocessor::order_minus
@ order_minus
Definition: PreProcessor.hh:52
preprocessor::order_factorial
@ order_factorial
Definition: PreProcessor.hh:47
preprocessor::print_stack
void print_stack() const
Definition: PreProcessor.cc:96
preprocessor::accu_t
Definition: PreProcessor.hh:101
preprocessor::order_equals
@ order_equals
Definition: PreProcessor.hh:55
preprocessor::order_comma
@ order_comma
Definition: PreProcessor.hh:63
preprocessor::eat_initial_whitespace_
bool eat_initial_whitespace_
Definition: PreProcessor.hh:76
preprocessor::is_bracket_
unsigned int is_bracket_(char32_t c) const
Definition: PreProcessor.cc:167
preprocessor::order_dot
@ order_dot
Definition: PreProcessor.hh:54
preprocessor::operator>>
friend std::istream & operator>>(std::istream &, preprocessor &)
Definition: PreProcessor.cc:55
preprocessor::is_opening_bracket_
unsigned int is_opening_bracket_(char32_t c) const
Definition: PreProcessor.cc:158
preprocessor::accu_t::bracket
unsigned int bracket
Definition: PreProcessor.hh:110
preprocessor::parse_internal_
void parse_internal_()
Definition: PreProcessor.cc:540
preprocessor::accu_t::erase
void erase()
Definition: PreProcessor.cc:496
preprocessor::accu_t::order
unsigned int order
Definition: PreProcessor.hh:108
preprocessor::order_plus
@ order_plus
Definition: PreProcessor.hh:53
preprocessor::operator<<
friend std::ostream & operator<<(std::ostream &, const preprocessor &)
Definition: PreProcessor.cc:67
tok_unequals
@ tok_unequals
Definition: PreProcessor.hh:27
preprocessor::accu_t::accu_t
accu_t()
Definition: PreProcessor.cc:491
preprocessor::order_wedge
@ order_wedge
Definition: PreProcessor.hh:51
tok_siblings
@ tok_siblings
Definition: PreProcessor.hh:27
preprocessor::order_prod
@ order_prod
Definition: PreProcessor.hh:50
preprocessor::order_colon
@ order_colon
Definition: PreProcessor.hh:62
preprocessor::cur_str
std::u32string cur_str
Definition: PreProcessor.hh:94
tok_sequence
@ tok_sequence
Definition: PreProcessor.hh:27
operator<<
std::ostream & operator<<(std::ostream &, const preprocessor &)
Definition: PreProcessor.cc:67
tok_wedge
@ tok_wedge
Definition: PreProcessor.hh:27
preprocessor::accu_t::parts
std::vector< std::u32string > parts
Definition: PreProcessor.hh:109
preprocessor::is_closing_bracket_
unsigned int is_closing_bracket_(char32_t c) const
Definition: PreProcessor.cc:149
preprocessor::bracket_strings_
void bracket_strings_(char32_t cb, std::u32string &obrack, std::u32string &cbrack) const
Definition: PreProcessor.cc:324
preprocessor::order_labels
order_labels
Definition: PreProcessor.hh:47
tok_arrow
@ tok_arrow
Definition: PreProcessor.hh:27
preprocessor::orders
const static char32_t orders[]
Definition: PreProcessor.hh:45
tok_declare
@ tok_declare
Definition: PreProcessor.hh:27
preprocessor::order_frac
@ order_frac
Definition: PreProcessor.hh:49
preprocessor::order_set_option
@ order_set_option
Definition: PreProcessor.hh:61
preprocessor::unwind_
bool unwind_(unsigned int tolevel, unsigned int bracketgoal=0, bool usebracket=true) const
Definition: PreProcessor.cc:342
preprocessor
Definition: PreProcessor.hh:36
preprocessor::is_infix_operator_
bool is_infix_operator_(char32_t c) const
Definition: PreProcessor.cc:89
preprocessor::cur_pos
unsigned int cur_pos
Definition: PreProcessor.hh:93
preprocessor::order_less_than
@ order_less_than
Definition: PreProcessor.hh:57
preprocessor::is_digits_
bool is_digits_(const std::u32string &str) const
Definition: PreProcessor.cc:141
preprocessor::next_is_product_
bool next_is_product_
Definition: PreProcessor.hh:75
preprocessor::order_arrow
@ order_arrow
Definition: PreProcessor.hh:60
preprocessor::accu_t::accu
std::u32string accu
Definition: PreProcessor.hh:107