summaryrefslogtreecommitdiffhomepage
path: root/DefinitionEx/include/starshatter
diff options
context:
space:
mode:
Diffstat (limited to 'DefinitionEx/include/starshatter')
-rw-r--r--DefinitionEx/include/starshatter/definition.h11
-rw-r--r--DefinitionEx/include/starshatter/definition/Parser.h43
-rw-r--r--DefinitionEx/include/starshatter/definition/Term.h167
-rw-r--r--DefinitionEx/include/starshatter/definition/Token.h143
4 files changed, 364 insertions, 0 deletions
diff --git a/DefinitionEx/include/starshatter/definition.h b/DefinitionEx/include/starshatter/definition.h
new file mode 100644
index 0000000..785261c
--- /dev/null
+++ b/DefinitionEx/include/starshatter/definition.h
@@ -0,0 +1,11 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+*/
+
+#pragma once
+
+#include "definition/Parser.h"
+#include "definition/Term.h"
+#include "definition/Token.h"
diff --git a/DefinitionEx/include/starshatter/definition/Parser.h b/DefinitionEx/include/starshatter/definition/Parser.h
new file mode 100644
index 0000000..50659b9
--- /dev/null
+++ b/DefinitionEx/include/starshatter/definition/Parser.h
@@ -0,0 +1,43 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+
+ AUTHOR: John DiCamillo
+
+
+ OVERVIEW
+ ========
+ Declaration of the generic Parser class
+*/
+
+#pragma once
+
+#include <starshatter/foundation/reader.h>
+#include <Text.h>
+
+#include "Term.h"
+
+// +-------------------------------------------------------------------+
+
+class Scanner;
+
+// +-------------------------------------------------------------------+
+
+class Parser
+{
+public:
+ Parser();
+ Parser(starshatter::foundation::Reader r);
+ ~Parser();
+
+ Term* ParseTerm();
+ Term* ParseTermBase();
+ Term* ParseTermRest(Term* base);
+ TermList* ParseTermList(int for_struct);
+ TermArray* ParseArray();
+ TermStruct* ParseStruct();
+
+private:
+ Scanner* lexer;
+};
diff --git a/DefinitionEx/include/starshatter/definition/Term.h b/DefinitionEx/include/starshatter/definition/Term.h
new file mode 100644
index 0000000..1bc30b6
--- /dev/null
+++ b/DefinitionEx/include/starshatter/definition/Term.h
@@ -0,0 +1,167 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+
+ AUTHOR: John DiCamillo
+
+
+ OVERVIEW
+ ========
+ Declaration of the Abstract Syntax Tree classes
+*/
+
+#pragma once
+
+#include <Text.h>
+#include <List.h>
+
+// +-------------------------------------------------------------------+
+
+class Term;
+class TermBool;
+class TermNumber;
+class TermText;
+class TermArray;
+class TermDef;
+class TermStruct;
+
+// +-------------------------------------------------------------------+
+
+class Term
+{
+public:
+ static const char* TYPENAME() { return "Term"; }
+
+ Term() { }
+ virtual ~Term() { }
+
+ virtual int operator==(const Term& rhs) const { return 0; }
+
+ virtual void print(int level=10) { }
+
+ // conversion tests
+ virtual Term* touch() { return this; }
+ virtual TermBool* isBool() { return 0; }
+ virtual TermNumber* isNumber() { return 0; }
+ virtual TermText* isText() { return 0; }
+ virtual TermArray* isArray() { return 0; }
+ virtual TermDef* isDef() { return 0; }
+ virtual TermStruct* isStruct() { return 0; }
+};
+
+Term* error(const char*, const char* = 0);
+
+// +-------------------------------------------------------------------+
+
+typedef List<Term> TermList;
+typedef ListIter<Term> TermListIter;
+
+// +-------------------------------------------------------------------+
+
+class TermBool : public Term
+{
+public:
+ static const char* TYPENAME() { return "TermBool"; }
+
+ TermBool(bool v) : val(v) { }
+
+ virtual void print(int level=10);
+ virtual TermBool* isBool() { return this; }
+ bool value() const { return val; }
+
+private:
+ bool val;
+};
+
+// +-------------------------------------------------------------------+
+
+class TermNumber : public Term
+{
+public:
+ static const char* TYPENAME() { return "TermNumber"; }
+
+ TermNumber(double v) : val(v) { }
+
+ virtual void print(int level=10);
+ virtual TermNumber* isNumber() { return this; }
+ double value() const { return val; }
+
+private:
+ double val;
+};
+
+// +-------------------------------------------------------------------+
+
+class TermText : public Term
+{
+public:
+ static const char* TYPENAME() { return "TermText"; }
+
+ TermText(const Text& v) : val(v) { }
+
+ virtual void print(int level=10);
+ virtual TermText* isText() { return this; }
+ Text value() const { return val; }
+
+private:
+ Text val;
+};
+
+// +-------------------------------------------------------------------+
+
+class TermArray : public Term
+{
+public:
+ static const char* TYPENAME() { return "TermArray"; }
+
+ TermArray(TermList* elist);
+ virtual ~TermArray();
+
+ virtual void print(int level=10);
+ virtual TermArray* isArray() { return this; }
+ TermList* elements() { return elems; }
+
+private:
+ TermList* elems;
+};
+
+// +-------------------------------------------------------------------+
+
+class TermStruct : public Term
+{
+public:
+ static const char* TYPENAME() { return "TermStruct"; }
+
+ TermStruct(TermList* elist);
+ virtual ~TermStruct();
+
+ virtual void print(int level=10);
+
+ virtual TermStruct* isStruct() { return this; }
+ TermList* elements() { return elems; }
+
+private:
+ TermList* elems;
+};
+
+// +-------------------------------------------------------------------+
+
+class TermDef : public Term
+{
+public:
+ static const char* TYPENAME() { return "TermDef"; }
+
+ TermDef(TermText* n, Term* v) : mname(n), mval(v) { }
+ virtual ~TermDef();
+
+ virtual void print(int level=10);
+ virtual TermDef* isDef() { return this; }
+
+ virtual TermText* name() { return mname; }
+ virtual Term* term() { return mval; }
+
+private:
+ TermText* mname;
+ Term* mval;
+};
diff --git a/DefinitionEx/include/starshatter/definition/Token.h b/DefinitionEx/include/starshatter/definition/Token.h
new file mode 100644
index 0000000..3dd8eb9
--- /dev/null
+++ b/DefinitionEx/include/starshatter/definition/Token.h
@@ -0,0 +1,143 @@
+/* Starshatter: The Open Source Project
+ Copyright (c) 2021-2024, Starshatter: The Open Source Project Contributors
+ Copyright (c) 2011-2012, Starshatter OpenSource Distribution Contributors
+ Copyright (c) 1997-2006, Destroyer Studios LLC.
+
+ AUTHOR: John DiCamillo
+
+
+ OVERVIEW
+ ========
+ Scanner class definition
+*/
+
+#pragma once
+
+#include <Dictionary.h>
+#include <starshatter/foundation/reader.h>
+#include <Text.h>
+
+#pragma warning( disable : 4237)
+
+// +-------------------------------------------------------------------+
+
+class Token;
+class Scanner;
+
+// +-------------------------------------------------------------------+
+
+class Token
+{
+ friend class Scanner;
+
+public:
+ // keywords must be alphanumeric identifiers or symbolic identifiers
+ enum Types { Undefined, Keyword, AlphaIdent, SymbolicIdent, Comment,
+ IntLiteral, FloatLiteral, StringLiteral, CharLiteral,
+ Dot, Comma, Colon, Semicolon,
+ LParen, RParen, LBracket, RBracket, LBrace, RBrace,
+ EOT, LastTokenType };
+
+ enum Alias { CompoundSeparator = Dot,
+ ItemSeparator = Comma,
+ StatementTerminator = Semicolon,
+ TypeIndicator = Colon,
+ Lambda = LastTokenType + 1 };
+
+ Token();
+ Token(const Token& rhs);
+ Token(int t);
+ Token(const char* s, int t, int k=0, int l=0, int c=0);
+ Token(const Text& s, int t, int k=0, int l=0, int c=0);
+ ~Token();
+
+ Token& operator = (const Token& rhs);
+
+ bool match(const Token& ref) const;
+
+ Text symbol() const;
+ int type() const { return mType; }
+ int key() const { return mKey; }
+ int line() const { return mLine; }
+ int column() const { return mColumn; }
+
+ Text typestr() const;
+
+ static Text describe(const Text& tok);
+ static void addKey(const Text& k, int v);
+ static void addKeys(Dictionary<int>& keys);
+ static bool findKey(const Text& k, int& v);
+ static void comments(const Text& begin, const Text& end);
+ static void altComments(const Text& begin, const Text& end);
+ static void hideComments(bool hide = true) { hidecom = hide; }
+
+ static char comBeg(unsigned int i) { return combeg[i]; }
+ static char comEnd(unsigned int i) { return comend[i]; }
+ static char altBeg(unsigned int i) { return altbeg[i]; }
+ static char altEnd(unsigned int i) { return altend[i]; }
+
+ static void close();
+
+protected:
+ int mLength;
+ union {
+ char mSymbol[8];
+ char* mFullSymbol;
+ };
+ int mType;
+ int mKey;
+ int mLine;
+ int mColumn;
+
+ static bool hidecom;
+ static char combeg[3];
+ static char comend[3];
+ static char altbeg[3];
+ static char altend[3];
+
+ static Dictionary<int> keymap;
+};
+
+// +-------------------------------------------------------------------+
+
+class Scanner
+{
+public:
+ Scanner();
+ Scanner(starshatter::foundation::Reader r);
+ Scanner(const Text& s);
+ Scanner(const Scanner& rhs);
+ virtual ~Scanner();
+
+ Scanner& operator = (const Scanner& rhs);
+
+ void Load(const Text& s);
+
+ enum Need { Demand, Request };
+ virtual Token Get(Need n = Demand);
+
+ void PutBack() { index = old_index; line = old_line; }
+ int GetCursor() { return index; }
+ int GetLine() { return line; }
+ void Reset(int c, int l) { index = old_index = c; line = old_line = l; }
+ Token Best() const { return best; }
+
+protected:
+ virtual int GetNumeric();
+ virtual bool IsSymbolic(char c);
+ virtual bool IsAlpha(char c);
+
+ starshatter::foundation::Reader reader;
+ char* str;
+
+ const char* p;
+ const char* eos;
+
+ size_t index;
+ size_t old_index;
+ Token best;
+ size_t length;
+ size_t line;
+ size_t old_line;
+ size_t lineStart;
+};