+2007-07-01 Jürg Billeter <j@bitron.ch>
+
+ * vala/parser.y, vala/valasymbolbuilder.vala,
+ vala/valasymbolresolver.vala, vala/valaenum.vala: add basic support
+ for enum methods
+
2007-06-30 Jürg Billeter <j@bitron.ch>
* vala/valasymbolbuilder.vala: fix current_symbol handling in
static ValaClass *current_class;
static ValaStruct *current_struct;
static ValaInterface *current_interface;
+static ValaEnum *current_enum;
typedef enum {
VALA_MODIFIER_NONE,
%type <struct_> struct_header
%type <interface> interface_declaration
%type <enum_> enum_declaration
-%type <list> enum_body
-%type <list> opt_enum_member_declarations
-%type <list> enum_member_declarations
-%type <enum_value> enum_member_declaration
%type <flags> flags_declaration
%type <list> flags_body
%type <list> opt_flags_member_declarations
;
enum_declaration
- : comment opt_attributes opt_access_modifier ENUM identifier opt_name_specifier enum_body
+ : comment opt_attributes opt_access_modifier ENUM identifier opt_name_specifier
{
GList *l;
ValaSourceReference *src;
}
src = src_com(@5, $1);
- $$ = vala_enum_new (name, src);
+ current_enum = vala_enum_new (name, src);
g_free (name);
g_object_unref (src);
- VALA_CODE_NODE($$)->attributes = $2;
+ VALA_CODE_NODE(current_enum)->attributes = $2;
if ($3 != 0) {
- VALA_DATA_TYPE($$)->access = $3;
- }
- for (l = $7; l != NULL; l = l->next) {
- vala_enum_add_value ($$, l->data);
- g_object_unref (l->data);
+ VALA_DATA_TYPE(current_enum)->access = $3;
}
}
+ enum_body
+ {
+ $$ = current_enum;
+ current_enum = NULL;
+ }
;
enum_body
- : OPEN_BRACE opt_enum_member_declarations CLOSE_BRACE
- {
- $$ = $2;
- }
+ : OPEN_BRACE opt_enum_member_declarations opt_enum_method_declarations CLOSE_BRACE
;
opt_enum_member_declarations
: /* empty */
- {
- $$ = NULL;
- }
| enum_member_declarations opt_comma
;
enum_member_declarations
: enum_member_declaration
- {
- $$ = g_list_append (NULL, $1);
- }
| enum_member_declarations COMMA enum_member_declaration
- {
- $$ = g_list_append ($1, $3);
- }
;
enum_member_declaration
: opt_attributes identifier
{
- $$ = vala_enum_value_new ($2);
+ ValaEnumValue *ev = vala_enum_value_new ($2);
g_free ($2);
+ vala_enum_add_value (current_enum, ev);
+ g_object_unref (ev);
}
| opt_attributes identifier ASSIGN expression
{
- $$ = vala_enum_value_new_with_value ($2, $4);
+ ValaEnumValue *ev = vala_enum_value_new_with_value ($2, $4);
g_free ($2);
g_object_unref ($4);
+ vala_enum_add_value (current_enum, ev);
+ g_object_unref (ev);
+ }
+ ;
+
+opt_enum_method_declarations
+ : /* empty */
+ | enum_method_declarations
+ ;
+
+enum_method_declarations
+ : SEMICOLON enum_method_declaration
+ | enum_method_declarations enum_method_declaration
+ ;
+
+enum_method_declaration
+ : method_declaration
+ {
+ /* skip declarations with errors */
+ if ($1 != NULL) {
+ vala_enum_add_method (current_enum, $1);
+ g_object_unref ($1);
+ }
}
;
*/
public class Vala.Enum : DataType {
private List<EnumValue> values;
+ private List<Method> methods;
private string cname;
private string cprefix;
+ private string lower_case_cprefix;
+ private string lower_case_csuffix;
/**
* Creates a new enum.
*
- * @param name type name
- * @param source reference to source code
- * @return newly created enum
+ * @param name type name
+ * @param source_reference reference to source code
+ * @return newly created enum
*/
- public Enum (string! _name, SourceReference source = null) {
- name = _name;
- source_reference = source;
+ public Enum (construct string! name, construct SourceReference source_reference = null) {
}
/**
values.append (value);
}
+ /**
+ * Adds the specified method as a member to this enum.
+ *
+ * @param m a method
+ */
+ public void add_method (Method! m) {
+ methods.append (m);
+ }
+
+ /**
+ * Returns a copy of the list of methods.
+ *
+ * @return list of methods
+ */
+ public List<weak Method> get_methods () {
+ return methods.copy ();
+ }
+
public override void accept (CodeVisitor! visitor) {
visitor.visit_enum (this);
}
foreach (EnumValue value in values) {
value.accept (visitor);
}
+
+ foreach (Method m in methods) {
+ m.accept (visitor);
+ }
}
public override string get_cname (bool const_type = false) {
}
return cname;
}
-
+
+ public override ref string get_lower_case_cprefix () {
+ if (lower_case_cprefix == null) {
+ lower_case_cprefix = "%s_".printf (get_lower_case_cname (null));
+ }
+ return lower_case_cprefix;
+ }
+
+ private string get_lower_case_csuffix () {
+ if (lower_case_csuffix == null) {
+ lower_case_csuffix = Namespace.camel_case_to_lower_case (name);
+ }
+ return lower_case_csuffix;
+ }
+
+ public override ref string get_lower_case_cname (string infix) {
+ if (infix == null) {
+ infix = "";
+ }
+ return "%s%s%s".printf (@namespace.get_lower_case_cprefix (), infix, get_lower_case_csuffix ());
+ }
+
public override ref string get_upper_case_cname (string infix) {
return "%s%s".printf (@namespace.get_lower_case_cprefix (), Namespace.camel_case_to_lower_case (name)).up ();
}
current_scope = current_scope.parent_symbol;
}
+ public override void visit_enum (Enum! en) {
+ current_scope = en.symbol;
+
+ en.accept_children (this);
+
+ current_scope = current_scope.parent_symbol;
+ }
+
public override void visit_callback (Callback! cb) {
current_scope = cb.symbol;