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
LR1.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 "XAutoPtr.h" // bux::C_AutoNode<>
5#include "Xtack.h" // bux::C_ResourceStack<>
6
7namespace bux {
8namespace LR1 {
9
10//
11// Constants
12//
13enum
14{
18 ACTION_REDUCE_MIN // Ensure it's the last ACTION_-constant
19};
20
21//
22// Types
23//
26
27class C_Parser;
28
30{
31 // Types
32 typedef std::function<void(C_Parser &parser, const F_GetProduced &args, C_LexPtr &ret)> FH_Reduce;
33
40
41 // Data
43
44 // Ctor/Dtor
45 constexpr I_ParserPolicy(T_LexID idError): m_IdError(idError) {}
46 virtual ~I_ParserPolicy() =default;
47
48 // (Almost) Pure virtuals
49 virtual size_t action(T_StateID state, T_LexID token) const =0;
51 virtual bool changeToken(T_LexID &token, C_LexPtr &attr) const;
53 virtual size_t getAcceptId() const =0;
55 virtual bool getTokenName(T_LexID token, std::string &name) const;
57 virtual T_StateID nextState(T_StateID state, T_LexID lex) const =0;
59 virtual void getReduceInfo(size_t id, C_ReduceInfo &info) const =0;
61 virtual void onError(C_Parser &parser, const C_SourcePos &pos, std::string_view message) const =0;
63
64 // Nonvirtuals
65 std::string printToken(T_LexID token) const;
66};
67
69
70class C_Parser: public I_Parser
71{
72public:
73
74 // Data
76
77 // Ctor/Dtor
78 C_Parser(const I_ParserPolicy &policy);
79
80 // Nonvirtuals
81 bool accepted() const { return m_Accepted; }
82 auto &getFinalLex() { return m_CurStack.top(); }
83 void onError(const C_SourcePos &pos, std::string_view message);
84 void reservePostShift(std::function<void()> calledOnce, unsigned shifts);
85
86 // Implement I_Parser
87 void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr) override;
88 std::string_view setSource(std::string_view src) override;
89
90private:
91
92 // Types
93 struct C_StateLR1: C_LexInfo
94 {
95 // Data
96 T_StateID m_StateID{};
97 T_LexID m_TokenID{};
98 };
99 typedef C_ResourceStack<C_StateLR1> C_StateStackLR1;
100
101 // Data
102 C_StateStackLR1 m_CurStack;
103 std::string_view m_CurSrc;
104 T_StateID m_ErrState;
105 T_LexID m_ErrToken;
106 C_SourcePos m_ErrPos;
107 std::function<void()> m_OnPostShift;
108 unsigned m_ShiftCountdown;
109 bool m_Accepted;
110
111 // Nonvirtuals
112 void add(T_LexID token, C_LexInfo &info, C_StateStackLR1 &unreadStack);
113 // Called by public add() or itself
114 T_StateID currentState() const;
115 //
116 void panicRollback(C_StateStackLR1 &unreadStack);
117 //
118 bool recover(T_LexID token, C_LexInfo &info, C_StateStackLR1 &unreadStack);
119 // Adjust the current state so that parsing can continue.
120 // Return true on success
121 bool reduceOn(size_t id, const C_SourcePos &pos);
122 //
123 void shift(T_LexID lex, C_LexInfo &info);
124 // Shift next state thru given token
125};
126
127template<class T_Data>
128struct C_NewLex: C_NewNode<C_LexDataT<T_Data>>
129{
130 // Nonvirtuals
131 C_NewLex() = default;
132 explicit C_NewLex(C_LexInfo &i): C_NewNode<C_LexDataT<T_Data>>(C_Void{})
133 {
134 if (!i)
135 this->assign(new C_LexDataT<T_Data>, true);
136 else if (!this->takeOver(i.m_attr))
137 RUNTIME_ERROR("{}", typeid(*i).name());
138 }
139 template<class...T_Args>
140 explicit C_NewLex(T_Args&&...args):
141 C_NewNode<C_LexDataT<T_Data>>(std::forward<T_Args>(args)...) {}
142};
143
144} // namespace LR1
145} // namespace bux
#define RUNTIME_ERROR(fmtStr,...)
Wrap FILE(DATE)#__LINE__ FUNCTION: msg into std::runtime_error.
Definition XException.h:32
bool takeOver(C_AutoNode< T2 > &another)
Definition XAutoPtr.h:125
void assign(C_LexDataT< T_Data > *ptr, bool owned)
Definition XAutoPtr.h:94
auto & top()
Definition Xtack.h:70
const I_ParserPolicy & m_Policy
Definition LR1.h:75
void onError(const C_SourcePos &pos, std::string_view message)
Definition LR1.cpp:183
void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr) override
Definition LR1.cpp:54
bool accepted() const
Definition LR1.h:81
auto & getFinalLex()
Definition LR1.h:82
C_Parser(const I_ParserPolicy &policy)
Definition LR1.cpp:46
std::string_view setSource(std::string_view src) override
Definition LR1.cpp:286
void reservePostShift(std::function< void()> calledOnce, unsigned shifts)
Definition LR1.cpp:280
F_GetProducedT< I_LexAttr, C_AutoNode > F_GetProduced
Definition LR1.h:25
C_LexInfoT< I_LexAttr, C_AutoNode > C_LexInfo
Definition LR1.h:68
C_AutoNode< I_LexAttr > C_LexPtr
Definition LR1.h:24
@ ACTION_REDUCE_MIN
Definition LR1.h:18
@ ACTION_ACCEPT
Definition LR1.h:16
@ ACTION_SHIFT
Definition LR1.h:15
@ ACTION_ERROR
Definition LR1.h:17
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
Render any copyable "type" token attribute on the fly.
Definition LexBase.h:146
C_Ptr< T > m_attr
Definition LexBase.h:55
C_NewLex(T_Args &&...args)
Definition LR1.h:140
C_NewLex(C_LexInfo &i)
Definition LR1.h:132
std::function< void(C_Parser &parser, const F_GetProduced &args, C_LexPtr &ret)> FH_Reduce
Definition LR1.h:32
virtual size_t action(T_StateID state, T_LexID token) const =0
Return action kind.
const T_LexID m_IdError
Definition LR1.h:42
std::string printToken(T_LexID token) const
Definition LR1.cpp:22
virtual T_StateID nextState(T_StateID state, T_LexID lex) const =0
Goto table.
virtual bool getTokenName(T_LexID token, std::string &name) const
Return true if token has a name.
Definition LR1.cpp:17
virtual size_t getAcceptId() const =0
Return id which will be passed as the 1st arg of getReduceInfo()
constexpr I_ParserPolicy(T_LexID idError)
Definition LR1.h:45
virtual ~I_ParserPolicy()=default
virtual void getReduceInfo(size_t id, C_ReduceInfo &info) const =0
Get reduction if available.
virtual bool changeToken(T_LexID &token, C_LexPtr &attr) const
A chance to save action error.
Definition LR1.cpp:12
virtual void onError(C_Parser &parser, const C_SourcePos &pos, std::string_view message) const =0
Report error (to log or to throw)