]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Comment handling improvements
authorFlorian Brosch <flo.brosch@gmail.com>
Tue, 1 Sep 2009 15:04:53 +0000 (17:04 +0200)
committerJürg Billeter <j@bitron.ch>
Fri, 4 Sep 2009 15:55:09 +0000 (17:55 +0200)
Fixes bug 529040, bug 540513, and bug 546096.

37 files changed:
ccode/valaccodewriter.vala
codegen/valaccodebasemodule.vala
codegen/valaccodedelegatemodule.vala
codegen/valaccodemethodmodule.vala
codegen/valaccodestructmodule.vala
codegen/valagerrormodule.vala
codegen/valagobjectmodule.vala
codegen/valagtypemodule.vala
vala/Makefile.am
vala/valaclass.vala
vala/valacomment.vala [new file with mode: 0644]
vala/valaconstant.vala
vala/valacreationmethod.vala
vala/valadelegate.vala
vala/valadynamicmethod.vala
vala/valadynamicproperty.vala
vala/valadynamicsignal.vala
vala/valaenum.vala
vala/valaenumvalue.vala
vala/valaerrorcode.vala
vala/valaerrordomain.vala
vala/valafield.vala
vala/valagenieparser.vala
vala/valageniescanner.vala
vala/valainterface.vala
vala/valamember.vala
vala/valamethod.vala
vala/valanamespace.vala
vala/valaobjecttypesymbol.vala
vala/valaparser.vala
vala/valaproperty.vala
vala/valascanner.vala
vala/valasignal.vala
vala/valasourcefile.vala
vala/valasourcereference.vala
vala/valastruct.vala
vala/valatypesymbol.vala

index 26326fbd9b8ea0995787447e8941d06a2e610dc5..11559cd54b8c32e44af1030b9e57f1ab23bee7d2 100644 (file)
@@ -142,7 +142,7 @@ public class Vala.CCodeWriter {
         * @param s a string
         */
        public void write_string (string s) {
-               stream.printf ("%s", s);
+               stream.puts (s);
                _bol = false;
        }
        
@@ -177,7 +177,7 @@ public class Vala.CCodeWriter {
                
                indent--;
                write_indent ();
-               stream.printf ("}");
+               stream.putc ('}');
        }
        
        /**
@@ -187,7 +187,7 @@ public class Vala.CCodeWriter {
         */
        public void write_comment (string text) {
                write_indent ();
-               stream.printf ("/*");
+               stream.puts ("/*");
                bool first = true;
                
                /* separate declaration due to missing memory management in foreach statements */
@@ -199,9 +199,17 @@ public class Vala.CCodeWriter {
                        } else {
                                first = false;
                        }
-                       stream.printf ("%s", line);
+
+                       var lineparts = line.split ("*/");
+
+                       for (int i = 0; lineparts[i] != null; i++) {
+                               stream.puts (lineparts[i]);
+                               if (lineparts[i+1] != null) {
+                                       stream.puts ("* /");
+                               }
+                       }
                }
-               stream.printf ("*/");
+               stream.puts ("*/");
                write_newline ();
        }
 }
index ed3ef180fd9d5676b18c41cc5dd05c429ac82f96..a5f628b99424ee7acdca17ae794100916099d811 100644 (file)
@@ -609,11 +609,6 @@ internal class Vala.CCodeBaseModule : CCodeModule {
 
                        source_type_member_definition.append (cfunc);
                }
