Spicy
look-ahead.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <memory>
6 #include <set>
7 #include <string>
8 #include <utility>
9 #include <vector>
10 
11 #include <spicy/ast/types/unit.h>
12 #include <spicy/compiler/detail/codegen/production.h>
13 
15 
16 namespace look_ahead {
17 enum class Default { First, Second, None };
18 } // namespace look_ahead
19 
25 public:
26  LookAhead(const std::string& symbol, Production alt1, Production alt2, look_ahead::Default def,
27  const Location& l = location::None)
28  : ProductionBase(symbol, l),
29  _alternatives(std::make_pair(std::move(alt1), std::move(alt2))),
30  _default(def),
31  _lahs(new std::pair<std::set<Production>, std::set<Production>>) {}
32 
33  LookAhead(const std::string& symbol, Production alt1, Production alt2, const Location& l = location::None)
34  : LookAhead(symbol, std::move(alt1), std::move(alt2), look_ahead::Default::None, l) {}
35 
37  const std::pair<Production, Production>& alternatives() const { return _alternatives; }
38 
40  look_ahead::Default default_() const { return _default; }
41 
47  const std::pair<std::set<Production>, std::set<Production>>& lookAheads() const { return *_lahs; }
48 
53  void setLookAheads(std::pair<std::set<Production>, std::set<Production>>&& lahs) { *_lahs = std::move(lahs); }
54 
55  // Production API
56  std::vector<std::vector<Production>> rhss() const { return {{_alternatives.first}, {_alternatives.second}}; }
57  std::optional<spicy::Type> type() const { return {}; }
58  bool nullable() const { return production::nullable(rhss()); }
59  bool eodOk() const { return nullable(); }
60  bool atomic() const { return false; }
61  std::string render() const;
62 
63 private:
64  std::pair<Production, Production> _alternatives;
65  look_ahead::Default _default;
66 
67  // This violates value-semantics but we need to share updates with
68  // existing copies of the production
69  std::shared_ptr<std::pair<std::set<Production>, std::set<Production>>> _lahs;
70 };
71 
72 } // namespace spicy::detail::codegen::production
void setLookAheads(std::pair< std::set< Production >, std::set< Production >> &&lahs)
Definition: look-ahead.h:53
Definition: production.h:24
const std::pair< Production, Production > & alternatives() const
Definition: look-ahead.h:37
Definition: production.h:120
Definition: production.h:170
Definition: grammar.h:15
Definition: location.h:17
const std::pair< std::set< Production >, std::set< Production > > & lookAheads() const
Definition: look-ahead.h:47
look_ahead::Default default_() const
Definition: look-ahead.h:40