summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-07-18 19:44:32 +0200
committerNathanael Sensfelder <SpamShield0@MultiAgentSystems.org>2020-07-18 19:44:32 +0200
commit9d1bf30db04f9e96940eaccc5721a9a8ed5c10c3 (patch)
tree4face623acc8f763bf5d7b01ebb4134765bed85e
parentbb22ba649662258368b86cbe7a0ef2b830f75c84 (diff)
Adds Text Nodes.
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/meta/TextNode.java25
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/valued_node/Cast.java13
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/valued_node/Newline.java24
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/valued_node/Paragraph.java59
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/valued_node/Sentence.java54
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/valued_node/Space.java24
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/valued_node/TextWithEffect.java110
-rw-r--r--src/core/src/tonkadur/fate/v1/lang/valued_node/ValueToText.java79
-rw-r--r--src/core/src/tonkadur/fate/v1/parser/FateLexer.g42
-rw-r--r--src/core/src/tonkadur/fate/v1/parser/FateParser.g4198
10 files changed, 559 insertions, 29 deletions
diff --git a/src/core/src/tonkadur/fate/v1/lang/meta/TextNode.java b/src/core/src/tonkadur/fate/v1/lang/meta/TextNode.java
new file mode 100644
index 0000000..fe4a829
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/meta/TextNode.java
@@ -0,0 +1,25 @@
+package tonkadur.fate.v1.lang.meta;
+
+import tonkadur.parser.Origin;
+
+import tonkadur.fate.v1.lang.type.Type;
+
+
+public abstract class TextNode extends ValueNode
+{
+ /***************************************************************************/
+ /**** PROTECTED ************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ protected TextNode (final Origin origin)
+ {
+ super(origin, Type.STRING);
+ }
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ /**** Accessors ************************************************************/
+ /**** Misc. ****************************************************************/
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/valued_node/Cast.java b/src/core/src/tonkadur/fate/v1/lang/valued_node/Cast.java
index afc2c0e..62a258f 100644
--- a/src/core/src/tonkadur/fate/v1/lang/valued_node/Cast.java
+++ b/src/core/src/tonkadur/fate/v1/lang/valued_node/Cast.java
@@ -1,9 +1,10 @@
package tonkadur.fate.v1.lang.valued_node;
-import java.util.Set;
-import java.util.Map;
-import java.util.HashMap;
+import java.util.Collection;
import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
import tonkadur.error.ErrorManager;
@@ -72,6 +73,12 @@ public class Cast extends ValueNode
Type.SIMPLE_BASE_TYPES
);
}
+
+ public static Collection<Type> get_allowed_casts_to (final Type t)
+ {
+ return allowed_type_changes.get(t);
+ }
+
/***************************************************************************/
/**** MEMBERS **************************************************************/
/***************************************************************************/
diff --git a/src/core/src/tonkadur/fate/v1/lang/valued_node/Newline.java b/src/core/src/tonkadur/fate/v1/lang/valued_node/Newline.java
new file mode 100644
index 0000000..3ffb797
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/valued_node/Newline.java
@@ -0,0 +1,24 @@
+package tonkadur.fate.v1.lang.valued_node;
+
+import tonkadur.parser.Origin;
+
+import tonkadur.fate.v1.lang.meta.TextNode;
+
+public class Newline extends TextNode
+{
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public Newline (final Origin origin)
+ {
+ super(origin);
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ return "(Newline)";
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/valued_node/Paragraph.java b/src/core/src/tonkadur/fate/v1/lang/valued_node/Paragraph.java
new file mode 100644
index 0000000..d695fe0
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/valued_node/Paragraph.java
@@ -0,0 +1,59 @@
+package tonkadur.fate.v1.lang.valued_node;
+
+import java.util.List;
+
+import tonkadur.parser.Origin;
+
+import tonkadur.fate.v1.lang.meta.TextNode;
+
+public class Paragraph extends TextNode
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final List<TextNode> content;
+
+ /***************************************************************************/
+ /**** PROTECTED ************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public Paragraph
+ (
+ final Origin origin,
+ final List<TextNode> content
+ )
+ {
+ super(origin);
+
+ this.content = content;
+ }
+
+ /**** Accessors ************************************************************/
+ public List<TextNode> get_content ()
+ {
+ return content;
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append("(Paragraph ");
+
+ for (final TextNode text: content)
+ {
+ sb.append(content.toString());
+ }
+
+ sb.append(")");
+
+ return sb.toString();
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/valued_node/Sentence.java b/src/core/src/tonkadur/fate/v1/lang/valued_node/Sentence.java
new file mode 100644
index 0000000..c79d422
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/valued_node/Sentence.java
@@ -0,0 +1,54 @@
+package tonkadur.fate.v1.lang.valued_node;
+
+import tonkadur.parser.Origin;
+
+import tonkadur.fate.v1.lang.type.Type;
+
+import tonkadur.fate.v1.lang.meta.TextNode;
+
+public class Sentence extends TextNode
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final String text;
+
+ /***************************************************************************/
+ /**** PROTECTED ************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public Sentence
+ (
+ final Origin origin,
+ final String text
+ )
+ {
+ super(origin);
+
+ this.text = text;
+ }
+
+ /**** Accessors ************************************************************/
+ public String get_text ()
+ {
+ return text;
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append("(Sentence ");
+ sb.append(text);
+ sb.append(")");
+
+ return sb.toString();
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/valued_node/Space.java b/src/core/src/tonkadur/fate/v1/lang/valued_node/Space.java
new file mode 100644
index 0000000..e6e7581
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/valued_node/Space.java
@@ -0,0 +1,24 @@
+package tonkadur.fate.v1.lang.valued_node;
+
+import tonkadur.parser.Origin;
+
+import tonkadur.fate.v1.lang.meta.TextNode;
+
+public class Space extends TextNode
+{
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public Space (final Origin origin)
+ {
+ super(origin);
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ return "(Space)";
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/valued_node/TextWithEffect.java b/src/core/src/tonkadur/fate/v1/lang/valued_node/TextWithEffect.java
new file mode 100644
index 0000000..705ef64
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/valued_node/TextWithEffect.java
@@ -0,0 +1,110 @@
+package tonkadur.fate.v1.lang.valued_node;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import tonkadur.parser.Origin;
+
+import tonkadur.fate.v1.lang.TextEffect;
+
+import tonkadur.fate.v1.lang.type.Type;
+
+import tonkadur.fate.v1.lang.meta.TextNode;
+import tonkadur.fate.v1.lang.meta.ValueNode;
+
+public class TextWithEffect extends TextNode
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final TextEffect effect;
+ protected final List<ValueNode> parameters;
+ protected final TextNode text;
+
+ /***************************************************************************/
+ /**** PROTECTED ************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ protected TextWithEffect
+ (
+ final Origin origin,
+ final TextEffect effect,
+ final List<ValueNode> parameters,
+ final TextNode text
+ )
+ {
+ super(origin);
+
+ this.effect = effect;
+ this.parameters = parameters;
+ this.text = text;
+ }
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public TextWithEffect
+ (
+ final Origin origin,
+ final TextEffect effect,
+ final TextNode text
+ )
+ {
+ super(origin);
+
+ this.effect = effect;
+ this.parameters = new ArrayList<ValueNode>();
+ this.text = text;
+ }
+
+ public static TextWithEffect build
+ (
+ final Origin origin,
+ final TextEffect effect,
+ final List<ValueNode> parameters,
+ final TextNode text
+ )
+ {
+ /* TODO: Checks */
+ return new TextWithEffect(origin, effect, parameters, text);
+ }
+
+ /**** Accessors ************************************************************/
+ public TextEffect get_effect ()
+ {
+ return effect;
+ }
+
+ public List<ValueNode> get_parameters ()
+ {
+ return parameters;
+ }
+
+ public TextNode get_text ()
+ {
+ return text;
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append("(TextWithEffect (");
+ sb.append(effect.get_name());
+
+ for (final ValueNode param: parameters)
+ {
+ sb.append(" ");
+ sb.append(param.toString());
+ }
+
+ sb.append(") ");
+ sb.append(text.toString());
+ sb.append(")");
+
+ return sb.toString();
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/lang/valued_node/ValueToText.java b/src/core/src/tonkadur/fate/v1/lang/valued_node/ValueToText.java
new file mode 100644
index 0000000..fcfac27
--- /dev/null
+++ b/src/core/src/tonkadur/fate/v1/lang/valued_node/ValueToText.java
@@ -0,0 +1,79 @@
+package tonkadur.fate.v1.lang.valued_node;
+
+import tonkadur.parser.Origin;
+
+import tonkadur.fate.v1.error.IncompatibleTypeException;
+import tonkadur.fate.v1.error.IncomparableTypeException;
+
+import tonkadur.fate.v1.lang.type.Type;
+
+import tonkadur.fate.v1.lang.meta.TextNode;
+import tonkadur.fate.v1.lang.meta.ValueNode;
+
+public class ValueToText extends TextNode
+{
+ /***************************************************************************/
+ /**** MEMBERS **************************************************************/
+ /***************************************************************************/
+ protected final ValueNode value;
+
+ /***************************************************************************/
+ /**** PROTECTED ************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ protected ValueToText (final ValueNode value)
+ {
+ super(value.get_origin());
+
+ this.value = value;
+ }
+
+ /***************************************************************************/
+ /**** PUBLIC ***************************************************************/
+ /***************************************************************************/
+ /**** Constructors *********************************************************/
+ public static ValueToText build (final ValueNode value)
+ throws
+ IncompatibleTypeException,
+ IncomparableTypeException
+ {
+ final Type value_base_type;
+
+ value_base_type = value.get_type().get_base_type();
+
+ if (value_base_type.equals(Type.STRING))
+ {
+ return new ValueToText(value);
+ }
+
+ return
+ new ValueToText
+ (
+ new Cast
+ (
+ value.get_origin(),
+ Type.STRING,
+ value
+ )
+ );
+ }
+
+ /**** Accessors ************************************************************/
+ public ValueNode get_value ()
+ {
+ return value;
+ }
+
+ /**** Misc. ****************************************************************/
+ @Override
+ public String toString ()
+ {
+ final StringBuilder sb = new StringBuilder();
+
+ sb.append("(ValueToText ");
+ sb.append(value.toString());
+ sb.append(")");
+
+ return sb.toString();
+ }
+}
diff --git a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4 b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
index 3654a30..26350ec 100644
--- a/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
+++ b/src/core/src/tonkadur/fate/v1/parser/FateLexer.g4
@@ -31,7 +31,7 @@ DECLARE_VARIABLE_KW: L_PAREN 'declare_variable';
DEFINE_MACRO_KW: L_PAREN 'define_macro';
DEFINE_SEQUENCE_KW: L_PAREN 'define_sequence';
DIVIDE_KW: L_PAREN ('divide'|'/');
-ENABLE_TEXT_PARAMETER_KW: L_PAREN 'text_effect';
+ENABLE_TEXT_EFFECT_KW: L_PAREN 'text_effect';
EQUALS_KW: L_PAREN ('equals'|'='|'==');
EVENT_KW: L_PAREN 'event';
EXTENSION_FIRST_LEVEL_KW: L_PAREN '@';
diff --git a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4 b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
index 4d111f5..e5c2733 100644
--- a/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
+++ b/src/core/src/tonkadur/fate/v1/parser/FateParser.g4
@@ -431,7 +431,14 @@ first_level_fate_instr:
{
/* TODO */
- /* TODO: no param alternative. */
+ /* Extension stuff */
+ System.out.println("Using extension FLI " + ($WORD.text));
+ }
+
+ | EXTENSION_FIRST_LEVEL_KW WORD WS* R_PAREN
+ {
+ /* TODO */
+
/* Extension stuff */
System.out.println("Using extension FLI " + ($WORD.text));
}
@@ -531,14 +538,6 @@ returns [InstructionNode result]
$result = null;
}
- | SET_EXPRESSION_KW WS+ value WS+ value_reference WS* R_PAREN
- {
- /* TODO */
-
- /* that one isn't resolved until the value is referenced */
- $result = null;
- }
-
| EVENT_KW WS+ WORD WS+ value_list WS* R_PAREN
{
/* TODO */
@@ -620,7 +619,7 @@ returns [InstructionNode result]
$result = null;
}
- | text+
+ | paragraph
{
/* TODO */
@@ -682,36 +681,155 @@ player_choice_cond_list:
}
;
-text:
+paragraph
+returns [TextNode result]
+@init
+{
+ final List<TextNode> content = new ArrayList();
+}
+:
+ first=text
+ {
+ content.add(($first.result));
+ }
+ (
+ (WS+ next_a=text)
+ {
+ if (!(content.get(content.size() - 1) instanceof Newline))
+ {
+ content.add(new Space(($next_a.result.get_origin())));
+ }
+
+ content.add(($next_a.result));
+ }
+ |
+ (next_b=text)
+ {
+ content.add(($next_b.result));
+ }
+ )*
+ {
+ if (content.size() == 1)
+ {
+ $result = content.get(0);
+ }
+ else
+ {
+ $result =
+ new Paragraph
+ (
+ ($first.result.get_origin()),
+ content
+ );
+ }
+ }
+;
+
+text
+returns [TextNode result]:
sentence
{
- /* TODO */
+ $result = ($sentence.result);
}
- | WS* ENABLE_TEXT_PARAMETER_KW WS+ WORD WS+ text+ WS* R_PAREN WS*
+ | ENABLE_TEXT_EFFECT_KW WS+ WORD WS+ paragraph WS* R_PAREN
{
- /* TODO */
+ final TextEffect effect;
+
+ effect =
+ WORLD.text_effects().get
+ (
+ CONTEXT.get_origin_at
+ (
+ ($WORD.getLine()),
+ ($WORD.getCharPositionInLine())
+ ),
+ ($WORD.text)
+ );
+
+ $result =
+ new TextWithEffect
+ (
+ CONTEXT.get_origin_at
+ (
+ ($WORD.getLine()),
+ ($WORD.getCharPositionInLine())
+ ),
+ effect,
+ ($paragraph.result)
+ );
}
- | WS* NEWLINE_KW WS*
+ | ENABLE_TEXT_EFFECT_KW WS+
+ L_PAREN
+ WORD WS+
+ value_list
+ R_PAREN WS+
+ paragraph WS*
+ R_PAREN
{
- /* TODO */
+ final TextEffect effect;
+
+ effect =
+ WORLD.text_effects().get
+ (
+ CONTEXT.get_origin_at
+ (
+ ($WORD.getLine()),
+ ($WORD.getCharPositionInLine())
+ ),
+ ($WORD.text)
+ );
+
+ $result =
+ TextWithEffect.build
+ (
+ CONTEXT.get_origin_at
+ (
+ ($ENABLE_TEXT_EFFECT_KW.getLine()),
+ ($ENABLE_TEXT_EFFECT_KW.getCharPositionInLine())
+ ),
+ effect,
+ ($value_list.result),
+ ($paragraph.result)
+ );
}
- | WS* non_text_value WS*
+ | NEWLINE_KW
{
- /* TODO */
+ $result =
+ new Newline
+ (
+ CONTEXT.get_origin_at
+ (
+ ($NEWLINE_KW.getLine()),
+ ($NEWLINE_KW.getCharPositionInLine())
+ )
+ );
}
-;
-sentence
- @init
+ | non_text_value
{
- final StringBuilder string_builder = new StringBuilder();
+ $result = ValueToText.build(($non_text_value.result));
}
- :
+;
+catch [final Throwable e]
+{
+ throw new ParseCancellationException(e);
+}
+
+sentence
+returns [TextNode result]
+@init
+{
+ final StringBuilder string_builder = new StringBuilder();
+}
+:
first_word=WORD
+ {
+ string_builder.append(($first_word.text));
+ }
(
WS+ next_word=WORD
{
@@ -720,7 +838,16 @@ sentence
}
)*
{
- string_builder.insert(0, ($first_word.text));
+ $result =
+ new Sentence
+ (
+ CONTEXT.get_origin_at
+ (
+ ($first_word.getLine()),
+ ($first_word.getCharPositionInLine())
+ ),
+ string_builder.toString()
+ );
}
;
@@ -1194,12 +1321,33 @@ returns [ValueNode result]
);
}
- | L_PAREN WS+ sentence WS* R_PAREN
+ | L_PAREN WS+ paragraph WS* R_PAREN
{
/* TODO */
$result = null;
}
+ | ENABLE_TEXT_EFFECT_KW WS+ WORD WS+ paragraph WS* R_PAREN
+ {
+ /* TODO */
+ }
+
+ | ENABLE_TEXT_EFFECT_KW WS+
+ L_PAREN
+ WORD WS+
+ value_list
+ R_PAREN WS+
+ paragraph WS*
+ R_PAREN
+ {
+ /* TODO */
+ }
+
+ | NEWLINE_KW
+ {
+ /* TODO */
+ }
+
| non_text_value
{
$result = ($non_text_value.result);