summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CMakeLists.txt1
-rw-r--r--src/core/common/Token.cpp14
-rw-r--r--src/core/common/Token.hpp67
-rw-r--r--src/core/model/Syntax.cpp58
-rw-r--r--src/core/model/Syntax.hpp196
-rw-r--r--src/core/parser/stack/Callbacks.hpp3
-rw-r--r--src/core/parser/stack/Handler.cpp2
-rw-r--r--src/core/parser/stack/Handler.hpp3
-rw-r--r--src/core/parser/stack/TokenStack.cpp4
-rw-r--r--src/core/parser/stack/TokenStack.hpp5
10 files changed, 266 insertions, 87 deletions
diff --git a/CMakeLists.txt b/CMakeLists.txt
index b206458..13de9ac 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -176,6 +176,7 @@ ADD_LIBRARY(ousia_core
src/core/model/Project
src/core/model/RootNode
src/core/model/Style
+ src/core/model/Syntax
src/core/model/Typesystem
src/core/parser/Parser
src/core/parser/ParserContext
diff --git a/src/core/common/Token.cpp b/src/core/common/Token.cpp
index e454ae4..17ce03e 100644
--- a/src/core/common/Token.cpp
+++ b/src/core/common/Token.cpp
@@ -20,19 +20,5 @@
namespace ousia {
-/* Class TokenSyntaxDescriptor */
-
-void TokenSyntaxDescriptor::insertIntoTokenSet(TokenSet &set) const
-{
- if (start != Tokens::Empty) {
- set.insert(start);
- }
- if (end != Tokens::Empty) {
- set.insert(end);
- }
- if (shortForm != Tokens::Empty) {
- set.insert(shortForm);
- }
-}
}
diff --git a/src/core/common/Token.hpp b/src/core/common/Token.hpp
index f89a0ce..f37151f 100644
--- a/src/core/common/Token.hpp
+++ b/src/core/common/Token.hpp
@@ -173,71 +173,6 @@ struct Token {
const SourceLocation &getLocation() const { return location; }
};
-/**
- * Class describing the user defined syntax for a single field or annotation.
- */
-struct TokenSyntaxDescriptor {
- /**
- * Possible start token or Tokens::Empty if no token is set.
- */
- TokenId start;
-
- /**
- * Possible end token or Tokens::Empty if no token is set.
- */
- TokenId end;
-
- /**
- * Possible representation token or Tokens::Empty if no token is set.
- */
- TokenId shortForm;
-
- /**
- * Flag specifying whether this TokenSyntaxDescriptor describes an
- * annotation.
- */
- bool isAnnotation;
-
- /**
- * Default constructor, sets all token ids to Tokens::Empty and isAnnotation
- * to false.
- */
- TokenSyntaxDescriptor()
- : start(Tokens::Empty),
- end(Tokens::Empty),
- shortForm(Tokens::Empty),
- isAnnotation(false)
- {
- }
-
- /**
- * Member initializer constructor.
- *
- * @param start is a possible start token.
- * @param end is a possible end token.
- * @param shortForm is a possible short form token.
- * @param isAnnotation is set to true if this syntax descriptor describes an
- * annotation.
- */
- TokenSyntaxDescriptor(TokenId start, TokenId end, TokenId shortForm,
- bool isAnnotation)
- : start(start),
- end(end),
- shortForm(shortForm),
- isAnnotation(isAnnotation)
- {
- }
-
- /**
- * Inserts all tokens referenced in this TokenSyntaxDescriptor into the
- * given TokenSet. Skips token ids set to Tokens::Empty.
- *
- * @param set is the TokenSet instance into which the Tokens should be
- * inserted.
- */
- void insertIntoTokenSet(TokenSet &set) const;
-};
}
-#endif /* _OUSIA_TOKENS_HPP_ */
-
+#endif /* _OUSIA_TOKENS_HPP_ */ \ No newline at end of file
diff --git a/src/core/model/Syntax.cpp b/src/core/model/Syntax.cpp
new file mode 100644
index 0000000..9dbaccc
--- /dev/null
+++ b/src/core/model/Syntax.cpp
@@ -0,0 +1,58 @@
+/*
+ Ousía
+ Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "Syntax.hpp"
+
+#include "Domain.hpp"
+
+namespace ousia {
+
+/* Class TokenSyntaxDescriptor */
+
+bool SyntaxDescriptor::isAnnotation() const
+{
+ return descriptor->isa(&RttiTypes::AnnotationClass);
+}
+bool SyntaxDescriptor::isFieldDescriptor() const
+{
+ return descriptor->isa(&RttiTypes::FieldDescriptor);
+}
+bool SyntaxDescriptor::isStruct() const
+{
+ return descriptor->isa(&RttiTypes::StructuredClass);
+}
+
+void SyntaxDescriptor::insertIntoTokenSet(TokenSet &set) const
+{
+ if (start != Tokens::Empty) {
+ set.insert(start);
+ }
+ if (end != Tokens::Empty) {
+ set.insert(end);
+ }
+ if (shortForm != Tokens::Empty) {
+ set.insert(shortForm);
+ }
+}
+
+bool SyntaxDescriptor::isEmpty() const
+{
+ return start == Tokens::Empty && end == Tokens::Empty &&
+ shortForm == Tokens::Empty;
+}
+} \ No newline at end of file
diff --git a/src/core/model/Syntax.hpp b/src/core/model/Syntax.hpp
new file mode 100644
index 0000000..4da3408
--- /dev/null
+++ b/src/core/model/Syntax.hpp
@@ -0,0 +1,196 @@
+/*
+ Ousía
+ Copyright (C) 2014, 2015 Benjamin Paaßen, Andreas Stöckel
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+/**
+ * @file Syntax.hpp
+ *
+ * This header contains the Descriptor classes for user definable syntax for
+ * Document entities or fields. These classes are referenced in Ontology.hpp.
+ */
+
+#ifndef _OUSIA_MODEL_SYNTAX_HPP_
+#define _OUSIA_MODEL_SYNTAX_HPP_
+
+#include <core/common/Token.hpp>
+#include "Node.hpp"
+
+namespace ousia {
+
+/**
+ * Class to describe a single token that shall be used as user-defined syntax.
+ */
+struct TokenDescriptor {
+ /**
+ * The string content of this token, if it is not a special one.
+ */
+ std::string token;
+ /**
+ * A flag to be set true if this TokenDescriptor uses a special token.
+ */
+ bool special;
+ /**
+ * An id to uniquely identify this token.
+ */
+ TokenId id;
+
+ /**
+ * Constructor for non-special tokens. The special flag is set to false and
+ * the id to Tokens::Empty.
+ *
+ * @param token The string content of this token, if it is not a special
+ * one.
+ */
+ TokenDescriptor(std::string token = std::string())
+ : token(std::move(token)), special(false), id(Tokens::Empty)
+ {
+ }
+
+ /**
+ * Constructor for special tokens. The token is set to an empty string and
+ * the special flag to true.
+ *
+ * @param id the id of the special token.
+ */
+ TokenDescriptor(TokenId id) : special(true), id(id) {}
+
+ /**
+ * Returns true if and only if neither a string nor an ID is given.
+ *
+ * @return true if and only if neither a string nor an ID is given.
+ */
+ bool isEmpty() const { return token.empty() && id == Tokens::Empty; }
+};
+
+/**
+ * Class describing the user defined syntax for a StructuredClass,
+ * AnnotationClass or FieldDescriptor.
+ *
+ * This class is used during parsing of a Document. It is used to describe
+ * the tokens relevant for one Descriptor that could be created at this point
+ * during parsing.
+ */
+struct SyntaxDescriptor {
+ /**
+ * Possible start token or Tokens::Empty if no token is set.
+ */
+ TokenId start;
+
+ /**
+ * Possible end token or Tokens::Empty if no token is set.
+ */
+ TokenId end;
+
+ /**
+ * Possible representation token or Tokens::Empty if no token is set.
+ */
+ TokenId shortForm;
+
+ /*
+ * The Descriptor this SyntaxDescriptor belongs to. As this may be
+ * a FieldDescriptor as well as a class Descriptor (StructuredClass or
+ * AnnotationClass) we can only use the class Node as inner argument here.
+ */
+ Rooted<Node> descriptor;
+ /*
+ * Given the current leaf in the parsed document the depth of a
+ * SyntaxDescriptor is defined as the number of transparent elements that
+ * would be needed to construct an instance of the referenced descriptor.
+ */
+ ssize_t depth;
+
+ /**
+ * Default constructor, sets all token ids to Tokens::Empty and the
+ * descriptor handle to nullptr.
+ */
+ SyntaxDescriptor()
+ : start(Tokens::Empty),
+ end(Tokens::Empty),
+ shortForm(Tokens::Empty),
+ descriptor(nullptr),
+ depth(-1)
+ {
+ }
+
+ /**
+ * Member initializer constructor.
+ *
+ * @param start is a possible start token.
+ * @param end is a possible end token.
+ * @param shortForm is a possible short form token.
+ * @param descriptor The Descriptor this SyntaxDescriptor belongs to.
+ * @param depth Given the current leaf in the parsed document the depth of a
+ * SyntaxDescriptor is defined as the number of transparent elements that
+ * would be needed to construct an instance of the referenced descriptor.
+ */
+ SyntaxDescriptor(TokenId start, TokenId end, TokenId shortForm,
+ Handle<Node> descriptor, ssize_t depth)
+ : start(start),
+ end(end),
+ shortForm(shortForm),
+ descriptor(descriptor),
+ depth(depth)
+ {
+ }
+
+ /**
+ * Inserts all tokens referenced in this SyntaxDescriptor into the
+ * given TokenSet. Skips token ids set to Tokens::Empty.
+ *
+ * @param set is the TokenSet instance into which the Tokens should be
+ * inserted.
+ */
+ void insertIntoTokenSet(TokenSet &set) const;
+
+ /**
+ * Returns true if and only if this SyntaxDescriptor belongs to an
+ * AnnotationClass.
+ *
+ * @return true if and only if this SyntaxDescriptor belongs to an
+ * AnnotationClass.
+ */
+ bool isAnnotation() const;
+
+ /**
+ * Returns true if and only if this SyntaxDescriptor belongs to a
+ * StrcturedClass.
+ *
+ * @return true if and only if this SyntaxDescriptor belongs to a
+ * StrcturedClass.
+ */
+ bool isStruct() const;
+
+ /**
+ * Returns true if and only if this SyntaxDescriptor belongs to a
+ * FieldDescriptor.
+ *
+ * @return true if and only if this SyntaxDescriptor belongs to a
+ * FieldDescriptor.
+ */
+ bool isFieldDescriptor() const;
+
+ /**
+ * Returns true if and only if this SyntaxDescriptor has only empty
+ * entries in start, end and short.
+ *
+ * @return true if and only if this SyntaxDescriptor has only empty
+ * entries in start, end and short.
+ */
+ bool isEmpty() const;
+};
+}
+#endif \ No newline at end of file
diff --git a/src/core/parser/stack/Callbacks.hpp b/src/core/parser/stack/Callbacks.hpp
index d7b2547..e471881 100644
--- a/src/core/parser/stack/Callbacks.hpp
+++ b/src/core/parser/stack/Callbacks.hpp
@@ -34,6 +34,7 @@
#include <core/common/Whitespace.hpp>
#include <core/common/Token.hpp>
+#include <core/model/Syntax.hpp>
namespace ousia {
@@ -96,7 +97,7 @@ public:
* @param tokens is a list of TokenSyntaxDescriptor instances that should be
* stored on the stack.
*/
- void pushTokens(const std::vector<TokenSyntaxDescriptor> &tokens);
+ void pushTokens(const std::vector<SyntaxDescriptor> &tokens);
/**
* Removes the previously pushed list of tokens from the stack.
diff --git a/src/core/parser/stack/Handler.cpp b/src/core/parser/stack/Handler.cpp
index 734976a..12df0fd 100644
--- a/src/core/parser/stack/Handler.cpp
+++ b/src/core/parser/stack/Handler.cpp
@@ -74,7 +74,7 @@ Variant Handler::readData()
return handlerData.callbacks.readData();
}
-void Handler::pushTokens(const std::vector<TokenSyntaxDescriptor> &tokens)
+void Handler::pushTokens(const std::vector<SyntaxDescriptor> &tokens)
{
handlerData.callbacks.pushTokens(tokens);
}
diff --git a/src/core/parser/stack/Handler.hpp b/src/core/parser/stack/Handler.hpp
index 848d395..19660d0 100644
--- a/src/core/parser/stack/Handler.hpp
+++ b/src/core/parser/stack/Handler.hpp
@@ -24,6 +24,7 @@
#include <core/common/Location.hpp>
#include <core/common/Variant.hpp>
#include <core/common/Whitespace.hpp>
+#include <core/model/Syntax.hpp>
namespace ousia {
@@ -200,7 +201,7 @@ protected:
* @param tokens is a list of TokenSyntaxDescriptor instances that should be
* stored on the stack.
*/
- void pushTokens(const std::vector<TokenSyntaxDescriptor> &tokens);
+ void pushTokens(const std::vector<SyntaxDescriptor> &tokens);
/**
* Calls the corresponding function in the HandlerCallbacks instance.
diff --git a/src/core/parser/stack/TokenStack.cpp b/src/core/parser/stack/TokenStack.cpp
index 6afeaed..ac1d94e 100644
--- a/src/core/parser/stack/TokenStack.cpp
+++ b/src/core/parser/stack/TokenStack.cpp
@@ -21,7 +21,7 @@
namespace ousia {
namespace parser_stack {
-void TokenStack::pushTokens(const std::vector<TokenSyntaxDescriptor> &tokens)
+void TokenStack::pushTokens(const std::vector<SyntaxDescriptor> &tokens)
{
stack.push_back(tokens);
}
@@ -35,7 +35,7 @@ TokenSet TokenStack::tokens() const
}
TokenSet res;
- for (const TokenSyntaxDescriptor &descr : stack.back()) {
+ for (const SyntaxDescriptor &descr : stack.back()) {
descr.insertIntoTokenSet(res);
}
return res;
diff --git a/src/core/parser/stack/TokenStack.hpp b/src/core/parser/stack/TokenStack.hpp
index 9669f50..af734bb 100644
--- a/src/core/parser/stack/TokenStack.hpp
+++ b/src/core/parser/stack/TokenStack.hpp
@@ -32,6 +32,7 @@
#include <vector>
#include <core/common/Token.hpp>
+#include <core/model/Syntax.hpp>
namespace ousia {
namespace parser_stack {
@@ -52,7 +53,7 @@ private:
* Stack containing vectors of TokenSyntaxDescriptor instances as given by
* the user.
*/
- std::vector<std::vector<TokenSyntaxDescriptor>> stack;
+ std::vector<std::vector<SyntaxDescriptor>> stack;
/**
* Constructor of the TokenStack class.
@@ -86,7 +87,7 @@ public:
* @param tokens is a list of TokenSyntaxDescriptor instances that should be
* stored on the stack.
*/
- void pushTokens(const std::vector<TokenSyntaxDescriptor> &tokens);
+ void pushTokens(const std::vector<SyntaxDescriptor> &tokens);
/**
* Removes the previously pushed list of tokens from the stack.