blob: 588c6f7f4fd4aade3707c452c83bbdbf5d32c6a8 [file] [log] [blame]
Shinichiro Hamaji1d545aa2015-06-23 15:29:13 +09001// 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 Hamaji645cca72015-09-24 17:04:21 +090015#ifndef EXPR_H_
16#define EXPR_H_
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090017
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090018#include <string>
19#include <vector>
20
21#include "string_piece.h"
22
23using namespace std;
24
25class Evaluator;
Shinichiro Hamajif24ed142015-07-13 20:57:42 -070026struct Loc;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090027
28class Evaluable {
29 public:
30 virtual void Eval(Evaluator* ev, string* s) const = 0;
Shinichiro Hamajifb415ad2015-08-14 17:19:34 +090031 string Eval(Evaluator*) const;
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090032
33 protected:
34 Evaluable();
35 virtual ~Evaluable();
36};
37
38class Value : public Evaluable {
39 public:
40 virtual ~Value();
41
42 virtual Value* Compact() { return this; }
43
Shinichiro Hamaji347e06f2016-01-05 14:41:53 +090044 virtual bool IsLiteral() const { return false; }
Shinichiro Hamaji92a47382016-02-17 17:19:21 +090045 // Only safe after IsLiteral() returns true.
46 virtual StringPiece GetLiteralValueUnsafe() const { return ""; }
Shinichiro Hamaji347e06f2016-01-05 14:41:53 +090047
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090048 string DebugString() const;
49
50 protected:
51 Value();
52 virtual string DebugString_() const = 0;
53};
54
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +090055enum struct ParseExprOpt {
56 NORMAL = 0,
57 DEFINE,
58 COMMAND,
Shinichiro Hamaji4d77b842015-06-27 06:10:18 +090059 FUNC,
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +090060};
61
Dan Willemsen3ce083f2017-10-11 22:17:48 -070062Value* ParseExprImpl(const Loc& loc,
63 StringPiece s,
64 const char* terms,
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +090065 ParseExprOpt opt,
Dan Willemsen3ce083f2017-10-11 22:17:48 -070066 size_t* index_out,
67 bool trim_right_space = false);
68Value* ParseExpr(const Loc& loc,
69 StringPiece s,
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +090070 ParseExprOpt opt = ParseExprOpt::NORMAL);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090071
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +090072string JoinValues(const vector<Value*>& vals, const char* sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090073
Shinichiro Hamaji784b9952015-06-23 14:29:32 +090074Value* NewExpr2(Value* v1, Value* v2);
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090075Value* NewExpr3(Value* v1, Value* v2, Value* v3);
76
Shinichiro Hamajib74b8902015-06-22 18:22:30 +090077Value* NewLiteral(StringPiece s);
Shinichiro Hamaji4c469b32015-06-15 19:53:36 +090078
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090079#endif // EXPR_H_