30 return "bux::TID_EOF";
32 return "<@> aka bux::ROOT_NID";
35 return std::format(
"bux::TOKENGEN_LB+{}", token -
TOKENGEN_LB);
37 auto out = std::format(
"0x{:x}", token);
38 if (isascii(
int(token)))
39 out.append(
" or \'").append(
asciiLiteral(token)) +=
'\'';
50 m_policy(root.m_policy),
51 m_userData(root.m_userData)
53 m_curTops.emplace_back(nestedTop);
59 info.
m_attr.reset(unownedAttr);
64 if (!m_accepted.empty())
65 onError(info,
"Already accepted");
67 std::vector<std::pair<C_StateLR1Ptr,F_OncePostShift>> reduced;
68 C_StateLR1Ptrs nextTops;
70 C_StateLR1Ptrs curTops{m_curTops};
71 while (!reduced.empty() || !curTops.empty() || !m_added)
79 std::tie(iTop,iPostShift) = reduced.back();
82 else if (!curTops.empty())
84 iTop = curTops.back();
87 const auto fromState = state(iTop);
93 C_StateLR1Ptr t{
new C_StateLR1};
99 nextTops.emplace_back(t);
103 iPostShift(nestedGLR);
104 nextTops.insert(nextTops.end(), nestedGLR.m_curTops.begin(), nestedGLR.m_curTops.end());
105 m_accepted.insert(m_accepted.end(), nestedGLR.m_accepted.begin(), nestedGLR.m_accepted.end());
113 m_accepted.push_back(ret->first);
115 onError(info,
"Reduction error on acception");
120 iPostShift(nestedGLR);
121 for (
auto &k: nestedGLR.m_curTops)
122 m_accepted.push_back(k);
124 if (!nestedGLR.m_accepted.empty())
125 onError(info,
"Doubly accepted");
132 if (
auto ret = reduceOn(prodId, iTop, info))
135 reduced.emplace_back(*ret);
137 curTops.emplace_back(ret->first);
140 onError(info,
"Reduction error on production "+std::to_string(prodId));
143 onError(info,
"Unknown action id "+std::to_string(j));
146 if (nextTops.empty() && m_accepted.empty())
153 if (
auto *attr = info.
m_attr.get())
154 out.append(
" of attr type ").append(
HRTN(*attr));
156 out +=
" with null attr";
158 if (
const auto n = m_curTops.size())
160 out +=
"\nStack dump";
162 out += std::format(
" on {} paths", n);
166 for (
auto i: m_curTops)
168 out += std::format(
"\nPath[{}]:", ind++);
171 out += std::format(
"\n({},{})\t", i->m_pos.m_Line, i->m_pos.m_Col);
173 out +=
HRTN(*i->m_attr);
182 m_curTops.swap(nextTops);
187 for (
auto &i: m_accepted)
196auto C_Parser::reduceOn(
size_t id, C_StateLR1Ptr iTop,
const C_SourcePos &pos) -> std::optional<T_Reduced>
199 m_policy.getReduceInfo(
id, ri);
202 while (arr.size() < size)
207 arr.emplace_back(iTop);
211 C_StateLR1Ptr r{
new C_StateLR1};
216 onError(pos,
"Production["+std::to_string(
id)+
"] out of range "+std::to_string(n)+
'/'+std::to_string(size));
218 return *arr.rbegin()[
int(n)];
221 r->m_pos = size? arr.back()->m_pos: pos;
223 const auto curStateId = state(iTop);
226 return std::optional<T_Reduced>{std::in_place, r, ps};
231 const auto ret = m_srcPath;
236T_StateID C_Parser::state(C_StateLR1Ptr &p)
238 return p? p->m_StateID:
decltype(p->m_StateID){};
241C_Parser::C_StateLR1::C_StateLR1(C_StateLR1 &another):
243 m_StateID(another.m_StateID),
244 m_TokenID(another.m_TokenID)
248void C_Parser::C_StateLR1::operator=(C_StateLR1 &another)
251 m_StateID =another.m_StateID;
252 m_TokenID =another.m_TokenID;
std::string_view setSource(std::string_view src) override
C_Parser(const I_ParserPolicy &policy)
const I_ParserPolicy & m_policy
void onError(const C_SourcePos &pos, std::string_view message)
void eachAccepted(std::function< void(C_LexPtr &)> apply)
void add(T_LexID token, unsigned line, unsigned col, I_LexAttr *unownedAttr) override
C_LexInfoT< const I_LexAttr, std::shared_ptr > C_LexInfo
std::function< void(C_Parser &parser)> F_OncePostShift
std::shared_ptr< const I_LexAttr > C_LexPtr
THE common namespace of bux library.
std::string asciiLiteral(uint32_t utf32)
C_LexInfoT & operator=(C_LexInfoT &another)
std::string_view m_Source
std::string printToken(T_LexID token) const
virtual bool getTokenName(T_LexID token, std::string &name) const
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 bool changeToken(T_LexID &token, C_LexPtr &attr) const