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: |
| 40 | virtual ~Value(); |
| 41 | |
| 42 | virtual Value* Compact() { return this; } |
| 43 | |
Shinichiro Hamaji | 347e06f | 2016-01-05 14:41:53 +0900 | [diff] [blame] | 44 | virtual bool IsLiteral() const { return false; } |
Shinichiro Hamaji | 505d0d2 | 2016-02-29 20:07:39 +0900 | [diff] [blame] | 45 | // Only safe after IsLiteral() returns true. |
| 46 | virtual StringPiece GetLiteralValueUnsafe() const { return ""; } |
Shinichiro Hamaji | 347e06f | 2016-01-05 14:41:53 +0900 | [diff] [blame] | 47 | |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 48 | string DebugString() const; |
| 49 | |
| 50 | protected: |
| 51 | Value(); |
| 52 | virtual string DebugString_() const = 0; |
| 53 | }; |
| 54 | |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 55 | enum struct ParseExprOpt { |
| 56 | NORMAL = 0, |
| 57 | DEFINE, |
| 58 | COMMAND, |
Shinichiro Hamaji | 4d77b84 | 2015-06-27 06:10:18 +0900 | [diff] [blame] | 59 | FUNC, |
Shinichiro Hamaji | 66bd7bc | 2015-06-19 16:54:06 +0900 | [diff] [blame] | 60 | }; |
| 61 | |
Shinichiro Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame] | 62 | Value* ParseExprImpl(const Loc& loc, StringPiece s, const char* terms, |
| 63 | ParseExprOpt opt, |
Shinichiro Hamaji | d146f4c | 2015-06-17 17:51:24 +0900 | [diff] [blame] | 64 | size_t* index_out, bool trim_right_space = false); |
Shinichiro Hamaji | 36b326f | 2015-06-26 08:56:13 +0900 | [diff] [blame] | 65 | Value* ParseExpr(const Loc& loc, StringPiece s, |
| 66 | ParseExprOpt opt = ParseExprOpt::NORMAL); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 67 | |
Shinichiro Hamaji | 76ff983 | 2015-06-18 17:11:22 +0900 | [diff] [blame] | 68 | string JoinValues(const vector<Value*>& vals, const char* sep); |
Shinichiro Hamaji | 776ca30 | 2015-06-06 03:52:48 +0900 | [diff] [blame] | 69 | |
Shinichiro Hamaji | 784b995 | 2015-06-23 14:29:32 +0900 | [diff] [blame] | 70 | Value* NewExpr2(Value* v1, Value* v2); |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 71 | Value* NewExpr3(Value* v1, Value* v2, Value* v3); |
| 72 | |
Shinichiro Hamaji | b74b890 | 2015-06-22 18:22:30 +0900 | [diff] [blame] | 73 | Value* NewLiteral(StringPiece s); |
Shinichiro Hamaji | 4c469b3 | 2015-06-15 19:53:36 +0900 | [diff] [blame] | 74 | |
Shinichiro Hamaji | 645cca7 | 2015-09-24 17:04:21 +0900 | [diff] [blame] | 75 | #endif // EXPR_H_ |