Spicy
switch.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <string>
6 #include <utility>
7 #include <vector>
8 
9 #include <spicy/ast/types/unit.h>
10 #include <spicy/compiler/detail/codegen/production.h>
11 
13 
19 public:
20  using Cases = std::vector<std::pair<std::vector<Expression>, Production>>;
21 
22  Switch(const std::string& symbol, Expression expr, Cases cases, std::optional<Production> default_,
23  AttributeSet attributes, const Location& l = location::None)
24  : ProductionBase(symbol, l),
25  _expression(std::move(expr)),
26  _cases(std::move(cases)),
27  _default(std::move(default_)),
28  _attributes(std::move(attributes)) {}
29 
30  const Expression& expression() const { return _expression; }
31  const Cases& cases() const { return _cases; }
32  const std::optional<Production>& default_() const { return _default; }
33  const AttributeSet& attributes() const { return _attributes; }
34 
35  // Production API
36  std::vector<std::vector<Production>> rhss() const;
37  std::optional<spicy::Type> type() const { return {}; }
38  bool nullable() const { return production::nullable(rhss()); }
39  bool eodOk() const {
40  // Always false. If one of the branches is ok with no data, it will
41  // indicate so itself.
42  return false;
43  }
44  bool atomic() const { return false; }
45  std::string render() const;
46 
47 private:
48  Expression _expression;
49  Cases _cases;
50  std::optional<Production> _default;
51  AttributeSet _attributes;
52 };
53 
54 } // namespace spicy::detail::codegen::production
Definition: production.h:24
Definition: production.h:120
const std::string & symbol() const
Definition: production.h:191
Definition: production.h:170
Definition: attribute.h:174
Definition: grammar.h:15
Definition: location.h:18
ProductionBase(std::string symbol, Location l=location::None)
Definition: production.h:181