bux API Reference 1.9.0
Static library of whatever are seen required in general purpose but not directly supported from Modern C++. Or whatever reusable originated from my side projects.
Loading...
Searching...
No Matches
GLR.h
Go to the documentation of this file.
1#pragma once
2
3#include "ParserBase.h" // bux::T_StateID, bux::F_GetProducedT<>, LexBase.h
4#include <memory> // std::shared_ptr<>
5#include <optional> // std::optional<>
6#include <vector> // std::vector<>
7
8namespace bux {
9namespace GLR {
10
11//
12// Constants
13//
14enum
15{
18 ACTION_REDUCE_MIN // Ensure it's the last ACTION_-constant
19};
20
21//
22// Types
23//
24typedef unsigned T_ActionId;
25typedef std::shared_ptr<const I_LexAttr> C_LexPtr;
28
29class C_Parser;
30typedef std::function<void(C_Parser &parser)> F_OncePostShift;
31typedef std::function<void(C_Parser&, const F_GetProduced&, C_LexPtr&, F_OncePostShift&)> FH_Reduce;
32
33class C_Parser;
34
36{
37 // Types
44
45 // Virtuals
46 virtual std::vector<T_ActionId> action(T_StateID state, T_LexID token) const = 0;
47 virtual bool changeToken(T_LexID &token, C_LexPtr &attr) const;
48 virtual size_t getAcceptId() const = 0;
49 virtual bool getTokenName(T_LexID token, std::string &name) const;
50 virtual T_StateID nextState(T_StateID state, T_LexID lex) const = 0;
51 virtual void getReduceInfo(size_t prodId, C_ReduceInfo &info) const = 0;
52 virtual void onError(C_Parser &parser, const C_SourcePos &pos, std::string_view message) const = 0;
53
54 // Nonvirtuals
55 std::string printToken(T_LexID token) const;
56};
57
58class C_Parser: public I_Parser
59{
60public:
61
62 // Data
64
65 // Nonvirtuals
66 C_Parser(const I_ParserPolicy &policy);
67 void eachAccepted(std::function<void(C_LexPtr &)> apply);
68 void onError(const C_SourcePos &pos, std::string_view message);
69 void userData(void *p) { m_userData = p; }
70 void *userData() const { return m_userData; }
71
72 // Implement I_Parser
73 void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr) override;
74 std::string_view setSource(std::string_view src) override;
75
76private:
77
78 // Types
79 struct C_StateLR1;
80 typedef std::shared_ptr<C_StateLR1> C_StateLR1Ptr;
81
82 struct C_StateLR1: C_LexInfo
83 {
84 // Data
85 C_StateLR1Ptr m_prev;
86 T_StateID m_StateID;
87 T_LexID m_TokenID;
88
89 // Nonvirtuals
90 C_StateLR1() = default;
91 C_StateLR1(C_StateLR1 &another);
92 void operator=(C_StateLR1 &another);
93 };
94 typedef std::vector<C_StateLR1Ptr> C_StateLR1Ptrs;
95 typedef std::pair<C_StateLR1Ptr,F_OncePostShift> T_Reduced;
96
97 // Data
98 void *m_userData{};
99 C_StateLR1Ptrs m_curTops, m_accepted;
100 std::string_view m_srcPath;
101 bool m_added{};
102
103 // Nonvirtuals
104 C_Parser(C_Parser &root, const C_StateLR1Ptr &nestedTop);
105 std::optional<T_Reduced> reduceOn(size_t id, C_StateLR1Ptr iTop, const C_SourcePos &pos);
106 static T_StateID state(C_StateLR1Ptr &p);
107};
108
109} // namespace GLR
110} // namespace bux
std::string_view setSource(std::string_view src) override
Definition GLR.cpp:229
C_Parser(const I_ParserPolicy &policy)
Definition GLR.cpp:45
const I_ParserPolicy & m_policy
Definition GLR.h:63
void userData(void *p)
Definition GLR.h:69
void * userData() const
Definition GLR.h:70
void onError(const C_SourcePos &pos, std::string_view message)
Definition GLR.cpp:191
void eachAccepted(std::function< void(C_LexPtr &)> apply)
Definition GLR.cpp:185
void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr) override
Definition GLR.cpp:56
std::function< void(C_Parser &, const F_GetProduced &, C_LexPtr &, F_OncePostShift &)> FH_Reduce
Definition GLR.h:31
C_LexInfoT< const I_LexAttr, std::shared_ptr > C_LexInfo
Definition GLR.h:26
std::function< void(C_Parser &parser)> F_OncePostShift
Definition GLR.h:30
unsigned T_ActionId
Definition GLR.h:24
std::shared_ptr< const I_LexAttr > C_LexPtr
Definition GLR.h:25
@ ACTION_SHIFT
Definition GLR.h:16
@ ACTION_ACCEPT
Definition GLR.h:17
@ ACTION_REDUCE_MIN
Definition GLR.h:18
F_GetProducedT< const I_LexAttr, std::shared_ptr > F_GetProduced
Definition GLR.h:27
THE common namespace of bux library.
Definition AtomiX.cpp:3
std::function< C_LexInfoT< T, C_Ptr > &(size_t)> F_GetProducedT
Definition ParserBase.h:20
uint32_t T_LexID
Definition LexBase.h:35
unsigned T_StateID
Definition ParserBase.h:17
std::string printToken(T_LexID token) const
Definition GLR.cpp:21
virtual bool getTokenName(T_LexID token, std::string &name) const
Definition GLR.cpp:16
virtual std::vector< T_ActionId > action(T_StateID state, T_LexID token) const =0
virtual size_t getAcceptId() const =0
virtual void onError(C_Parser &parser, const C_SourcePos &pos, std::string_view message) const =0
virtual T_StateID nextState(T_StateID state, T_LexID lex) const =0
virtual void getReduceInfo(size_t prodId, C_ReduceInfo &info) const =0
virtual bool changeToken(T_LexID &token, C_LexPtr &attr) const
Definition GLR.cpp:11