Class: YARDGo::Parser::Go

Inherits:
YARD::Parser::Base
  • Object
show all
Includes:
YARD, YARD::CodeObjects
Defined in:
lib/yard-go/parser.rb

Constant Summary collapse

@@matches =
{
  comment: %r{^\s*//(.*)},
  package: /^package (\w+)/,
  function: /^func (\w+)\((.*?)\)(.*?)\{/,
  struct: /^type (\w+) struct\s+\{/,
  interface: /^type (\w+) interface\s+\{/,
  method: /^func \(\w+\s+\*?(.+?)\) (\w+)\((.*?)\)(.*)\{/,
  close_brace: /^\}/,
  close_paren: /^\)/,
  single_var: /^(var|const)\s+(.+?)\s*=\s*(.+)/,
  multi_var: /^(var|const)\s*\(/
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, file) ⇒ Go

Returns a new instance of Go.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yard-go/parser.rb', line 31

def initialize(source, file)
  YARD::CodeObjects.send(:remove_const, :NSEP)
  YARD::CodeObjects.const_set(:NSEP, '/')
  YARD::CodeObjects.send(:remove_const, :NSEPQ)
  YARD::CodeObjects.const_set(:NSEPQ, '/')
  YARD::CodeObjects.send(:remove_const, :ISEP)
  YARD::CodeObjects.const_set(:ISEP, '.')
  YARD::CodeObjects.send(:remove_const, :ISEPQ)
  YARD::CodeObjects.const_set(:ISEPQ, '.')
  @lines = source.split(/\r?\n/)
  @file = file
  @svc_docs = {}
  @ast = []
  @structs = {}
  @lineno = 0
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



29
30
31
# File 'lib/yard-go/parser.rb', line 29

def file
  @file
end

Instance Method Details

#enumeratorObject



27
# File 'lib/yard-go/parser.rb', line 27

def enumerator; @ast end

#parseObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/yard-go/parser.rb', line 48

def parse
  clear_comments
  consume_until do
    case line
    when @@matches[:comment]
      add_comment($1)

    when @@matches[:package]
      @ast << s(:package, package_name: $1)

    when @@matches[:function]
      t = s(:function, name: $1, args: $2, ret: $3)
      attach_source(t)
      @ast << t

    when @@matches[:struct]
      t = init_struct($1)
      consume_fields(t)

    when @@matches[:interface]
      t = s(:interface, name: $1, fields: [])
      consume_fields(t)
      @ast << t

    when @@matches[:method]
      t = init_struct($1)
      m = s(:method, type: $1, name: $2, args: $3, ret: $4)
      attach_source(m)
      t.meths.push(m)

    when @@matches[:single_var]
      type, lhs, src = $1.to_sym, $2, [$3]
      name, vartype = *lhs.split(/\s+/)
      
      @lineno += 1
      consume_until %r{^(\w|\/\/)} do
        src << line
      end
      @lineno -= 1

      @ast << s(type, name: name, vartype: vartype, value: src.join("\n"))

    when @@matches[:multi_var]
      type = $1.to_sym
      clear_comments

      @lineno += 1
      consume_until @@matches[:close_paren] do
        case line
        when @@matches[:comment]
          add_comment($1)
        when /^(\s*)(.+?)\s*=\s*(.+)/m
          mindent, lhs, src = $1.length, $2, [$3]
          name, vartype = *lhs.split(/\s+/)
          
          @lineno += 1
          consume_until @@matches[:close_paren] do
            indent = (line[/^(\s+)/, 1] || "").length
            break if indent < mindent
            break if indent == mindent && line =~ /^\s+(\w|\/\/)/
            src << line
          end
          @lineno -= 1

          @ast << s(type, name: name, vartype: vartype, value: src.join("\n"))
        end
      end

    else
      if @comments.size > 0
        @ast << s(:comment)
      else
        clear_comments
      end
    end
  end

  @ast += @structs.values

  self
end

#tokenizeObject



26
# File 'lib/yard-go/parser.rb', line 26

def tokenize; [] end