| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596 |
- // This file is distributed under the BSD License.
- // See "license.txt" for details.
- // Copyright 2009-2012, Jonathan Turner ([email protected])
- // Copyright 2009-2017, Jason Turner ([email protected])
- // http://www.chaiscript.com
- // This is an open source non-commercial project. Dear PVS-Studio, please check it.
- // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
- #ifndef CHAISCRIPT_PARSER_HPP_
- #define CHAISCRIPT_PARSER_HPP_
- #include <exception>
- #include <iostream>
- #include <memory>
- #include <sstream>
- #include <string>
- #include <vector>
- #include <cctype>
- #include <cstring>
- #include "../dispatchkit/boxed_value.hpp"
- #include "chaiscript_common.hpp"
- #include "chaiscript_optimizer.hpp"
- #include "chaiscript_tracer.hpp"
- #include "../utility/fnv1a.hpp"
- #include "../utility/static_string.hpp"
- #if defined(CHAISCRIPT_UTF16_UTF32)
- #include <locale>
- #include <codecvt>
- #endif
- #if defined(CHAISCRIPT_MSVC) && defined(max) && defined(min)
- #define CHAISCRIPT_PUSHED_MIN_MAX
- #pragma push_macro("max") // Why Microsoft? why? This is worse than bad
- #undef max
- #pragma push_macro("min")
- #undef min
- #endif
- namespace chaiscript
- {
- /// \brief Classes and functions used during the parsing process.
- namespace parser
- {
- /// \brief Classes and functions internal to the parsing process. Not supported for the end user.
- namespace detail
- {
- enum Alphabet
- { symbol_alphabet = 0
- , keyword_alphabet
- , int_alphabet
- , float_alphabet
- , x_alphabet
- , hex_alphabet
- , b_alphabet
- , bin_alphabet
- , id_alphabet
- , white_alphabet
- , int_suffix_alphabet
- , float_suffix_alphabet
- , max_alphabet
- , lengthof_alphabet = 256
- };
- // Generic for u16, u32 and wchar
- template<typename string_type>
- struct Char_Parser_Helper
- {
- // common for all implementations
- static std::string u8str_from_ll(long long val)
- {
- typedef std::string::value_type char_type;
- char_type c[2];
- c[1] = char_type(val);
- c[0] = char_type(val >> 8);
- if (c[0] == 0)
- {
- return std::string(1, c[1]); // size, character
- }
- return std::string(c, 2); // char buffer, size
- }
- static string_type str_from_ll(long long val)
- {
- typedef typename string_type::value_type target_char_type;
- #if defined (CHAISCRIPT_UTF16_UTF32)
- // prepare converter
- std::wstring_convert<std::codecvt_utf8<target_char_type>, target_char_type> converter;
- // convert
- return converter.from_bytes(u8str_from_ll(val));
- #else
- // no conversion available, just put value as character
- return string_type(1, target_char_type(val)); // size, character
- #endif
- }
- };
- // Specialization for char AKA UTF-8
- template<>
- struct Char_Parser_Helper<std::string>
- {
- static std::string str_from_ll(long long val)
- {
- // little SFINAE trick to avoid base class
- return Char_Parser_Helper<std::true_type>::u8str_from_ll(val);
- }
- };
- }
- template<typename Tracer, typename Optimizer>
- class ChaiScript_Parser final : public ChaiScript_Parser_Base {
- void *get_tracer_ptr() override {
- return &m_tracer;
- }
- static std::array<std::array<bool, detail::lengthof_alphabet>, detail::max_alphabet> build_alphabet()
- {
- std::array<std::array<bool, detail::lengthof_alphabet>, detail::max_alphabet> alphabet;
- for (auto &alpha : alphabet) {
- alpha.fill(false);
- }
- alphabet[detail::symbol_alphabet][static_cast<size_t>('?')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('+')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('-')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('*')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('/')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('|')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('&')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('^')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('=')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('.')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('<')]=true;
- alphabet[detail::symbol_alphabet][static_cast<size_t>('>')]=true;
- for ( size_t c = 'a' ; c <= 'z' ; ++c ) { alphabet[detail::keyword_alphabet][c]=true; }
- for ( size_t c = 'A' ; c <= 'Z' ; ++c ) { alphabet[detail::keyword_alphabet][c]=true; }
- for ( size_t c = '0' ; c <= '9' ; ++c ) { alphabet[detail::keyword_alphabet][c]=true; }
- alphabet[detail::keyword_alphabet][static_cast<size_t>('_')]=true;
- for ( size_t c = '0' ; c <= '9' ; ++c ) { alphabet[detail::int_alphabet][c]=true; }
- for ( size_t c = '0' ; c <= '9' ; ++c ) { alphabet[detail::float_alphabet][c]=true; }
- alphabet[detail::float_alphabet][static_cast<size_t>('.')]=true;
- for ( size_t c = '0' ; c <= '9' ; ++c ) { alphabet[detail::hex_alphabet][c]=true; }
- for ( size_t c = 'a' ; c <= 'f' ; ++c ) { alphabet[detail::hex_alphabet][c]=true; }
- for ( size_t c = 'A' ; c <= 'F' ; ++c ) { alphabet[detail::hex_alphabet][c]=true; }
- alphabet[detail::x_alphabet][static_cast<size_t>('x')]=true;
- alphabet[detail::x_alphabet][static_cast<size_t>('X')]=true;
- for ( size_t c = '0' ; c <= '1' ; ++c ) { alphabet[detail::bin_alphabet][c]=true; }
- alphabet[detail::b_alphabet][static_cast<size_t>('b')]=true;
- alphabet[detail::b_alphabet][static_cast<size_t>('B')]=true;
- for ( size_t c = 'a' ; c <= 'z' ; ++c ) { alphabet[detail::id_alphabet][c]=true; }
- for ( size_t c = 'A' ; c <= 'Z' ; ++c ) { alphabet[detail::id_alphabet][c]=true; }
- alphabet[detail::id_alphabet][static_cast<size_t>('_')] = true;
- alphabet[detail::white_alphabet][static_cast<size_t>(' ')]=true;
- alphabet[detail::white_alphabet][static_cast<size_t>('\t')]=true;
- alphabet[detail::int_suffix_alphabet][static_cast<size_t>('l')] = true;
- alphabet[detail::int_suffix_alphabet][static_cast<size_t>('L')] = true;
- alphabet[detail::int_suffix_alphabet][static_cast<size_t>('u')] = true;
- alphabet[detail::int_suffix_alphabet][static_cast<size_t>('U')] = true;
- alphabet[detail::float_suffix_alphabet][static_cast<size_t>('l')] = true;
- alphabet[detail::float_suffix_alphabet][static_cast<size_t>('L')] = true;
- alphabet[detail::float_suffix_alphabet][static_cast<size_t>('f')] = true;
- alphabet[detail::float_suffix_alphabet][static_cast<size_t>('F')] = true;
- return alphabet;
- }
- static const std::array<std::array<bool, detail::lengthof_alphabet>, detail::max_alphabet> &create_alphabet()
- {
- static const auto alpha = build_alphabet();
- return alpha;
- }
- static const std::vector<std::vector<utility::Static_String>> &create_operator_matches() {
- static const std::vector<std::vector<utility::Static_String>> operator_matches {
- {"?"},
- {"||"},
- {"&&"},
- {"|"},
- {"^"},
- {"&"},
- {"==", "!="},
- {"<", "<=", ">", ">="},
- {"<<", ">>"},
- //We share precedence here but then separate them later
- {"+", "-"},
- {"*", "/", "%"},
- {"++", "--", "-", "+", "!", "~"}
- };
- return operator_matches;
- }
- static const std::array<Operator_Precidence, 12> &create_operators() {
- static const std::array<Operator_Precidence, 12> operators = { {
- Operator_Precidence::Ternary_Cond,
- Operator_Precidence::Logical_Or,
- Operator_Precidence::Logical_And,
- Operator_Precidence::Bitwise_Or,
- Operator_Precidence::Bitwise_Xor,
- Operator_Precidence::Bitwise_And,
- Operator_Precidence::Equality,
- Operator_Precidence::Comparison,
- Operator_Precidence::Shift,
- Operator_Precidence::Addition,
- Operator_Precidence::Multiplication,
- Operator_Precidence::Prefix
- } };
- return operators;
- }
- static const utility::Static_String &multiline_comment_end()
- {
- static const utility::Static_String s("*/");
- return s;
- }
- static const utility::Static_String &multiline_comment_begin()
- {
- static const utility::Static_String s("/*");
- return s;
- }
- static const utility::Static_String &singleline_comment()
- {
- static const utility::Static_String s("//");
- return s;
- }
- static const utility::Static_String &annotation()
- {
- static const utility::Static_String s("#");
- return s;
- }
- static const utility::Static_String &cr_lf()
- {
- static const utility::Static_String s("\r\n");
- return s;
- }
- const std::array<std::array<bool, detail::lengthof_alphabet>, detail::max_alphabet> &m_alphabet = create_alphabet();
- const std::vector<std::vector<utility::Static_String>> &m_operator_matches = create_operator_matches();
- const std::array<Operator_Precidence, 12> &m_operators = create_operators();
- std::shared_ptr<std::string> m_filename;
- std::vector<eval::AST_Node_Impl_Ptr<Tracer>> m_match_stack;
- struct Position
- {
- Position() = default;
- Position(std::string::const_iterator t_pos, std::string::const_iterator t_end)
- : line(1), col(1), m_pos(t_pos), m_end(t_end), m_last_col(1)
- {
- }
- static std::string str(const Position &t_begin, const Position &t_end) {
- return std::string(t_begin.m_pos, t_end.m_pos);
- }
- Position &operator++() {
- if (m_pos != m_end) {
- if (*m_pos == '\n') {
- ++line;
- m_last_col = std::exchange(col, 1);
- } else {
- ++col;
- }
- ++m_pos;
- }
- return *this;
- }
- Position &operator--() {
- --m_pos;
- if (*m_pos == '\n') {
- --line;
- col = m_last_col;
- } else {
- --col;
- }
- return *this;
- }
- Position &operator+=(size_t t_distance) {
- *this = (*this) + t_distance;
- return *this;
- }
- Position operator+(size_t t_distance) const {
- Position ret(*this);
- for (size_t i = 0; i < t_distance; ++i) {
- ++ret;
- }
- return ret;
- }
- Position &operator-=(size_t t_distance) {
- *this = (*this) - t_distance;
- return *this;
- }
- Position operator-(size_t t_distance) const {
- Position ret(*this);
- for (size_t i = 0; i < t_distance; ++i) {
- --ret;
- }
- return ret;
- }
- bool operator==(const Position &t_rhs) const {
- return m_pos == t_rhs.m_pos;
- }
- bool operator!=(const Position &t_rhs) const {
- return m_pos != t_rhs.m_pos;
- }
- bool has_more() const {
- return m_pos != m_end;
- }
- size_t remaining() const {
- return static_cast<size_t>(std::distance(m_pos, m_end));
- }
- const char& operator*() const {
- if (m_pos == m_end) {
- static const char ktmp ='\0';
- return ktmp;
- } else {
- return *m_pos;
- }
- }
- int line = -1;
- int col = -1;
- private:
- std::string::const_iterator m_pos;
- std::string::const_iterator m_end;
- int m_last_col = -1;
- };
- Position m_position;
- Tracer m_tracer;
- Optimizer m_optimizer;
- void validate_object_name(const std::string &name) const
- {
- if (!Name_Validator::valid_object_name(name)) {
- throw exception::eval_error("Invalid Object Name: " + name, File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- public:
- explicit ChaiScript_Parser(Tracer tracer = Tracer(), Optimizer optimizer=Optimizer())
- : m_tracer(std::move(tracer)),
- m_optimizer(std::move(optimizer))
- {
- m_match_stack.reserve(2);
- }
- Tracer &get_tracer()
- {
- return m_tracer;
- }
- Optimizer &get_optimizer()
- {
- return m_optimizer;
- }
- ChaiScript_Parser(const ChaiScript_Parser &) = delete;
- ChaiScript_Parser &operator=(const ChaiScript_Parser &) = delete;
- ChaiScript_Parser(ChaiScript_Parser &&) = default;
- ChaiScript_Parser &operator=(ChaiScript_Parser &&) = delete;
- /// test a char in an m_alphabet
- bool char_in_alphabet(char c, detail::Alphabet a) const { return m_alphabet[a][static_cast<uint8_t>(c)]; }
- /// Prints the parsed ast_nodes as a tree
- void debug_print(const AST_Node &t, std::string prepend = "") const override {
- std::cout << prepend << "(" << ast_node_type_to_string(t.identifier) << ") " << t.text << " : " << t.start().line << ", " << t.start().column << '\n';
- for (const auto &node : t.get_children()) {
- debug_print(node.get(), prepend + " ");
- }
- }
- /// Helper function that collects ast_nodes from a starting position to the top of the stack into a new AST node
- template<typename NodeType>
- void build_match(size_t t_match_start, std::string t_text = "") {
- bool is_deep = false;
- Parse_Location filepos = [&]()->Parse_Location{
- //so we want to take everything to the right of this and make them children
- if (t_match_start != m_match_stack.size()) {
- is_deep = true;
- return Parse_Location(
- m_filename,
- m_match_stack[t_match_start]->location.start.line,
- m_match_stack[t_match_start]->location.start.column,
- m_position.line,
- m_position.col
- );
- } else {
- return Parse_Location(
- m_filename,
- m_position.line,
- m_position.col,
- m_position.line,
- m_position.col
- );
- }
- }();
- std::vector<eval::AST_Node_Impl_Ptr<Tracer>> new_children;
- if (is_deep) {
- new_children.assign(std::make_move_iterator(m_match_stack.begin() + static_cast<int>(t_match_start)),
- std::make_move_iterator(m_match_stack.end()));
- m_match_stack.erase(m_match_stack.begin() + static_cast<int>(t_match_start), m_match_stack.end());
- }
- /// \todo fix the fact that a successful match that captured no ast_nodes doesn't have any real start position
- m_match_stack.push_back(
- m_optimizer.optimize(
- chaiscript::make_unique<chaiscript::eval::AST_Node_Impl<Tracer>, NodeType>(
- std::move(t_text),
- std::move(filepos),
- std::move(new_children)))
- );
- }
- /// Reads a symbol group from input if it matches the parameter, without skipping initial whitespace
- inline auto Symbol_(const utility::Static_String &sym)
- {
- const auto len = sym.size();
- if (m_position.remaining() >= len) {
- const char *file_pos = &(*m_position);
- for (size_t pos = 0; pos < len; ++pos)
- {
- if (sym.c_str()[pos] != file_pos[pos]) { return false; }
- }
- m_position += len;
- return true;
- }
- return false;
- }
- /// Skips any multi-line or single-line comment
- bool SkipComment() {
- if (Symbol_(multiline_comment_begin())) {
- while (m_position.has_more()) {
- if (Symbol_(multiline_comment_end())) {
- break;
- } else if (!Eol_()) {
- ++m_position;
- }
- }
- return true;
- } else if (Symbol_(singleline_comment())) {
- while (m_position.has_more()) {
- if (Symbol_(cr_lf())) {
- m_position -= 2;
- break;
- } else if (Char_('\n')) {
- --m_position;
- break;
- } else {
- ++m_position;
- }
- }
- return true;
- } else if (Symbol_(annotation())) {
- while (m_position.has_more()) {
- if (Symbol_(cr_lf())) {
- m_position -= 2;
- break;
- } else if (Char_('\n')) {
- --m_position;
- break;
- } else {
- ++m_position;
- }
- }
- return true;
- }
- return false;
- }
- /// Skips ChaiScript whitespace, which means space and tab, but not cr/lf
- /// jespada: Modified SkipWS to skip optionally CR ('\n') and/or LF+CR ("\r\n")
- bool SkipWS(bool skip_cr=false) {
- bool retval = false;
- while (m_position.has_more()) {
- auto end_line = (*m_position != 0) && ((*m_position == '\n') || (*m_position == '\r' && *(m_position+1) == '\n'));
- if ( char_in_alphabet(*m_position,detail::white_alphabet) || (skip_cr && end_line)) {
- if(end_line) {
- if(*m_position == '\r') {
- // discards lf
- ++m_position;
- }
- }
- ++m_position;
- retval = true;
- }
- else if (SkipComment()) {
- retval = true;
- } else {
- break;
- }
- }
- return retval;
- }
- /// Reads the optional exponent (scientific notation) and suffix for a Float
- bool read_exponent_and_suffix() {
- // Support a form of scientific notation: 1e-5, 35.5E+8, 0.01e19
- if (m_position.has_more() && (std::tolower(*m_position) == 'e')) {
- ++m_position;
- if (m_position.has_more() && ((*m_position == '-') || (*m_position == '+'))) {
- ++m_position;
- }
- auto exponent_pos = m_position;
- while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
- ++m_position;
- }
- if (m_position == exponent_pos) {
- // Require at least one digit after the exponent
- return false;
- }
- }
- // Parse optional float suffix
- while (m_position.has_more() && char_in_alphabet(*m_position, detail::float_suffix_alphabet))
- {
- ++m_position;
- }
- return true;
- }
- /// Reads a floating point value from input, without skipping initial whitespace
- bool Float_() {
- if (m_position.has_more() && char_in_alphabet(*m_position,detail::float_alphabet) ) {
- while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
- ++m_position;
- }
- if (m_position.has_more() && (std::tolower(*m_position) == 'e')) {
- // The exponent is valid even without any decimal in the Float (1e8, 3e-15)
- return read_exponent_and_suffix();
- }
- else if (m_position.has_more() && (*m_position == '.')) {
- ++m_position;
- if (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet)) {
- while (m_position.has_more() && char_in_alphabet(*m_position,detail::int_alphabet) ) {
- ++m_position;
- }
- // After any decimal digits, support an optional exponent (3.7e3)
- return read_exponent_and_suffix();
- } else {
- --m_position;
- }
- }
- }
- return false;
- }
- /// Reads a hex value from input, without skipping initial whitespace
- bool Hex_() {
- if (m_position.has_more() && (*m_position == '0')) {
- ++m_position;
- if (m_position.has_more() && char_in_alphabet(*m_position, detail::x_alphabet) ) {
- ++m_position;
- if (m_position.has_more() && char_in_alphabet(*m_position, detail::hex_alphabet)) {
- while (m_position.has_more() && char_in_alphabet(*m_position, detail::hex_alphabet) ) {
- ++m_position;
- }
- while (m_position.has_more() && char_in_alphabet(*m_position, detail::int_suffix_alphabet))
- {
- ++m_position;
- }
- return true;
- }
- else {
- --m_position;
- }
- }
- else {
- --m_position;
- }
- }
- return false;
- }
- /// Reads an integer suffix
- void IntSuffix_() {
- while (m_position.has_more() && char_in_alphabet(*m_position, detail::int_suffix_alphabet))
- {
- ++m_position;
- }
- }
- /// Reads a binary value from input, without skipping initial whitespace
- bool Binary_() {
- if (m_position.has_more() && (*m_position == '0')) {
- ++m_position;
- if (m_position.has_more() && char_in_alphabet(*m_position, detail::b_alphabet) ) {
- ++m_position;
- if (m_position.has_more() && char_in_alphabet(*m_position, detail::bin_alphabet) ) {
- while (m_position.has_more() && char_in_alphabet(*m_position, detail::bin_alphabet) ) {
- ++m_position;
- }
- return true;
- } else {
- --m_position;
- }
- } else {
- --m_position;
- }
- }
- return false;
- }
- /// Parses a floating point value and returns a Boxed_Value representation of it
- static Boxed_Value buildFloat(const std::string &t_val)
- {
- bool float_ = false;
- bool long_ = false;
- auto i = t_val.size();
- for (; i > 0; --i)
- {
- char val = t_val[i-1];
- if (val == 'f' || val == 'F')
- {
- float_ = true;
- } else if (val == 'l' || val == 'L') {
- long_ = true;
- } else {
- break;
- }
- }
- if (float_)
- {
- return const_var(parse_num<float>(t_val.substr(0,i)));
- } else if (long_) {
- return const_var(parse_num<long double>(t_val.substr(0,i)));
- } else {
- return const_var(parse_num<double>(t_val.substr(0,i)));
- }
- }
- static Boxed_Value buildInt(const int base, const std::string &t_val, const bool prefixed)
- {
- bool unsigned_ = false;
- bool long_ = false;
- bool longlong_ = false;
- auto i = t_val.size();
- for (; i > 0; --i)
- {
- const char val = t_val[i-1];
- if (val == 'u' || val == 'U')
- {
- unsigned_ = true;
- } else if (val == 'l' || val == 'L') {
- if (long_)
- {
- longlong_ = true;
- }
- long_ = true;
- } else {
- break;
- }
- }
- const auto val = prefixed?std::string(t_val.begin()+2,t_val.end()):t_val;
- #ifdef __GNUC__
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wsign-compare"
- #ifdef CHAISCRIPT_CLANG
- #pragma GCC diagnostic ignored "-Wtautological-compare"
- #endif
- #endif
- try {
- auto u = std::stoll(val,nullptr,base);
- if (!unsigned_ && !long_ && u >= std::numeric_limits<int>::min() && u <= std::numeric_limits<int>::max()) {
- return const_var(static_cast<int>(u));
- } else if ((unsigned_ || base != 10) && !long_ && u >= std::numeric_limits<unsigned int>::min() && u <= std::numeric_limits<unsigned int>::max()) {
- return const_var(static_cast<unsigned int>(u));
- } else if (!unsigned_ && !longlong_ && u >= std::numeric_limits<long>::min() && u <= std::numeric_limits<long>::max()) {
- return const_var(static_cast<long>(u));
- } else if ((unsigned_ || base != 10) && !longlong_ && u >= std::numeric_limits<unsigned long>::min() && u <= std::numeric_limits<unsigned long>::max()) {
- return const_var(static_cast<unsigned long>(u));
- } else if (!unsigned_ && u >= std::numeric_limits<long long>::min() && u <= std::numeric_limits<long long>::max()) {
- return const_var(static_cast<long long>(u));
- } else {
- return const_var(static_cast<unsigned long long>(u));
- }
- } catch (const std::out_of_range &) {
- // too big to be signed
- try {
- auto u = std::stoull(val,nullptr,base);
- if (!longlong_ && u >= std::numeric_limits<unsigned long>::min() && u <= std::numeric_limits<unsigned long>::max()) {
- return const_var(static_cast<unsigned long>(u));
- } else {
- return const_var(static_cast<unsigned long long>(u));
- }
- } catch (const std::out_of_range &) {
- // it's just simply too big
- return const_var(std::numeric_limits<long long>::max());
- }
- }
- #ifdef __GNUC__
- #pragma GCC diagnostic pop
- #endif
- }
- template<typename T, typename ... Param>
- std::unique_ptr<eval::AST_Node_Impl<Tracer>> make_node(std::string t_match, const int t_prev_line, const int t_prev_col, Param && ...param)
- {
- return chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, T>(std::move(t_match), Parse_Location(m_filename, t_prev_line, t_prev_col, m_position.line, m_position.col), std::forward<Param>(param)...);
- }
- /// Reads a number from the input, detecting if it's an integer or floating point
- bool Num() {
- SkipWS();
- const auto start = m_position;
- if (m_position.has_more() && char_in_alphabet(*m_position, detail::float_alphabet) ) {
- try {
- if (Hex_()) {
- auto match = Position::str(start, m_position);
- auto bv = buildInt(16, match, true);
- m_match_stack.emplace_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(match), start.line, start.col, std::move(bv)));
- return true;
- }
- if (Binary_()) {
- auto match = Position::str(start, m_position);
- auto bv = buildInt(2, match, true);
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(match), start.line, start.col, std::move(bv)));
- return true;
- }
- if (Float_()) {
- auto match = Position::str(start, m_position);
- auto bv = buildFloat(match);
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(match), start.line, start.col, std::move(bv)));
- return true;
- }
- else {
- IntSuffix_();
- auto match = Position::str(start, m_position);
- if (!match.empty() && (match[0] == '0')) {
- auto bv = buildInt(8, match, false);
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(match), start.line, start.col, std::move(bv)));
- }
- else if (!match.empty()) {
- auto bv = buildInt(10, match, false);
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(match), start.line, start.col, std::move(bv)));
- } else {
- return false;
- }
- return true;
- }
- } catch (const std::invalid_argument &) {
- // error parsing number passed in to buildFloat/buildInt
- return false;
- }
- }
- else {
- return false;
- }
- }
- /// Reads an identifier from input which conforms to C's identifier naming conventions, without skipping initial whitespace
- bool Id_() {
- if (m_position.has_more() && char_in_alphabet(*m_position, detail::id_alphabet)) {
- while (m_position.has_more() && char_in_alphabet(*m_position, detail::keyword_alphabet) ) {
- ++m_position;
- }
- return true;
- } else if (m_position.has_more() && (*m_position == '`')) {
- ++m_position;
- const auto start = m_position;
- while (m_position.has_more() && (*m_position != '`')) {
- if (Eol()) {
- throw exception::eval_error("Carriage return in identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
- }
- else {
- ++m_position;
- }
- }
- if (start == m_position) {
- throw exception::eval_error("Missing contents of identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
- }
- else if (!m_position.has_more()) {
- throw exception::eval_error("Incomplete identifier literal", File_Position(m_position.line, m_position.col), *m_filename);
- }
- ++m_position;
- return true;
- }
- return false;
- }
- /// Reads (and potentially captures) an identifier from input
- bool Id(const bool validate) {
- SkipWS();
- const auto start = m_position;
- if (Id_()) {
- auto text = Position::str(start, m_position);
- const auto text_hash = utility::fnv1a_32(text.c_str());
- if (validate) {
- validate_object_name(text);
- }
- #ifdef CHAISCRIPT_MSVC
- #pragma warning(push)
- #pragma warning(disable : 4307)
- #endif
- switch (text_hash) {
- case utility::fnv1a_32("true"): {
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col, const_var(true)));
- } break;
- case utility::fnv1a_32("false"): {
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col, const_var(false)));
- } break;
- case utility::fnv1a_32("Infinity"): {
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
- const_var(std::numeric_limits<double>::infinity())));
- } break;
- case utility::fnv1a_32("NaN"): {
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
- const_var(std::numeric_limits<double>::quiet_NaN())));
- } break;
- case utility::fnv1a_32("__LINE__"): {
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
- const_var(start.line)));
- } break;
- case utility::fnv1a_32("__FILE__"): {
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
- const_var(m_filename)));
- } break;
- case utility::fnv1a_32("__FUNC__"): {
- std::string fun_name = "NOT_IN_FUNCTION";
- for (size_t idx = m_match_stack.size() - 1; idx > 0; --idx)
- {
- if (m_match_stack[idx-1]->identifier == AST_Node_Type::Id
- && m_match_stack[idx-0]->identifier == AST_Node_Type::Arg_List) {
- fun_name = m_match_stack[idx-1]->text;
- }
- }
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
- const_var(fun_name)));
- } break;
- case utility::fnv1a_32("__CLASS__"): {
- std::string fun_name = "NOT_IN_CLASS";
- for (size_t idx = m_match_stack.size() - 1; idx > 1; --idx)
- {
- if (m_match_stack[idx-2]->identifier == AST_Node_Type::Id
- && m_match_stack[idx-1]->identifier == AST_Node_Type::Id
- && m_match_stack[idx-0]->identifier == AST_Node_Type::Arg_List) {
- fun_name = m_match_stack[idx-2]->text;
- }
- }
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
- const_var(fun_name)));
- } break;
- case utility::fnv1a_32("_"): {
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(std::move(text), start.line, start.col,
- Boxed_Value(std::make_shared<dispatch::Placeholder_Object>())));
- } break;
- default: {
- std::string val = std::move(text);
- if (*start == '`') {
- // 'escaped' literal, like an operator name
- val = Position::str(start+1, m_position-1);
- }
- m_match_stack.push_back(make_node<eval::Id_AST_Node<Tracer>>(val, start.line, start.col));
- } break;
- }
- #ifdef CHAISCRIPT_MSVC
- #pragma warning(pop)
- #endif
- return true;
- } else {
- return false;
- }
- }
- /// Reads an argument from input
- bool Arg(const bool t_type_allowed = true) {
- const auto prev_stack_top = m_match_stack.size();
- SkipWS();
- if (!Id(true)) {
- return false;
- }
- SkipWS();
- if (t_type_allowed) {
- Id(true);
- }
- build_match<eval::Arg_AST_Node<Tracer>>(prev_stack_top);
- return true;
- }
- /// Reads a quoted string from input, without skipping initial whitespace
- bool Quoted_String_() {
- if (m_position.has_more() && (*m_position == '\"')) {
- char prev_char = *m_position;
- ++m_position;
- int in_interpolation = 0;
- bool in_quote = false;
- while (m_position.has_more() && ((*m_position != '\"') || ((*m_position == '\"') && (in_interpolation > 0)) || ((*m_position == '\"') && (prev_char == '\\')))) {
- if (!Eol_()) {
- if (prev_char == '$' && *m_position == '{') {
- ++in_interpolation;
- } else if (prev_char != '\\' && *m_position == '"') {
- in_quote = !in_quote;
- } else if (*m_position == '}' && !in_quote) {
- --in_interpolation;
- }
- if (prev_char == '\\') {
- prev_char = 0;
- } else {
- prev_char = *m_position;
- }
- ++m_position;
- }
- }
- if (m_position.has_more()) {
- ++m_position;
- } else {
- throw exception::eval_error("Unclosed quoted string", File_Position(m_position.line, m_position.col), *m_filename);
- }
- return true;
- }
- return false;
- }
- template<typename string_type>
- struct Char_Parser
- {
- string_type &match;
- typedef typename string_type::value_type char_type;
- bool is_escaped = false;
- bool is_interpolated = false;
- bool saw_interpolation_marker = false;
- bool is_octal = false;
- bool is_hex = false;
- bool is_unicode = false;
- const bool interpolation_allowed;
- string_type octal_matches;
- string_type hex_matches;
- Char_Parser(string_type &t_match, const bool t_interpolation_allowed)
- : match(t_match),
- interpolation_allowed(t_interpolation_allowed)
- {
- }
- Char_Parser &operator=(const Char_Parser &) = delete;
- ~Char_Parser(){
- try {
- if (is_octal) {
- process_octal();
- }
- if (is_hex) {
- process_hex();
- }
- if (is_unicode) {
- process_unicode();
- }
- } catch (const std::invalid_argument &) {
- // escape sequence was invalid somehow, we'll pick this
- // up in the next part of parsing
- }
- }
- void process_hex()
- {
- if (!hex_matches.empty()) {
- auto val = stoll(hex_matches, nullptr, 16);
- match.push_back(char_type(val));
- }
- hex_matches.clear();
- is_escaped = false;
- is_hex = false;
- }
- void process_octal()
- {
- if (!octal_matches.empty()) {
- auto val = stoll(octal_matches, nullptr, 8);
- match.push_back(char_type(val));
- }
- octal_matches.clear();
- is_escaped = false;
- is_octal = false;
- }
- void process_unicode()
- {
- if (!hex_matches.empty()) {
- auto val = stoll(hex_matches, nullptr, 16);
- hex_matches.clear();
- match += detail::Char_Parser_Helper<string_type>::str_from_ll(val);
- }
- is_escaped = false;
- is_unicode = false;
- }
- void parse(const char_type t_char, const int line, const int col, const std::string &filename) {
- const bool is_octal_char = t_char >= '0' && t_char <= '7';
- const bool is_hex_char = (t_char >= '0' && t_char <= '9')
- || (t_char >= 'a' && t_char <= 'f')
- || (t_char >= 'A' && t_char <= 'F');
- if (is_octal) {
- if (is_octal_char) {
- octal_matches.push_back(t_char);
- if (octal_matches.size() == 3) {
- process_octal();
- }
- return;
- } else {
- process_octal();
- }
- } else if (is_hex) {
- if (is_hex_char) {
- hex_matches.push_back(t_char);
- if (hex_matches.size() == 2*sizeof(char_type)) {
- // This rule differs from the C/C++ standard, but ChaiScript
- // does not offer the same workaround options, and having
- // hexadecimal sequences longer than can fit into the char
- // type is undefined behavior anyway.
- process_hex();
- }
- return;
- } else {
- process_hex();
- }
- } else if (is_unicode) {
- if (is_hex_char) {
- hex_matches.push_back(t_char);
- if(hex_matches.size() == 4) {
- // Format is specified to be 'slash'uABCD
- // on collecting from A to D do parsing
- process_unicode();
- }
- return;
- } else {
- // Not a unicode anymore, try parsing any way
- // May be someone used 'slash'uAA only
- process_unicode();
- }
- }
- if (t_char == '\\') {
- if (is_escaped) {
- match.push_back('\\');
- is_escaped = false;
- } else {
- is_escaped = true;
- }
- } else {
- if (is_escaped) {
- if (is_octal_char) {
- is_octal = true;
- octal_matches.push_back(t_char);
- } else if (t_char == 'x') {
- is_hex = true;
- } else if (t_char == 'u') {
- is_unicode = true;
- } else {
- switch (t_char) {
- case ('\'') : match.push_back('\''); break;
- case ('\"') : match.push_back('\"'); break;
- case ('?') : match.push_back('?'); break;
- case ('a') : match.push_back('\a'); break;
- case ('b') : match.push_back('\b'); break;
- case ('f') : match.push_back('\f'); break;
- case ('n') : match.push_back('\n'); break;
- case ('r') : match.push_back('\r'); break;
- case ('t') : match.push_back('\t'); break;
- case ('v') : match.push_back('\v'); break;
- case ('$') : match.push_back('$'); break;
- default: throw exception::eval_error("Unknown escaped sequence in string", File_Position(line, col), filename);
- }
- is_escaped = false;
- }
- } else if (interpolation_allowed && t_char == '$') {
- saw_interpolation_marker = true;
- } else {
- match.push_back(t_char);
- }
- }
- }
- };
- /// Reads (and potentially captures) a quoted string from input. Translates escaped sequences.
- bool Quoted_String() {
- SkipWS();
- const auto start = m_position;
- if (Quoted_String_()) {
- std::string match;
- const auto prev_stack_top = m_match_stack.size();
- bool is_interpolated = [&]()->bool {
- Char_Parser<std::string> cparser(match, true);
- auto s = start + 1, end = m_position - 1;
- while (s != end) {
- if (cparser.saw_interpolation_marker) {
- if (*s == '{') {
- //We've found an interpolation point
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, const_var(match)));
- if (cparser.is_interpolated) {
- //If we've seen previous interpolation, add on instead of making a new one
- build_match<eval::Binary_Operator_AST_Node<Tracer>>(prev_stack_top, "+");
- }
- //We've finished with the part of the string up to this point, so clear it
- match.clear();
- std::string eval_match;
- ++s;
- while ((s != end) && (*s != '}')) {
- eval_match.push_back(*s);
- ++s;
- }
- if (*s == '}') {
- cparser.is_interpolated = true;
- ++s;
- const auto tostr_stack_top = m_match_stack.size();
- m_match_stack.push_back(make_node<eval::Id_AST_Node<Tracer>>("to_string", start.line, start.col));
- const auto ev_stack_top = m_match_stack.size();
- try {
- m_match_stack.push_back(parse_instr_eval(eval_match));
- } catch (const exception::eval_error &e) {
- throw exception::eval_error(e.what(), File_Position(start.line, start.col), *m_filename);
- }
- build_match<eval::Arg_List_AST_Node<Tracer>>(ev_stack_top);
- build_match<eval::Fun_Call_AST_Node<Tracer>>(tostr_stack_top);
- build_match<eval::Binary_Operator_AST_Node<Tracer>>(prev_stack_top, "+");
- } else {
- throw exception::eval_error("Unclosed in-string eval", File_Position(start.line, start.col), *m_filename);
- }
- } else {
- match.push_back('$');
- }
- cparser.saw_interpolation_marker = false;
- } else {
- cparser.parse(*s, start.line, start.col, *m_filename);
- ++s;
- }
- }
- return cparser.is_interpolated;
- }();
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, const_var(match)));
- if (is_interpolated) {
- build_match<eval::Binary_Operator_AST_Node<Tracer>>(prev_stack_top, "+");
- }
- return true;
- } else {
- return false;
- }
- }
- /// Reads a character group from input, without skipping initial whitespace
- bool Single_Quoted_String_() {
- bool retval = false;
- if (m_position.has_more() && (*m_position == '\'')) {
- retval = true;
- char prev_char = *m_position;
- ++m_position;
- while (m_position.has_more() && ((*m_position != '\'') || ((*m_position == '\'') && (prev_char == '\\')))) {
- if (!Eol_()) {
- if (prev_char == '\\') {
- prev_char = 0;
- } else {
- prev_char = *m_position;
- }
- ++m_position;
- }
- }
- if (m_position.has_more()) {
- ++m_position;
- } else {
- throw exception::eval_error("Unclosed single-quoted string", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- return retval;
- }
- /// Reads (and potentially captures) a char group from input. Translates escaped sequences.
- bool Single_Quoted_String() {
- SkipWS();
- const auto start = m_position;
- if (Single_Quoted_String_()) {
- std::string match;
- {
- // scope for cparser destructor
- Char_Parser<std::string> cparser(match, false);
- for (auto s = start + 1, end = m_position - 1; s != end; ++s) {
- cparser.parse(*s, start.line, start.col, *m_filename);
- }
- }
- if (match.size() != 1) {
- throw exception::eval_error("Single-quoted strings must be 1 character long", File_Position(m_position.line, m_position.col), *m_filename);
- }
- m_match_stack.push_back(make_node<eval::Constant_AST_Node<Tracer>>(match, start.line, start.col, const_var(char(match.at(0)))));
- return true;
- }
- else {
- return false;
- }
- }
- /// Reads a char from input if it matches the parameter, without skipping initial whitespace
- bool Char_(const char c) {
- if (m_position.has_more() && (*m_position == c)) {
- ++m_position;
- return true;
- } else {
- return false;
- }
- }
- /// Reads (and potentially captures) a char from input if it matches the parameter
- bool Char(const char t_c) {
- SkipWS();
- return Char_(t_c);
- }
- /// Reads a string from input if it matches the parameter, without skipping initial whitespace
- bool Keyword_(const utility::Static_String &t_s) {
- const auto len = t_s.size();
- if (m_position.remaining() >= len) {
- auto tmp = m_position;
- for (size_t i = 0; tmp.has_more() && i < len; ++i) {
- if (*tmp != t_s.c_str()[i]) {
- return false;
- }
- ++tmp;
- }
- m_position = tmp;
- return true;
- }
- return false;
- }
- /// Reads (and potentially captures) a string from input if it matches the parameter
- bool Keyword(const utility::Static_String &t_s) {
- SkipWS();
- const auto start = m_position;
- bool retval = Keyword_(t_s);
- // ignore substring matches
- if ( retval && m_position.has_more() && char_in_alphabet(*m_position, detail::keyword_alphabet) ) {
- m_position = start;
- retval = false;
- }
- return retval;
- }
- bool is_operator(const std::string &t_s) const {
- return std::any_of(m_operator_matches.begin(), m_operator_matches.end(),
- [t_s](const std::vector<utility::Static_String> &opers) {
- return std::any_of(opers.begin(), opers.end(),
- [t_s](const utility::Static_String &s) {
- return t_s == s.c_str();
- });
- });
- }
- /// Reads (and potentially captures) a symbol group from input if it matches the parameter
- bool Symbol(const utility::Static_String &t_s, const bool t_disallow_prevention=false) {
- SkipWS();
- const auto start = m_position;
- bool retval = Symbol_(t_s);
- // ignore substring matches
- if (retval && m_position.has_more() && (t_disallow_prevention == false) && char_in_alphabet(*m_position,detail::symbol_alphabet)) {
- if (*m_position != '=' && is_operator(Position::str(start, m_position)) && !is_operator(Position::str(start, m_position+1))) {
- // don't throw this away, it's a good match and the next is not
- } else {
- m_position = start;
- retval = false;
- }
- }
- return retval;
- }
- /// Reads an end-of-line group from input, without skipping initial whitespace
- bool Eol_(const bool t_eos = false) {
- bool retval = false;
- if (m_position.has_more() && (Symbol_(cr_lf()) || Char_('\n'))) {
- retval = true;
- //++m_position.line;
- m_position.col = 1;
- } else if (m_position.has_more() && !t_eos && Char_(';')) {
- retval = true;
- }
- return retval;
- }
- /// Reads until the end of the current statement
- bool Eos() {
- SkipWS();
- return Eol_(true);
- }
- /// Reads (and potentially captures) an end-of-line group from input
- bool Eol() {
- SkipWS();
- return Eol_();
- }
- /// Reads a comma-separated list of values from input. Id's only, no types allowed
- bool Id_Arg_List() {
- SkipWS(true);
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Arg(false)) {
- retval = true;
- while (Eol()) {}
- while (Char(',')) {
- while (Eol()) {}
- if (!Arg(false)) {
- throw exception::eval_error("Unexpected value in parameter list", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- }
- build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
- SkipWS(true);
- return retval;
- }
- /// Reads a comma-separated list of values from input, for function declarations
- bool Decl_Arg_List() {
- SkipWS(true);
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Arg()) {
- retval = true;
- while (Eol()) {}
- while (Char(',')) {
- while (Eol()) {}
- if (!Arg()) {
- throw exception::eval_error("Unexpected value in parameter list", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- }
- build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
- SkipWS(true);
- return retval;
- }
- /// Reads a comma-separated list of values from input
- bool Arg_List() {
- SkipWS(true);
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Equation()) {
- retval = true;
- while (Eol()) {}
- while (Char(',')) {
- while (Eol()) {}
- if (!Equation()) {
- throw exception::eval_error("Unexpected value in parameter list", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- }
- build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
- SkipWS(true);
- return retval;
- }
- /// Reads possible special container values, including ranges and map_pairs
- bool Container_Arg_List() {
- bool retval = false;
- SkipWS(true);
- const auto prev_stack_top = m_match_stack.size();
- if (Value_Range()) {
- retval = true;
- build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
- } else if (Map_Pair()) {
- retval = true;
- while (Eol()) {}
- while (Char(',')) {
- while (Eol()) {}
- if (!Map_Pair()) {
- throw exception::eval_error("Unexpected value in container", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
- } else if (Operator()) {
- retval = true;
- while (Eol()) {}
- while (Char(',')) {
- while (Eol()) {}
- if (!Operator()) {
- throw exception::eval_error("Unexpected value in container", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
- }
- SkipWS(true);
- return retval;
- }
- /// Reads a lambda (anonymous function) from input
- bool Lambda() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("fun")) {
- retval = true;
- if (Char('[')) {
- Id_Arg_List();
- if (!Char(']')) {
- throw exception::eval_error("Incomplete anonymous function bind", File_Position(m_position.line, m_position.col), *m_filename);
- }
- } else {
- // make sure we always have the same number of nodes
- build_match<eval::Arg_List_AST_Node<Tracer>>(prev_stack_top);
- }
- if (Char('(')) {
- Decl_Arg_List();
- if (!Char(')')) {
- throw exception::eval_error("Incomplete anonymous function", File_Position(m_position.line, m_position.col), *m_filename);
- }
- } else {
- throw exception::eval_error("Incomplete anonymous function", File_Position(m_position.line, m_position.col), *m_filename);
- }
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete anonymous function", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Lambda_AST_Node<Tracer>>(prev_stack_top);
- }
- return retval;
- }
- /// Reads a function definition from input
- bool Def(const bool t_class_context = false, const std::string &t_class_name = "") {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("def")) {
- retval = true;
- if (t_class_context) {
- m_match_stack.push_back(make_node<eval::Id_AST_Node<Tracer>>(t_class_name, m_position.line, m_position.col));
- }
- if (!Id(true)) {
- throw exception::eval_error("Missing function name in definition", File_Position(m_position.line, m_position.col), *m_filename);
- }
- bool is_method = false;
- if (Symbol("::")) {
- //We're now a method
- is_method = true;
- if (!Id(true)) {
- throw exception::eval_error("Missing method name in definition", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- if (Char('(')) {
- Decl_Arg_List();
- if (!Char(')')) {
- throw exception::eval_error("Incomplete function definition", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- while (Eos()) {}
- if (Char(':')) {
- if (!Operator()) {
- throw exception::eval_error("Missing guard expression for function", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete function definition", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (is_method || t_class_context) {
- build_match<eval::Method_AST_Node<Tracer>>(prev_stack_top);
- } else {
- build_match<eval::Def_AST_Node<Tracer>>(prev_stack_top);
- }
- }
- return retval;
- }
- /// Reads a function definition from input
- bool Try() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("try")) {
- retval = true;
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'try' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- bool has_matches = true;
- while (has_matches) {
- while (Eol()) {}
- has_matches = false;
- if (Keyword("catch")) {
- const auto catch_stack_top = m_match_stack.size();
- if (Char('(')) {
- if (!(Arg() && Char(')'))) {
- throw exception::eval_error("Incomplete 'catch' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (Char(':')) {
- if (!Operator()) {
- throw exception::eval_error("Missing guard expression for catch", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- }
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'catch' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Catch_AST_Node<Tracer>>(catch_stack_top);
- has_matches = true;
- }
- }
- while (Eol()) {}
- if (Keyword("finally")) {
- const auto finally_stack_top = m_match_stack.size();
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'finally' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Finally_AST_Node<Tracer>>(finally_stack_top);
- }
- build_match<eval::Try_AST_Node<Tracer>>(prev_stack_top);
- }
- return retval;
- }
- /// Reads an if/else if/else block from input
- bool If() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("if")) {
- retval = true;
- if (!Char('(')) {
- throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (!Equation()) {
- throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- const bool is_if_init = Eol() && Equation();
- if (!Char(')')) {
- throw exception::eval_error("Incomplete 'if' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'if' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- bool has_matches = true;
- while (has_matches) {
- while (Eol()) {}
- has_matches = false;
- if (Keyword("else")) {
- if (If()) {
- has_matches = true;
- } else {
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'else' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- has_matches = true;
- }
- }
- }
- const auto num_children = m_match_stack.size() - prev_stack_top;
- if ((is_if_init && num_children == 3)
- || (!is_if_init && num_children == 2)) {
- m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
- }
- if (!is_if_init) {
- build_match<eval::If_AST_Node<Tracer>>(prev_stack_top);
- } else {
- build_match<eval::If_AST_Node<Tracer>>(prev_stack_top+1);
- build_match<eval::Block_AST_Node<Tracer>>(prev_stack_top);
- }
- }
- return retval;
- }
- /// Reads a class block from input
- bool Class(const bool t_class_allowed) {
- bool retval = false;
- size_t prev_stack_top = m_match_stack.size();
- if (Keyword("class")) {
- if (!t_class_allowed) {
- throw exception::eval_error("Class definitions only allowed at top scope", File_Position(m_position.line, m_position.col), *m_filename);
- }
- retval = true;
- if (!Id(true)) {
- throw exception::eval_error("Missing class name in definition", File_Position(m_position.line, m_position.col), *m_filename);
- }
- const auto class_name = m_match_stack.back()->text;
- while (Eol()) {}
- if (!Class_Block(class_name)) {
- throw exception::eval_error("Incomplete 'class' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Class_AST_Node<Tracer>>(prev_stack_top);
- }
- return retval;
- }
- /// Reads a while block from input
- bool While() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("while")) {
- retval = true;
- if (!Char('(')) {
- throw exception::eval_error("Incomplete 'while' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (!(Operator() && Char(')'))) {
- throw exception::eval_error("Incomplete 'while' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'while' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::While_AST_Node<Tracer>>(prev_stack_top);
- }
- return retval;
- }
- /// Reads the ranged `for` conditions from input
- bool Range_Expression() {
- // the first element will have already been captured by the For_Guards() call that preceeds it
- return Char(':') && Equation();
- }
- /// Reads the C-style `for` conditions from input
- bool For_Guards() {
- if (!(Equation() && Eol()))
- {
- if (!Eol())
- {
- return false;
- } else {
- m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
- }
- }
- if (!(Equation() && Eol()))
- {
- if (!Eol())
- {
- return false;
- } else {
- m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Constant_AST_Node<Tracer>>(Boxed_Value(true)));
- }
- }
- if (!Equation())
- {
- m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
- }
- return true;
- }
- /// Reads a for block from input
- bool For() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("for")) {
- retval = true;
- if (!Char('(')) {
- throw exception::eval_error("Incomplete 'for' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- const bool classic_for = For_Guards() && Char(')');
- if (!classic_for && !(Range_Expression() && Char(')'))) {
- throw exception::eval_error("Incomplete 'for' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'for' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- const auto num_children = m_match_stack.size() - prev_stack_top;
- if (classic_for) {
- if (num_children != 4) {
- throw exception::eval_error("Incomplete 'for' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::For_AST_Node<Tracer>>(prev_stack_top);
- } else {
- if (num_children != 3) {
- throw exception::eval_error("Incomplete ranged-for expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Ranged_For_AST_Node<Tracer>>(prev_stack_top);
- }
- }
- return retval;
- }
- /// Reads a case block from input
- bool Case() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("case")) {
- retval = true;
- if (!Char('(')) {
- throw exception::eval_error("Incomplete 'case' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (!(Operator() && Char(')'))) {
- throw exception::eval_error("Incomplete 'case' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'case' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Case_AST_Node<Tracer>>(prev_stack_top);
- } else if (Keyword("default")) {
- retval = true;
- while (Eol()) {}
- if (!Block()) {
- throw exception::eval_error("Incomplete 'default' block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Default_AST_Node<Tracer>>(prev_stack_top);
- }
- return retval;
- }
- /// Reads a switch statement from input
- bool Switch() {
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("switch")) {
- if (!Char('(')) {
- throw exception::eval_error("Incomplete 'switch' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (!(Operator() && Char(')'))) {
- throw exception::eval_error("Incomplete 'switch' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- while (Eol()) {}
- if (Char('{')) {
- while (Eol()) {}
- while (Case()) {
- while (Eol()) { } // eat
- }
- while (Eol()) { } // eat
- if (!Char('}')) {
- throw exception::eval_error("Incomplete block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- else {
- throw exception::eval_error("Incomplete block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Switch_AST_Node<Tracer>>(prev_stack_top);
- return true;
- } else {
- return false;
- }
- }
- /// Reads a curly-brace C-style class block from input
- bool Class_Block(const std::string &t_class_name) {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Char('{')) {
- retval = true;
- Class_Statements(t_class_name);
- if (!Char('}')) {
- throw exception::eval_error("Incomplete class block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (m_match_stack.size() == prev_stack_top) {
- m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
- }
- build_match<eval::Block_AST_Node<Tracer>>(prev_stack_top);
- }
- return retval;
- }
- /// Reads a curly-brace C-style block from input
- bool Block() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Char('{')) {
- retval = true;
- Statements();
- if (!Char('}')) {
- throw exception::eval_error("Incomplete block", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (m_match_stack.size() == prev_stack_top) {
- m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
- }
- build_match<eval::Block_AST_Node<Tracer>>(prev_stack_top);
- }
- return retval;
- }
- /// Reads a return statement from input
- bool Return() {
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("return")) {
- Operator();
- build_match<eval::Return_AST_Node<Tracer>>(prev_stack_top);
- return true;
- } else {
- return false;
- }
- }
- /// Reads a break statement from input
- bool Break() {
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("break")) {
- build_match<eval::Break_AST_Node<Tracer>>(prev_stack_top);
- return true;
- } else {
- return false;
- }
- }
- /// Reads a continue statement from input
- bool Continue() {
- const auto prev_stack_top = m_match_stack.size();
- if (Keyword("continue")) {
- build_match<eval::Continue_AST_Node<Tracer>>(prev_stack_top);
- return true;
- } else {
- return false;
- }
- }
- /// Reads a dot expression(member access), then proceeds to check if it's a function or array call
- bool Dot_Fun_Array() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (Lambda() || Num() || Quoted_String() || Single_Quoted_String() ||
- Paren_Expression() || Inline_Container() || Id(false))
- {
- retval = true;
- bool has_more = true;
- while (has_more) {
- has_more = false;
- if (Char('(')) {
- has_more = true;
- Arg_List();
- if (!Char(')')) {
- throw exception::eval_error("Incomplete function call", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Fun_Call_AST_Node<Tracer>>(prev_stack_top);
- /// \todo Work around for method calls until we have a better solution
- if (!m_match_stack.back()->children.empty()) {
- if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Dot_Access) {
- if (m_match_stack.empty()) { throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (m_match_stack.back()->children.empty()) { throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
- }
- auto dot_access = std::move(m_match_stack.back()->children[0]);
- auto func_call = std::move(m_match_stack.back());
- m_match_stack.pop_back();
- func_call->children.erase(func_call->children.begin());
- if (dot_access->children.empty()) { throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
- }
- func_call->children.insert(func_call->children.begin(), std::move(dot_access->children.back()));
- dot_access->children.pop_back();
- dot_access->children.push_back(std::move(func_call));
- if (dot_access->children.size() != 2) { throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
- }
- m_match_stack.push_back(std::move(dot_access));
- }
- }
- } else if (Char('[')) {
- has_more = true;
- if (!(Operator() && Char(']'))) {
- throw exception::eval_error("Incomplete array access", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Array_Call_AST_Node<Tracer>>(prev_stack_top);
- }
- else if (Symbol(".")) {
- has_more = true;
- if (!(Id(true))) {
- throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if ( std::distance(m_match_stack.begin() + static_cast<int>(prev_stack_top), m_match_stack.end()) != 2) {
- throw exception::eval_error("Incomplete dot access fun call", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Dot_Access_AST_Node<Tracer>>(prev_stack_top);
- }
- }
- }
- return retval;
- }
- /// Reads a variable declaration from input
- bool Var_Decl(const bool t_class_context = false, const std::string &t_class_name = "") {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (t_class_context && (Keyword("attr") || Keyword("auto") || Keyword("var"))) {
- retval = true;
- m_match_stack.push_back(make_node<eval::Id_AST_Node<Tracer>>(t_class_name, m_position.line, m_position.col));
- if (!Id(true)) {
- throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Attr_Decl_AST_Node<Tracer>>(prev_stack_top);
- } else if (Keyword("auto") || Keyword("var") ) {
- retval = true;
- if (Reference()) {
- // we built a reference node - continue
- } else if (Id(true)) {
- build_match<eval::Var_Decl_AST_Node<Tracer>>(prev_stack_top);
- } else {
- throw exception::eval_error("Incomplete variable declaration", File_Position(m_position.line, m_position.col), *m_filename);
- }
- } else if (Keyword("global")) {
- retval = true;
- if (!(Reference() || Id(true))) {
- throw exception::eval_error("Incomplete global declaration", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Global_Decl_AST_Node<Tracer>>(prev_stack_top);
- } else if (Keyword("attr")) {
- retval = true;
- if (!Id(true)) {
- throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (!Symbol("::")) {
- throw exception::eval_error("Incomplete attribute declaration", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (!Id(true)) {
- throw exception::eval_error("Missing attribute name in definition", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Attr_Decl_AST_Node<Tracer>>(prev_stack_top);
- }
- return retval;
- }
- /// Reads an expression surrounded by parentheses from input
- bool Paren_Expression() {
- if (Char('(')) {
- if (!Operator()) {
- throw exception::eval_error("Incomplete expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if (!Char(')')) {
- throw exception::eval_error("Missing closing parenthesis ')'", File_Position(m_position.line, m_position.col), *m_filename);
- }
- return true;
- } else {
- return false;
- }
- }
- /// Reads, and identifies, a short-form container initialization from input
- bool Inline_Container() {
- const auto prev_stack_top = m_match_stack.size();
- if (Char('[')) {
- Container_Arg_List();
- if (!Char(']')) {
- throw exception::eval_error("Missing closing square bracket ']' in container initializer", File_Position(m_position.line, m_position.col), *m_filename);
- }
- if ((prev_stack_top != m_match_stack.size()) && (!m_match_stack.back()->children.empty())) {
- if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Value_Range) {
- build_match<eval::Inline_Range_AST_Node<Tracer>>(prev_stack_top);
- }
- else if (m_match_stack.back()->children[0]->identifier == AST_Node_Type::Map_Pair) {
- build_match<eval::Inline_Map_AST_Node<Tracer>>(prev_stack_top);
- }
- else {
- build_match<eval::Inline_Array_AST_Node<Tracer>>(prev_stack_top);
- }
- }
- else {
- build_match<eval::Inline_Array_AST_Node<Tracer>>(prev_stack_top);
- }
- return true;
- } else {
- return false;
- }
- }
- /// Parses a variable specified with a & aka reference
- bool Reference() {
- const auto prev_stack_top = m_match_stack.size();
- if (Symbol("&")) {
- if (!Id(true)) {
- throw exception::eval_error("Incomplete '&' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Reference_AST_Node<Tracer>>(prev_stack_top);
- return true;
- } else {
- return false;
- }
- }
- /// Reads a unary prefixed expression from input
- bool Prefix() {
- const auto prev_stack_top = m_match_stack.size();
- using SS = utility::Static_String;
- constexpr const std::array<utility::Static_String, 6> prefix_opers{{
- SS{"++"},
- SS{"--"},
- SS{"-"},
- SS{"+"},
- SS{"!"},
- SS{"~"}
- }};
- for (const auto &oper : prefix_opers)
- {
- const bool is_char = oper.size() == 1;
- if ((is_char && Char(oper.c_str()[0])) || (!is_char && Symbol(oper)))
- {
- if (!Operator(m_operators.size()-1)) {
- throw exception::eval_error("Incomplete prefix '" + std::string(oper.c_str()) + "' expression", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Prefix_AST_Node<Tracer>>(prev_stack_top, oper.c_str());
- return true;
- }
- }
- return false;
- }
- /// Parses any of a group of 'value' style ast_node groups from input
- bool Value() {
- return Var_Decl() || Dot_Fun_Array() || Prefix();
- }
- bool Operator_Helper(const size_t t_precedence, std::string &oper) {
- for (auto & elem : m_operator_matches[t_precedence]) {
- if (Symbol(elem)) {
- oper = elem.c_str();
- return true;
- }
- }
- return false;
- }
- bool Operator(const size_t t_precedence = 0) {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- if (m_operators[t_precedence] != Operator_Precidence::Prefix) {
- if (Operator(t_precedence+1)) {
- retval = true;
- std::string oper;
- while (Operator_Helper(t_precedence, oper)) {
- while (Eol()) {}
- if (!Operator(t_precedence+1)) {
- throw exception::eval_error("Incomplete '" + oper + "' expression",
- File_Position(m_position.line, m_position.col), *m_filename);
- }
- switch (m_operators[t_precedence]) {
- case(Operator_Precidence::Ternary_Cond) :
- if (Symbol(":")) {
- if (!Operator(t_precedence+1)) {
- throw exception::eval_error("Incomplete '" + oper + "' expression",
- File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::If_AST_Node<Tracer>>(prev_stack_top);
- }
- else {
- throw exception::eval_error("Incomplete '" + oper + "' expression",
- File_Position(m_position.line, m_position.col), *m_filename);
- }
- break;
- case(Operator_Precidence::Addition) :
- case(Operator_Precidence::Multiplication) :
- case(Operator_Precidence::Shift) :
- case(Operator_Precidence::Equality) :
- case(Operator_Precidence::Bitwise_And) :
- case(Operator_Precidence::Bitwise_Xor) :
- case(Operator_Precidence::Bitwise_Or) :
- case(Operator_Precidence::Comparison) :
- build_match<eval::Binary_Operator_AST_Node<Tracer>>(prev_stack_top, oper);
- break;
- case(Operator_Precidence::Logical_And) :
- build_match<eval::Logical_And_AST_Node<Tracer>>(prev_stack_top, oper);
- break;
- case(Operator_Precidence::Logical_Or) :
- build_match<eval::Logical_Or_AST_Node<Tracer>>(prev_stack_top, oper);
- break;
- case(Operator_Precidence::Prefix) :
- assert(false); // cannot reach here because of if() statement at the top
- break;
- // default:
- // throw exception::eval_error("Internal error: unhandled ast_node", File_Position(m_position.line, m_position.col), *m_filename);
- }
- }
- }
- } else {
- return Value();
- }
- return retval;
- }
- /// Reads a pair of values used to create a map initialization from input
- bool Map_Pair() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- const auto prev_pos = m_position;
- if (Operator()) {
- if (Symbol(":")) {
- retval = true;
- if (!Operator()) {
- throw exception::eval_error("Incomplete map pair", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Map_Pair_AST_Node<Tracer>>(prev_stack_top);
- }
- else {
- m_position = prev_pos;
- while (prev_stack_top != m_match_stack.size()) {
- m_match_stack.pop_back();
- }
- }
- }
- return retval;
- }
- /// Reads a pair of values used to create a range initialization from input
- bool Value_Range() {
- bool retval = false;
- const auto prev_stack_top = m_match_stack.size();
- const auto prev_pos = m_position;
- if (Operator()) {
- if (Symbol("..")) {
- retval = true;
- if (!Operator()) {
- throw exception::eval_error("Incomplete value range", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Value_Range_AST_Node<Tracer>>(prev_stack_top);
- }
- else {
- m_position = prev_pos;
- while (prev_stack_top != m_match_stack.size()) {
- m_match_stack.pop_back();
- }
- }
- }
- return retval;
- }
- /// Parses a string of binary equation operators
- bool Equation() {
- const auto prev_stack_top = m_match_stack.size();
- using SS = utility::Static_String;
- if (Operator()) {
- for (const auto &sym : {SS{"="}, SS{":="}, SS{"+="}, SS{"-="}, SS{"*="}, SS{"/="}, SS{"%="}, SS{"<<="}, SS{">>="}, SS{"&="}, SS{"^="}, SS{"|="}})
- {
- if (Symbol(sym, true)) {
- SkipWS(true);
- if (!Equation()) {
- throw exception::eval_error("Incomplete equation", File_Position(m_position.line, m_position.col), *m_filename);
- }
- build_match<eval::Equation_AST_Node<Tracer>>(prev_stack_top, sym.c_str());
- return true;
- }
- }
- return true;
- }
- return false;
- }
- /// Parses statements allowed inside of a class block
- bool Class_Statements(const std::string &t_class_name) {
- bool retval = false;
- bool has_more = true;
- bool saw_eol = true;
- while (has_more) {
- const auto start = m_position;
- if (Def(true, t_class_name) || Var_Decl(true, t_class_name)) {
- if (!saw_eol) {
- throw exception::eval_error("Two function definitions missing line separator", File_Position(start.line, start.col), *m_filename);
- }
- has_more = true;
- retval = true;
- saw_eol = true;
- } else if (Eol()) {
- has_more = true;
- retval = true;
- saw_eol = true;
- } else {
- has_more = false;
- }
- }
- return retval;
- }
- /// Top level parser, starts parsing of all known parses
- bool Statements(const bool t_class_allowed = false) {
- bool retval = false;
- bool has_more = true;
- bool saw_eol = true;
- while (has_more) {
- const auto start = m_position;
- if (Def() || Try() || If() || While() || Class(t_class_allowed) || For() || Switch()) {
- if (!saw_eol) {
- throw exception::eval_error("Two function definitions missing line separator", File_Position(start.line, start.col), *m_filename);
- }
- has_more = true;
- retval = true;
- saw_eol = true;
- }
- else if (Return() || Break() || Continue() || Equation()) {
- if (!saw_eol) {
- throw exception::eval_error("Two expressions missing line separator", File_Position(start.line, start.col), *m_filename);
- }
- has_more = true;
- retval = true;
- saw_eol = false;
- }
- else if (Block() || Eol()) {
- has_more = true;
- retval = true;
- saw_eol = true;
- }
- else {
- has_more = false;
- }
- }
- return retval;
- }
- AST_NodePtr parse(const std::string &t_input, const std::string &t_fname) override
- {
- ChaiScript_Parser<Tracer, Optimizer> parser(m_tracer, m_optimizer);
- return parser.parse_internal(t_input, t_fname);
- }
- eval::AST_Node_Impl_Ptr<Tracer> parse_instr_eval(const std::string &t_input)
- {
- auto last_position = m_position;
- auto last_filename = m_filename;
- auto last_match_stack = std::exchange(m_match_stack, decltype(m_match_stack){});
- auto retval = parse_internal(t_input, "instr eval");
- m_position = std::move(last_position);
- m_filename = std::move(last_filename);
- m_match_stack = std::move(last_match_stack);
- return eval::AST_Node_Impl_Ptr<Tracer>(dynamic_cast<eval::AST_Node_Impl<Tracer>*>(retval.release()));
- }
- /// Parses the given input string, tagging parsed ast_nodes with the given m_filename.
- AST_NodePtr parse_internal(const std::string &t_input, std::string t_fname) {
- m_position = Position(t_input.begin(), t_input.end());
- m_filename = std::make_shared<std::string>(std::move(t_fname));
- if ((t_input.size() > 1) && (t_input[0] == '#') && (t_input[1] == '!')) {
- while (m_position.has_more() && (!Eol())) {
- ++m_position;
- }
- }
- if (Statements(true)) {
- if (m_position.has_more()) {
- throw exception::eval_error("Unparsed input", File_Position(m_position.line, m_position.col), *m_filename);
- } else {
- build_match<eval::File_AST_Node<Tracer>>(0);
- }
- } else {
- m_match_stack.push_back(chaiscript::make_unique<eval::AST_Node_Impl<Tracer>, eval::Noop_AST_Node<Tracer>>());
- }
- AST_NodePtr retval(std::move(m_match_stack.front()));
- m_match_stack.clear();
- return retval;
- }
- };
- }
- }
- #if defined(CHAISCRIPT_MSVC) && defined(CHAISCRIPT_PUSHED_MIN_MAX)
- #undef CHAISCRIPT_PUSHED_MIN_MAX
- #pragma pop_macro("min")
- #pragma pop_macro("max")
- #endif
- #endif /* CHAISCRIPT_PARSER_HPP_ */
|