Class: Murdoc::Scanner

Inherits:
Object
  • Object
show all
Defined in:
lib/murdoc/scanner.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(language) ⇒ Scanner

Returns a new instance of Scanner.



7
8
9
# File 'lib/murdoc/scanner.rb', line 7

def initialize(language)
  @language = language
end

Instance Attribute Details

#languageObject (readonly)

Returns the value of attribute language.



5
6
7
# File 'lib/murdoc/scanner.rb', line 5

def language
  @language
end

Instance Method Details

#call(source, do_not_count_comment_lines = false) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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
# File 'lib/murdoc/scanner.rb', line 11

def call(source, do_not_count_comment_lines = false)
  paragraphs = []
  ss = StringScanner.new(source)
  line = i = src_line = 0

  loop do
    comment_lines = []
    code_lines = []

    # Multi line comments
    if has_mlc?
      while (ss.scan(mlcb_regex))
        comment = ''

        while (!ss.eos? && !ss.match?(/.*#{mlce_regex}/))
          i += 1
          comment << ss.scan(/.*?$/)
          comment << ss.getch.to_s
        end

        if (fragment = ss.scan(/.*#{mlce_regex}/))
          comment << fragment.sub(mlce_regex, '')
        end

        ss.scan(/[ \t]*\n/) # skip trailing whitespace and a newline
        comment_lines << remove_common_space_prefix(comment)
      end
    end

    # Single line comments
    if has_slc?
      while (ss.scan(slc_regex))
        comment = ''
        comment << ss.scan(/.*?$/)
        comment << ss.getch.to_s
        comment_lines << comment
        i += 1
      end
    end


    # Code
    empty_leading_lines_count = skip_empty_lines(ss)
    i += empty_leading_lines_count
    src_line += empty_leading_lines_count

    line = do_not_count_comment_lines ? src_line : i
    while (!comment_start?(ss) && !ss.eos?)
      code = ss.scan(/^.*?$/)
      code << ss.getch.to_s
      code_lines << code
      i += 1
      src_line += 1
    end

    code = post_process_code(code_lines.join(''))
    comments = post_process_comments(comment_lines.join(''))

    paragraphs << Paragraph.new(code,
                                comments,
                                line,
                                language.name)

    break if ss.eos?
  end

  paragraphs
end