]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add *.valadoc format
authorFlorian Brosch <flo.brosch@gmail.com>
Wed, 1 Sep 2010 04:43:16 +0000 (06:43 +0200)
committerFlorian Brosch <flo.brosch@gmail.com>
Wed, 1 Sep 2010 17:32:14 +0000 (19:32 +0200)
src/libvaladoc/Makefile.am
src/libvaladoc/api/tree.vala
src/libvaladoc/documentation/documentationparser.vala
src/libvaladoc/importer/documentationimporter.vala
src/libvaladoc/importer/valadocdocumentationimporter.vala [new file with mode: 0644]
src/libvaladoc/importer/valadocdocumentationimporterscanner.vala [new file with mode: 0644]
src/libvaladoc/parser/tokentype.vala
src/valadoc/valadoc.vala

index cde45008653a03f56b8a172b7a2a580698f6163b..e5f91024a620c269d0cc991c5179a34fabd16443 100644 (file)
@@ -40,6 +40,8 @@ libvaladoc_la_VALASOURCES = \
        importer/girdocumentationimporter.vala \
        importer/documentationimporter.vala \
        importer/girdocumentationbuilder.vala \
+       importer/valadocdocumentationimporter.vala \
+       importer/valadocdocumentationimporterscanner.vala \
        api/array.vala \
        api/class.vala \
        api/constant.vala \
index 7a3d698546ffced77904f1d002bd3d1d4713bc1b..5d07446f7de4856240b893e44749e95c4edc8ba1 100644 (file)
@@ -365,15 +365,6 @@ public class Valadoc.Api.Tree {
                return true;
        }
 
