fix compile errors
This commit is contained in:
parent
b2a1883369
commit
aaeb6438a1
27 changed files with 19 additions and 21 deletions
62
.back/old/Parser/public/CLR.hpp
Normal file
62
.back/old/Parser/public/CLR.hpp
Normal file
|
|
@ -0,0 +1,62 @@
|
|||
#pragma once
|
||||
|
||||
#include "CfGrammar.hpp"
|
||||
|
||||
namespace tp {
|
||||
class CLR {
|
||||
public:
|
||||
struct ASTNode {
|
||||
const CfGrammar::Rule* production = nullptr;
|
||||
Map<String, String> terminals;
|
||||
};
|
||||
|
||||
class Automation {
|
||||
public:
|
||||
class State {};
|
||||
};
|
||||
|
||||
class TerminalStream {
|
||||
public:
|
||||
typedef ualni TerminalID;
|
||||
|
||||
public:
|
||||
TerminalStream() = default;
|
||||
virtual TerminalID getNextTerminal() = 0;
|
||||
};
|
||||
|
||||
public:
|
||||
struct BuildError {
|
||||
String description;
|
||||
};
|
||||
|
||||
struct ParseError {
|
||||
const Automation::State* state = nullptr;
|
||||
String::Index location = 0;
|
||||
};
|
||||
|
||||
private:
|
||||
class SententialStack {};
|
||||
|
||||
public:
|
||||
CLR() = default;
|
||||
|
||||
void setGrammar(const CfGrammar& grammar);
|
||||
void setTerminal(const String& name, TerminalStream::TerminalID id);
|
||||
void build();
|
||||
|
||||
void parse(TerminalStream* stream, List<ASTNode>& out, ASTNode** root);
|
||||
|
||||
[[nodiscard]] bool isBuild() { return mState == ParserState::BUILD; }
|
||||
[[nodiscard]] const BuildError& getBuildError() { return mBuildError; }
|
||||
[[nodiscard]] const ParseError& getParseError() { return mParseError; }
|
||||
|
||||
private:
|
||||
enum class ParserState { NONE, BUILD, FAILED } mState = ParserState::NONE;
|
||||
|
||||
ParseError mParseError;
|
||||
BuildError mBuildError;
|
||||
|
||||
SententialStack mStack;
|
||||
Automation mAutomation;
|
||||
};
|
||||
}
|
||||
89
.back/old/Parser/public/CfGrammar.hpp
Normal file
89
.back/old/Parser/public/CfGrammar.hpp
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
#pragma once
|
||||
|
||||
#include "List.hpp"
|
||||
#include "Map.hpp"
|
||||
#include "Strings.hpp"
|
||||
|
||||
namespace tp {
|
||||
|
||||
struct CfGrammar {
|
||||
struct Rule {
|
||||
struct Arg {
|
||||
String id;
|
||||
bool isTerminal = false;
|
||||
bool isEpsilon = false;
|
||||
|
||||
bool operator==(const Arg& in) const { return (id == in.id) && (isEpsilon == in.isEpsilon) && (isTerminal == in.isTerminal); }
|
||||
};
|
||||
|
||||
String id;
|
||||
List<Arg> args;
|
||||
|
||||
bool operator==(const Rule& in) const { return (id == in.id) && (args == in.args); }
|
||||
|
||||
[[nodiscard]] bool isProductive() const {
|
||||
for (auto arg : args) {
|
||||
if (arg->id == id) return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
struct Sentence {
|
||||
struct Term {
|
||||
String id;
|
||||
bool terminal;
|
||||
bool operator==(const Term& in) const { return (id == in.id) && (terminal == in.terminal); }
|
||||
};
|
||||
List<Term> terms;
|
||||
};
|
||||
|
||||
List<Rule> rules;
|
||||
String startTerminal;
|
||||
|
||||
private:
|
||||
struct NonTerminal {
|
||||
|
||||
List<Rule*> rules;
|
||||
Map<String, NonTerminal*> references;
|
||||
Map<String, NonTerminal*> referencing;
|
||||
|
||||
[[nodiscard]] bool isProductive() const {
|
||||
for (auto rule : rules) {
|
||||
if (rule->isProductive()) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
[[nodiscard]] bool isLooped(Map<String, ualni>& processed, const String& id) const {
|
||||
for (auto ref : referencing) {
|
||||
if (processed.presents(ref->key)) return true;
|
||||
}
|
||||
processed.put(id, {});
|
||||
for (auto ref : referencing) {
|
||||
if (ref->val->isLooped(processed, ref->key)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
struct Terminal {};
|
||||
|
||||
Map<String, Terminal> mTerminals;
|
||||
Map<String, NonTerminal> mNonTerminals;
|
||||
bool mIsLooped = false;
|
||||
|
||||
public:
|
||||
static struct CfGrammarParserState* initializeCfGrammarParser();
|
||||
static void deinitializeCfGrammarParser(CfGrammarParserState*);
|
||||
|
||||
public:
|
||||
bool parse(CfGrammarParserState* context, const String& source);
|
||||
bool compile();
|
||||
|
||||
void generateSentences(List<Sentence>& out);
|
||||
static void printSentence(Sentence& in);
|
||||
|
||||
[[nodiscard]] bool isLooped() const;
|
||||
};
|
||||
}
|
||||
7
.back/old/Parser/public/Parser.hpp
Normal file
7
.back/old/Parser/public/Parser.hpp
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
#pragma once
|
||||
|
||||
#include "CLR.hpp"
|
||||
|
||||
namespace tp {
|
||||
extern ModuleManifest gModuleParser;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue