Shinichiro Hamaji | 1d545aa | 2015-06-23 15:29:13 +0900 | [diff] [blame] | 1 | // Copyright 2015 Google Inc. All rights reserved |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 15 | #ifndef EXPR_H_ |
| 16 | #define EXPR_H_ |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 17 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 18 | #include <string> |
| 19 | #include <vector> |
| 20 | |
| 21 | #include "string_piece.h" |
| 22 | |
| 23 | using namespace std; |
| 24 | |
| 25 | class Evaluator; |
Shinichiro Hamaji | f24ed14 | 2015-07-13 20:57:42 -0700 | [diff] [blame] | 26 | struct Loc; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 27 | |
| 28 | class Evaluable { |
| 29 | public: |
| 30 | virtual void Eval(Evaluator* ev, string* s) const = 0; |
Shinichiro Hamaji | fb415ad | 2015-08-14 17:19:34 +0900 | [diff] [blame] | 31 | string Eval(Evaluator*) const; |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 32 | |
| 33 | protected: |
| 34 | Evaluable(); |
| 35 | virtual ~Evaluable(); |
| 36 | }; |
| 37 | |
| 38 | class Value : public Evaluable { |
| 39 | public: |
Sasha Smundak | ae1d58c | 2018-08-22 09:39:42 -0700 | [diff] [blame] | 40 | // All NewExpr calls take ownership of the Value instances. |
Dan Willemsen | ee57a3f | 2018-11-05 16:18:44 -0800 | [diff] [blame] | 41 | static Value* NewExpr(Value* v1, Value* v2); |
| 42 | static Value* NewExpr(Value* v1, Value* v2, Value* v3); |
| 43 | static Value* NewExpr(vector<Value*>* values); |
Sasha Smundak | ae1d58c | 2018-08-22 09:39:42 -0700 | [diff] [blame] | 44 | |
Dan Willemsen | ee57a3f | 2018-11-05 16:18:44 -0800 | [diff] [blame] | 45 | static Value* NewLiteral(StringPiece s); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 46 | virtual ~Value(); |
Shinichiro Hamaji | 347e06f | 2016-01-05 14:41:53 +0900 | [diff] [blame] | 47 | virtual bool IsLiteral() const { return false; } |
Shinichiro Hamaji | 92a4738 | 2016-02-17 17:19:21 +0900 | [diff] [blame] | 48 | // Only safe after IsLiteral() returns true. |
| 49 | virtual StringPiece GetLiteralValueUnsafe() const { return ""; } |
Shinichiro Hamaji | 347e06f | 2016-01-05 14:41:53 +0900 | [diff] [blame] | 50 | |
Dan Willemsen | ee57a3f | 2018-11-05 16:18:44 -0800 | [diff] [blame] | 51 | static string DebugString(const Value*); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 52 | |
| 53 | protected: |
| 54 | Value(); |
| 55 | virtual string DebugString_() const = 0; |
| 56 | }; |
| 57 | |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 58 | enum struct ParseExprOpt { |
| 59 | NORMAL = 0, |
| 60 | DEFINE, |
| 61 | COMMAND, |
Shinichiro Hamaji | 4d77b84 | 2015-06-27 06:10:18 +0900 | [diff] [blame] | 62 | FUNC, |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 63 | }; |
| 64 | |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 65 | Value* ParseExprImpl(const Loc& loc, |
| 66 | StringPiece s, |
| 67 | const char* terms, |
Shinichiro Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame] | 68 | ParseExprOpt opt, |
Dan Willemsen | 3ce083f | 2017-10-11 22:17:48 -0700 | [diff] [blame] | 69 | size_t* index_out, |
| 70 | bool trim_right_space = false); |
| 71 | Value* ParseExpr(const Loc& loc, |
| 72 | StringPiece s, |
Shinichiro Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame] | 73 | ParseExprOpt opt = ParseExprOpt::NORMAL); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 74 | |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 75 | string JoinValues(const vector<Value*>& vals, const char* sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 76 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 77 | #endif // EXPR_H_ |