-       private Package? find_package_by_name (string name) {
-               foreach (Package pkg in packages) {
-                       if (name == pkg.name) {
-                               return pkg;
-                       }
-               }
-               return null;
-       }
-
        private Package? find_package_for_file (Vala.SourceFile vfile) {
                foreach (Package pkg in this.packages) {
                        if (pkg.is_package_for_file (vfile))
@@ -424,14 +415,20 @@ public class Valadoc.Api.Tree {
                }
        }
 
-       public void import_documentation (DocumentationImporter importer, string[] packages, string[] import_directories) {
+       public void import_documentation (DocumentationImporter[] importers, string[] packages, string[] import_directories) {
                foreach (string pkg_name in packages) {
-                       string? path = get_file_path ("%s.%s".printf (pkg_name, importer.file_extension), import_directories);
+                       bool imported = false;
+                       foreach (DocumentationImporter importer in importers) {
+                               string? path = get_file_path ("%s.%s".printf (pkg_name, importer.file_extension), import_directories);
 
-                       if (path == null) {
+                               if (path != null) {
+                                       importer.process (path);
+                                       imported = true;
+                               }
+                       }
+
+                       if (imported == false) {
                                Vala.Report.error (null, "%s not found in specified import directories".printf (pkg_name));
-                       } else {
-                               importer.process (path);
                        }
                }
        }
index 0c735c94c2e1867cf41cfcdecaa42d7ea5d8cd64..82dd65891f10d5699bac42f41b1e9c28fea2c0b2 100644 (file)
@@ -74,10 +74,13 @@ public class Valadoc.DocumentationParser : Object, ResourceLocator {
        private Scanner _scanner;
 
        public Comment? parse (Api.Node element, Vala.Comment source_comment) {
-               weak string content = source_comment.content;
                var source_ref = source_comment.source_reference;
+               return parse_comment_str (element, source_comment.content, source_ref.file.filename, source_ref.first_line, source_ref.first_column);
+       }
+
+       public Comment? parse_comment_str (Api.Node element, string content, string filename, int first_line, int first_column) {
                try {
-                       Comment doc_comment = parse_comment (content, source_ref.file.filename, source_ref.first_line, source_ref.first_column);
+                       Comment doc_comment = parse_comment (content, filename, first_line, first_column);
                        doc_comment.check (_tree, element, _reporter, _settings);
                        return doc_comment;
                } catch (ParserError error) {
index 9cdf7b04fee033a205379404ec4c5d6533104dee..85ba188f285f434052984aa4a2d1e7f7e1485299 100644 (file)
@@ -1,6 +1,6 @@
 /* resourcelocator.vala
  *
- * Copyright (C) 2008-2009 Florian Brosch, Didier Villevalois
+ * 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
diff --git a/src/libvaladoc/importer/valadocdocumentationimporter.vala b/src/libvaladoc/importer/valadocdocumentationimporter.vala
new file mode 100644 (file)
index 0000000..e895c94
--- /dev/null
@@ -0,0 +1,143 @@
+/* resourcelocator.vala
+ *
+ * Copyright (C) 2010 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 Gee;
+using Valadoc;
+using Valadoc.Content;
+
+public class Valadoc.Importer.ValadocDocumentationImporter : DocumentationImporter, ResourceLocator {
+       public override string file_extension { get { return "valadoc"; } }
+
+       private ValadoDocumentationScanner _scanner;
+       private DocumentationParser _doc_parser;
+       private Parser _parser;
+
+       private MappedFile _mapped_file;
+       private string _filename;
+       private string _cname;
+       private StringBuilder _comment;
+       private SourceLocation _comment_location;
+
+       private ErrorReporter reporter;
+
+       public ValadocDocumentationImporter (Api.Tree tree, DocumentationParser parser, ModuleLoader modules, Settings settings, ErrorReporter reporter) {
+               base (tree, modules, settings);
+               this.reporter = reporter;
+
+               _scanner = new ValadoDocumentationScanner (settings);
+               _doc_parser = parser;
+
+               _scanner = new ValadoDocumentationScanner (settings);
+               _parser = new Parser (settings, _scanner, reporter);
+               _scanner.set_parser (_parser);
+
+               _comment = new StringBuilder ();
+               
+               // init parser rules:
+               Rule unprinted_spaces = Rule.many ({
+                       Rule.one_of ({
+                               TokenType.VALADOC_SPACE,
+                               TokenType.VALADOC_TAB
+                       })
+               });
+
+               Rule empty_lines = Rule.many ({
+                       Rule.one_of ({
+                               unprinted_spaces,
+                               TokenType.VALADOC_EOL
+                       })
+               })
+               .set_name ("EmptyLines");
+
+               Rule optional_empty_lines = Rule.option ({
+                       empty_lines
+               });
+
+               Rule documentation = Rule.seq ({
+                       TokenType.ANY_WORD.action ((token) => { _cname = token.to_string (); }),
+                       optional_empty_lines,
+                       TokenType.VALADOC_COMMENT_START.action ((token) => { _comment_location = token.end; }),
+                       Rule.many ({
+                               Rule.one_of ({
+                                       TokenType.ANY_WORD.action ((token) => { _comment.append (token.to_string ()); }),
+                                       TokenType.VALADOC_COMMENT_START.action ((token) => { _comment.append (token.to_string ()); }),
+                                       TokenType.VALADOC_SPACE.action ((token) => { _comment.append (token.to_string ()); }),
+                                       TokenType.VALADOC_TAB.action ((token) => { _comment.append (token.to_string ()); }),
+                                       TokenType.VALADOC_EOL.action ((token) => { _comment.append (token.to_string ()); })
+                               })
+                       }),
+                       TokenType.VALADOC_COMMENT_END
+               })
+               .set_name ("Documentation")
+               .set_reduce (() => {
+                       add_documentation (_cname, _comment, _filename, _comment_location);
+                       _comment.erase ();
+                       _cname = null;
+               });
+
+               Rule file = Rule.many ({
+                       optional_empty_lines,
+                       documentation,
+                       optional_empty_lines
+               })
+               .set_name ("ValadocFile");
+
+               _parser.set_root_rule (file);
+       }
+
+       private void add_documentation (string symbol_name, StringBuilder comment, string filename, SourceLocation src_ref) {
+               Api.Node? symbol = null;
+
+               if (symbol_name.has_prefix ("c::")) {
+                       symbol = tree.search_symbol_cstr (symbol_name.offset (3));
+               } else {
+                       symbol = tree.search_symbol_str (null, symbol_name);
+               }
+
+               if (symbol == null) {
+                       reporter.simple_warning ("%s does not exist".printf (symbol_name));
+               } else {
+                       var docu = _doc_parser.parse_comment_str (symbol, comment.str, filename, src_ref.line, src_ref.column);
+                       if (docu != null) {
+                               symbol.documentation = docu;
+                       }
+               }
+       }
+
+       public override void process (string filename) {
+               try {
+                       _filename = filename;
+                       _mapped_file = new MappedFile (filename, false);
+                       _parser.parse ((string) _mapped_file.get_contents (), filename, 0, 0);
+               } catch (FileError err) {
+                       reporter.simple_error ("Unable to map file `%s': %s".printf (filename, err.message));
+               } catch (ParserError err) {
+               }
+       }
+
+       public string resolve (string path) {
+               return path;
+       }
+}
+
+
diff --git a/src/libvaladoc/importer/valadocdocumentationimporterscanner.vala b/src/libvaladoc/importer/valadocdocumentationimporterscanner.vala
new file mode 100644 (file)
index 0000000..26069ac
--- /dev/null
@@ -0,0 +1,184 @@
+/* valadodocumentationscanner.vala
+ *
+ * Copyright (C) 2010 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>
+ */
+
+public class Valadoc.Importer.ValadoDocumentationScanner : Object, Scanner {
+
+       public ValadoDocumentationScanner (Settings settings) {
+               _settings = settings;
+       }
+
+       private Settings _settings;
+       private Parser _parser;
+
+       private string _content;
+       private int _index;
+       private bool _stop;
+       private int _last_index;
+       private int _last_line;
+       private int _last_column;
+       private int _line;
+       private int _column;
+       private unichar _last_char;
+       private int _skip;
+       private StringBuilder _current_string = new StringBuilder ();
+
+       public void set_parser (Parser parser) {
+               _parser = parser;
+       }
+
+       public virtual void reset () {
+               _stop = false;
+               _last_index = 0;
+               _last_line = 0;
+               _last_column = 0;
+               _line = 0;
+               _column = 0;
+               _last_char = 0;
+               _skip = 0;
+               _current_string.erase (0, -1);
+       }
+
+       public void scan (string _content) throws ParserError {
+               this._content = _content;
+               for (_index = 0; !_stop && _index < _content.length; _index++) {
+                       unichar c = _content[_index];
+                       accept (c);
+               }
+       }
+
+       public void end () throws ParserError {
+               emit_token (TokenType.EOF);
+       }
+
+       public virtual void stop () {
+               _stop = true;
+       }
+
+       public int get_line () {
+               return _line;
+       }
+
+       public virtual string get_line_content () {
+               int i = _index;
+               while (i > 0 && _content[i-1] != '\n') {
+                       i--;
+               }
+               StringBuilder builder = new StringBuilder ();
+               while (i < _content.length && _content[i] != '\n') {
+                       unichar c = _content[i++];
+                       if (c == '\t') {
+                               builder.append (" ");
+                       } else {
+                               builder.append_unichar (c);
+                       }
+               }
+               return builder.str;
+       }
+
+       protected unichar get_next_char (int offset = 1) {
+               return _content[_index + offset];
+       }
+
+       protected void accept (unichar c) throws ParserError {
+               _column++;
+               if (_skip == 0) {
+                       switch (c) {
+                       case '/':
+                               if (get_next_char (1) == '*') {
+                                       emit_token (TokenType.VALADOC_COMMENT_START);
+                                       _skip = 1;
+                               } else {
+                                       append_char (c);
+                               }
+                               break;
+
+                       case '*':
+                               if (get_next_char (1) == '/') {
+                                       emit_token (TokenType.VALADOC_COMMENT_END);
+                                       _skip = 1;
+                               } else {
+                                       append_char (c);
+                               }
+                               break;
+
+                       case '\t':
+                               emit_token (TokenType.VALADOC_TAB);
+                               break;
+
+                       case ' ':
+                               emit_token (TokenType.VALADOC_SPACE);
+                               break;
+
+                       case '\n':
+                               emit_token (TokenType.VALADOC_EOL);
+                               _line++;
+                               _column = 0;
+                               _last_column = 0;
+                               break;
+
+                       default:
+                               append_char (c);
+                               break;
+                       }
+               } else {
+                       _skip--;
+               }
+               _last_char = c;
+       }
+
+       private void append_char (unichar c) {
+               _current_string.append_unichar (c);
+       }
+
+       public virtual int get_line_start_column () {
+               return 0;
+       }
+
+       private SourceLocation get_begin () {
+               return SourceLocation (_last_line, get_line_start_column () + _last_column);
+       }
+
+       private SourceLocation get_end (int offset = 0) {
+               return SourceLocation (_line, get_line_start_column () + _column + offset);
+       }
+
+       private void emit_current_word () throws ParserError {
+               if (_current_string.len > 0) {
+                       _parser.accept_token (new Token.from_word (_current_string.str, get_begin (), get_end (-1)));
+                       _current_string.erase (0, -1);
+
+                       _last_index = _index;
+                       _last_line = _line;
+                       _last_column = _column - 1;
+               }
+       }
+
+       private void emit_token (TokenType type) throws ParserError {
+               emit_current_word ();
+
+               _parser.accept_token (new Token.from_type (type, get_begin (), get_end (_skip)));
+
+               _last_index = _index;
+               _last_line = _line;
+               _last_column = _column;
+       }
+}
index bd53da9f6602fd42a1e3dbbccea2e1144c62551f..44108906d94debcba8396f3fd546d8042a039363 100644 (file)
@@ -118,6 +118,14 @@ public class Valadoc.TokenType : Object {
        public static TokenType GTKDOC_MEMBER_ELEMENT_OPEN;
        public static TokenType GTKDOC_DOT;
 
+       // .valadoc
+       public static TokenType VALADOC_COMMENT_START;
+       public static TokenType VALADOC_COMMENT_END;
+       public static TokenType VALADOC_ANY_WORD;
+       public static TokenType VALADOC_SPACE;
+       public static TokenType VALADOC_TAB;
+       public static TokenType VALADOC_EOL;
+
        private static bool initialized = false;
 
        internal static void init_token_types () {
@@ -215,6 +223,14 @@ public class Valadoc.TokenType : Object {
                        GTKDOC_SPACE = SPACE;
                        GTKDOC_EOF = EOF;
 
+                       VALADOC_COMMENT_START = new TokenType.basic ("/*");
+                       VALADOC_COMMENT_END = new TokenType.basic ("*/");
+                       VALADOC_ANY_WORD = ANY_WORD;
+                       VALADOC_SPACE = SPACE;
+                       VALADOC_TAB = TAB;
+                       VALADOC_EOL = EOL;
+
+
                        initialized = true;
                }
        }
index 8946dba0d6d67e5d515038dedcb86bb839fb06d7..1c6b73ec7d73e72e1f42bed38b29065c057ae97e 100755 (executable)
@@ -209,8 +209,13 @@ public class ValaDoc : Object {
                        return quit (reporter);
                }
 
-               var gir_importer = new GirDocumentationImporter (doctree, docparser, modules, settings);
-               doctree.import_documentation (gir_importer, import_packages, import_directories);
+
+               DocumentationImporter[] importers = {
+                       new GirDocumentationImporter (doctree, docparser, modules, settings),
+                       new ValadocDocumentationImporter (doctree, docparser, modules, settings, reporter)
+               };
+
+               doctree.import_documentation (importers, import_packages, import_directories);
                if (reporter.errors > 0) {
                        return quit (reporter);
                }