doc.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. // Copyright 2013 Google, Inc. All rights reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // Gypsy is a simplified YAML parser written in Go. It is intended to be used as
  15. // a simple configuration file, and as such does not support a lot of the more
  16. // nuanced syntaxes allowed in full-fledged YAML. YAML does not allow indent with
  17. // tabs, and GYPSY does not ever consider a tab to be a space character. It is
  18. // recommended that your editor be configured to convert tabs to spaces when
  19. // editing Gypsy config files.
  20. //
  21. // Gypsy understands the following to be a list:
  22. //
  23. // - one
  24. // - two
  25. // - three
  26. //
  27. // This is parsed as a `yaml.List`, and can be retrieved from the
  28. // `yaml.Node.List()` method. In this case, each element of the `yaml.List` would
  29. // be a `yaml.Scalar` whose value can be retrieved with the `yaml.Scalar.String()`
  30. // method.
  31. //
  32. // Gypsy understands the following to be a mapping:
  33. //
  34. // key: value
  35. // foo: bar
  36. // running: away
  37. //
  38. // A mapping is an unordered list of `key:value` pairs. All whitespace after the
  39. // colon is stripped from the value and is used for alignment purposes during
  40. // export. If the value is not a list or a map, everything after the first
  41. // non-space character until the end of the line is used as the `yaml.Scalar`
  42. // value.
  43. //
  44. // Gypsy allows arbitrary nesting of maps inside lists, lists inside of maps, and
  45. // maps and/or lists nested inside of themselves.
  46. //
  47. // A map inside of a list:
  48. //
  49. // - name: John Smith
  50. // age: 42
  51. // - name: Jane Smith
  52. // age: 45
  53. //
  54. // A list inside of a map:
  55. //
  56. // schools:
  57. // - Meadow Glen
  58. // - Forest Creek
  59. // - Shady Grove
  60. // libraries:
  61. // - Joseph Hollingsworth Memorial
  62. // - Andrew Keriman Memorial
  63. //
  64. // A list of lists:
  65. //
  66. // - - one
  67. // - two
  68. // - three
  69. // - - un
  70. // - deux
  71. // - trois
  72. // - - ichi
  73. // - ni
  74. // - san
  75. //
  76. // A map of maps:
  77. //
  78. // google:
  79. // company: Google, Inc.
  80. // ticker: GOOG
  81. // url: http://google.com/
  82. // yahoo:
  83. // company: Yahoo, Inc.
  84. // ticker: YHOO
  85. // url: http://yahoo.com/
  86. //
  87. // In the case of a map of maps, all sub-keys must be on subsequent lines and
  88. // indented equally. It is allowable for the first key/value to be on the same
  89. // line if there is more than one key/value pair, but this is not recommended.
  90. //
  91. // Values can also be expressed in long form (leading whitespace of the first line
  92. // is removed from it and all subsequent lines). In the normal (baz) case,
  93. // newlines are treated as spaces, all indentation is removed. In the folded case
  94. // (bar), newlines are treated as spaces, except pairs of newlines (e.g. a blank
  95. // line) are treated as a single newline, only the indentation level of the first
  96. // line is removed, and newlines at the end of indented lines are preserved. In
  97. // the verbatim (foo) case, only the indent at the level of the first line is
  98. // stripped. The example:
  99. //
  100. // foo: |
  101. // lorem ipsum dolor
  102. // sit amet
  103. // bar: >
  104. // lorem ipsum
  105. //
  106. // dolor
  107. //
  108. // sit amet
  109. // baz:
  110. // lorem ipsum
  111. // dolor sit amet
  112. //
  113. // The YAML subset understood by Gypsy can be expressed (loosely) in the following
  114. // grammar (not including comments):
  115. //
  116. // OBJECT = MAPPING | SEQUENCE | SCALAR .
  117. // SHORT-OBJECT = SHORT-MAPPING | SHORT-SEQUENCE | SHORT-SCALAR .
  118. // EOL = '\n'
  119. //
  120. // MAPPING = { LONG-MAPPING | SHORT-MAPPING } .
  121. // SEQUENCE = { LONG-SEQUENCE | SHORT-SEQUENCE } .
  122. // SCALAR = { LONG-SCALAR | SHORT-SCALAR } .
  123. //
  124. // LONG-MAPPING = { INDENT KEY ':' OBJECT EOL } .
  125. // SHORT-MAPPING = '{' KEY ':' SHORT-OBJECT { ',' KEY ':' SHORT-OBJECT } '}' EOL .
  126. //
  127. // LONG-SEQUENCE = { INDENT '-' OBJECT EOL } EOL .
  128. // SHORT-SEQUENCE = '[' SHORT-OBJECT { ',' SHORT-OBJECT } ']' EOL .
  129. //
  130. // LONG-SCALAR = ( '|' | '>' | ) EOL { INDENT SHORT-SCALAR EOL }
  131. // SHORT-SCALAR = { alpha | digit | punct | ' ' | '\t' } .
  132. //
  133. // KEY = { alpha | digit }
  134. // INDENT = { ' ' }
  135. //
  136. // Any line where the first non-space character is a sharp sign (#) is a comment.
  137. // It will be ignored.
  138. // Only full-line comments are allowed.
  139. package yaml
  140. // BUG(kevlar): Multi-line strings are currently not supported.