NouVeL/NVL/Parser.cpp

518 lines
14 KiB
C++

#include "Parser.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <srell.hpp>
#include <utility>
#include "Environment.h"
namespace {
using namespace NVL;
struct ParseGroup {
const String accept;
operator String() const {
return accept;
}
bool operator== (const String& other) const {
return accept == other;
}
};
struct Match {
const String accept;
operator Char() const {
if (accept.length() == 1)
return accept[0];
else {
std::cerr << "NVL: Cannot demote Match " << to_std_string(accept) << " to char" << std::endl;
return '\0';
}
}
bool operator== (const String& other) const {
return accept == other;
}
};
const ParseGroup NUMERIC { u"1234567890" };
const Match DECIMAL_DOT { u"." };
const Match NEGATIVE { u"-" };
const ParseGroup ALPHA { u"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" };
const Match ARRAY_OPEN { u"[" };
const Match ARRAY_CLOSE { u"]" };
const Match ARRAY_DELIM { u"," };
const Match GROUP_OPEN { u"(" };
const Match GROUP_CLOSE { u")" };
const Match QUOTE { u"\"" };
const Match COMMENT_BEGIN { u"//" };
const Match DIALOGUE_OPEN { u"<<-" };
const Match DIALOGUE_CLOSE { u"->>" };
const Match BEGIN { u"BEGIN" };
const Match END { u"END" };
const ParseGroup SYMBOL { ALPHA.accept + NUMERIC.accept + u"_"};
const Match SPECIAL_SYMBOLS[] {
{ u"+" },
{ u"-" },
{ u"*" },
{ u"/" },
{ u"=?" },
{ u">?" },
{ u"<?" },
{ u"<=?" },
{ u">=?" }
};
const ParseGroup WS { u" \t\v\f\r\n" };
const ParseGroup SEPARATOR {
WS.accept +
Char(ARRAY_OPEN) +
Char(ARRAY_CLOSE) +
Char(GROUP_OPEN) +
Char(GROUP_CLOSE) +
Char(ARRAY_DELIM) +
COMMENT_BEGIN.accept[0] +
u'\0'
};
const Match NEWLINE { u"\n" };
const ParseGroup ESCAPED { u"\\\"" };
const Match ESCAPE { u"\\" };
// Dialogue mode matches
const Match MARKUP_OPEN { u"[" };
const Match MARKUP_CLOSE { u"]" };
const Match SPEAKER_OPEN { u"[" };
const Match SPEAKER_CLOSE { u"]" };
const Match MARKUP_TEXT_OPEN { u"{" };
const Match MARKUP_TEXT_CLOSE { u"}" };
const Match TEMPLATE_IND { u"$" };
const Match TEMPLATE_OPEN { u"{" };
const Match TEMPLATE_CLOSE { u"}" };
const Match COMMAND_ESCAPE { u"*!" };
const ParseGroup DIALOGUE_ESCAPED_SINGLE {
ESCAPE.accept +
Char(MARKUP_OPEN) +
Char(MARKUP_CLOSE) +
Char(MARKUP_TEXT_OPEN) +
Char(MARKUP_TEXT_CLOSE) +
// Char(SPEAKER_OPEN) +
// Char(SPEAKER_CLOSE) +
Char(TEMPLATE_IND)
// Char(TEMPLATE_OPEN) +
// Char(TEMPLATE_CLOSE)
};
String read_file_to_string(const std::string& path) {
std::ifstream f(path);
{ // Some apps on Windows adds this signature in front of UTF-8 files when saving
char a, b, c;
a = f.get(); b = f.get(); c = f.get();
if (a != (char)0xEF || b != (char)0xBB || c != (char)0xBF)
f.seekg(0);
else
std::cerr << "NVL: Windows UTF-8 BOM skipped" << std::endl;
}
std::stringstream buffer;
buffer << f.rdbuf();
return to_NVL_string(buffer.str());
}
Vector<String> split_string_by_lines(const String& str) {
Vector<String> lines;
size_t pos = 0;
size_t prev = 0;
while ((pos = str.find(NEWLINE, prev)) != String::npos) {
lines.push_back(str.substr(prev, pos - prev));
prev = pos + 1;
}
lines.push_back(str.substr(prev));
return lines;
}
inline bool IsNumeric(const String& str) {
bool negative = str[0] == NEGATIVE;
bool had_dot = false;
for (auto& c : negative ? str.substr(1) : str) {
if (NUMERIC.accept.find(c) == String::npos) {
if (c == DECIMAL_DOT.accept[0]) {
if (had_dot)
return false;
else
had_dot = true;
}
else
return false;
}
}
if (had_dot + negative == str.length())
return false;
return true;
}
inline bool ContainsOnlyWS(const String& s) {
for (auto& c : s) {
if (WS.accept.find(c) == String::npos)
return false;
}
return true;
}
void SkipWS(const String& f, size_t& pos) {
while (WS.accept.find(f[pos]) != String::npos)
pos++;
}
void SkipToNextLine(const String& f, size_t& pos) {
while (f[pos] != NEWLINE.accept[0])
pos++;
pos++;
}
void SkipComments(const String& f, size_t& pos) {
SkipWS(f, pos);
while (f.substr(pos, 2) == COMMENT_BEGIN.accept) {
SkipToNextLine(f, pos);
SkipWS(f, pos);
}
}
void SkipOverFirstChar(const String& f, size_t& pos) {
SkipWS(f, pos);
pos++;
}
String GetToken(const String& f, size_t& pos) {
SkipWS(f, pos);
auto start = pos;
while (++pos) {
if (SEPARATOR.accept.find(f[pos]) != String::npos)
break;
}
return f.substr(start, pos - start);
}
String PeekToken(const String& f, size_t pos) {
SkipWS(f, pos);
auto start = pos;
while (++pos) {
if (SEPARATOR.accept.find(f[pos]) != String::npos)
break;
}
return f.substr(start, pos - start);
}
bool IsLegalSymbolName(const String& token) {
for (const auto& x: SPECIAL_SYMBOLS) {
if (token == x.accept)
return true;
}
if (ALPHA.accept.find(token[0]) == String::npos)
return false;
for (auto& i : token)
if (SYMBOL.accept.find(i) == String::npos)
return false;
return true;
}
Parse::Object ParseExpression(const String& f, size_t& pos);
Parse::Object ParseArray(const String& f, size_t& pos, u32 layer) {
SkipComments(f, pos);
Vector<Parse::Object> array{};
array.push_back(ParseExpression(f, pos));
while (PeekToken(f, pos)[0] != ARRAY_CLOSE) {
if (PeekToken(f, pos)[0] == ARRAY_DELIM)
SkipOverFirstChar(f, pos);
else
throw std::runtime_error("Invalid array member");
array.push_back(ParseExpression(f, pos));
}
return { Parse::Type::Array, array };
}
String ParseString(const String& f, size_t& pos) {
SkipComments(f, pos);
Vector<size_t> discards{};
auto start = ++pos; // skip opening quote
do {
if (f[pos] == QUOTE) {
break;
}
else if (f[pos] == ESCAPE) {
if (ESCAPED.accept.find(f[pos]) != String::npos) {
discards.push_back(pos++);
}
else
throw std::runtime_error("Unrecognized escape sequence");
}
else if (f[pos] == NEWLINE) {
throw std::runtime_error("Unclosed String");
}
} while (pos++);
auto str = f.substr(start, pos++ - start);
for (size_t i = 0; i < discards.size(); i++) {
str.erase(discards[i] - start - i, 1);
}
return str;
}
u32 GetProcedureArity(const String& key) {
return Environment::ENVIRONMENT.get(key).length;
}
Parse::Command ParseCommand(const String& f, size_t& pos) {
SkipComments(f, pos);
auto proc = GetToken(f, pos);
if (!IsLegalSymbolName(proc)) throw std::runtime_error("Illegal Procedure name");
Parse::Command c{ Parse::Object{ Parse::Type::Symbol, proc } };
for (u32 i = 0; i < GetProcedureArity(proc); i++) {
c.push_back(ParseExpression(f, pos));
}
SkipComments(f, pos);
return c;
}
Parse::Object ParseExpression(const String& f, size_t& pos) {
SkipComments(f, pos);
auto t = PeekToken(f, pos);
if (t[0] == ARRAY_OPEN) {
SkipOverFirstChar(f, pos);
auto c = ParseArray(f, pos, 0);
if (PeekToken(f, pos)[0] != ARRAY_CLOSE)
throw std::runtime_error("Cannot match closing Array");
else
SkipOverFirstChar(f, pos);
return c;
}
else if (t[0] == GROUP_OPEN) {
SkipOverFirstChar(f, pos);
auto c = ParseCommand(f, pos);
if (PeekToken(f, pos)[0] != GROUP_CLOSE)
throw std::runtime_error("Cannot match closing subexpression");
else
SkipOverFirstChar(f, pos);
return Parse::Object{ Parse::Type::Subexpression, c };
}
else if (t[0] == GROUP_CLOSE)
throw std::runtime_error("Cannot match closing subexpression, likely too few arguments");
else if (t[0] == QUOTE)
return { Parse::Type::String, ParseString(f, pos) };
else if (t[0] == ARRAY_CLOSE)
throw std::runtime_error("Cannot match closing array");
else {
auto token = GetToken(f, pos);
if (IsNumeric(token))
return { std::stof(to_std_string(token)) };
else if (IsLegalSymbolName(token))
return { Parse::Type::Symbol, token };
else
throw std::runtime_error("Illegal symbol");
}
}
/*
* NVL Markup Parsetree
*
* * - Vec:N - Vec:2 - Str: Str segment contents
* |
* - Vec:N - Str: T of Markup
* |
* OR
* |
* - Vec:2 - Str: T of Markup
* |
* - Vec: Params
*/
Parse::Object MatchMarkup(const String& s) {
static const srell::basic_regex<Char>
typer(uR"((?<!\\)\[([^\]]+)\]\s*\{([^\}]+)\})"), // G1 -> Specifiers, G2 -> Contents
effect(uR"(\s*(?:([^,\(\)]+?)\s*\(\s*([^\(\)]+?)\s*\)|([^,\(\)]+?))\s*(?:,\s*|$))"), // G1 & G2 -> Func, G3 -> Attr
param(uR"(([^,]+?)\s*(?:,\s*|$))"); // Comma split of func args
srell::match_results<String::const_iterator> tags_match, effects_match, params_match;
bool has_markup = false;
// Match tags
Vector<Parse::Object> segments;
String::const_iterator tags_start(s.cbegin());
while (srell::regex_search(tags_start, s.cend(), tags_match, typer)) {
has_markup = true;
String before {tags_match.prefix().first, tags_match[0].first};
if (!before.empty())
segments.push_back({ Parse::Type::Array, {
{ Parse::Type::String, before },
{ Parse::Type::Array, Vector<Parse::Object>{} }
}});
String inner = tags_match[2].str(); // markupped
// Match markup options
Vector<Parse::Object> effects{};
String::const_iterator effects_start(tags_match[1].first);
while (srell::regex_search(effects_start, tags_match[1].second, effects_match, effect)) {
if (effects_match[3].matched) { // no params
effects.push_back({ Parse::Type::String, effects_match[3].str() });
}
else { // no params
// Comma split
Vector<Parse::Object> args;
String::const_iterator params_start(effects_match[2].first);
while (srell::regex_search(params_start, effects_match[2].second, params_match, param)) {
size_t temp = 0;
args.push_back(ParseExpression(params_match[1].str() + SEPARATOR.accept[0], temp)); // PeekToken will freak out if I don't do this
params_start = params_match.suffix().first;
}
effects.push_back({ Parse::Type::Array, Vector<Parse::Object>{ { Parse::Type::String, effects_match[1].str() }, { Parse::Type::Array, args } } });
}
effects_start = effects_match.suffix().first;
}
tags_start = tags_match.suffix().first;
segments.push_back({ Parse::Type::Array, Vector<Parse::Object>{ { Parse::Type::String, inner }, { Parse::Type::Array, effects } } });
}
if (has_markup) {
String end {tags_match.suffix().first, tags_match.suffix().second};
if (!end.empty())
segments.push_back({ Parse::Type::Array, {
{ Parse::Type::String, end },
{ Parse::Type::Array, Vector<Parse::Object>{} }
}});
return { Parse::Type::Array, segments };
}
else {
return { Parse::Type::Array, Vector<Parse::Object>{
{ Parse::Type::Array, {
{ Parse::Type::String, s} , { Parse::Type::Array, Vector<Parse::Object>{} }
} } }
};
}
}
Parse::Command ParseDialogue(const String& s) {
if (s.substr(0, 2) == COMMAND_ESCAPE.accept) {
size_t dummy = 0;
// Pad a space towards the end, the helpers do not expect strings to immediately terminate
return ParseCommand(s.substr(2) + u" ", dummy);
}
// assume arity for SwitchSpeaker and Say
if (s.back() == SPEAKER_CLOSE) {
if (s.front() == SPEAKER_OPEN) {
auto name = s.substr(1, s.length() - 2);
// if (IsLegalSymbolName(name))
return { { Parse::Type::Symbol, u"SwitchSpeaker" }, { Parse::Type::String, name } };
}
else
throw std::runtime_error("Malformed speaker command");
}
return { { Parse::Type::Symbol, u"Say" }, MatchMarkup(s) };
}
Parse::Scene ParseScene(const String& f, size_t& pos) {
SkipComments(f, pos);
if (!(GetToken(f, pos) == BEGIN.accept))
throw std::runtime_error("Could not match accept at root");
auto scene_name = GetToken(f, pos);
if (!IsLegalSymbolName(scene_name)) throw std::runtime_error("Illegal Scene name");
Parse::Scene s{ scene_name };
bool dialogue_mode = false;
SkipComments(f, pos);
while (PeekToken(f, pos) != END.accept) {
if (!dialogue_mode)
if (PeekToken(f, pos) == DIALOGUE_OPEN.accept) {
dialogue_mode = true;
GetToken(f, pos); // skip DIALOGUE_OPEN
SkipComments(f, pos);
}
else
s.append(ParseCommand(f, pos));
else {
auto end = f.find(NEWLINE.accept + DIALOGUE_CLOSE.accept, pos);
if (end == String::npos)
throw std::runtime_error("Dialogue does not terminate");
auto lines = split_string_by_lines(f.substr(pos, end - pos));
for (auto& l : lines) {
if (!l.empty() && !ContainsOnlyWS(l))
s.append(ParseDialogue(l));
}
dialogue_mode = false;
pos = end;
GetToken(f, pos); // skip DIALOGUE_CLOSE
SkipComments(f, pos);
}
}
if (dialogue_mode)
throw std::runtime_error("Illegal Scene end");
GetToken(f, pos); // skip END
SkipComments(f, pos);
return s;
}
}
namespace NVL::Parse {
Object::Object(const Number& v) : type(Type::Number), value(v) { }
Object::Object(Type t, const String& v) : type(t), value(v) {
if (t != Type::String && t != Type::Symbol) throw std::runtime_error("Bad type when constructing object!");
}
Object::Object(Type t, const Vector<Object>& v) : type(t), value(v) {
if (t != Type::Array && t != Type::Subexpression) throw std::runtime_error("Bad type when constructing object!");
}
Object::Object(Number&& v) : type(Type::Number), value(std::move(v)) {
}
Object::Object(Type t, String&& v) : type(t), value(std::move(v)) {
if (t != Type::String && t != Type::Symbol) throw std::runtime_error("Bad type when constructing object!");
}
Object::Object(Type t, Vector<Object>&& v) : type(t), value(std::move(v)) {
if (t != Type::Array && t != Type::Subexpression) throw std::runtime_error("Bad type when constructing object!");
}
Vector<Scene> ParseFile(const std::string& path) {
String f = read_file_to_string(path);
Vector<Scene> list {}; // Vector of scenes which each contain a vector of Parses
for (size_t i = 0; i < f.length(); i++) {
list.push_back(ParseScene(f, i));
}
return list;
}
}