[vala/wip/genie: 3/3] genie: Allow method definitions in enums



commit 2ffe17e77faa0abfb32830389f91e3ab97873176
Author: vlad1777d <naumovvladislav mail ru>
Date:   Sat May 6 21:38:58 2017 +0300

    genie: Allow method definitions in enums
    
    https://bugzilla.gnome.org/show_bug.cgi?id=776835

 tests/genie/enums/enum_methods.gs |   83 +++++++++++++++++++++++++++++++++++++
 vala/valagenieparser.vala         |   38 +++++++++++------
 2 files changed, 107 insertions(+), 14 deletions(-)
---
diff --git a/tests/genie/enums/enum_methods.gs b/tests/genie/enums/enum_methods.gs
new file mode 100644
index 0000000..94d4d4c
--- /dev/null
+++ b/tests/genie/enums/enum_methods.gs
@@ -0,0 +1,83 @@
+enum Aa1
+       AAE
+       AAF
+       def static met (): int
+               return 1
+       AAZ
+
+enum Aa2
+       AAE,
+       AAF
+       def static met (): int
+               return 2
+       AAZ,
+
+enum Aa3
+       AAE,
+       AAF
+       AAZ,
+       def static met (): int
+               return 3
+
+enum Aa4
+       AAE,
+       AAF
+       AAZ
+       def static met (): int
+               return 4
+
+enum Bb1
+       AAE,
+       AAF
+       
+       def static minimum (): int
+               enumc: EnumClass = (EnumClass) typeof (Bb1).class_ref ()
+               return enumc.minimum
+
+       def static maximum (): int
+               enumc: EnumClass = (EnumClass) typeof (Bb1).class_ref ()
+               return enumc.maximum
+       
+       AAZ = 101
+       
+       def static get_by_nick (nick: string, out result: Bb1): bool
+               enumc: EnumClass = (EnumClass) typeof (Bb1).class_ref ()
+               eval: unowned EnumValue? = enumc.get_value_by_nick (nick)
+               if eval == null
+                       print "was null"
+                       return false
+               else
+                       result = (Bb1) eval.value
+                       return true
+       
+       AMM = 30
+
+
+init
+       var a_1_1 = Aa1.AAE
+       var a_1_2 = Aa1.AAF
+       var a_1_3 = Aa1.AAZ
+       assert Aa1.met () == 1
+       
+       var a_2_1 = Aa2.AAE
+       var a_2_2 = Aa2.AAF
+       var a_2_3 = Aa2.AAZ
+       assert Aa2.met () == 2
+       
+       var a_3_1 = Aa3.AAE
+       var a_3_2 = Aa3.AAF
+       var a_3_3 = Aa3.AAZ
+       assert Aa3.met () == 3
+       
+       var a_4_1 = Aa4.AAE
+       var a_4_2 = Aa4.AAF
+       var a_4_3 = Aa4.AAZ
+       assert Aa4.met () == 4
+       
+       assert Bb1.minimum () == 0
+       assert Bb1.maximum () == 101
+       b_1_1: Bb1    // for out of method
+       Bb1.get_by_nick ("aaz", out b_1_1)
+       assert b_1_1 == 101
+       var b_1_2 = Bb1.AMM
+       assert b_1_2 == 30
diff --git a/vala/valagenieparser.vala b/vala/valagenieparser.vala
index ed08cd7..f7b140a 100644
--- a/vala/valagenieparser.vala
+++ b/vala/valagenieparser.vala
@@ -3629,23 +3629,33 @@ public class Vala.Genie.Parser : CodeVisitor {
                                // allow trailing comma
                                break;
                        }
-                       var value_attrs = parse_attributes (false);
-                       var value_begin = get_location (); 
-                       string id = parse_identifier ();
-                       comment = scanner.pop_comment ();
+                       if (current () == TokenType.DEF) {
+                               // enum methods
+                               if ((tokens[index - 1].type != TokenType.EOL) && (tokens[index - 1].type != 
TokenType.DEDENT)) {
+                                       Report.error (get_src (get_location ()), "method definition must 
start from new line");
+                               }
+                               var en_method = parse_declaration ();
+                               en.add_method (en_method as Method);
+                       } else {
+                               // values
+                               var value_attrs = parse_attributes (false);
+                               var value_begin = get_location ();
+                               string id = parse_identifier ();
+                               comment = scanner.pop_comment ();
 
-                       Expression value = null;
-                       if (accept (TokenType.ASSIGN)) {
-                               value = parse_expression ();
-                       }
+                               Expression value = null;
+                               if (accept (TokenType.ASSIGN)) {
+                                       value = parse_expression ();
+                               }
 
-                       var ev = new EnumValue (id, value, get_src (value_begin), comment);
-                       ev.access = SymbolAccessibility.PUBLIC;
-                       set_attributes (ev, value_attrs);
+                               var ev = new EnumValue (id, value, get_src (value_begin), comment);
+                               ev.access = SymbolAccessibility.PUBLIC;
+                               set_attributes (ev, value_attrs);
 
-                       en.add_value (ev);
-                       if (expect_separator ()) {
-                               accept (TokenType.EOL);
+                               en.add_value (ev);
+                               if (expect_separator ()) {
+                                       accept (TokenType.EOL);
+                               }
                        }
                } while (true);
                


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]