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 15 #ifndef EXPR_H_ 16 #define EXPR_H_ 17 18 #include <string> 19 #include <vector> 20 21 #include "string_piece.h" 22 23 using namespace std; 24 25 class Evaluator; 26 struct Loc; 27 28 class Evaluable { 29 public: 30 virtual void Eval(Evaluator* ev, string* s) const = 0; 31 string Eval(Evaluator*) const; 32 33 protected: 34 Evaluable(); 35 virtual ~Evaluable(); 36 }; 37 38 class Value : public Evaluable { 39 public: 40 // All NewExpr calls take ownership of the Value instances. 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); 44 45 static Value* NewLiteral(StringPiece s); 46 virtual ~Value(); 47 virtual bool IsLiteral() const { return false; } 48 // Only safe after IsLiteral() returns true. 49 virtual StringPiece GetLiteralValueUnsafe() const { return ""; } 50 51 static string DebugString(const Value*); 52 53 protected: 54 Value(); 55 virtual string DebugString_() const = 0; 56 }; 57 58 enum struct ParseExprOpt { 59 NORMAL = 0, 60 DEFINE, 61 COMMAND, 62 FUNC, 63 }; 64 65 Value* ParseExprImpl(const Loc& loc, 66 StringPiece s, 67 const char* terms, 68 ParseExprOpt opt, 69 size_t* index_out, 70 bool trim_right_space = false); 71 Value* ParseExpr(const Loc& loc, 72 StringPiece s, 73 ParseExprOpt opt = ParseExprOpt::NORMAL); 74 75 string JoinValues(const vector<Value*>& vals, const char* sep); 76 77 #endif // EXPR_H_ 78