OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [go/] [doc/] [example.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors. All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
// Extract example functions from package ASTs.
6
 
7
package doc
8
 
9
import (
10
        "go/ast"
11
        "go/printer"
12
        "strings"
13
        "unicode"
14
        "unicode/utf8"
15
)
16
 
17
type Example struct {
18
        Name   string                 // name of the item being demonstrated
19
        Body   *printer.CommentedNode // code
20
        Output string                 // expected output
21
}
22
 
23
func Examples(pkg *ast.Package) []*Example {
24
        var examples []*Example
25
        for _, src := range pkg.Files {
26
                for _, decl := range src.Decls {
27
                        f, ok := decl.(*ast.FuncDecl)
28
                        if !ok {
29
                                continue
30
                        }
31
                        name := f.Name.Name
32
                        if !isTest(name, "Example") {
33
                                continue
34
                        }
35
                        examples = append(examples, &Example{
36
                                Name: name[len("Example"):],
37
                                Body: &printer.CommentedNode{
38
                                        Node:     f.Body,
39
                                        Comments: src.Comments,
40
                                },
41
                                Output: f.Doc.Text(),
42
                        })
43
                }
44
        }
45
        return examples
46
}
47
 
48
// isTest tells whether name looks like a test, example, or benchmark.
49
// It is a Test (say) if there is a character after Test that is not a
50
// lower-case letter. (We don't want Testiness.)
51
func isTest(name, prefix string) bool {
52
        if !strings.HasPrefix(name, prefix) {
53
                return false
54
        }
55
        if len(name) == len(prefix) { // "Test" is ok
56
                return true
57
        }
58
        rune, _ := utf8.DecodeRuneInString(name[len(prefix):])
59
        return !unicode.IsLower(rune)
60
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.