-
-               CCodeComment comment = null;
-               if (source_file.comment != null) {
-                       comment = new CCodeComment (source_file.comment);
-               }
                
                var writer = new CCodeWriter (source_file.get_csource_filename ());
                if (!writer.open ()) {
@@ -621,9 +616,15 @@ internal class Vala.CCodeBaseModule : CCodeModule {
                        return;
                }
                writer.line_directives = context.debug;
-               if (comment != null) {
-                       comment.write (writer);
+
+               var comments = source_file.get_comments();
+               if (comments != null) {
+                       foreach (Comment comment in comments) {
+                               var ccomment = new CCodeComment (comment.content);
+                               ccomment.write (writer);
+                       }
                }
+
                writer.write_newline ();
                source_declarations.include_directives.write (writer);
                writer.write_newline ();
@@ -687,10 +688,6 @@ internal class Vala.CCodeBaseModule : CCodeModule {
                                cenum.add_value (new CCodeEnumValue (ev.get_cname (), (CCodeExpression) ev.value.ccodenode));
                        }
                }
-               
-               if (en.source_reference.comment != null) {
-                       decl_space.add_type_definition (new CCodeComment (en.source_reference.comment));
-               }
 
                decl_space.add_type_definition (cenum);
                decl_space.add_type_definition (new CCodeNewline ());
@@ -1568,11 +1565,6 @@ internal class Vala.CCodeBaseModule : CCodeModule {
                        if (stmt.error) {
                                continue;
                        }
-
-                       var src = stmt.source_reference;
-                       if (src != null && src.comment != null) {
-                               cblock.add_statement (new CCodeComment (src.comment));
-                       }
                        
                        if (stmt.ccodenode is CCodeFragment) {
                                foreach (CCodeNode cstmt in ((CCodeFragment) stmt.ccodenode).get_children ()) {
index 2293047905c7d5d658b468be1a3d88b2cb310e62..678ffe781332f2935b6940b01ad3319c3ac6a5a5 100644 (file)
@@ -103,10 +103,6 @@ internal class Vala.CCodeDelegateModule : CCodeArrayModule {
                }
 
                var ctypedef = new CCodeTypeDefinition (return_type_cname, cfundecl);
-
-               if (d.source_reference != null && d.source_reference.comment != null) {
-                       decl_space.add_type_definition (new CCodeComment (d.source_reference.comment));
-               }
                decl_space.add_type_definition (ctypedef);
        }
 
index ce19438d3837f8ef9e5ea9b5a8d5f0d0d3048350..72412c89a87e1a687b1b7c8369726babbe370776 100644 (file)
@@ -455,9 +455,6 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
                                }
 
                                if (!m.coroutine) {
-                                       if (m.source_reference != null && m.source_reference.comment != null) {
-                                               source_type_member_definition.append (new CCodeComment (m.source_reference.comment));
-                                       }
                                        source_type_member_definition.append (function);
                                }
                        
@@ -900,9 +897,6 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
 
                vfunc.block = vblock;
 
-               if (m.is_abstract && m.source_reference != null && m.source_reference.comment != null) {
-                       source_type_member_definition.append (new CCodeComment (m.source_reference.comment));
-               }
                source_type_member_definition.append (vfunc);
        }
 
index f6175984216595a8beab81cd05c8fff20eb6f681..d8f5d4db7ac25bbeecdddcbffe4bceb291d87f20 100644 (file)
@@ -101,9 +101,6 @@ internal class Vala.CCodeStructModule : CCodeBaseModule {
 
                decl_space.add_type_declaration (new CCodeTypeDefinition ("struct _%s".printf (st.get_cname ()), new CCodeVariableDeclarator (st.get_cname ())));
 
-               if (st.source_reference.comment != null) {
-                       decl_space.add_type_definition (new CCodeComment (st.source_reference.comment));
-               }
                decl_space.add_type_definition (instance_struct);
 
                var function = new CCodeFunction (st.get_dup_function (), st.get_cname () + "*");
index cadaf8c011a1df644a8cc0da4a6e7af863d5cbc1..2ab64a0b6ee0e944603397a0e6516a26bed9530e 100644 (file)
@@ -49,9 +49,6 @@ internal class Vala.GErrorModule : CCodeDelegateModule {
                        }
                }
 
-               if (edomain.source_reference.comment != null) {
-                       decl_space.add_type_definition (new CCodeComment (edomain.source_reference.comment));
-               }
                decl_space.add_type_definition (cenum);
 
                string quark_fun_name = edomain.get_lower_case_cprefix () + "quark";
index e148b31fb9534b65c4b3b5e1f3befd8fa50ccef4..aaab3f87ab8eedd0187a05fd17d2add0dfa4ad26 100644 (file)
@@ -483,9 +483,6 @@ internal class Vala.GObjectModule : GTypeModule {
                
                        function.block = cblock;
 
-                       if (c.source_reference.comment != null) {
-                               source_type_member_definition.append (new CCodeComment (c.source_reference.comment));
-                       }
                        source_type_member_definition.append (function);
                } else if (c.binding == MemberBinding.CLASS) {
                        // class constructor
index 8e7a245d8b5a3dfcd34802b5ab4714ed1396d99c..6b7f3ec583ce62730ac87b9af95030c86b247e54 100644 (file)
@@ -309,9 +309,6 @@ internal class Vala.GTypeModule : GErrorModule {
                        }
                }
 
-               if (cl.source_reference.comment != null) {
-                       decl_space.add_type_definition (new CCodeComment (cl.source_reference.comment));
-               }
                if (!cl.is_compact || cl.base_class == null) {
                        // derived compact classes do not have a struct
                        decl_space.add_type_definition (instance_struct);
@@ -1773,9 +1770,6 @@ internal class Vala.GTypeModule : GErrorModule {
                        }
                }
 
-               if (iface.source_reference.comment != null) {
-                       decl_space.add_type_definition (new CCodeComment (iface.source_reference.comment));
-               }
                decl_space.add_type_definition (type_struct);
 
                var type_fun = create_interface_register_function (iface);
index 31b2845d2202e8413dbf7d3017e4aee61c9ada22..e5bdde695d9081be8e2e210e1629bb4afa6e1fb8 100644 (file)
@@ -40,6 +40,7 @@ libvalacore_la_VALASOURCES = \
        valacodenode.vala \
        valacodevisitor.vala \
        valacodewriter.vala \
+       valacomment.vala \
        valaconditionalexpression.vala \
        valaconstant.vala \
        valaconstructor.vala \
index e4b193f05ee5cfa223d56cfa6ab1de9bc150345d..f88efabaae2b30500c7205df4a6e9db6a8392841 100644 (file)
@@ -241,10 +241,11 @@ public class Vala.Class : ObjectTypeSymbol {
         *
         * @param name   type name
         * @param source reference to source code
+        * @param comment class documentation
         * @return       newly created class
         */
-       public Class (string name, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Class (string name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
        }
 
        /**
diff --git a/vala/valacomment.vala b/vala/valacomment.vala
new file mode 100644 (file)
index 0000000..0e48f89
--- /dev/null
@@ -0,0 +1,45 @@
+/* valacomment.vala
+ *
+ * Copyright (C) 2008-2009  Florian Brosch
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Florian Brosch <flo.brosch@gmail.com>
+ */
+
+using GLib;
+
+/**
+ * A documentation comment used by valadoc
+ */
+public class Vala.Comment {
+       public Comment (string comment, SourceReference _source_reference) {
+               source_reference = _source_reference;
+               content = comment;
+       }
+
+       /**
+        * The text describing the referenced source code.
+        */
+       public string content { set; get; }
+
+       /**
+        * References the location in the source file where this code node has
+        * been written.
+        */
+       public SourceReference source_reference { get; set; }
+}
+
index 54722c37ab80e7e053d35c21eb03f9b59d58176a..b382bcb5909a6cb6ac9ebf2a2e52ba04507e69fd 100644 (file)
@@ -67,8 +67,8 @@ public class Vala.Constant : Member, Lockable {
         * @param source_reference reference to source code
         * @return                 newly created constant
         */
-       public Constant (string name, DataType type_reference, Expression? initializer, SourceReference? source_reference) {
-               base (name, source_reference);
+       public Constant (string name, DataType type_reference, Expression? initializer, SourceReference? source_reference, Comment? comment = null) {
+               base (name, source_reference, comment);
                this.type_reference = type_reference;
                this.initializer = initializer;
        }
index 3051e52eb89df272cdacdecfbae0d30f70eeecc2..79cfa8eb07c41fa3ed5a7b17c2fab93b35b424bd 100644 (file)
@@ -59,8 +59,8 @@ public class Vala.CreationMethod : Method {
         * @param source_reference reference to source code
         * @return                 newly created method
         */
-       public CreationMethod (string? class_name, string? name, SourceReference? source_reference = null) {
-               base (name, new VoidType (), source_reference);
+       public CreationMethod (string? class_name, string? name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, new VoidType (), source_reference, comment);
                this.class_name = class_name;
 
                carray_length_parameter_position = -3;
index 033058db72f1139681d70c860439167286810711..8af715773901bd9a96b221bc8ef003af362d2752 100644 (file)
@@ -90,8 +90,8 @@ public class Vala.Delegate : TypeSymbol {
         * @param source      reference to source code
         * @return            newly created delegate
         */
-       public Delegate (string? name, DataType return_type, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Delegate (string? name, DataType return_type, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
                this.return_type = return_type;
 
                // error is -1 (right of user_data)
index 96f0cfa5cc0207865df030d921a0a1c9294c44cb..06c58fbbadc6a2d7e2f2f07d0b8d3a604ebbaa18 100644 (file)
@@ -34,8 +34,8 @@ public class Vala.DynamicMethod : Method {
        private string cname;
        static int dynamic_method_id;
 
-       public DynamicMethod (DataType dynamic_type, string name, DataType return_type, SourceReference? source_reference = null) {
-               base (name, return_type, source_reference);
+       public DynamicMethod (DataType dynamic_type, string name, DataType return_type, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, return_type, source_reference, comment);
                this.dynamic_type = dynamic_type;
        }
 
index c7ee1253a44fe63c2f25f57bea05be0bc39ca34d..3139f5f5fcd3ac8289a26eed0fa258fc8e3f94a6 100644 (file)
@@ -29,8 +29,8 @@ using Gee;
 public class Vala.DynamicProperty : Property {
        public DataType dynamic_type { get; set; }
 
-       public DynamicProperty (DataType dynamic_type, string name, SourceReference? source_reference = null) {
-               base (name, null, null, null, source_reference);
+       public DynamicProperty (DataType dynamic_type, string name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, null, null, null, source_reference, comment);
                this.dynamic_type = dynamic_type;
        }
 
index 695c302e938ee30f90db0b1b6efb6f316af7aa7b..dc1757eb4557ae43526f5ebbe03ecf311b1f7a75 100644 (file)
@@ -30,8 +30,8 @@ public class Vala.DynamicSignal : Signal {
 
        public Expression handler { get; set; }
 
-       public DynamicSignal (DataType dynamic_type, string name, DataType return_type, SourceReference? source_reference = null) {
-               base (name, return_type, source_reference);
+       public DynamicSignal (DataType dynamic_type, string name, DataType return_type, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, return_type, source_reference, comment);
                this.dynamic_type = dynamic_type;
        }
 
index 1d1e8c1120f84969a8153446b243f7da821690b2..b348f9c91eb3a6ea0acfa57c9a879e2cdb42cf1a 100644 (file)
@@ -52,8 +52,8 @@ public class Vala.Enum : TypeSymbol {
         * @param source_reference reference to source code
         * @return                 newly created enum
         */
-       public Enum (string name, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Enum (string name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
        }
        
        /**
index 8fbcc572977fb081423ad4292d06715c503f35b6..eaade92c15af1f86175fbfb29cecb1dced517be0 100644 (file)
@@ -31,6 +31,8 @@ public class Vala.EnumValue : Symbol {
         */
        public Expression value { get; set; }
 
+       public Comment comment { get; set; }
+
        private string cname;
 
        /**
@@ -39,8 +41,9 @@ public class Vala.EnumValue : Symbol {
         * @param name enum value name
         * @return     newly created enum value
         */
-       public EnumValue (string name, SourceReference? source_reference = null) {
+       public EnumValue (string name, SourceReference? source_reference = null, Comment? comment = null) {
                base (name, source_reference);
+               this.comment = comment;
        }
 
        /**
@@ -50,8 +53,8 @@ public class Vala.EnumValue : Symbol {
         * @param value numerical representation
         * @return      newly created enum value
         */
-       public EnumValue.with_value (string name, Expression value, SourceReference? source_reference = null) {
-               this (name, source_reference);
+       public EnumValue.with_value (string name, Expression value, SourceReference? source_reference = null, Comment? comment = null) {
+               this (name, source_reference, comment);
                this.value = value;
        }
        
index 83ba9a6874da33b7e9bc8bc9a589dbf5cdaab4aa..24d7f60ecb838afd288a4b91045154c8677fe718 100644 (file)
@@ -39,8 +39,8 @@ public class Vala.ErrorCode : TypeSymbol {
         * @param name enum value name
         * @return     newly created enum value
         */
-       public ErrorCode (string name, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public ErrorCode (string name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
        }
 
        /**
index eecd97c8ecbdd16bbacf3165887fde97ea4f2ed7..6802fa88abab52a022c1188a3f284d6656089cb5 100644 (file)
@@ -41,8 +41,8 @@ public class Vala.ErrorDomain : TypeSymbol {
         * @param source_reference reference to source code
         * @return                 newly created error domain
         */
-       public ErrorDomain (string name, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public ErrorDomain (string name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
        }
        
        /**
index 43336a89f490ff7e0b7badd6e654342f0ff35285..3573354c3a8876bed2080397a2b317aad840a2c0 100644 (file)
@@ -115,8 +115,8 @@ public class Vala.Field : Member, Lockable {
         * @param source reference to source code
         * @return       newly created field
         */
-       public Field (string name, DataType field_type, Expression? initializer, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Field (string name, DataType field_type, Expression? initializer, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
                this.field_type = field_type;
                this.initializer = initializer;
        }
index 8c965e7f7b8b950c8a5621a334085f3ed80af644..f61f65a3177866f0ff88918c7e497d28817a0317 100644 (file)
@@ -40,7 +40,7 @@ public class Vala.Genie.Parser : CodeVisitor {
        // number of tokens in buffer
        int size;
 
-       string comment;
+       Comment comment;
        
        string class_name;
        
@@ -194,14 +194,6 @@ public class Vala.Genie.Parser : CodeVisitor {
                return new SourceReference (scanner.source_file, begin.line, begin.column, tokens[last_index].end.line, tokens[last_index].end.column);
        }
 
-       SourceReference get_src_com (SourceLocation begin) {
-               int last_index = (index + BUFFER_SIZE - 1) % BUFFER_SIZE;
-
-               var src = new SourceReference.with_comment (scanner.source_file, begin.line, begin.column, tokens[last_index].end.line, tokens[last_index].end.column, comment);
-               comment = null;
-               return src;
-       }
-
        SourceReference get_current_src () {
                return new SourceReference (scanner.source_file, tokens[index].begin.line, tokens[index].begin.column, tokens[index].end.line, tokens[index].end.column);
        }
@@ -1564,7 +1556,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                                                expect (TokenType.EOL);
                                        }
                
-                                       stmt =  new EmptyStatement (get_src_com (begin));
+                                       stmt =  new EmptyStatement (get_src (begin));
                                        break;                          
 
 
@@ -1700,7 +1692,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 
                comment = scanner.pop_comment ();
 
-               var block = new Block (get_src_com (get_location ()));
+               var block = new Block (get_src (get_location ()));
                block.add_statement (parse_embedded_statement_without_block ());
                return block;
 
@@ -1730,7 +1722,7 @@ public class Vala.Genie.Parser : CodeVisitor {
        Block parse_block () throws ParseError {
                var begin = get_location ();
                expect (TokenType.INDENT);
-               var block = new Block (get_src_com (begin));
+               var block = new Block (get_src (begin));
                parse_statements (block);
                if (!accept (TokenType.DEDENT)) {
                        // only report error if it's not a secondary error
@@ -1752,7 +1744,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                accept (TokenType.SEMICOLON);
                expect_terminator ();
 
-               return new EmptyStatement (get_src_com (begin));
+               return new EmptyStatement (get_src (begin));
        }
 
        void add_local_var_variable (Block block, string id)  throws ParseError {
@@ -1811,7 +1803,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                if (accept (TokenType.ASSIGN)) {
                        initializer = parse_expression ();
                }
-               return new LocalVariable (variable_type, id, initializer, get_src_com (begin));
+               return new LocalVariable (variable_type, id, initializer, get_src (begin));
        }
 
        Statement parse_expression_statement () throws ParseError {
@@ -1824,7 +1816,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                        expect_terminator ();
                }
 
-               return new ExpressionStatement (expr, get_src_com (begin));
+               return new ExpressionStatement (expr, get_src (begin));
        }
 
        Expression parse_statement_expression () throws ParseError {
@@ -1847,7 +1839,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                        accept (TokenType.EOL);
                }
 
-               var src = get_src_com (begin);
+               var src = get_src (begin);
                var true_stmt = parse_embedded_statement ();
                Block false_stmt = null;
                if (accept (TokenType.ELSE)) {
@@ -1870,19 +1862,19 @@ public class Vala.Genie.Parser : CodeVisitor {
 
                expect (TokenType.EOL);
 
-               var stmt = new SwitchStatement (condition, get_src_com (begin));
+               var stmt = new SwitchStatement (condition, get_src (begin));
                expect (TokenType.INDENT);
                while (current () != TokenType.DEDENT) {
-                       var section = new SwitchSection (get_src_com (begin));
+                       var section = new SwitchSection (get_src (begin));
                        
                        if (accept (TokenType.WHEN)) {
                                do {
-                                       section.add_label (new SwitchLabel (parse_expression (), get_src_com (begin)));
+                                       section.add_label (new SwitchLabel (parse_expression (), get_src (begin)));
                                }
                                while (accept (TokenType.COMMA));
                        } else {
                                expect (TokenType.DEFAULT);
-                               section.add_label (new SwitchLabel.with_default (get_src_com (begin)));
+                               section.add_label (new SwitchLabel.with_default (get_src (begin)));
                        }
 
                        if (!accept (TokenType.EOL)) {
@@ -1892,7 +1884,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                        parse_statements (section);
 
                        /* add break statement for each block */
-                       var break_stmt =  new BreakStatement (get_src_com (begin));
+                       var break_stmt =  new BreakStatement (get_src (begin));
                        section.add_statement (break_stmt);
 
                        stmt.add_section (section);
@@ -1913,7 +1905,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                }
 
                var body = parse_embedded_statement ();
-               return new WhileStatement (condition, body, get_src_com (begin));
+               return new WhileStatement (condition, body, get_src (begin));
        }
 
        Statement parse_do_statement () throws ParseError {
@@ -1927,7 +1919,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 
                expect_terminator ();
                
-               return new DoStatement (body, condition, get_src_com (begin));
+               return new DoStatement (body, condition, get_src (begin));
        }
 
 
@@ -2008,7 +2000,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                        expect (TokenType.DO);
                }
 
-               var src = get_src_com (begin);
+               var src = get_src (begin);
                var body = parse_embedded_statement ();
                var stmt = new ForStatement (condition, body, src);
 
@@ -2046,7 +2038,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                if (!accept (TokenType.EOL)) {
                        expect (TokenType.DO);
                }
-               var src = get_src_com (begin);
+               var src = get_src (begin);
                var body = parse_embedded_statement ();
                return new ForeachStatement (type, id, collection, body, src);
        }
@@ -2055,14 +2047,14 @@ public class Vala.Genie.Parser : CodeVisitor {
                var begin = get_location ();
                expect (TokenType.BREAK);
                expect_terminator ();
-               return new BreakStatement (get_src_com (begin));
+               return new BreakStatement (get_src (begin));
        }
 
        Statement parse_continue_statement () throws ParseError {
                var begin = get_location ();
                expect (TokenType.CONTINUE);
                expect_terminator ();
-               return new ContinueStatement (get_src_com (begin));
+               return new ContinueStatement (get_src (begin));
        }
 
        Statement parse_return_statement () throws ParseError {
@@ -2073,7 +2065,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                        expr = parse_expression ();
                }
                expect_terminator ();
-               return new ReturnStatement (expr, get_src_com (begin));
+               return new ReturnStatement (expr, get_src (begin));
        }
 
        Statement parse_yield_statement () throws ParseError {
@@ -2092,7 +2084,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                expect (TokenType.RAISE);
                var expr = parse_expression ();
                expect_terminator ();
-               return new ThrowStatement (expr, get_src_com (begin));
+               return new ThrowStatement (expr, get_src (begin));
        }
 
        Statement parse_try_statement () throws ParseError {
@@ -2110,7 +2102,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                } else {
                        finally_clause = parse_finally_clause ();
                }
-               var stmt = new TryStatement (try_block, finally_clause, get_src_com (begin));
+               var stmt = new TryStatement (try_block, finally_clause, get_src (begin));
                foreach (CatchClause clause in catch_clauses) {
                        stmt.add_catch_clause (clause);
                }
@@ -2148,7 +2140,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                var expr = parse_expression ();
                expect (TokenType.CLOSE_PARENS);
                var stmt = parse_embedded_statement ();
-               return new LockStatement (expr, stmt, get_src_com (begin));
+               return new LockStatement (expr, stmt, get_src (begin));
        }
 
        Statement parse_delete_statement () throws ParseError {
@@ -2156,7 +2148,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                expect (TokenType.DELETE);
                var expr = parse_expression ();
                expect_terminator ();
-               return new DeleteStatement (expr, get_src_com (begin));
+               return new DeleteStatement (expr, get_src (begin));
        }
 
        Gee.List<Attribute>? parse_attributes () throws ParseError {
@@ -2347,7 +2339,11 @@ public class Vala.Genie.Parser : CodeVisitor {
                var begin = get_location ();
                expect (TokenType.NAMESPACE);
                var sym = parse_symbol_name ();
-               var ns = new Namespace (sym.name, get_src_com (begin));
+               var ns = new Namespace (sym.name, get_src (begin));
+               if (comment != null) {
+                       ns.add_comment (comment);
+                       comment = null;
+               }
                set_attributes (ns, attrs);
                expect (TokenType.EOL);
                parse_declarations (ns);
@@ -2453,7 +2449,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 
                accept (TokenType.EOL);
 
-               var cl = new Class (sym.name, get_src_com (begin));
+               var cl = new Class (sym.name, get_src (begin), comment);
 
                if (ModifierFlags.PRIVATE in flags) {
                        cl.access = SymbolAccessibility.PRIVATE;
@@ -2572,7 +2568,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                        array_type.element_type.value_owned = false;
                }
 
-               var c = new Constant (id, type, initializer, get_src_com (begin));
+               var c = new Constant (id, type, initializer, get_src (begin), comment);
                c.access = get_access (id);
                
                if (ModifierFlags.EXTERN in flags || scanner.source_file.external_package) {
@@ -2595,7 +2591,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 
                var type = parse_type ();
 
-               var f = new Field (id, type, null, get_src_com (begin));
+               var f = new Field (id, type, null, get_src (begin), comment);
 
                if (ModifierFlags.ABSTRACT in flags || ModifierFlags.VIRTUAL in flags || ModifierFlags.OVERRIDE in flags) {
                        Report.error (f.source_reference, "abstract, virtual, and override modifiers are not applicable to fields");
@@ -2657,8 +2653,8 @@ public class Vala.Genie.Parser : CodeVisitor {
                var begin = get_location ();
                DataType type = new VoidType ();
                expect (TokenType.INIT);
-               
-               var method = new Method (id, type, get_src_com (begin));
+
+               var method = new Method (id, type, get_src (begin), comment);
                method.access = SymbolAccessibility.PUBLIC;
                
                set_attributes (method, attrs);
@@ -2704,15 +2700,13 @@ public class Vala.Genie.Parser : CodeVisitor {
 
                expect (TokenType.CLOSE_PARENS);
 
-
                /* deal with return value */
                if (accept (TokenType.COLON)) {
                        type = parse_type ();
                        parse_type_parameter_list ();
                }
 
-
-               var method = new Method (id, type, get_src_com (begin));
+               var method = new Method (id, type, get_src (begin), comment);
                if (ModifierFlags.PRIVATE in flags) {
                        method.access = SymbolAccessibility.PRIVATE;
                } else {
@@ -2844,7 +2838,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 
                var type = parse_type (false);
 
-               var prop = new Property (id, type, null, null, get_src_com (begin));
+               var prop = new Property (id, type, null, null, get_src (begin), comment);
                if (ModifierFlags.PRIVATE in flags) {
                        prop.access = SymbolAccessibility.PRIVATE;
                } else {
@@ -2993,7 +2987,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                        type = new VoidType ();
                }
 
-               var sig = new Vala.Signal (id, type, get_src_com (begin));
+               var sig = new Vala.Signal (id, type, get_src (begin), comment);
                if (ModifierFlags.PRIVATE in flags) {
                        sig.access = SymbolAccessibility.PRIVATE;
                } else {
@@ -3022,7 +3016,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                expect (TokenType.INIT);
                var flags = parse_member_declaration_modifiers ();
 
-               var c = new Constructor (get_src_com (begin));
+               var c = new Constructor (get_src (begin));
                if (ModifierFlags.STATIC in flags) {
                        c.binding = MemberBinding.STATIC;
                } else if (ModifierFlags.CLASS in flags) {
@@ -3037,7 +3031,7 @@ public class Vala.Genie.Parser : CodeVisitor {
        Destructor parse_destructor_declaration (Gee.List<Attribute>? attrs) throws ParseError {
                var begin = get_location ();
                expect (TokenType.FINAL);
-               var d = new Destructor (get_src_com (begin));
+               var d = new Destructor (get_src (begin));
                accept_block ();
                d.body = parse_block ();
                return d;
@@ -3054,7 +3048,8 @@ public class Vala.Genie.Parser : CodeVisitor {
                if (accept (TokenType.COLON)) {
                        base_type = parse_type ();
                }
-               var st = new Struct (sym.name, get_src_com (begin));
+
+               var st = new Struct (sym.name, get_src (begin), comment);
                if (ModifierFlags.PRIVATE in flags) {
                        st.access = SymbolAccessibility.PRIVATE;
                } else {
@@ -3114,7 +3109,8 @@ public class Vala.Genie.Parser : CodeVisitor {
                                base_types.add (type);
                        } while (accept (TokenType.COMMA));
                }
-               var iface = new Interface (sym.name, get_src_com (begin));
+
+               var iface = new Interface (sym.name, get_src (begin), comment);
                if (ModifierFlags.PRIVATE in flags) {
                        iface.access = SymbolAccessibility.PRIVATE;
                } else {
@@ -3180,8 +3176,8 @@ public class Vala.Genie.Parser : CodeVisitor {
                expect (TokenType.ENUM);
                var flags = parse_type_declaration_modifiers ();
 
-               var sym = parse_symbol_name (); 
-               var en = new Enum (sym.name, get_src_com (begin));
+               var sym = parse_symbol_name ();
+               var en = new Enum (sym.name, get_src (begin), comment);
                if (ModifierFlags.PRIVATE in flags) {
                        en.access = SymbolAccessibility.PRIVATE;
                } else {
@@ -3202,8 +3198,8 @@ public class Vala.Genie.Parser : CodeVisitor {
                        var value_attrs = parse_attributes ();
                        var value_begin = get_location (); 
                        string id = parse_identifier ();
-                       
-                       var ev = new EnumValue (id, get_src (value_begin));
+                       comment = scanner.pop_comment ();
+                       var ev = new EnumValue (id, get_src (value_begin), comment);
                        set_attributes (ev, value_attrs);
                        
                        if (accept (TokenType.ASSIGN)) {
@@ -3236,7 +3232,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                var flags = parse_type_declaration_modifiers ();
 
                var sym = parse_symbol_name ();
-               var ed = new ErrorDomain (sym.name, get_src_com (begin));
+               var ed = new ErrorDomain (sym.name, get_src (begin), comment);
                if (ModifierFlags.PRIVATE in flags) {
                        ed.access = SymbolAccessibility.PRIVATE;
                } else {
@@ -3256,8 +3252,8 @@ public class Vala.Genie.Parser : CodeVisitor {
                        var code_attrs = parse_attributes ();
                        var code_begin = get_location ();
                        string id = parse_identifier ();
-
-                       var ec = new ErrorCode (id, get_src (code_begin));
+                       comment = scanner.pop_comment ();
+                       var ec = new ErrorCode (id, get_src (code_begin), comment);
                        set_attributes (ec, code_attrs);
                        if (accept (TokenType.ASSIGN)) {
                                ec.value = parse_expression ();
@@ -3273,7 +3269,6 @@ public class Vala.Genie.Parser : CodeVisitor {
                while (sym.inner != null) {
                        sym = sym.inner;
                        var ns = new Namespace (sym.name, ed.source_reference);
-                       
                        if (result is Namespace) {
                                ns.add_namespace ((Namespace) result);
                        } else {
@@ -3404,26 +3399,24 @@ public class Vala.Genie.Parser : CodeVisitor {
                expect (TokenType.CONSTRUCT);
                parse_member_declaration_modifiers ();
 
-
                if (accept (TokenType.OPEN_PARENS)) {
                        /* create default name using class name */
-                       method = new CreationMethod (class_name, null, get_src_com (begin));
+                       method = new CreationMethod (class_name, null, get_src (begin), comment);
                } else {
                        var sym = parse_symbol_name ();
                        if (sym.inner == null) {
                        
                                if (sym.name != class_name) {
-                                       method = new CreationMethod (class_name, sym.name, get_src_com (begin));
+                                       method = new CreationMethod (class_name, sym.name, get_src (begin), comment);
                                } else {
-                                       method = new CreationMethod (sym.name, null, get_src_com (begin));
+                                       method = new CreationMethod (sym.name, null, get_src (begin), comment);
                                }
                        } else {
-                               method = new CreationMethod (sym.inner.name, sym.name, get_src_com (begin));
+                               method = new CreationMethod (sym.inner.name, sym.name, get_src (begin), comment);
                        }
                        expect (TokenType.OPEN_PARENS);
                }
 
-
                if (current () != TokenType.CLOSE_PARENS) {
                        do {
                                var param = parse_parameter ();
@@ -3486,7 +3479,7 @@ public class Vala.Genie.Parser : CodeVisitor {
                        type = new VoidType ();
                }
 
-               var d = new Delegate (sym.name, type, get_src_com (begin));
+               var d = new Delegate (sym.name, type, get_src (begin), comment);
 
                if (accept (TokenType.RAISES)) {
                        do {
@@ -3526,7 +3519,6 @@ public class Vala.Genie.Parser : CodeVisitor {
                while (sym.inner != null) {
                        sym = sym.inner;
                        var ns = new Namespace (sym.name, d.source_reference);
-
                        if (result is Namespace) {
                                ns.add_namespace ((Namespace) result);
                        } else {
index ecbb426a4d479cae5717e312c7b08628f4483623..e39191c4899383c70fa1d5f769500d73e60f4fd6 100644 (file)
@@ -46,7 +46,7 @@ public class Vala.Genie.Scanner {
        TokenType last_token;
        bool parse_started;
 
-       string _comment;
+       Comment _comment;
        
        Conditional[] conditional_stack;
 
@@ -1039,14 +1039,11 @@ public class Vala.Genie.Scanner {
                if (current[1] == '/') {
                        // single-line comment
                        current += 2;
-                       char* begin = current;
-                       
+
                        // skip until end of line or end of file
                        while (current < end && current[0] != '\n') {
                                current++;
                        }
-                       
-                       push_comment (((string) begin).ndup ((long) (current - begin)), line == 1);
 
                        /* do not ignore EOL if comment does not exclusively occupy the line */
                        if (current[0] == '\n' && last_token == TokenType.EOL) {
@@ -1057,6 +1054,12 @@ public class Vala.Genie.Scanner {
                        }
                } else {
                        // delimited comment
+                       SourceReference source_reference = null;
+
+                       if (current[2] == '*') {
+                               source_reference = new SourceReference (source_file, line, column, line, column);
+                       }
+
                        current += 2;
                        char* begin = current;
                        int begin_line = line;
@@ -1073,7 +1076,12 @@ public class Vala.Genie.Scanner {
                                Report.error (new SourceReference (source_file, line, column, line, column), "syntax error, expected */");
                                return true;
                        }
-                       push_comment (((string) begin).ndup ((long) (current - begin)), begin_line == 1);
+
+                       if (source_reference != null) {
+                               string comment = ((string) begin).ndup ((long) (current - begin));
+                               push_comment (comment, source_reference, begin_line == 1 && comment[0] != '*');
+                       }
+
                        current += 2;
                        column += 2;
                }
@@ -1103,17 +1111,13 @@ public class Vala.Genie.Scanner {
                }
        }
 
-
-
-
-       void push_comment (string comment_item, bool file_comment) {
-               if (_comment == null) {
-                       _comment = comment_item;
-               } else {
-                       _comment = "%s\n%s".printf (_comment, comment_item);
+       void push_comment (string comment_item, SourceReference source_reference, bool file_comment) {
+               if (comment_item[0] == '*') {
+                       _comment = new Comment (comment_item, source_reference);
                }
+
                if (file_comment) {
-                       source_file.comment = _comment;
+                       source_file.add_comment (new Comment (comment_item, source_reference));
                        _comment = null;
                }
        }
@@ -1123,23 +1127,17 @@ public class Vala.Genie.Scanner {
         *
         * @return saved comment
         */
-       public string? pop_comment () {
+       public Comment? pop_comment () {
                if (_comment == null) {
                        return null;
                }
 
-               var result_builder = new StringBuilder (_comment);
+               var comment = _comment;
                _comment = null;
-
-               weak string index;
-               while ((index = result_builder.str.chr (-1, '\t')) != null) {
-                       result_builder.erase (result_builder.str.pointer_to_offset (index), 1);
-               }
-
-               return result_builder.str;
+               return comment;
        }
-       
-               bool pp_whitespace () {
+
+       bool pp_whitespace () {
                bool found = false;
                while (current < end && current[0].isspace () && current[0] != '\n') {
                        found = true;
index 99664778cc63fa7c56084c92cf3d0e5dafc908f7..44de8f6c28d45b34a0938d1bd178d4cedc98fa1f 100644 (file)
@@ -88,8 +88,8 @@ public class Vala.Interface : ObjectTypeSymbol {
         * @param source reference to source code
         * @return       newly created interface
         */
-       public Interface (string name, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Interface (string name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
        }
 
        /**
index 8628d1a691e3c7bc99e83739a561801caf63d731..b0bdfafa73e93b33345cac4e92cc25157ce47fc8 100644 (file)
@@ -27,6 +27,8 @@ using Gee;
  * Represents a general class member.
  */
 public abstract class Vala.Member : Symbol {
+       public Comment comment { get; set; }
+
        private Gee.List<string> cheader_filenames = new ArrayList<string> ();
 
        /**
@@ -35,8 +37,9 @@ public abstract class Vala.Member : Symbol {
         */
        public bool hides { get; set; }
 
-       public Member (string? name, SourceReference? source_reference) {
+       public Member (string? name, SourceReference? source_reference, Comment? comment = null) {
                base (name, source_reference);
+               this.comment = comment;
        }
 
        public override void accept (CodeVisitor visitor) {
index 0d2190296345618c17051220f50d1abc721a3b3b..5c8119bd59cc19d44b01da3af8d9fb59efd94849 100644 (file)
@@ -241,8 +241,8 @@ public class Vala.Method : Member {
         * @param source      reference to source code
         * @return            newly created method
         */
-       public Method (string? name, DataType return_type, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Method (string? name, DataType return_type, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
                this.return_type = return_type;
 
                carray_length_parameter_position = -3;
index 14f93c45191d4c28c9b83de5dfa56c46ea70eca4..7a31c8f896d8bfcaeaa23922c1a04c6ae500a4b5 100644 (file)
@@ -37,6 +37,8 @@ public class Vala.Namespace : Symbol {
        private Gee.List<Field> fields = new ArrayList<Field> ();
        private Gee.List<Method> methods = new ArrayList<Method> ();
 
+       private Gee.List<Comment> comments = new ArrayList<Comment> ();
+
        private Gee.List<string> cprefixes = new ArrayList<string> ();
        private string lower_case_cprefix;
        
@@ -56,6 +58,19 @@ public class Vala.Namespace : Symbol {
                access = SymbolAccessibility.PUBLIC;
        }
        
+       public void add_comment (Comment comment) {
+               comments.add (comment);
+       }
+
+       /**
+        * Returns a copy of the list of namespaces.
+        *
+        * @return comment list
+        */
+       public Gee.List<Comment> get_comments () {
+               return new ReadOnlyList<Comment> (comments);
+       }
+
        /**
         * Adds the specified namespace to this source file.
         *
@@ -68,6 +83,7 @@ public class Vala.Namespace : Symbol {
                        if (old_ns.external_package && !ns.external_package) {
                                old_ns.source_reference = ns.source_reference;
                        }
+
                        foreach (Namespace sub_ns in ns.get_namespaces ()) {
                                old_ns.add_namespace (sub_ns);
                        }
@@ -98,6 +114,9 @@ public class Vala.Namespace : Symbol {
                        foreach (Method m in ns.get_methods ()) {
                                old_ns.add_method (m);
                        }
+                       foreach (Comment c in ns.get_comments ()) {
+                               old_ns.add_comment (c);
+                       }
                } else {
                        namespaces.add (ns);
                        scope.add (ns.name, ns);
index 752290c9f64184221e012662c45f70d2b856055a..b5c6632768c8c99ccf03b264d8baceb0fad4e46e 100644 (file)
@@ -32,8 +32,8 @@ using Gee;
 public abstract class Vala.ObjectTypeSymbol : TypeSymbol {
        private Gee.List<TypeParameter> type_parameters = new ArrayList<TypeParameter> ();
 
-       public ObjectTypeSymbol (string name, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public ObjectTypeSymbol (string name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
        }
 
        public abstract Gee.List<Method> get_methods ();
index 78d1581200e416d3e5e845ed3db4038c845a549c..569bff4777b90c33d1128fa6c91b38617bb2bf0c 100644 (file)
@@ -38,7 +38,7 @@ public class Vala.Parser : CodeVisitor {
        // number of tokens in buffer
        int size;
 
-       string comment;
+       Comment comment;
 
        const int BUFFER_SIZE = 32;
 
@@ -147,14 +147,6 @@ public class Vala.Parser : CodeVisitor {
                return new SourceReference (scanner.source_file, begin.line, begin.column, tokens[last_index].end.line, tokens[last_index].end.column);
        }
 
-       SourceReference get_src_com (SourceLocation begin) {
-               int last_index = (index + BUFFER_SIZE - 1) % BUFFER_SIZE;
-
-               var src = new SourceReference.with_comment (scanner.source_file, begin.line, begin.column, tokens[last_index].end.line, tokens[last_index].end.column, comment);
-               comment = null;
-               return src;
-       }
-
        SourceReference get_current_src () {
                return new SourceReference (scanner.source_file, tokens[index].begin.line, tokens[index].begin.column, tokens[index].end.line, tokens[index].end.column);
        }
@@ -308,12 +300,14 @@ public class Vala.Parser : CodeVisitor {
 
        public void parse_file (SourceFile source_file) {
                scanner = new Scanner (source_file);
+               parse_file_comments ();
 
                index = -1;
                size = 0;
                
                next ();
 
+
                try {
                        parse_using_directives ();
                        parse_declarations (context.root, true);
@@ -324,6 +318,10 @@ public class Vala.Parser : CodeVisitor {
                scanner = null;
        }
 
+       void parse_file_comments () {
+               scanner.parse_file_comments ();
+       }
+
        void skip_symbol_name () throws ParseError {
                do {
                        skip_identifier ();
@@ -1249,6 +1247,7 @@ public class Vala.Parser : CodeVisitor {
                        try {
                                Statement stmt = null;
                                bool is_decl = false;
+
                                comment = scanner.pop_comment ();
                                switch (current ()) {
                                case TokenType.OPEN_BRACE:
@@ -1380,7 +1379,7 @@ public class Vala.Parser : CodeVisitor {
 
                comment = scanner.pop_comment ();
 
-               var block = new Block (get_src_com (get_location ()));
+               var block = new Block (get_src (get_location ()));
                block.add_statement (parse_embedded_statement_without_block ());
                return block;
 
@@ -1410,7 +1409,7 @@ public class Vala.Parser : CodeVisitor {
        Block parse_block () throws ParseError {
                var begin = get_location ();
                expect (TokenType.OPEN_BRACE);
-               var block = new Block (get_src_com (begin));
+               var block = new Block (get_src (begin));
                parse_statements (block);
                if (!accept (TokenType.CLOSE_BRACE)) {
                        // only report error if it's not a secondary error
@@ -1428,7 +1427,7 @@ public class Vala.Parser : CodeVisitor {
        Statement parse_empty_statement () throws ParseError {
                var begin = get_location ();
                expect (TokenType.SEMICOLON);
-               return new EmptyStatement (get_src_com (begin));
+               return new EmptyStatement (get_src (begin));
        }
 
        void parse_local_variable_declarations (Block block) throws ParseError {
@@ -1459,14 +1458,14 @@ public class Vala.Parser : CodeVisitor {
                if (accept (TokenType.ASSIGN)) {
                        initializer = parse_expression ();
                }
-               return new LocalVariable (type, id, initializer, get_src_com (begin));
+               return new LocalVariable (type, id, initializer, get_src (begin));
        }
 
        Statement parse_expression_statement () throws ParseError {
                var begin = get_location ();
                var expr = parse_statement_expression ();
                expect (TokenType.SEMICOLON);
-               return new ExpressionStatement (expr, get_src_com (begin));
+               return new ExpressionStatement (expr, get_src (begin));
        }
 
        Expression parse_statement_expression () throws ParseError {
@@ -1482,7 +1481,7 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.OPEN_PARENS);
                var condition = parse_expression ();
                expect (TokenType.CLOSE_PARENS);
-               var src = get_src_com (begin);
+               var src = get_src (begin);
                var true_stmt = parse_embedded_statement ();
                Block false_stmt = null;
                if (accept (TokenType.ELSE)) {
@@ -1497,16 +1496,16 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.OPEN_PARENS);
                var condition = parse_expression ();
                expect (TokenType.CLOSE_PARENS);
-               var stmt = new SwitchStatement (condition, get_src_com (begin));
+               var stmt = new SwitchStatement (condition, get_src (begin));
                expect (TokenType.OPEN_BRACE);
                while (current () != TokenType.CLOSE_BRACE) {
-                       var section = new SwitchSection (get_src_com (begin));
+                       var section = new SwitchSection (get_src (begin));
                        do {
                                if (accept (TokenType.CASE)) {
-                                       section.add_label (new SwitchLabel (parse_expression (), get_src_com (begin)));
+                                       section.add_label (new SwitchLabel (parse_expression (), get_src (begin)));
                                } else {
                                        expect (TokenType.DEFAULT);
-                                       section.add_label (new SwitchLabel.with_default (get_src_com (begin)));
+                                       section.add_label (new SwitchLabel.with_default (get_src (begin)));
                                }
                                expect (TokenType.COLON);
                        } while (current () == TokenType.CASE || current () == TokenType.DEFAULT);
@@ -1524,7 +1523,7 @@ public class Vala.Parser : CodeVisitor {
                var condition = parse_expression ();
                expect (TokenType.CLOSE_PARENS);
                var body = parse_embedded_statement ();
-               return new WhileStatement (condition, body, get_src_com (begin));
+               return new WhileStatement (condition, body, get_src (begin));
        }
 
        Statement parse_do_statement () throws ParseError {
@@ -1536,7 +1535,7 @@ public class Vala.Parser : CodeVisitor {
                var condition = parse_expression ();
                expect (TokenType.CLOSE_PARENS);
                expect (TokenType.SEMICOLON);
-               return new DoStatement (body, condition, get_src_com (begin));
+               return new DoStatement (body, condition, get_src (begin));
        }
 
        Statement parse_for_statement () throws ParseError {
@@ -1589,7 +1588,7 @@ public class Vala.Parser : CodeVisitor {
                        } while (accept (TokenType.COMMA));
                }
                expect (TokenType.CLOSE_PARENS);
-               var src = get_src_com (begin);
+               var src = get_src (begin);
                var body = parse_embedded_statement ();
                var stmt = new ForStatement (condition, body, src);
                foreach (Expression init in initializer_list) {
@@ -1618,7 +1617,7 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.IN);
                var collection = parse_expression ();
                expect (TokenType.CLOSE_PARENS);
-               var src = get_src_com (begin);
+               var src = get_src (begin);
                var body = parse_embedded_statement ();
                return new ForeachStatement (type, id, collection, body, src);
        }
@@ -1627,14 +1626,14 @@ public class Vala.Parser : CodeVisitor {
                var begin = get_location ();
                expect (TokenType.BREAK);
                expect (TokenType.SEMICOLON);
-               return new BreakStatement (get_src_com (begin));
+               return new BreakStatement (get_src (begin));
        }
 
        Statement parse_continue_statement () throws ParseError {
                var begin = get_location ();
                expect (TokenType.CONTINUE);
                expect (TokenType.SEMICOLON);
-               return new ContinueStatement (get_src_com (begin));
+               return new ContinueStatement (get_src (begin));
        }
 
        Statement parse_return_statement () throws ParseError {
@@ -1645,7 +1644,7 @@ public class Vala.Parser : CodeVisitor {
                        expr = parse_expression ();
                }
                expect (TokenType.SEMICOLON);
-               return new ReturnStatement (expr, get_src_com (begin));
+               return new ReturnStatement (expr, get_src (begin));
        }
 
        Statement parse_yield_statement () throws ParseError {
@@ -1664,7 +1663,7 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.THROW);
                var expr = parse_expression ();
                expect (TokenType.SEMICOLON);
-               return new ThrowStatement (expr, get_src_com (begin));
+               return new ThrowStatement (expr, get_src (begin));
        }
 
        Statement parse_try_statement () throws ParseError {
@@ -1681,7 +1680,7 @@ public class Vala.Parser : CodeVisitor {
                } else {
                        finally_clause = parse_finally_clause ();
                }
-               var stmt = new TryStatement (try_block, finally_clause, get_src_com (begin));
+               var stmt = new TryStatement (try_block, finally_clause, get_src (begin));
                foreach (CatchClause clause in catch_clauses) {
                        stmt.add_catch_clause (clause);
                }
@@ -1716,7 +1715,7 @@ public class Vala.Parser : CodeVisitor {
                var expr = parse_expression ();
                expect (TokenType.CLOSE_PARENS);
                var stmt = parse_embedded_statement ();
-               return new LockStatement (expr, stmt, get_src_com (begin));
+               return new LockStatement (expr, stmt, get_src (begin));
        }
 
        Statement parse_delete_statement () throws ParseError {
@@ -1724,7 +1723,7 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.DELETE);
                var expr = parse_expression ();
                expect (TokenType.SEMICOLON);
-               return new DeleteStatement (expr, get_src_com (begin));
+               return new DeleteStatement (expr, get_src (begin));
        }
 
        Gee.List<Attribute>? parse_attributes () throws ParseError {
@@ -1939,7 +1938,12 @@ public class Vala.Parser : CodeVisitor {
                var begin = get_location ();
                expect (TokenType.NAMESPACE);
                var sym = parse_symbol_name ();
-               var ns = new Namespace (sym.name, get_src_com (begin));
+               var ns = new Namespace (sym.name, get_src (begin));
+               if (comment != null) {
+                       ns.add_comment (comment);
+                       comment = null;
+               }
+
                set_attributes (ns, attrs);
                parse_declarations (ns);
 
@@ -2018,7 +2022,7 @@ public class Vala.Parser : CodeVisitor {
                        } while (accept (TokenType.COMMA));
                }
 
-               var cl = new Class (sym.name, get_src_com (begin));
+               var cl = new Class (sym.name, get_src (begin), comment);
                cl.access = access;
                if (ModifierFlags.ABSTRACT in flags) {
                        cl.is_abstract = true;
@@ -2125,7 +2129,7 @@ public class Vala.Parser : CodeVisitor {
                        array_type.element_type.value_owned = false;
                }
 
-               var c = new Constant (id, type, initializer, get_src_com (begin));
+               var c = new Constant (id, type, initializer, get_src (begin), comment);
                c.access = access;
                if (ModifierFlags.EXTERN in flags || scanner.source_file.external_package) {
                        c.external = true;
@@ -2146,7 +2150,7 @@ public class Vala.Parser : CodeVisitor {
 
                type = parse_inline_array_type (type);
 
-               var f = new Field (id, type, null, get_src_com (begin));
+               var f = new Field (id, type, null, get_src (begin), comment);
                f.access = access;
                set_attributes (f, attrs);
                if (ModifierFlags.STATIC in flags) {
@@ -2193,7 +2197,7 @@ public class Vala.Parser : CodeVisitor {
                var type = parse_type ();
                string id = parse_identifier ();
                var type_param_list = parse_type_parameter_list ();
-               var method = new Method (id, type, get_src_com (begin));
+               var method = new Method (id, type, get_src (begin), comment);
                method.access = access;
                set_attributes (method, attrs);
                foreach (TypeParameter type_param in type_param_list) {
@@ -2286,7 +2290,7 @@ public class Vala.Parser : CodeVisitor {
                }
 
                string id = parse_identifier ();
-               var prop = new Property (id, type, null, null, get_src_com (begin));
+               var prop = new Property (id, type, null, null, get_src (begin), comment);
                prop.access = access;
                set_attributes (prop, attrs);
                if (ModifierFlags.STATIC in flags) {
@@ -2402,7 +2406,7 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.SIGNAL);
                var type = parse_type ();
                string id = parse_identifier ();
-               var sig = new Signal (id, type, get_src_com (begin));
+               var sig = new Signal (id, type, get_src (begin), comment);
                sig.access = access;
                set_attributes (sig, attrs);
                if (ModifierFlags.VIRTUAL in flags) {
@@ -2433,7 +2437,7 @@ public class Vala.Parser : CodeVisitor {
                if (ModifierFlags.NEW in flags) {
                        throw new ParseError.SYNTAX (get_error ("`new' modifier not allowed on constructor"));
                }
-               var c = new Constructor (get_src_com (begin));
+               var c = new Constructor (get_src (begin));
                if (ModifierFlags.STATIC in flags) {
                        c.binding = MemberBinding.STATIC;
                } else if (ModifierFlags.CLASS in flags) {
@@ -2453,7 +2457,7 @@ public class Vala.Parser : CodeVisitor {
                if (ModifierFlags.NEW in flags) {
                        throw new ParseError.SYNTAX (get_error ("`new' modifier not allowed on destructor"));
                }
-               var d = new Destructor (get_src_com (begin));
+               var d = new Destructor (get_src (begin));
                if (ModifierFlags.STATIC in flags) {
                        d.binding = MemberBinding.STATIC;
                } else if (ModifierFlags.CLASS in flags) {
@@ -2474,7 +2478,7 @@ public class Vala.Parser : CodeVisitor {
                if (accept (TokenType.COLON)) {
                        base_type = parse_type ();
                }
-               var st = new Struct (sym.name, get_src_com (begin));
+               var st = new Struct (sym.name, get_src (begin), comment);
                st.access = access;
                if (ModifierFlags.EXTERN in flags || scanner.source_file.external_package) {
                        st.external = true;
@@ -2492,6 +2496,7 @@ public class Vala.Parser : CodeVisitor {
                Symbol result = st;
                while (sym.inner != null) {
                        sym = sym.inner;
+
                        var ns = new Namespace (sym.name, st.source_reference);
                        if (result is Namespace) {
                                ns.add_namespace ((Namespace) result);
@@ -2531,7 +2536,7 @@ public class Vala.Parser : CodeVisitor {
                                base_types.add (type);
                        } while (accept (TokenType.COMMA));
                }
-               var iface = new Interface (sym.name, get_src_com (begin));
+               var iface = new Interface (sym.name, get_src (begin), comment);
                iface.access = access;
                if (ModifierFlags.EXTERN in flags || scanner.source_file.external_package) {
                        iface.external = true;
@@ -2590,7 +2595,7 @@ public class Vala.Parser : CodeVisitor {
                var flags = parse_type_declaration_modifiers ();
                expect (TokenType.ENUM);
                var sym = parse_symbol_name ();
-               var en = new Enum (sym.name, get_src_com (begin));
+               var en = new Enum (sym.name, get_src (begin), comment);
                en.access = access;
                if (ModifierFlags.EXTERN in flags || scanner.source_file.external_package) {
                        en.external = true;
@@ -2607,7 +2612,8 @@ public class Vala.Parser : CodeVisitor {
                        var value_attrs = parse_attributes ();
                        var value_begin = get_location ();
                        string id = parse_identifier ();
-                       var ev = new EnumValue (id, get_src (value_begin));
+                       comment = scanner.pop_comment ();
+                       var ev = new EnumValue (id, get_src (value_begin), comment);
                        set_attributes (ev, value_attrs);
                        if (accept (TokenType.ASSIGN)) {
                                ev.value = parse_expression ();
@@ -2648,7 +2654,7 @@ public class Vala.Parser : CodeVisitor {
                var flags = parse_type_declaration_modifiers ();
                expect (TokenType.ERRORDOMAIN);
                var sym = parse_symbol_name ();
-               var ed = new ErrorDomain (sym.name, get_src_com (begin));
+               var ed = new ErrorDomain (sym.name, get_src (begin), comment);
                ed.access = access;
                if (ModifierFlags.EXTERN in flags || scanner.source_file.external_package) {
                        ed.external = true;
@@ -2665,7 +2671,8 @@ public class Vala.Parser : CodeVisitor {
                        var code_attrs = parse_attributes ();
                        var code_begin = get_location ();
                        string id = parse_identifier ();
-                       var ec = new ErrorCode (id, get_src (code_begin));
+                       comment = scanner.pop_comment ();
+                       var ec = new ErrorCode (id, get_src (code_begin), comment);
                        set_attributes (ec, code_attrs);
                        if (accept (TokenType.ASSIGN)) {
                                ec.value = parse_expression ();
@@ -2827,9 +2834,9 @@ public class Vala.Parser : CodeVisitor {
                }
                CreationMethod method;
                if (sym.inner == null) {
-                       method = new CreationMethod (sym.name, null, get_src_com (begin));
+                       method = new CreationMethod (sym.name, null, get_src (begin), comment);
                } else {
-                       method = new CreationMethod (sym.inner.name, sym.name, get_src_com (begin));
+                       method = new CreationMethod (sym.inner.name, sym.name, get_src (begin), comment);
                }
                if (ModifierFlags.EXTERN in flags) {
                        method.external = true;
@@ -2876,7 +2883,7 @@ public class Vala.Parser : CodeVisitor {
                var type = parse_type ();
                var sym = parse_symbol_name ();
                var type_param_list = parse_type_parameter_list ();
-               var d = new Delegate (sym.name, type, get_src_com (begin));
+               var d = new Delegate (sym.name, type, get_src (begin), comment);
                d.access = access;
                set_attributes (d, attrs);
                if (ModifierFlags.STATIC in flags) {
index 64eaa8ec13a47c7a66bad6a1bcf8b77f9083d176..7b7994971cbee9e370bc51b3eca753e10f079c09 100644 (file)
@@ -199,8 +199,8 @@ public class Vala.Property : Member, Lockable {
         * @param source       reference to source code
         * @return             newly created property
         */
-       public Property (string name, DataType? property_type, PropertyAccessor? get_accessor, PropertyAccessor? set_accessor, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Property (string name, DataType? property_type, PropertyAccessor? get_accessor, PropertyAccessor? set_accessor, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
                this.property_type = property_type;
                this.get_accessor = get_accessor;
                this.set_accessor = set_accessor;
index a08bdaa9187391fea29378fc65e0cc5b5516f9e8..a38c4804c2629d184b491ac3a58d9207c68e9907 100644 (file)
@@ -35,7 +35,7 @@ public class Vala.Scanner {
        int line;
        int column;
 
-       string _comment;
+       Comment _comment;
 
        Conditional[] conditional_stack;
 
@@ -1076,7 +1076,7 @@ public class Vala.Scanner {
                return found;
        }
 
-       bool comment () {
+       bool comment (bool file_comment = false) {
                if (current > end - 2
                    || current[0] != '/'
                    || (current[1] != '/' && current[1] != '*')) {
@@ -1084,19 +1084,37 @@ public class Vala.Scanner {
                }
 
                if (current[1] == '/') {
+                       SourceReference source_reference = null;
+                       if (file_comment) {
+                               source_reference = new SourceReference (source_file, line, column, line, column);
+                       }
+
                        // single-line comment
                        current += 2;
                        char* begin = current;
+
                        // skip until end of line or end of file
                        while (current < end && current[0] != '\n') {
                                current++;
                        }
-                       push_comment (((string) begin).ndup ((long) (current - begin)), line == 1);
+
+                       if (source_reference != null) {
+                               push_comment (((string) begin).ndup ((long) (current - begin)), source_reference, file_comment);
+                       }
                } else {
-                       // delimited comment
+                       SourceReference source_reference = null;
+
+                       if (file_comment && current[2] == '*') {
+                               return false;
+                       }
+
+                       if (current[2] == '*' || file_comment) {
+                               source_reference = new SourceReference (source_file, line, column, line, column);
+                       }
+
                        current += 2;
+
                        char* begin = current;
-                       int begin_line = line;
                        while (current < end - 1
                               && (current[0] != '*' || current[1] != '/')) {
                                if (current[0] == '\n') {
@@ -1106,11 +1124,16 @@ public class Vala.Scanner {
                                current++;
                                column++;
                        }
+
                        if (current == end - 1) {
                                Report.error (new SourceReference (source_file, line, column, line, column), "syntax error, expected */");
                                return true;
                        }
-                       push_comment (((string) begin).ndup ((long) (current - begin)), begin_line == 1);
+
+                       if (source_reference != null) {
+                               push_comment (((string) begin).ndup ((long) (current - begin)), source_reference, file_comment);
+                       }
+
                        current += 2;
                        column += 2;
                }
@@ -1123,37 +1146,35 @@ public class Vala.Scanner {
                }
        }
 
-       void push_comment (string comment_item, bool file_comment) {
-               if (_comment == null) {
-                       _comment = comment_item;
-               } else {
-                       _comment = "%s\n%s".printf (_comment, comment_item);
+       public void parse_file_comments () {
+               while (whitespace () || comment (true)) {
+               }
+       }
+
+       void push_comment (string comment_item, SourceReference source_reference, bool file_comment) {
+               if (comment_item[0] == '*') {
+                       _comment = new Comment (comment_item, source_reference);
                }
+
                if (file_comment) {
-                       source_file.comment = _comment;
+                       source_file.add_comment (new Comment (comment_item, source_reference));
                        _comment = null;
                }
        }
-       
+
        /**
         * Clears and returns the content of the comment stack.
         *
         * @return saved comment
         */
-       public string? pop_comment () {
+       public Comment? pop_comment () {
                if (_comment == null) {
                        return null;
                }
-               
-               var result_builder = new StringBuilder (_comment);
+
+               var comment = _comment;
                _comment = null;
-               
-               string* index;
-               while ((index = result_builder.str.chr (-1, '\t')) != null) {
-                       result_builder.erase ((long) (index - (string*) result_builder.str), 1);
-               }
-               
-               return result_builder.str;
+               return comment;
        }
 }
 
index 79ede7374386dff2663a338ae39e57e46983096c..f51a92290091553005b967569663a259a251e330 100644 (file)
@@ -81,8 +81,8 @@ public class Vala.Signal : Member, Lockable {
         * @param source      reference to source code
         * @return            newly created signal
         */
-       public Signal (string name, DataType return_type, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Signal (string name, DataType return_type, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
                this.return_type = return_type;
        }
        
index 0ec442924e196b625654b41fb3ddfd920fad2034..c85751767da3b2c47f099ba912f60583be1f985b 100644 (file)
@@ -32,10 +32,6 @@ public class Vala.SourceFile {
         */
        public string filename { get; set; }
        
-       /**
-        * The header comment of this source file.
-        */
-       public string comment { get; set; }
        
        /**
         * Specifies whether this file is a VAPI package file.
@@ -55,6 +51,8 @@ public class Vala.SourceFile {
                }
        }
 
+       private ArrayList<Comment> comments = new ArrayList<Comment> ();
+
        private Gee.List<UsingDirective> using_directives = new ArrayList<UsingDirective> ();
 
        private Gee.List<CodeNode> nodes = new ArrayList<CodeNode> ();
@@ -81,7 +79,23 @@ public class Vala.SourceFile {
                this.context = context;
                this.content = content;
        }
-       
+
+       /**
+        * Adds a header comment to this source file.
+        */
+       public void add_comment (Comment comment) {
+               comments.add (comment);
+       }
+
+       /**
+        * Returns a copy of the list of header comments.
+        *
+        * @return list of comments
+        */
+       public Gee.List<Comment> get_comments () {
+               return new ReadOnlyList<Comment> (comments);
+       }
+
        /**
         * Adds a new using directive with the specified namespace.
         *
index fd3a0808f3c75bd542217b142c843bd9b09fbca4..4afe4d8a02d93a63deebe3a168c70be080775d38 100644 (file)
@@ -50,11 +50,6 @@ public class Vala.SourceReference {
         * The last column number of the referenced source code.
         */
        public int last_column { get; set; }
-
-       /**
-        * The text describing the referenced source code.
-        */
-       public string comment { get; set; }
        
        /**
         * Creates a new source reference.
@@ -74,26 +69,6 @@ public class Vala.SourceReference {
                last_column = _last_column;
        }
        
-       /**
-        * Creates a new commented source reference.
-        *
-        * @param file         a source file
-        * @param first_line   first line number
-        * @param first_column first column number
-        * @param last_line    last line number
-        * @param last_column  last column number
-        * @param comment      code comment
-        * @return             newly created source reference
-        */
-       public SourceReference.with_comment (SourceFile _file, int _first_line, int _first_column, int _last_line, int _last_column, string? _comment) {
-               file = _file;
-               first_line = _first_line;
-               first_column = _first_column;
-               last_line = _last_line;
-               last_column = _last_column;
-               comment = _comment;
-       }
-       
        /**
         * Returns a string representation of this source reference.
         *
index dd561834aa8967a91739825cae7f81833ef04f76..e060d8c7966add65cef16463212da93b4017d871 100644 (file)
@@ -102,8 +102,8 @@ public class Vala.Struct : TypeSymbol {
         * @param source_reference reference to source code
         * @return                 newly created struct
         */
-       public Struct (string name, SourceReference? source_reference = null) {
-               base (name, source_reference);
+       public Struct (string name, SourceReference? source_reference = null, Comment? comment = null) {
+               base (name, source_reference, comment);
        }
 
        /**
index afaa1553f330ba02f64ca1e33864ad9754266254..df328d93e8a83d46f00f67e9ab9dd49d8872e3ee 100644 (file)
@@ -29,10 +29,13 @@ using Gee;
  * code or imported from an external library with a Vala API file.
  */
 public abstract class Vala.TypeSymbol : Symbol {
+       public Comment? comment { get; set; }
+
        private Gee.List<string> cheader_filenames = new ArrayList<string> ();
 
-       public TypeSymbol (string? name, SourceReference? source_reference = null) {
+       public TypeSymbol (string? name, SourceReference? source_reference = null, Comment? comment = null) {
                base (name, source_reference);
+               this.comment = comment;
        }
 
        /**