style_guide.html 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <html>
  2. <head>
  3. <title>Style Guide</title>
  4. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  5. <link rel="stylesheet" href="theme/style.css" type="text/css">
  6. </head>
  7. <body>
  8. <table width="100%" border="0" background="theme/bkd2.gif" cellspacing="2">
  9. <tr>
  10. <td width="10">
  11. </td>
  12. <td width="85%"> <font size="6" face="Verdana, Arial, Helvetica, sans-serif"><b>Style
  13. Guide </b></font></td>
  14. <td width="112"><a href="http://spirit.sf.net"><img src="theme/spirit.gif" width="112" height="48" align="right" border="0"></a></td>
  15. </tr>
  16. </table>
  17. <br>
  18. <table border="0">
  19. <tr>
  20. <td width="10"></td>
  21. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  22. <td width="30"><a href="portability.html"><img src="theme/l_arr.gif" border="0"></a></td>
  23. <td width="30"><a href="techniques.html"><img src="theme/r_arr.gif" border="0"></a></td>
  24. </tr>
  25. </table>
  26. <p> At some point, especially when there are lots of semantic actions attached
  27. to various points, the grammar tends to be quite difficult to follow. In order
  28. to keep an easy-to-read, consistent en aesthetically pleasing look to the Spirit
  29. code, the following coding styleguide is advised. </p>
  30. <p>This coding style is adapted and extended from the ANTLR/PCCTS style (Terrence
  31. Parr) and <a href="http://groups.yahoo.com/group/boost/files/coding_guidelines.html">Boost
  32. coding guidelines</a> (David Abrahams and Nathan Myers) and is the combined
  33. work of Joel de Guzman, Chris Uzdavinis and Hartmut Kaiser.</p>
  34. <ul>
  35. <li> Rule names use std C++ (Boost) convention. The rule name may be very long.</li>
  36. <li>The '=' is neatly indented 4 spaces below. Like Boost, use spaces instead
  37. of tabs. </li>
  38. <li>Breaking the operands into separate lines puts the semantic actions neatly
  39. to the right. </li>
  40. <li>Semicolon at the last line terminates the rule. </li>
  41. <li>The adjacent parts of a sequence should be indented accordingly to have
  42. all, what belongs to one level, at one indentation level.</li>
  43. </ul>
  44. <pre><span class=identifier> program
  45. </span><span class=special>= </span><span class=identifier>program_heading </span><span class=special>[</span><span class=identifier>heading_action</span><span class=special>]
  46. </span><span class=identifier> </span><span class=special> &gt;&gt; </span><span class=identifier>block </span><span class=special>[</span><span class=identifier>block_action</span><span class=special>]
  47. </span><span class=identifier> </span><span class=special> &gt;&gt; </span><span class=literal>'.'
  48. </span><span class=identifier> </span><span class=special>| </span><span class=identifier>another_sequence
  49. </span><span class=special>&gt;&gt; </span><span class=identifier>etc
  50. </span><span class=identifier> </span><span class=special>;</span></pre>
  51. <ul>
  52. <li>Prefer literals in the grammar instead of identifiers. e.g. <tt>&quot;program&quot;</tt>
  53. instead of <tt>PROGRAM</tt>, <tt>'&gt;='</tt> instead of <tt>GTE</tt> and
  54. <tt>'.' </tt>instead of <tt>DOT</tt>. This makes it much easier to read. If
  55. this isn't possible (for instance where the used tokens must be identified
  56. through integers) capitalized identifiers should be used instead. </li>
  57. <li> Breaking the operands may not be needed for short expressions. e.g. <tt>*(','
  58. &gt;&gt; file_identifier)</tt> as long as the line does not exceed 80 characters.
  59. </li>
  60. <li> If a sequence fits on one line, put spaces inside the parentheses to clearly
  61. separate them from the rules. </li>
  62. </ul>
  63. <pre> <span class=identifier>program_heading
  64. </span><span class=special>= </span><span class=identifier>as_lower_d</span><span class=special>[</span><span class=string>&quot;program&quot;</span><span class=special>]
  65. &gt;&gt; </span><span class=identifier>identifier
  66. </span><span class=special>&gt;&gt; </span><span class=literal>'('
  67. </span><span class=special>&gt;&gt; </span><span class=identifier>file_identifier
  68. </span><span class=special>&gt;&gt; *( </span><span class=literal>',' </span><span class=special>&gt;&gt; </span><span class=identifier>file_identifier </span><span class=special>)
  69. &gt;&gt; </span><span class=literal>')'
  70. </span><span class=special>&gt;&gt; </span><span class=literal>';'
  71. </span><span class=special>;</span></pre>
  72. <ul>
  73. <li> Nesting directives: If a rule does not fit on one line (80 characters)
  74. it should be continued on the next line intended by one level. </li>
  75. <li>The brackets of directives, semantic expressions (using Phoenix or LL lambda
  76. expressions) or parsers should be placed as follows. </li>
  77. </ul>
  78. <pre> <span class=identifier>identifier
  79. </span><span class=special>= </span><span class=identifier>nocase
  80. </span><span class=special>[
  81. </span><span class=identifier>lexeme
  82. </span><span class=special>[
  83. </span><span class=identifier>alpha </span><span class=special>&gt;&gt; *(</span><span class=identifier>alnum </span><span class=special>| </span><span class=literal>'_'</span><span class=special>) [</span><span class=identifier>id_action</span><span class=special>]
  84. ]
  85. ]
  86. ;</span></pre>
  87. <ul>
  88. <li> Nesting unary operators (e.g.Kleene star) </li>
  89. <li>Unary rule operators (Kleene star, <tt>'!'</tt>, <tt>'+'</tt> etc.) should
  90. be moved out one space before the corresponding indentation level, if this
  91. rule has a body or a sequence after it, which does not fit on on line. This
  92. makes the formatting more consistent and moves the rule 'body' at the same
  93. indentation level as the rule itself, highlighting the unary operator.</li>
  94. </ul>
  95. <pre><span class=special> </span><span class=identifier>block
  96. </span><span class=special>= *( </span><span class=identifier>label_declaration_part
  97. </span><span class=special>| </span><span class=identifier>constant_definition_part
  98. </span><span class=special>| </span><span class=identifier>type_definition_part
  99. </span><span class=special>| </span><span class=identifier>variable_declaration_part
  100. </span><span class=special>| </span><span class=identifier>procedure_and_function_declaration_part
  101. </span><span class=special>)
  102. &gt;&gt; </span><span class=identifier>statement_part
  103. </span><span class=special>;</span></pre>
  104. <table border="0">
  105. <tr>
  106. <td width="10"></td>
  107. <td width="30"><a href="../index.html"><img src="theme/u_arr.gif" border="0"></a></td>
  108. <td width="30"><a href="portability.html"><img src="theme/l_arr.gif" border="0"></a></td>
  109. <td width="30"><a href="techniques.html"><img src="theme/r_arr.gif" border="0"></a></td>
  110. </tr>
  111. </table>
  112. <br>
  113. <hr size="1">
  114. <p class="copyright">Copyright &copy; 2001-2003 Joel de Guzman<br>
  115. Copyright &copy; 2001-2002 Hartmut Kaiser<br>
  116. Copyright &copy; 2001-2002 Chris Uzdavinis<br>
  117. <br>
  118. <font size="2">Use, modification and distribution is subject to the Boost Software
  119. License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  120. http://www.boost.org/LICENSE_1_0.txt)</font></p>
  121. <p class="copyright">&nbsp;</p>
  122. </body>
  123. </html>