[valadoc] Add *.valadoc format



commit 46edfd90996cc180404ae868132bb45588717846
Author: Florian Brosch <flo brosch gmail com>
Date:   Wed Sep 1 06:43:16 2010 +0200

    Add *.valadoc format

 src/libvaladoc/Makefile.am                         |    2 +
 src/libvaladoc/api/tree.vala                       |   25 ++--
 .../documentation/documentationparser.vala         |    7 +-
 src/libvaladoc/importer/documentationimporter.vala |    2 +-
 .../importer/valadocdocumentationimporter.vala     |  143 +++++++++++++++
 .../valadocdocumentationimporterscanner.vala       |  184 ++++++++++++++++++++
 src/libvaladoc/parser/tokentype.vala               |   16 ++
 src/valadoc/valadoc.vala                           |    9 +-
 8 files changed, 369 insertions(+), 19 deletions(-)
---
diff --git a/src/libvaladoc/Makefile.am b/src/libvaladoc/Makefile.am
index cde4500..e5f9102 100644
--- a/src/libvaladoc/Makefile.am
+++ b/src/libvaladoc/Makefile.am
@@ -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 \
diff --git a/src/libvaladoc/api/tree.vala b/src/libvaladoc/api/tree.vala
index 7a3d698..5d07446 100644
--- a/src/libvaladoc/api/tree.vala
+++ b/src/libvaladoc/api/tree.vala
@@ -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);
 			}
 		}
 	}
diff --git a/src/libvaladoc/documentation/documentationparser.vala b/src/libvaladoc/documentation/documentationparser.vala
index 0c735c9..82dd658 100644
--- a/src/libvaladoc/documentation/documentationparser.vala
+++ b/src/libvaladoc/documentation/documentationparser.vala
@@ -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) {
diff --git a/src/libvaladoc/importer/documentationimporter.vala b/src/libvaladoc/importer/documentationimporter.vala
index 9cdf7b0..85ba188 100644
--- a/src/libvaladoc/importer/documentationimporter.vala
+++ b/src/libvaladoc/importer/documentationimporter.vala
@@ -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
index 0000000..e895c94
--- /dev/null
+++ b/src/libvaladoc/importer/valadocdocumentationimporter.vala
@@ -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
index 0000000..26069ac
--- /dev/null
+++ b/src/libvaladoc/importer/valadocdocumentationimporterscanner.vala
@@ -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;
+	}
+}
diff --git a/src/libvaladoc/parser/tokentype.vala b/src/libvaladoc/parser/tokentype.vala
index bd53da9..4410890 100644
--- a/src/libvaladoc/parser/tokentype.vala
+++ b/src/libvaladoc/parser/tokentype.vala
@@ -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;
 		}
 	}
diff --git a/src/valadoc/valadoc.vala b/src/valadoc/valadoc.vala
index 8946dba..1c6b73e 100755
--- a/src/valadoc/valadoc.vala
+++ b/src/valadoc/valadoc.vala
@@ -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);
 		}



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