blob: 2fe9527efb423f51a37b20f404160e1737734cb1 [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:
Sasha Smundakae1d58c2018-08-22 09:39:42 -070040 // All NewExpr calls take ownership of the Value instances.
Dan Willemsenee57a3f2018-11-05 16:18:44 -080041 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 Smundakae1d58c2018-08-22 09:39:42 -070044
Dan Willemsenee57a3f2018-11-05 16:18:44 -080045 static Value* NewLiteral(StringPiece s);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090046 virtual ~Value();
Shinichiro Hamaji347e06f2016-01-05 14:41:53 +090047 virtual bool IsLiteral() const { return false; }
Shinichiro Hamaji92a47382016-02-17 17:19:21 +090048 // Only safe after IsLiteral() returns true.
49 virtual StringPiece GetLiteralValueUnsafe() const { return ""; }
Shinichiro Hamaji347e06f2016-01-05 14:41:53 +090050
Dan Willemsenee57a3f2018-11-05 16:18:44 -080051 static string DebugString(const Value*);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090052
53 protected:
54 Value();
55 virtual string DebugString_() const = 0;
56};
57
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +090058enum struct ParseExprOpt {
59 NORMAL = 0,
60 DEFINE,
61 COMMAND,
Shinichiro Hamaji4d77b842015-06-27 06:10:18 +090062 FUNC,
Shinichiro Hamaji66bd7bc2015-06-19 16:54:06 +090063};
64
Dan Willemsen3ce083f2017-10-11 22:17:48 -070065Value* ParseExprImpl(const Loc& loc,
66 StringPiece s,
67 const char* terms,
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +090068 ParseExprOpt opt,
Dan Willemsen3ce083f2017-10-11 22:17:48 -070069 size_t* index_out,
70 bool trim_right_space = false);
71Value* ParseExpr(const Loc& loc,
72 StringPiece s,
Shinichiro Hamaji36b326f2015-06-26 08:56:13 +090073 ParseExprOpt opt = ParseExprOpt::NORMAL);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090074
Shinichiro Hamaji76ff9832015-06-18 17:11:22 +090075string JoinValues(const vector<Value*>& vals, const char* sep);
Shinichiro Hamaji776ca302015-06-06 03:52:48 +090076
Shinichiro Hamaji645cca72015-09-24 17:04:21 +090077#endif // EXPR_H_