style_guide.qbk 3.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. [/==============================================================================
  2. Copyright (C) 2001-2011 Joel de Guzman
  3. Copyright (C) 2001-2011 Hartmut Kaiser
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ===============================================================================/]
  7. [section Style Guide]
  8. At some point, especially when there are lots of semantic actions attached to
  9. various points, the grammar tends to be quite difficult to follow. In order to
  10. keep an easy-to-read, consistent and aesthetically pleasing look to the Spirit
  11. code, the following coding style guide is advised.
  12. This coding style is adapted and extended from the ANTLR\/PCCTS style and
  13. [@http://www.boost.org/development/requirements.html Boost Library Requirements
  14. and Guidelines] and is the combined work of Joel de Guzman, Chris Uzdavinis,
  15. and Hartmut Kaiser.
  16. * Rule names use std C++ (Boost) convention. The rule name may be very long.
  17. * The '=' is neatly indented 4 spaces below. Like in Boost, use spaces instead
  18. of tabs.
  19. * Breaking the operands into separate lines puts the semantic actions neatly
  20. to the right.
  21. * Semicolon at the last line terminates the rule.
  22. * The adjacent parts of a sequence should be indented accordingly to have all,
  23. what belongs to one level, at one indentation level.
  24. program
  25. = program_heading [heading_action]
  26. >> block [block_action]
  27. >> '.'
  28. | another_sequence
  29. >> etc
  30. ;
  31. * Prefer literals in the grammar instead of identifiers. e.g. `"program"` instead
  32. of `PROGRAM`, `'>='` instead of `GTE` and `'.'` instead of `DOT`. This makes it much
  33. easier to read. If this isn't possible (for instance where the used tokens
  34. must be identified through integers) capitalized identifiers should be used
  35. instead.
  36. * Breaking the operands may not be needed for short expressions.
  37. e.g. `*(',' >> file_identifier)` as long as the line does not
  38. exceed 80 characters.
  39. * If a sequence fits on one line, put spaces inside the parentheses
  40. to clearly separate them from the rules.
  41. program_heading
  42. = no_case["program"]
  43. >> identifier
  44. >> '('
  45. >> file_identifier
  46. >> *( ',' >> file_identifier )
  47. >> ')'
  48. >> ';'
  49. ;
  50. * Nesting directives: If a rule does not fit on one line (80 characters)
  51. it should be continued on the next line intended by one level. The brackets
  52. of directives, semantic expressions (using Phoenix or LL lambda expressions)
  53. or parsers should be placed as follows.
  54. identifier
  55. = no_case
  56. [
  57. lexeme
  58. [
  59. alpha >> *(alnum | '_') [id_action]
  60. ]
  61. ]
  62. ;
  63. * Nesting unary operators (e.g.Kleene star): Unary rule operators
  64. (Kleene star, `'!'`, `'+'` etc.) should be moved out one space before
  65. the corresponding indentation level, if this rule has a body or a
  66. sequence after it, which does not fit on on line. This makes the
  67. formatting more consistent and moves the rule 'body' at the same
  68. indentation level as the rule itself, highlighting the unary operator.
  69. block
  70. = *( label_declaration_part
  71. | constant_definition_part
  72. | type_definition_part
  73. | variable_declaration_part
  74. | procedure_and_function_declaration_part
  75. )
  76. >> statement_part
  77. ;
  78. [endsect]