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   virtual ~Value();
41 
Compact()42   virtual Value* Compact() { return this; }
43 
IsLiteral()44   virtual bool IsLiteral() const { return false; }
45   // Only safe after IsLiteral() returns true.
GetLiteralValueUnsafe()46   virtual StringPiece GetLiteralValueUnsafe() const { return ""; }
47 
48   string DebugString() const;
49 
50  protected:
51   Value();
52   virtual string DebugString_() const = 0;
53 };
54 
55 enum struct ParseExprOpt {
56   NORMAL = 0,
57   DEFINE,
58   COMMAND,
59   FUNC,
60 };
61 
62 Value* ParseExprImpl(const Loc& loc, StringPiece s, const char* terms,
63                      ParseExprOpt opt,
64                      size_t* index_out, bool trim_right_space = false);
65 Value* ParseExpr(const Loc& loc, StringPiece s,
66                  ParseExprOpt opt = ParseExprOpt::NORMAL);
67 
68 string JoinValues(const vector<Value*>& vals, const char* sep);
69 
70 Value* NewExpr2(Value* v1, Value* v2);
71 Value* NewExpr3(Value* v1, Value* v2, Value* v3);
72 
73 Value* NewLiteral(StringPiece s);
74 
75 #endif  // EXPR_H_
76