summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-07-03 00:11:22 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-07-03 00:11:22 +0200
commit4570d75ffcfc091b1a6b68d42a475d49d0047fa9 (patch)
treeebc23b8b1c197600e0ebed0151592cd4fe306081
parentd2d3c1cf67ed72879b3146732f7573db2b57bfcb (diff)
Reorganizing a bit...
-rw-r--r--src/core/src/tonkadur/Main.java4
-rw-r--r--src/core/src/tonkadur/fate/v1/Utils.java34
-rw-r--r--src/core/src/tonkadur/fate/v1/error/InputException.java52
-rw-r--r--src/core/src/tonkadur/fate/v1/error/TypeAlreadyDeclaredException.java34
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/World.java99
-rw-r--r--src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 (renamed from src/core/src/tonkadur/parser/LangLexer.g4)6
-rw-r--r--src/core/src/tonkadur/fate/v1/parser/FateParser.g4 (renamed from src/core/src/tonkadur/parser/LangParser.g4)44
-rw-r--r--src/core/src/tonkadur/parser/Fate.java25
8 files changed, 258 insertions, 40 deletions
diff --git a/src/core/src/tonkadur/Main.java b/src/core/src/tonkadur/Main.java
index 3d90395..76790e9 100644
--- a/src/core/src/tonkadur/Main.java
+++ b/src/core/src/tonkadur/Main.java
@@ -2,7 +2,7 @@ package tonkadur;
import java.io.IOException;
-import tonkadur.parser.Fate;
+import tonkadur.fate.v1.Utils;
public class Main
{
@@ -12,6 +12,6 @@ public class Main
public static void main (final String[] args)
throws IOException
{
- Fate.parse_file(args[0]);
+ Utils.parse_file(args[0]);
}
}
diff --git a/src/core/src/tonkadur/fate/v1/Utils.java b/src/core/src/tonkadur/fate/v1/Utils.java
new file mode 100644
index 0000000..048b3f2
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/Utils.java
@@ -0,0 +1,34 @@
+package tonkadur.fate.v1;
+
+import java.io.IOException;
+
+import org.antlr.v4.runtime.CharStreams;
+import org.antlr.v4.runtime.CommonTokenStream;
+
+import tonkadur.fate.v1.parser.FateLexer;
+import tonkadur.fate.v1.parser.FateParser;
+
+import tonkadur.fate.v1.lang.World;
+
+public class Utils
+{
+ /* Utility class. */
+ private Utils () {}
+
+ public static void add_file_content
+ (
+ final String filename,
+ final World world
+ )
+ throws IOException
+ {
+ final CommonTokenStream tokens;
+ final FateLexer lexer;
+ final FateParser parser;
+
+ lexer = new FateLexer(CharStreams.fromFileName(filename));
+ tokens = new CommonTokenStream(lexer);
+ parser = new FateParser(tokens);
+ parser.fate_file(world);
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/error/InputException.java b/src/core/src/tonkadur/fate/v1/error/InputException.java
new file mode 100644
index 0000000..d2921cb
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/error/InputException.java
@@ -0,0 +1,52 @@
+package tonkadur.fate.v1.error;
+
+abstract class InputException extends Throwable
+{
+ /***************************************************************************/
+ /**** STATIC MEMBERS *******************************************************/
+ /***************************************************************************/
+ private static final long serialVersionUID = 42L;
+
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected String filename = "";
+ protected int line = -1;
+ protected int column = -1;
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ public InputException set_location
+ (
+ final String filename,
+ final int line,
+ final int column
+ )
+ {
+ this.filename = filename;
+ this.line = line;
+ this.column = column;
+
+ return this;
+ }
+
+ public String get_location ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append(filename);
+ sb.append(":");
+ sb.append(line);
+ sb.append(",");
+ sb.append(column);
+
+ return sb.toString();
+ }
+
+ @Override
+ public String toString ()
+ {
+ return get_location();
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/error/TypeAlreadyDeclaredException.java b/src/core/src/tonkadur/fate/v1/error/TypeAlreadyDeclaredException.java
new file mode 100644
index 0000000..673a3bd
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/error/TypeAlreadyDeclaredException.java
@@ -0,0 +1,34 @@
+package tonkadur.fate.v1.error;
+
+import tonkadur.fate.v1.lang.Type;
+
+public class TypeAlreadyDeclaredException extends InputException
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final Type original_type;
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ public TypeAlreadyDeclaredException (final Type original_type)
+ {
+ this.original_type = original_type;
+ }
+
+ @Override
+ public String toString ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append(super.toString());
+ sb.append(" Type '");
+ sb.append(original_type.get_name());
+ sb.append("' already declared in ");
+ sb.append(original_type.get_source().toString());
+ sb.append(".");
+
+ return sb.toString();
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/World.java b/src/core/src/tonkadur/fate/v1/lang/World.java
new file mode 100644
index 0000000..fb0f0fe
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/World.java
@@ -0,0 +1,99 @@
+package tonkadur.fate.v1.lang;
+
+import tonkadur.fate.v1.error.EventAlreadyDeclaredException;
+import tonkadur.fate.v1.error.TypeAlreadyDeclaredException;
+import tonkadur.fate.v1.error.TextEffectAlreadyDeclaredException;
+import tonkadur.fate.v1.error.UnknownTypeException;
+
+public class World
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final Collection<String> loaded_files;
+ protected final Collection<String> text_effects;
+ protected final Map<String, Event> events;
+ protected final Map<String, Macro> macros;
+ protected final Map<String, Sequence> sequences;
+ protected final Map<String, Type> types;
+ protected final Map<String, Variable> variables;
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+
+ /**** Constructors *********************************************************/
+ public World ()
+ {
+ loaded_files = new HashSet<String>();
+ text_effects = new HashSet<String>();
+
+ events = new HashMap<String, Event>();
+ macros = new HashMap<String, Macro>();
+ sequences = new HashMap<String, Sequence>();
+ types = new HashMap<String, Type>();
+ variables = new HashMap<String, Variable>();
+
+ for (final Type t: Type.BASE_TYPES)
+ {
+ types.add(t.get_name(), t);
+ }
+ }
+
+ /**** Accessors ************************************************************/
+ /**** Loaded Files ****/
+ public Collection<String> get_loaded_files ()
+ {
+ return loaded_files.clone();
+ }
+
+ public boolean has_loaded_file (final String name)
+ {
+ return loaded_files.contains(name);
+ }
+
+ public void add_loaded_file (final String name)
+ {
+ loaded_files.add(name);
+ }
+
+ /**** Text Effects ****/
+ public Collection<String> get_text_effects ()
+ {
+ return text_effects.clone();
+ }
+
+ public boolean has_text_effect (final String name)
+ {
+ return text_effects.contains(name);
+ }
+
+ public void add_text_effect (final String name)
+ throws TextEffectAlreadyDeclaredException
+ {
+ text_effects.add(name);
+ }
+
+ /**** Events ****/
+ public Collection<Event> get_events ()
+ {
+ return events.values();
+ }
+
+ public boolean has_event (final String name)
+ {
+ return events.containsKey(name);
+ }
+
+ public void add_event (final String name, final List<Type> parameter_types)
+ throws EventAlreadyDeclaredException, UnknownTypeException
+ {
+
+ }
+
+ /**** Misc. ****************************************************************/
+
+ /***************************************************************************/
+ /**** PRIVATE **************************************************************/
+ /***************************************************************************/
+}
diff --git a/src/core/src/tonkadur/parser/LangLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
index 9ef92b8..80fe1fd 100644
--- a/src/core/src/tonkadur/parser/LangLexer.g4
+++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
@@ -1,8 +1,8 @@
-lexer grammar LangLexer;
+lexer grammar FateLexer;
@header
{
- package tonkadur.parser;
+ package tonkadur.fate.v1.parser;
}
@@ -26,6 +26,7 @@ DECLARE_ALIAS_TYPE_KW: L_PAREN 'declare_alias_type' WS*;
DECLARE_DICT_TYPE_KW: L_PAREN 'declare_dict_type' WS*;
DECLARE_ENUM_TYPE_KW: L_PAREN 'declare_enum_type' WS*;
DECLARE_EVENT_TYPE_KW: L_PAREN 'declare_event_type' WS*;
+DECLARE_TEXT_EFFECT: L_PAREN 'declare_text_effect' WS*;
DECLARE_VARIABLE_KW: L_PAREN 'declare_variable' WS*;
DEFINE_MACRO_KW: L_PAREN 'define_macro' WS*;
DEFINE_SEQUENCE_KW: L_PAREN 'define_sequence' WS*;
@@ -33,6 +34,7 @@ DIVIDE_KW: L_PAREN 'divide' WS*;
ENABLE_TEXT_PARAMETER_KW: L_PAREN 'enable_text_parameter' WS*;
EQUALS_KW: L_PAREN 'equals' WS*;
EVENT_KW: L_PAREN 'event' WS*;
+FATE_VERSION_KW: L_PAREN 'fate_version' WS*;
GREATER_EQUAL_THAN_KW: L_PAREN 'greater_equal_than' WS*;
GREATER_THAN_KW: L_PAREN 'greater_than' WS*;
IF_ELSE_KW: L_PAREN 'if_else' WS*;
diff --git a/src/core/src/tonkadur/parser/LangParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
index c44acfe..c0be842 100644
--- a/src/core/src/tonkadur/parser/LangParser.g4
+++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
@@ -1,13 +1,13 @@
-parser grammar LangParser;
+parser grammar FateParser;
options
{
- tokenVocab = LangLexer;
+ tokenVocab = FateLexer;
}
@header
{
- package tonkadur.parser;
+ package tonkadur.fate.v1.parser;
}
@members
@@ -19,11 +19,13 @@ options
/******************************************************************************/
/******************************************************************************/
fate_file:
- (WS*
+ WS* FATE_VERSION_KW WORD L_PAREN WS*
+ (
(first_level_fate_instr|general_fate_instr)
{
}
- WS*)*
+ WS*
+ )*
EOF
{
}
@@ -44,6 +46,10 @@ first_level_fate_instr:
{
}
+ | DECLARE_TEXT_EFFECT_KW range=WORD R_PAREN
+ {
+ }
+
| ADD_VARIABLE_ATTRIBUTE_KW WORD WS+ WORD R_PAREN
{
}
@@ -109,10 +115,6 @@ general_fate_instr:
{
}
- | ENABLE_TEXT_PARAMETER_KW value WS+ sentence R_PAREN
- {
- }
-
| ASSERT_KW value R_PAREN
{
}
@@ -125,7 +127,21 @@ general_fate_instr:
{
}
- | sentence
+ | text
+ {
+ }
+;
+
+text:
+ sentence text*
+ {
+ }
+
+ | (ENABLE_TEXT_PARAMETER_KW WORD WS+ text R_PAREN) text*
+ {
+ }
+
+ | non_text_value text*
{
}
;
@@ -263,10 +279,16 @@ value:
{
}
- | L_PAREN WS+ sentence WS+ R_PAREN
+ | L_PAREN WS* sentence R_PAREN
{
}
+ | non_text_value
+ {
+ }
+;
+
+non_text_value:
| IF_ELSE_KW value WS+ value WS+ value R_PAREN
{
}
diff --git a/src/core/src/tonkadur/parser/Fate.java b/src/core/src/tonkadur/parser/Fate.java
deleted file mode 100644
index 9169975..0000000
--- a/src/core/src/tonkadur/parser/Fate.java
+++ /dev/null
@@ -1,25 +0,0 @@
-package tonkadur.parser;
-
-import java.io.IOException;
-
-import org.antlr.v4.runtime.CharStreams;
-import org.antlr.v4.runtime.CommonTokenStream;
-
-public class Fate
-{
- /* Utility class. */
- private Fate () {}
-
- public static void parse_file (final String filename)
- throws IOException
- {
- final CommonTokenStream tokens;
- final LangLexer lexer;
- final LangParser parser;
-
- lexer = new LangLexer(CharStreams.fromFileName(filename));
- tokens = new CommonTokenStream(lexer);
- parser = new LangParser(tokens);
- parser.fate_file();
- }
-}