Guest User

Untitled

a guest
Apr 15th, 2018
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Vala 17.81 KB | None | 0 0
  1. From 550c36044c98d8c2bfbfc9f6cf3cb7b7737e312f Mon Sep 17 00:00:00 2001
  2. From: Florian Brosch <flo.brosch@gmail.com>
  3. Date: Tue, 29 Nov 2011 01:56:46 +0100
  4. Subject: [PATCH] Add support for gir-comments
  5.  
  6. ---
  7.  gee/Makefile.am          |    1 +
  8.  gee/hashmap.vala         |   47 ++++++++++++++++++
  9.  gee/map.vala             |    9 +++
  10.  gee/mapiterator.vala     |   52 +++++++++++++++++++
  11.  vala/Makefile.am         |    1 +
  12.  vala/valacomment.vala    |    2 +-
  13.  vala/valagircomment.vala |   50 +++++++++++++++++++
  14.  vala/valagirparser.vala  |  122 ++++++++++++++++++++++++++++++++++++++++++---
  15.  8 files changed, 274 insertions(+), 10 deletions(-)
  16.  create mode 100644 gee/mapiterator.vala
  17.  create mode 100644 vala/valagircomment.vala
  18.  
  19. diff --git a/gee/Makefile.am b/gee/Makefile.am
  20. index ab31565..f9ac2fc 100644
  21. --- a/gee/Makefile.am
  22. +++ b/gee/Makefile.am
  23. @@ -17,6 +17,7 @@ libgee_la_VALASOURCES = \
  24.     hashmap.vala \
  25.     hashset.vala \
  26.     iterable.vala \
  27. +   mapiterator.vala \
  28.     iterator.vala \
  29.     list.vala \
  30.     map.vala \
  31. diff --git a/gee/hashmap.vala b/gee/hashmap.vala
  32. index 5159fe3..1fb2ed3 100644
  33. --- a/gee/hashmap.vala
  34. +++ b/gee/hashmap.vala
  35. @@ -74,6 +74,10 @@ public class Vala.HashMap<K,V> : Map<K,V> {
  36.         return new ValueCollection<K,V> (this);
  37.     }
  38.  
  39. +   public override Vala.MapIterator<K,V> map_iterator () {
  40. +       return new MapIterator<K,V> (this);
  41. +   }
  42. +
  43.     private Node<K,V>** lookup_node (K key) {
  44.         uint hash_value = _key_hash_func (key);
  45.         Node<K,V>** node = &_nodes[hash_value % _array_size];
  46. @@ -224,6 +228,49 @@ public class Vala.HashMap<K,V> : Map<K,V> {
  47.         }
  48.     }
  49.  
  50. +   private class MapIterator<K,V> : Vala.MapIterator<K, V> {
  51. +       public HashMap<K,V> map {
  52. +           set {
  53. +               _map = value;
  54. +               _stamp = _map._stamp;
  55. +           }
  56. +       }
  57. +
  58. +       private HashMap<K,V> _map;
  59. +       private int _index = -1;
  60. +       private weak Node<K,V> _node;
  61. +
  62. +       // concurrent modification protection
  63. +       private int _stamp;
  64. +
  65. +       public MapIterator (HashMap map) {
  66. +           this.map = map;
  67. +       }
  68. +
  69. +       public override bool next () {
  70. +           if (_node != null) {
  71. +               _node = _node.next;
  72. +           }
  73. +           while (_node == null && _index + 1 < _map._array_size) {
  74. +               _index++;
  75. +               _node = _map._nodes[_index];
  76. +           }
  77. +           return (_node != null);
  78. +       }
  79. +
  80. +       public override K? get_key () {
  81. +           assert (_stamp == _map._stamp);
  82. +           assert (_node != null);
  83. +           return _node.key;
  84. +       }
  85. +
  86. +       public override V? get_value () {
  87. +           assert (_stamp == _map._stamp);
  88. +           assert (_node != null);
  89. +           return _node.value;
  90. +       }
  91. +   }
  92. +
  93.     private class KeyIterator<K,V> : Iterator<K> {
  94.         public HashMap<K,V> map {
  95.             set {
  96. diff --git a/gee/map.vala b/gee/map.vala
  97. index e78f794..2c96a3d 100644
  98. --- a/gee/map.vala
  99. +++ b/gee/map.vala
  100. @@ -84,5 +84,14 @@ public abstract class Vala.Map<K,V> {
  101.      * read-only collections.
  102.      */
  103.     public abstract void clear ();
  104. +
  105. +   /**
  106. +    * Returns a Iterator that can be used for simple iteration over a
  107. +    * map.
  108. +    *
  109. +    * @return a Iterator that can be used for simple iteration over a
  110. +    *         map
  111. +    */
  112. +   public abstract MapIterator<K,V> map_iterator ();
  113.  }
  114.  
  115. diff --git a/gee/mapiterator.vala b/gee/mapiterator.vala
  116. new file mode 100644
  117. index 0000000..78243c2
  118. --- /dev/null
  119. +++ b/gee/mapiterator.vala
  120. @@ -0,0 +1,52 @@
  121. +/* mapiterator.vala
  122. + *
  123. + * Copyright (C) 2011  Florian Brosch
  124. + *
  125. + * This library is free software; you can redistribute it and/or
  126. + * modify it under the terms of the GNU Lesser General Public
  127. + * License as published by the Free Software Foundation; either
  128. + * version 2.1 of the License, or (at your option) any later version.
  129. +
  130. + * This library is distributed in the hope that it will be useful,
  131. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  132. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  133. + * Lesser General Public License for more details.
  134. +
  135. + * You should have received a copy of the GNU Lesser General Public
  136. + * License along with this library; if not, write to the Free Software
  137. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  138. + *
  139. + * Author:
  140. + *     Florian Brosch <flo.brosch@gmail.com>
  141. + */
  142. +
  143. +
  144. +
  145. +/**
  146. + * An iterator over a map.
  147. + */
  148. +public abstract class Vala.MapIterator<K,V> {
  149. +   /**
  150. +    * Advances to the next element in the iteration.
  151. +    *
  152. +    * @return true if the iterator has a next element
  153. +    */
  154. +   public abstract bool next ();
  155. +
  156. +   /**
  157. +    * Returns the current key in the iteration.
  158. +    *
  159. +    * @return the current key in the iteration
  160. +    */
  161. +   public abstract K get_key ();
  162. +
  163. +   /**
  164. +    * Returns the current value in the iteration.
  165. +    *
  166. +    * @return the current value in the iteration
  167. +    */
  168. +   public abstract V get_value ();
  169. +}
  170. +
  171. +
  172. +
  173. diff --git a/vala/Makefile.am b/vala/Makefile.am
  174. index 936a2cb..999a63a 100644
  175. --- a/vala/Makefile.am
  176. +++ b/vala/Makefile.am
  177. @@ -40,6 +40,7 @@ libvalacore_la_VALASOURCES = \
  178.     valacodevisitor.vala \
  179.     valacodewriter.vala \
  180.     valacomment.vala \
  181. +   valagircomment.vala \
  182.     valaconditionalexpression.vala \
  183.     valaconstant.vala \
  184.     valaconstructor.vala \
  185. diff --git a/vala/valacomment.vala b/vala/valacomment.vala
  186. index 181d8a1..0e48f89 100644
  187. --- a/vala/valacomment.vala
  188. +++ b/vala/valacomment.vala
  189. @@ -34,7 +34,7 @@ public class Vala.Comment {
  190.     /**
  191.      * The text describing the referenced source code.
  192.      */
  193. -   public virtual string content { set; get; }
  194. +   public string content { set; get; }
  195.  
  196.     /**
  197.      * References the location in the source file where this code node has
  198. diff --git a/vala/valagircomment.vala b/vala/valagircomment.vala
  199. new file mode 100644
  200. index 0000000..10c1a76
  201. --- /dev/null
  202. +++ b/vala/valagircomment.vala
  203. @@ -0,0 +1,50 @@
  204. +/* valagircomment.vala
  205. + *
  206. + * Copyright (C) 2011  Florian Brosch
  207. + *
  208. + * This library is free software; you can redistribute it and/or
  209. + * modify it under the terms of the GNU Lesser General Public
  210. + * License as published by the Free Software Foundation; either
  211. + * version 2.1 of the License, or (at your option) any later version.
  212. + *
  213. + * This library is distributed in the hope that it will be useful,
  214. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  215. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  216. + * Lesser General Public License for more details.
  217. + *
  218. + * You should have received a copy of the GNU Lesser General Public
  219. + * License along with this library; if not, write to the Free Software
  220. + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
  221. + *
  222. + * Author:
  223. + *     Florian Brosch <flo.brosch@gmail.com>
  224. + */
  225. +
  226. +using GLib;
  227. +
  228. +
  229. +/**
  230. + * A documentation comment used by valadoc
  231. + */
  232. +public class Vala.GirComment : Comment {
  233. +   private HashMap<string, Comment> parameter_content = new HashMap<string, Comment> ();
  234. +
  235. +   public MapIterator<string, Comment> parameter_iterator () {
  236. +       return this.parameter_content.map_iterator ();
  237. +   }
  238. +
  239. +   public Comment? return_content { get; set; }
  240. +
  241. +   public GirComment (string? comment, SourceReference _source_reference) {
  242. +       base (comment ?? "", _source_reference);
  243. +   }
  244. +
  245. +   internal void add_content_for_parameter (string name, Comment comment) {
  246. +       parameter_content.set (name, comment);
  247. +   }
  248. +
  249. +   public Comment? get_content_for_parameter (string name) {
  250. +       return parameter_content.get (name);
  251. +   }
  252. +}
  253. +
  254. diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
  255. index 21b63d7..1aca142 100644
  256. --- a/vala/valagirparser.vala
  257. +++ b/vala/valagirparser.vala
  258. @@ -489,6 +489,7 @@ public class Vala.GirParser : CodeVisitor {
  259.         public ArrayList<Node> members = new ArrayList<Node> (); // guarantees fields order
  260.         public HashMap<string, ArrayList<Node>> scope = new HashMap<string, ArrayList<Node>> (str_hash, str_equal);
  261.  
  262. +       public GirComment comment;
  263.         public Symbol symbol;
  264.         public bool new_symbol;
  265.         public bool merged;
  266. @@ -1166,10 +1167,6 @@ public class Vala.GirParser : CodeVisitor {
  267.  
  268.     void next () {
  269.         current_token = reader.read_token (out begin, out end);
  270. -
  271. -       // Skip *all* <doc> tags
  272. -       if (current_token == MarkupTokenType.START_ELEMENT && reader.name == "doc")
  273. -           skip_element();
  274.     }
  275.  
  276.     void start_element (string name) {
  277. @@ -1923,6 +1920,13 @@ public class Vala.GirParser : CodeVisitor {
  278.         // not enough information, symbol will be created while processing the tree
  279.  
  280.         next ();
  281. +
  282. +       if (current.comment == null) {
  283. +           current.comment = parse_symbol_doc ();
  284. +       } else {
  285. +           parse_symbol_doc ();
  286. +       }
  287. +
  288.         bool no_array_length = false;
  289.         current.base_type = element_get_type (parse_type (null, null, true), true, ref no_array_length);
  290.  
  291. @@ -1949,6 +1953,44 @@ public class Vala.GirParser : CodeVisitor {
  292.         }
  293.     }
  294.  
  295. +   GirComment? parse_symbol_doc () {
  296. +       if (reader.name != "doc") {
  297. +           return null;
  298. +       }
  299. +
  300. +       start_element ("doc");
  301. +       next ();
  302. +
  303. +       GirComment? comment = null;
  304. +
  305. +       if (current_token == MarkupTokenType.TEXT) {
  306. +           comment = new GirComment (reader.content, current.source_reference);
  307. +           next ();
  308. +       }
  309. +
  310. +       end_element ("doc");
  311. +       return comment;
  312. +   }
  313. +
  314. +   Comment? parse_doc () {
  315. +       if (reader.name != "doc") {
  316. +           return null;
  317. +       }
  318. +
  319. +       start_element ("doc");
  320. +       next ();
  321. +
  322. +       Comment? comment = null;
  323. +
  324. +       if (current_token == MarkupTokenType.TEXT) {
  325. +           comment = new Comment (reader.content, current.source_reference);
  326. +           next ();
  327. +       }
  328. +
  329. +       end_element ("doc");
  330. +       return comment;
  331. +   }
  332. +
  333.     void parse_enumeration (string element_name = "enumeration", bool error_domain = false) {
  334.         start_element (element_name);
  335.         push_node (element_get_name (), true);
  336. @@ -1974,7 +2016,9 @@ public class Vala.GirParser : CodeVisitor {
  337.         string common_prefix = null;
  338.  
  339.         next ();
  340. -      
  341. +
  342. +       sym.comment = parse_symbol_doc ();
  343. +
  344.         while (current_token == MarkupTokenType.START_ELEMENT) {
  345.             if (!push_metadata ()) {
  346.                 skip_element ();
  347. @@ -2024,6 +2068,8 @@ public class Vala.GirParser : CodeVisitor {
  348.         current.symbol = ev;
  349.         next ();
  350.  
  351. +       ev.comment = parse_symbol_doc ();
  352. +
  353.         pop_node ();
  354.         end_element ("member");
  355.     }
  356. @@ -2042,16 +2088,21 @@ public class Vala.GirParser : CodeVisitor {
  357.         current.symbol = ec;
  358.         next ();
  359.  
  360. +       ec.comment = parse_symbol_doc ();
  361. +
  362.         pop_node ();
  363.         end_element ("member");
  364.     }
  365.  
  366. -   DataType parse_return_value (out string? ctype = null, out int array_length_idx = null, out bool no_array_length = null, out bool array_null_terminated = null) {
  367. +   DataType parse_return_value (out string? ctype = null, out int array_length_idx = null, out bool no_array_length = null, out bool array_null_terminated = null, out Comment? comment = null) {
  368.         start_element ("return-value");
  369.  
  370.         string transfer = reader.get_attribute ("transfer-ownership");
  371.         string allow_none = reader.get_attribute ("allow-none");
  372.         next ();
  373. +
  374. +       comment = parse_doc ();
  375. +
  376.         var transfer_elements = transfer != "container";
  377.         var type = parse_type (out ctype, out array_length_idx, transfer_elements, out no_array_length, out array_null_terminated);
  378.         if (transfer == "full" || transfer == "container") {
  379. @@ -2065,7 +2116,7 @@ public class Vala.GirParser : CodeVisitor {
  380.         return type;
  381.     }
  382.  
  383. -   Parameter parse_parameter (out int array_length_idx = null, out int closure_idx = null, out int destroy_idx = null, out string? scope = null, string? default_name = null) {
  384. +   Parameter parse_parameter (out int array_length_idx = null, out int closure_idx = null, out int destroy_idx = null, out string? scope = null, out Comment? comment, string? default_name = null) {
  385.         Parameter param;
  386.  
  387.         array_length_idx = -1;
  388. @@ -2107,6 +2158,9 @@ public class Vala.GirParser : CodeVisitor {
  389.         }
  390.  
  391.         next ();
  392. +
  393. +       comment = parse_doc ();
  394. +
  395.         if (reader.name == "varargs") {
  396.             start_element ("varargs");
  397.             next ();
  398. @@ -2349,6 +2403,9 @@ public class Vala.GirParser : CodeVisitor {
  399.  
  400.         bool first_field = true;
  401.         next ();
  402. +
  403. +       st.comment = parse_symbol_doc ();
  404. +
  405.         while (current_token == MarkupTokenType.START_ELEMENT) {
  406.             if (!push_metadata ()) {
  407.                 if (first_field && reader.name == "field") {
  408. @@ -2408,6 +2465,9 @@ public class Vala.GirParser : CodeVisitor {
  409.         cl.external = true;
  410.  
  411.         next ();
  412. +
  413. +       cl.comment = parse_symbol_doc ();
  414. +
  415.         var first_field = true;
  416.         while (current_token == MarkupTokenType.START_ELEMENT) {
  417.             if (!push_metadata ()) {
  418. @@ -2486,6 +2546,9 @@ public class Vala.GirParser : CodeVisitor {
  419.  
  420.  
  421.         next ();
  422. +
  423. +       iface.comment = parse_symbol_doc ();
  424. +
  425.         while (current_token == MarkupTokenType.START_ELEMENT) {
  426.             if (!push_metadata ()) {
  427.                 skip_element ();
  428. @@ -2528,12 +2591,16 @@ public class Vala.GirParser : CodeVisitor {
  429.  
  430.         string allow_none = reader.get_attribute ("allow-none");
  431.         next ();
  432. +
  433. +       var comment = parse_symbol_doc ();
  434. +
  435.         var type = parse_type ();
  436.         bool no_array_length = true;
  437.         type = element_get_type (type, true, ref no_array_length);
  438.  
  439.         var field = new Field (current.name, type, null, current.source_reference);
  440.         field.access = SymbolAccessibility.PUBLIC;
  441. +       field.comment = comment;
  442.         if (type is ArrayType) {
  443.             if (no_array_length) {
  444.                 field.set_attribute_bool ("CCode", "array_length", false);
  445. @@ -2554,11 +2621,15 @@ public class Vala.GirParser : CodeVisitor {
  446.         push_node (element_get_name().replace ("-", "_"), false);
  447.  
  448.         next ();
  449. +
  450. +       var comment = parse_symbol_doc ();
  451. +
  452.         bool no_array_length;
  453.         bool array_null_terminated;
  454.         var type = parse_type (null, null, false, out no_array_length, out array_null_terminated);
  455.         type = element_get_type (type, true, ref no_array_length);
  456.         var prop = new Property (current.name, type, null, null, current.source_reference);
  457. +       prop.comment = comment;
  458.         prop.access = SymbolAccessibility.PUBLIC;
  459.         prop.external = true;
  460.         if (current.parent.symbol is Interface) {
  461. @@ -2612,13 +2683,23 @@ public class Vala.GirParser : CodeVisitor {
  462.         string invoker = reader.get_attribute ("invoker");
  463.  
  464.         next ();
  465. +
  466. +       var comment = parse_symbol_doc ();
  467. +
  468.         DataType return_type;
  469.         string return_ctype = null;
  470.         int return_array_length_idx = -1;
  471.         bool return_no_array_length = false;
  472.         bool return_array_null_terminated = false;
  473.         if (current_token == MarkupTokenType.START_ELEMENT && reader.name == "return-value") {
  474. -           return_type = parse_return_value (out return_ctype, out return_array_length_idx, out return_no_array_length, out return_array_null_terminated);
  475. +           Comment? return_comment;
  476. +           return_type = parse_return_value (out return_ctype, out return_array_length_idx, out return_no_array_length, out return_array_null_terminated, out return_comment);
  477. +           if (return_comment != null) {
  478. +               if (comment == null) {
  479. +                   comment = new GirComment (null, current.source_reference);
  480. +               }
  481. +               comment.return_content = return_comment;
  482. +           }
  483.         } else {
  484.             return_type = new VoidType ();
  485.         }
  486. @@ -2652,6 +2733,7 @@ public class Vala.GirParser : CodeVisitor {
  487.         }
  488.  
  489.         s.access = SymbolAccessibility.PUBLIC;
  490. +       s.comment = comment;
  491.         s.external = true;
  492.  
  493.         if (s is Method) {
  494. @@ -2739,8 +2821,9 @@ public class Vala.GirParser : CodeVisitor {
  495.                 int array_length_idx, closure_idx, destroy_idx;
  496.                 string scope;
  497.                 string default_param_name = null;
  498. +               Comment? param_comment;
  499.                 default_param_name = "arg%d".printf (parameters.size);
  500. -               var param = parse_parameter (out array_length_idx, out closure_idx, out destroy_idx, out scope, default_param_name);
  501. +               var param = parse_parameter (out array_length_idx, out closure_idx, out destroy_idx, out scope, out param_comment, default_param_name);
  502.                 if (array_length_idx != -1) {
  503.                     current.array_length_parameters.add (array_length_idx);
  504.                 }
  505. @@ -2750,6 +2833,14 @@ public class Vala.GirParser : CodeVisitor {
  506.                 if (destroy_idx != -1) {
  507.                     current.destroy_parameters.add (destroy_idx);
  508.                 }
  509. +               if (param_comment != null) {
  510. +                   if (comment == null) {
  511. +                       comment = new GirComment (null, s.source_reference);
  512. +                       s.comment = comment;
  513. +                   }
  514. +
  515. +                   comment.add_content_for_parameter ((param.ellipsis)? "..." : param.name, param_comment);
  516. +               }
  517.  
  518.                 var info = new ParameterInfo (param, array_length_idx, closure_idx, destroy_idx);
  519.  
  520. @@ -2808,6 +2899,9 @@ public class Vala.GirParser : CodeVisitor {
  521.         cl.external = true;
  522.  
  523.         next ();
  524. +
  525. +       cl.comment = parse_symbol_doc ();
  526. +
  527.         while (current_token == MarkupTokenType.START_ELEMENT) {
  528.             if (!push_metadata ()) {
  529.                 skip_element ();
  530. @@ -2852,6 +2946,9 @@ public class Vala.GirParser : CodeVisitor {
  531.         st.external = true;
  532.  
  533.         next ();
  534. +
  535. +       st.comment = parse_symbol_doc ();
  536. +
  537.         while (current_token == MarkupTokenType.START_ELEMENT) {
  538.             if (!push_metadata ()) {
  539.                 skip_element ();
  540. @@ -2886,10 +2983,14 @@ public class Vala.GirParser : CodeVisitor {
  541.         push_node (element_get_name (), false);
  542.  
  543.         next ();
  544. +
  545. +       var comment = parse_symbol_doc ();
  546. +
  547.         var type = parse_type ();
  548.         var c = new Constant (current.name, type, null, current.source_reference);
  549.         current.symbol = c;
  550.         c.access = SymbolAccessibility.PUBLIC;
  551. +       c.comment = comment;
  552.         c.external = true;
  553.  
  554.         pop_node ();
  555. @@ -3016,6 +3117,7 @@ public class Vala.GirParser : CodeVisitor {
  556.                 // threat target="none" as a new struct
  557.                 st.base_type = base_type;
  558.             }
  559. +           st.comment = alias.comment;
  560.             st.external = true;
  561.             st.set_simple_type (simple_type);
  562.             alias.symbol = st;
  563. @@ -3025,6 +3127,7 @@ public class Vala.GirParser : CodeVisitor {
  564.             if (base_type != null) {
  565.                 cl.add_base_type (base_type);
  566.             }
  567. +           cl.comment = alias.comment;
  568.             cl.external = true;
  569.             alias.symbol = cl;
  570.         }
  571. @@ -3295,6 +3398,7 @@ public class Vala.GirParser : CodeVisitor {
  572.             finish_method_node.process (this);
  573.             var finish_method = (Method) finish_method_node.symbol;
  574.             if (finish_method is CreationMethod) {
  575. +// TODO
  576.                 method = new CreationMethod (((CreationMethod) finish_method).class_name, null, m.source_reference);
  577.                 method.access = m.access;
  578.                 method.binding = m.binding;
  579. --
  580. 1.7.7.4
Add Comment
Please, Sign In to add comment