| 1 | // Copyright 2018 Google LLC |
| 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 | |
| 15 | // Package common defines types and utilities common to expression parsing, |
| 16 | // checking, and interpretation |
| 17 | package common |
| 18 | |
| 19 | import ( |
| 20 | "strings" |
| 21 | "unicode" |
| 22 | ) |
| 23 | |
| 24 | // DocKind indicates the type of documentation element. |
| 25 | type DocKind int |
| 26 | |
| 27 | const ( |
| 28 | // DocEnv represents environment variable documentation. |
| 29 | DocEnv DocKind = iota + 1 |
| 30 | // DocFunction represents function documentation. |
| 31 | DocFunction |
| 32 | // DocOverload represents function overload documentation. |
| 33 | DocOverload |
| 34 | // DocVariable represents variable documentation. |
| 35 | DocVariable |
| 36 | // DocMacro represents macro documentation. |
| 37 | DocMacro |
| 38 | // DocExample represents example documentation. |
| 39 | DocExample |
| 40 | ) |
| 41 | |
| 42 | // Doc holds the documentation details for a specific program element like |
| 43 | // a variable, function, macro, or example. |
| 44 | type Doc struct { |
| 45 | // Kind specifies the type of documentation element (e.g., Function, Variable). |
| 46 | Kind DocKind |
| 47 | |
| 48 | // Name is the identifier of the documented element (e.g., function name, variable name). |
| 49 | Name string |
| 50 | |
| 51 | // Type is the data type associated with the element, primarily used for variables. |
| 52 | Type string |
| 53 | |
| 54 | // Signature represents the function or overload signature. |
| 55 | Signature string |
| 56 | |
| 57 | // Description holds the textual description of the element, potentially spanning multiple lines. |
| 58 | Description string |
| 59 | |
| 60 | // Children holds nested documentation elements, such as overloads for a function |
| 61 | // or examples for a function/macro. |
| 62 | Children []*Doc |
| 63 | } |
| 64 | |
| 65 | // MultilineDescription combines multiple lines into a newline separated string. |
| 66 | func MultilineDescription(lines ...string) string { |
| 67 | return strings.Join(lines, "\n") |
| 68 | } |
| 69 | |
| 70 | // ParseDescription takes a single string containing newline characters and splits |
| 71 | // it into a multiline description. All empty lines will be skipped. |
| 72 | // |
| 73 | // Returns an empty string if the input string is empty. |
| 74 | func ParseDescription(doc string) string { |
| 75 | var lines []string |
| 76 | if len(doc) != 0 { |
| 77 | // Split the input string by newline characters. |
| 78 | for _, line := range strings.Split(doc, "\n") { |
| 79 | l := strings.TrimRightFunc(line, unicode.IsSpace) |
| 80 | if len(l) == 0 { |
| 81 | continue |
| 82 | } |
| 83 | lines = append(lines, l) |
| 84 | } |
| 85 | } |
| 86 | // Return an empty slice if the input is empty. |
| 87 | return MultilineDescription(lines...) |
| 88 | } |
| 89 | |
| 90 | // ParseDescriptions splits a documentation string into multiple multi-line description |
| 91 | // sections, using blank lines as delimiters. |
| 92 | func ParseDescriptions(doc string) []string { |
| 93 | var examples []string |
| 94 | if len(doc) != 0 { |
| 95 | lines := strings.Split(doc, "\n") |
| 96 | lineStart := 0 |
| 97 | for i, l := range lines { |
| 98 | // Trim trailing whitespace to identify effectively blank lines. |
| 99 | l = strings.TrimRightFunc(l, unicode.IsSpace) |
| 100 | // If a line is blank, it marks the end of the current section. |
| 101 | if len(l) == 0 { |
| 102 | // Start the next section after the blank line. |
| 103 | ex := lines[lineStart:i] |
| 104 | if len(ex) != 0 { |
| 105 | examples = append(examples, MultilineDescription(ex...)) |
| 106 | } |
| 107 | lineStart = i + 1 |
| 108 | } |
| 109 | } |
| 110 | // Append the last section if it wasn't terminated by a blank line. |
| 111 | if lineStart < len(lines) { |
| 112 | examples = append(examples, MultilineDescription(lines[lineStart:]...)) |
| 113 | } |
| 114 | } |
| 115 | return examples |
| 116 | } |
| 117 | |
| 118 | // NewVariableDoc creates a new Doc struct specifically for documenting a variable. |
| 119 | func NewVariableDoc(name, celType, description string) *Doc { |
| 120 | return &Doc{ |
| 121 | Kind: DocVariable, |
| 122 | Name: name, |
| 123 | Type: celType, |
| 124 | Description: ParseDescription(description), |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | // NewFunctionDoc creates a new Doc struct for documenting a function. |
| 129 | func NewFunctionDoc(name, description string, overloads ...*Doc) *Doc { |
| 130 | return &Doc{ |
| 131 | Kind: DocFunction, |
| 132 | Name: name, |
| 133 | Description: ParseDescription(description), |
| 134 | Children: overloads, |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | // NewOverloadDoc creates a new Doc struct for a function example. |
| 139 | func NewOverloadDoc(id, signature string, examples ...*Doc) *Doc { |
| 140 | return &Doc{ |
| 141 | Kind: DocOverload, |
| 142 | Name: id, |
| 143 | Signature: signature, |
| 144 | Children: examples, |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | // NewMacroDoc creates a new Doc struct for documenting a macro. |
| 149 | func NewMacroDoc(name, description string, examples ...*Doc) *Doc { |
| 150 | return &Doc{ |
| 151 | Kind: DocMacro, |
| 152 | Name: name, |
| 153 | Description: ParseDescription(description), |
| 154 | Children: examples, |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // NewExampleDoc creates a new Doc struct specifically for holding an example. |
| 159 | func NewExampleDoc(ex string) *Doc { |
| 160 | return &Doc{ |
| 161 | Kind: DocExample, |
| 162 | Description: ex, |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | // Documentor is an interface for types that can provide their own documentation. |
| 167 | type Documentor interface { |
| 168 | // Documentation returns the documentation coded by the DocKind to assist |
| 169 | // with text formatting. |
| 170 | Documentation() *Doc |
| 171 | } |