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