3#include "elementa/license.inc"
4#include "elementa/checks.inc"
6#ifndef ELEMENTA_PARSING_PARSING_H
7#define ELEMENTA_PARSING_PARSING_H
20#include "elementa/base/serial_channels.h"
75 "Unexpected parsed element",
89 "Expected some parsed element",
103 "Invalid parsed element",details)} {}
116 "Redefined parsed element",details)} {}
129 "Undefined parsed element",details)} {}
185 Info & operator=(
const Info & oth) =
default;
186 Info & operator=(
Info && oth) =
default;
187 ~Info(
void) =
default;
197 ELE_CODE_TRACE({},
"StepInfo::(ma)constr.Creating match SI this =" <<
206 ELE_CODE_TRACE({},
"StepInfo::(ri)constr. Creating rule SI this =" <<
216 ELE_CODE_TRACE({},
"StepInfo::assign, oth!=this = " <<
220 info = oth.info; }
return(*
this); }
225 info{std::move(oth.info)}
231 ELE_CODE_TRACE({},
"StepInfo::move-assign, oth!=this = " <<
234 {
kind = oth.kind;
loc = std::move(oth.loc);
235 info = std::move(oth.info); }
return(*
this); }
245 (
kind == Kind::kMatch ? (*info.
vt) == (*(o.info.
vt)) :
246 (*info.
rit) == (*(o.info.
rit)))); }
303 Info & operator=(
const Info & oth) =
default;
304 Info & operator=(
Info && oth) =
default;
305 ~Info(
void) =
default;
312 kind{Kind::kEmptyTable},
319 kind{Kind::kUnexpTerminal},
333 {
kind = oth.
kind;
loc = oth.
loc; info = oth.info; }
return(*
this); }
339 info{std::move(oth.info)}
345 {
kind = oth.kind;
loc = std::move(oth.loc);
346 info = std::move(oth.info); }
return(*
this); }
355 (
kind == Kind::kUnexpTerminal ?
441 gr_{gr},lexer_{lex},transftid_{transftid} {}
529 template <
template <
class,
class>
class Gr>
548 using BaseTree::BaseTree;
601 ASTObserver(
const AST::BaseTree::BGraph & exemplar):ast_{exemplar} {}
616 {
return(pendingtrees_.empty() ?
typename AST::Ptr{} :
617 pendingtrees_.back()); }
An index in a LL(1) parsing table: non-terminal + terminal.
An index of a rule in the grammar, i.e., an iterator on the rules.
Abstract class for observing the steps of the parser.
virtual void errorStep(const ErrorInfo &e, Parser &p)=0
A method to receive any error indication related to parsing.
virtual void okStep(const StepInfo &i, Parser &p)=0
A method to receive the result of a parsing step when it is ok.
std::pair< CodeElement, RHS > Rule
A rule in the grammar (only one alternative for the left side).
TokenId CodeElement
Unique code of an element of the grammar, either terminal or non-term.
#define ELE_CODE_TRACE_OFF
Place this inside local scope (e.g., routine) to deactivate traces there.
const char * explanation(void) const noexcept
Return the explanation only. It will live as long as this exception.
Base class for all errors / exceptions in Elementa. Just derive from it.
#define ELE_CLASS_EXCOVERRIDE(C)
Shortening macro that must be used inside classes derived from Exc.
#define ELE_CODE_UNIMPLEMENTED
To throw an unimplemented exception.
LongestUnsigned to_number(const void *p)
Convert a pointer address to a number.
All graph classes derive from this one.
A mutable iterator that can have multiple iteration behaviors. Forward case.
std::shared_ptr< Token > Ptr
Safe pointer to a token.
A lexer that produces terminals.
Base class for any token (i.e., lexical element) produced by a lexer.
ErrorInfo(const UnexpectedTerminal &ut, const elementa::base::SerChLoc::Ptr &l)
Constructor for unexpected terminal.
AST::Ptr lastSubtree(void)
Return a pointer to the last formed subtree in the AST.
ErrorInfo(const ErrorInfo &oth)
Copy constructor.
Lexer & lexer(void) const noexcept
Return a reference to the internal lexer.
std::pair< CFGrammar::CodeElement, Token::Ptr > UnexpectedTerminal
An unexpected terminal: first is the expected one, second the found one.
ErrorInfo & operator=(const ErrorInfo &oth)
Copy assignment.
virtual Result parseStep(Observer &obs)=0
Must do one step of the parsing, leaving the parser ready for the next.
ASTObserver(const AST::BaseTree::BGraph &exemplar)
Create an empty AST based on the given implementation of graphs.
virtual void reset(void)=0
Must prepare to begin a new parsing (not needed after construction).
Parser(const CFGrammar &gr, Lexer &lex, const TransfTokenId &transftid=[](Token &) ->void{})
Constructor for a grammar.
AST & ast(void)
Return a reference to the completely formed AST.
bool operator==(const StepInfo &o) const
Equality operator.
char EdgeData
Edge data type.
static BaseGraph::VRange_Pred::Pred kPredTerminal
A predicate to iterate on AST nodes that are terminals.
bool operator!=(const ErrorInfo &o) const
Unequality.
void moveToNextMatch(BaseGraph::eiterator &eit) const
Increments EIT until its destination is a match or end() is reached.
virtual void printStatus(elementa::base::OutSerCh &chout) const =0
Must serialize current status in text format through CHOUT, for debug.
void errorStep(const ErrorInfo &e, Parser &p)
A method to receive any error indication related to parsing.
const CFGrammar & grammar(void) const noexcept
Return a reference to the internal grammar.
std::string to_string(const CFGrammar &gr) const
Conversor to a string.
const StepInfo & edgeEnd(const BaseGraph::eiterator &eit) const
Return a reference to the StepInfo of the destination of eit.
~StepInfo(void)
Destructor.
const CFGrammar::Rule & edgeEndRule(const BaseGraph::eiterator &eit, CFGrammar::CodeElement c) const
Return the rule of the grammar of a rule node in the tree.
ELE_CLASS_ENUM(Kind, kMatch, kRule)
Kinds of result after a successful parsing step.
Gr< VertexData, EdgeData > GraphImpl
Possible implementations of the graph that supports the tree.
Parser(void)=default
Destructor.
bool operator!=(const StepInfo &o) const
Unequality.
void parse(Observer &obs)
Parse the input until a complete sentence of the language is read.
StepInfo(const StepInfo &oth)
Copy constructor.
StepInfo(const Token::Ptr &vt, const elementa::base::SerChLoc::Ptr &l)
Constructor of kind kMatch.
std::function< void(Token &) > TransfTokenId
Transformation of TokenId into CFGrammar::CodeElement.
StepInfo(const CFGrammar::RuleIterator &ri, const elementa::base::SerChLoc::Ptr &l)
Constructor of kind kRule.
StepInfo & operator=(StepInfo &&oth)
Move assignment.
elementa::base::SerChLoc::Ptr loc
Position in the input of token/rule.
typename elementa::adts::Tree< VertexData, EdgeData > BaseTree
Tree type for the AST.
ELE_CLASS_ENUM(Result, kFinished, kMoreSteps, kNotInTable, kMismatch)
Result codes after a parsing step.
StepInfo(StepInfo &&oth)
Move constructor.
StepInfo & operator=(const StepInfo &oth)
Copy assignment.
std::string to_string(const CFGrammar &gr) const
Conversor to a string.
ErrorInfo & operator=(ErrorInfo &&oth)
Move assignment.
elementa::base::SerChLoc::Ptr loc
loc of parser when error.
const std::string & edgeEndMatch(const BaseGraph::eiterator &eit, CFGrammar::CodeElement c) const
Return the token value of a match node in the tree.
bool operator==(const ErrorInfo &o) const
Equality operator.
~ErrorInfo(void)
Destructor.
void okStep(const StepInfo &i, Parser &p)
A method to receive the result of a parsing step when it is ok.
ErrorInfo(ErrorInfo &&oth)
Move constructor.
void moveToNextRule(BaseGraph::eiterator &eit) const
Increments EIT until its destination is a rule or end() is reached.
ErrorInfo(const CFGrammar::LL1TableIndex &ti, const elementa::base::SerChLoc::Ptr &l)
Constructor for table index.
ELE_CLASS_ENUM(Kind, kUnexpTerminal, kEmptyTable)
Kinds of errors during parsing.
An AST (abstract syntax tree) for a parsing.
A Parser::Observer that creates an AST during parsing.
Parameters to inform about a parsing error.
Abtract base class for any parser of context-free grammars.
Info associated to a sucessful parsing step.
std::shared_ptr< SerChLoc > Ptr
Safe pointer for polymorphic behaviours.
std::ostream OutSerCh
"Base class" that represents any output serial channel in Elementa.
std::string concatWithMiddle(const std::string &s1, const std::string &s2, const std::string &m=". ")
Concatenate two strings putting a middle one only if the second is not empty.
std::shared_ptr< Tree > Ptr
Pointer to a tree.
std::vector< Ptr > Forest
A forest is a set of pointers to trees.
This class is the base for any tree implementation.
Info data about the error, depending on the kind.
CFGrammar::LL1TableIndex emptytableindex
kEmptyTable: a table index is not filled with anything.
Info(const UnexpectedTerminal &ut)
Constructor from unexpected pair.
UnexpectedTerminal unexppair
kUnexpTerminal: the expected and found terminals, respectively.
Info(const CFGrammar::LL1TableIndex &ti)
Constructor from table index.
Information depending on the kind.
Info(const CFGrammar::RuleIterator &ri)
Constructor from a rule iterator.
Token::Ptr vt
kMatch: code of the terminal plus its value.
CFGrammar::RuleIterator rit
kRule: rule that is to be emitted.
Info(const Token::Ptr &v)
Constructor from a valued terminal.