Gorgon Game Engine
Utils.h
Go to the documentation of this file.
1 #include "../../Scripting.h"
2 
3 namespace Gorgon { namespace Scripting { namespace Compilers {
4 
8  template<class ...P_>
9  int CheckInputFor(const std::string &input, int &ch, P_ ...args) {
10  char allowed[] = {args...};
11  int elements = sizeof...(args);
12 
13  auto errstr = [&] ()-> std::string {
14  std::string ret = "Expected ";
15  for(int i = 0; i < elements; i++) {
16  if(i != 0) {
17  ret.push_back('/');
18  }
19  ret.push_back(allowed[i]);
20  }
21 
22  return ret;
23  };
24 
25  if((int)input.length() <= ch) {
26  throw ParseError({ExceptionType::UnexpectedToken, errstr() + ", end of string encountered.", ch, 0});
27  }
28 
29  char c = input[ch++];
30 
31  for(int i = 0; i < sizeof...(args); i++) {
32  if(allowed[i] == c) {
33  return i;
34  }
35  }
36 
37  ch--;
38  throw ParseError({ExceptionType::UnexpectedToken, errstr() + ", found: " + input.substr(ch, 1), ch, 0});
39  }
40 
44  inline std::string ExtractQuotes(const std::string &input, int &ch) {
45  std::string ret = "";
46 
47  int quotes = CheckInputFor(input, ch, '\'', '"') + 1;
48 
49  int start = ch;
50 
51  bool escape = false;
52  int escapenum = 0;
53  Gorgon::Byte num;
54  for(; ch < (int)input.size(); ch++) {
55  char c = input[ch];
56  if(escape) {
57  if(c == '"') {
58  ret.push_back('"');
59  }
60  else if(c == '\'') {
61  ret.push_back('\'');
62  }
63  else if(c == '\\') {
64  ret.push_back('\\');
65  }
66  else if(c == 'n') {
67  ret.push_back('\n');
68  }
69  else if(c == 't') {
70  ret.push_back('\t');
71  }
72  else if(c >= '0' && c <= '9') {
73  escapenum++;
74  num = (num << 4) + (c - '0');
75  }
76  else if(tolower(c) >= 'a' && tolower(c) <= 'f') {
77  escapenum++;
78  num = (num << 4) + (tolower(c) - 'a' + 10);
79  }
80  else {
81  throw ParseError{ExceptionType::UnexpectedToken, "Invalid escape sequence: \\" + input.substr(ch, 1), ch, 0};
82  }
83 
84  if(escapenum != 1) {
85  escape = false;
86  }
87  if(escapenum == 2) {
88  ret.push_back((char)num);
89  escapenum = 0;
90  }
91  }
92  else {
93  if(c == '\\') {
94  escape = true;
95  num = 0;
96  }
97  else if(c == '"' && quotes == 2) {
98  break;
99  }
100  else if(c == '\'' && quotes == 1) {
101  break;
102  }
103  else {
104  ret.push_back(c);
105  }
106  }
107  }
108 
109  CheckInputFor(input, ch, quotes == 2 ? '"' : '\'');
110 
111  return ret;
112  }
113 
114 } } }
Gorgon::Scripting::Compilers::ASTToSVG
void ASTToSVG(const std::string &line, ASTNode &tree, const std::vector< std::string > &compiled={}, bool show=false)
Converts given AST to an SVG file.
Definition: AST.cpp:88
Gorgon::Input::Mouse::None
@ None
Definition: Mouse.h:32
Gorgon::swap
void swap(Event< Source_, Args_... > &l, Event< Source_, Args_... > &r)
Swaps two events.
Definition: Event.h:351
Gorgon::String::From
std::enable_if< decltype(gorgon__enum_tr_loc(T_()))::isupgradedenum, std::string >::type From(const T_ &e)
Definition: Enum.h:303
Gorgon::Resource::GID::Data
constexpr Type Data
Data resource.
Definition: GID.h:164
Gorgon::String::Replace
std::string Replace(std::string str, const std::string &find, const std::string &replace)
String replace that does not use regex.
Definition: String.h:349
Gorgon::Scripting::Compilers::ASTNode::Operator
@ Operator
This node represents an operator. All operators in GorgonScript are left associative and binary.
Definition: AST.h:46
Gorgon::Scripting::Compilers::PrintAST
void PrintAST(ASTNode &tree)
Recursively prints an AST.
Definition: AST.cpp:120
Gorgon::Scripting::Compilers::ExtractQuotes
std::string ExtractQuotes(const std::string &input, int &ch)
Extracts a string that is in quotes.
Definition: Utils.h:44
Gorgon::Scripting::Compilers::ASTNode::Identifier
@ Identifier
This node is an identifier.
Definition: AST.h:40
AST.h
Gorgon::Scripting::Compilers::ASTNode::Literal
@ Literal
This node represents a literal. Literal member of ASTNode should be set.
Definition: AST.h:25
Gorgon::Float
float Float
Represents floating point data type.
Definition: Types.h:16
Gorgon::Scripting::Compilers::ASTCompiler::Compile
unsigned Compile(ASTNode *tree)
This function compiles given abstract syntax tree, returns the number of instructions generated.
Definition: AST.cpp:355
Utils.h
Language.h
Gorgon::Scripting::Compilers::ASTNode::Keyword
@ Keyword
This node is a keyword call.
Definition: AST.h:57
Gorgon::Scripting::Compilers::GetPrecedence
int GetPrecedence(const std::string &op)
Returns the precedence of the given operator.
Definition: Language.h:12
Gorgon::Scripting::Compilers::ASTNode::FunctionCall
@ FunctionCall
This node represents a function call.
Definition: AST.h:30
Gorgon::Scripting::Compilers::ASTCompiler::IsReady
bool IsReady() const
If this function returns true, it is ok to use instructions from the list.
Definition: AST.h:127
Gorgon::Scripting::Compilers::ASTCompiler::Finalize
void Finalize()
Definition: AST.h:129
Gorgon::Utils::ASSERT_FALSE
void ASSERT_FALSE(const std::string &message, int skip=1, int depth=4)
Definition: Assert.h:192
Gorgon::Scripting::Types::Namespace
const Scripting::Type & Namespace()
Definition: Reflection.h:315
Gorgon::Graphics::internal::isspace
bool isspace(Glyph g)
Definition: Font.cpp:96
Gorgon::Scripting::ParameterTemplateType
Type * ParameterTemplateType()
Definition: Runtime.cpp:8
Gorgon
Root namespace for Gorgon Game Engine.
Definition: Any.h:19
Gorgon::Char
uint32_t Char
Definition: Types.h:46
Gorgon::Scripting::Compilers::Programming::Finalize
virtual void Finalize() override
Finalizes the compilation.
Gorgon::Scripting::Compilers::ASTNode::Variable
@ Variable
This node represents a variable identifier.
Definition: AST.h:43
Gorgon::Scripting::Compilers::ASTNode::MethodCall
@ MethodCall
Same as function call, this just calls method variant if it exists, if not it will print out return v...
Definition: AST.h:34
Gorgon::Scripting::Compilers::Disassemble
std::string Disassemble(const Instruction *)
Disassembles the given instruction.
Definition: Generator.cpp:55
Gorgon::Scripting::Compilers::Programming::Compile
virtual unsigned Compile(const std::string &input, unsigned long pline) override
Asks the compiler to compile the given input.
ASSERT
#define ASSERT(expression, message,...)
Replaces regular assert to allow messages and backtrace.
Definition: Assert.h:161
Gorgon::Scripting::DefineEnumStringsCM
DefineEnumStringsCM(StaticMember, MemberType, {StaticMember::RegularType, "RegularType"}, {StaticMember::EventType, "EventType"}, {StaticMember::EnumType, "EnumType"}, {StaticMember::Namespace, "Namespace"}, {StaticMember::DataMember, "DataMember"}, {StaticMember::Function, "Function"}, {StaticMember::Constant, "Constant"},)
Gorgon::String::ToLower
std::string ToLower(std::string str)
Converts the given string to lowercase.
Definition: String.h:416
Gorgon::Geometry::operator!=
bool operator!=(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:391
Gorgon::Scripting::Compilers::ASTNode::Member
@ Member
This node represents a membership. Membership should be parsed as left associative.
Definition: AST.h:37
Gorgon::Scripting::Compilers::ASTNode::Assignment
@ Assignment
This node is an assignment. This node should be top level.
Definition: AST.h:60
Gorgon::Geometry::operator==
bool operator==(PointList< P_ > &left, const PointList< P_ > &right)
Comparison: this operation is expensive: O(n).
Definition: PointList.h:385
Gorgon::Byte
unsigned char Byte
Represents smallest cell in memory.
Definition: Types.h:9
Gorgon::Scripting::KeywordNames
std::set< std::string, String::CaseInsensitiveLess > KeywordNames
Definition: Scripting.cpp:13
Gorgon::Scripting::Compilers::ASTNode::Index
@ Index
This node represents an indexing operation.
Definition: AST.h:50
Gorgon::Scripting::Compilers::Base::List
std::vector< Instruction > List
The instructions that are compiled.
Definition: Compilers.h:51
Gorgon::GL::UBOBindingPoint::Type
Type
Definition: Shader.h:14
Gorgon::Scripting::Compilers::ASTNode::Empty
@ Empty
This node is empty, possibly a placeholder for an identifier.
Definition: AST.h:63
Gorgon::Scripting::Compilers::ASTNode::List
@ List
List of expressions to be compiled.
Definition: AST.h:66
Gorgon::Scripting::Compilers::ASTNode::Construct
@ Construct
This node is a constructor node.
Definition: AST.h:54
Gorgon::Scripting::Compilers::CheckInputFor
int CheckInputFor(const std::string &input, int &ch, P_ ...args)
Checks if the input string contains one of the given characters at current point.
Definition: Utils.h:9
Gorgon::Scripting::Compilers::ASTNode::NodeType
NodeType
Node type.
Definition: AST.h:23