Class: SportDb::Parser::Linter

Inherits:
Object
  • Object
show all
Defined in:
lib/fbtok/linter.rb

Overview

note - Linter for now nested inside Parser - keep? why? why not?

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLinter

Returns a new instance of Linter.



21
22
23
# File 'lib/fbtok/linter.rb', line 21

def initialize
  @errors = []
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



19
20
21
# File 'lib/fbtok/linter.rb', line 19

def errors
  @errors
end

Class Method Details

.debug=(value) ⇒ Object



9
# File 'lib/fbtok/linter.rb', line 9

def self.debug=(value) @@debug = value; end

.debug?Boolean

note: default is FALSE

Returns:

  • (Boolean)


10
# File 'lib/fbtok/linter.rb', line 10

def self.debug?()      @@debug ||= false; end

.warn=(value) ⇒ Object

turn warn(ings) into errors (default is false)



14
# File 'lib/fbtok/linter.rb', line 14

def self.warn=(value) @@warn = value; end

.warn?Boolean

note: default is FALSE

Returns:

  • (Boolean)


15
# File 'lib/fbtok/linter.rb', line 15

def self.warn?()      @@warn ||= false; end

Instance Method Details

#debug?Boolean

Returns:

  • (Boolean)


11
# File 'lib/fbtok/linter.rb', line 11

def debug?()  self.class.debug?; end

#errors?Boolean

Returns:

  • (Boolean)


26
# File 'lib/fbtok/linter.rb', line 26

def errors?() @errors.size > 0; end

#read(path, parse: true) ⇒ Object

parse - false (default) - tokenize (only)

- true            - tokenize & parse


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
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
# File 'lib/fbtok/linter.rb', line 33

def read( path, parse: true )
  ## note: every (new) read call - resets errors list to empty
  @errors = []
  @tree   = []

  outline = QuickMatchOutline.read( path )

  outline.each_para do |lines|
  
       ## flatten lines (array of strings) into all-in-one string
       txt  = lines.reduce( String.new ) do |mem, line|
         mem << line
         mem << "\n"
         mem
       end

     if parse
       if debug?
         puts "lines:"
         pp txt   
       end
 
       ##   pass along debug flag to parser (& tokenizer)?
       parser = RaccMatchParser.new( txt )   ## use own parser instance (not shared) - why? why not?
       tree, errors = parser.parse_with_errors

         if errors.size > 0
            ## add to "global" error list
            ##   make a triplet tuple (file / msg / line text)
            errors.each do |msg|
                @errors << [path, msg]
            end
         end

       if debug?
         puts "parse tree:"
         pp tree  
       end

       @tree += tree   ## add nodes

     else   ## process for tokenize only

        if debug?
          puts "lines:"
          pp txt   
        end

##
##   add (bakc) a line-by-line tracing (debug) option - why? why not?
##      now debug output is by section (not line-by-line)

        lexer = Lexer.new( txt )
        t, errors  =  lexer.tokenize_with_errors
                            
         if errors.size > 0
            ## add to "global" error list
            ##   make a triplet tuple (file / msg / line text)
            errors.each do |msg|
                @errors << [path, msg]
            end
         end

         if debug?
           puts "tokens:"
           pp t   
         end

         @tree += t   ## add tokens to "tree"
      end   # parse? (or tokenize?) 
   end  # each para (node)


   ##
   ## auto-add error if no tokens/tree nodes
   ##   note - do NOT report empty file error by default 
   if @tree.empty? && warn?  ## @tree.size == 0
      @errors << [path, "empty; no #{parse ? 'parse tree nodes' : 'tokens'}"]
   end

   @tree   ## return parse tree 
end

#warn?Boolean

Returns:

  • (Boolean)


16
# File 'lib/fbtok/linter.rb', line 16

def warn?()   self.class.warn?; end