Class: Syntaxer::Reader::DSLReader

Inherits:
Object
  • Object
show all
Defined in:
lib/syntaxer/reader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDSLReader

Returns a new instance of DSLReader.



12
13
14
15
# File 'lib/syntaxer/reader.rb', line 12

def initialize
  @rules = LanguageRules.new
  @ignore_folders = []
end

Instance Attribute Details

#ignored_foldersObject (readonly)

Returns the value of attribute ignored_folders.



10
11
12
# File 'lib/syntaxer/reader.rb', line 10

def ignored_folders
  @ignored_folders
end

#rulesObject (readonly)

Returns the value of attribute rules.



10
11
12
# File 'lib/syntaxer/reader.rb', line 10

def rules
  @rules
end

Class Method Details

.build(skip_default_rules = false, default_rules_file = SYNTAXER_RULES_FILE) ⇒ Object



54
55
56
57
58
# File 'lib/syntaxer/reader.rb', line 54

def build(skip_default_rules = false, default_rules_file = SYNTAXER_RULES_FILE)
  reader = new
  reader.parse(File.read(default_rules_file)) unless skip_default_rules
  reader
end

.load(*dsl_files) ⇒ DSLReader

Read files from params and send to #parse method to fill in LanguageDefinition objects.

Parameters:

  • dsl_files (Array)

    Files with syntaxer rules and language definitions to be used in syntax checking

Returns:

Raises:



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/syntaxer/reader.rb', line 41

def load(*dsl_files)
  reader = build
  dsl_files = [dsl_files].flatten
  dsl_files.each do |file|
    begin
      reader.parse(File.read(file))
    rescue SystemCallError
      raise ::Syntaxer::Reader::DSLFileNotFoundError, "Error reading syntaxer rules file with path '#{file}'!  Please ensure it exists and that it is accessible."
    end
  end
  reader
end

Instance Method Details

#add_rule(rule) ⇒ Object



17
18
19
20
# File 'lib/syntaxer/reader.rb', line 17

def add_rule rule
  @rules << rule
  self
end

#exec_rule(exec_string) ⇒ Object

Create exec rule for language. It may be console string and ruby method

Parameters:

  • (String|Proc)


140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/syntaxer/reader.rb', line 140

def exec_rule(exec_string)
  # if it is string create default console runner
  if !exec_string.respond_to?(:call) || exec_string.is_a?(String)
    current_rule.executor = exec_string.scan(/\w+/).first
    current_rule.exec_existence = system("which #{current_rule.executor} > /dev/null")
    exec_rule = Syntaxer::Runner.default(exec_string)
    current_rule.deferred = false
  else
    # if it is proc call it and pass current rule
    current_rule.exec_existence = true
    current_rule.deferred = true # we have run it after all console checkers
    exec_rule = exec_string.call
  end
  current_rule.exec_rule = exec_rule
end

#extensions(*args) ⇒ Object



128
129
130
# File 'lib/syntaxer/reader.rb', line 128

def extensions(*args)
  current_rule.extensions = args
end

#files_count(syntaxer) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/syntaxer/reader.rb', line 22

def files_count syntaxer
  @rules.map{ |rule|
    if rule.deferred
      0 # skip such files
    else
      rule.files_list(syntaxer.root_path).length
    end
  }.inject(:+)
end

#folders(*args) ⇒ args Also known as: f

Note:

This method won’t check if files are really exist.

Stub for DSL folders method.

Examples:

folders('app/**/*', 'lib/**/*') #=> ['app/**/*', 'lib/**/*']
folders() # => DEFAULT_FILES_MASK

Parameters:

  • args (Array(String))

    File regexps to be assigned for particular language

Returns:

  • (args)

See Also:



124
125
126
# File 'lib/syntaxer/reader.rb', line 124

def folders(*args)
  current_rule.folders = (args.empty? ? [DEFAULT_FILES_MASK] : args.flatten)
end

#ignore_folders(ignore_folders) ⇒ Object



156
157
158
# File 'lib/syntaxer/reader.rb', line 156

def ignore_folders(ignore_folders)
  current_rule.ignore_folders = ignore_folders    
end

#languages(*args) {|langs_name| ... } ⇒ nil Also known as: lang

The languages block executor

Examples:

languages :ruby do
  folders 'app/**/*', 'lib/**/*'
end

Yields:

  • (langs_name)

    Description of block

Returns:

  • (nil)

See Also:



105
106
107
108
109
110
# File 'lib/syntaxer/reader.rb', line 105

def languages(*args, &block)
  args.each do |lang|          
    @current_rule = @rules.find_or_create(lang)
    self.instance_eval(&block)
  end
end

#overall_ignore_folders(*args) ⇒ Object Also known as: ignore



160
161
162
# File 'lib/syntaxer/reader.rb', line 160

def overall_ignore_folders(*args)
  @ignored_folders = args
end

#parse(dsl_data) ⇒ Object

Parses a syntaxer DSL specification from the string given.

Examples:

Syntaxer::Reader::DSLReader.new.parse%|
  syntaxer do
    languages :ruby
      folders 'app/**/*', 'lib/**/*'
    end
  end
| 

Parameters:

  • dsl_data (String)

    Text of actual data to be parsed

Raises:



74
75
76
77
78
# File 'lib/syntaxer/reader.rb', line 74

def parse(dsl_data)
  self.instance_eval(dsl_data)
rescue SyntaxError, NoMethodError, NameError => e
  raise DSLSyntaxError, "Illegal DSL syntax: #{e}"
end

#specific_files(*args) ⇒ Object



132
133
134
# File 'lib/syntaxer/reader.rb', line 132

def specific_files(*args)
  current_rule.specific_files = args
end

#syntaxer { ... } ⇒ Object

The top level block executor

Examples:

syntaxer do
  languages :ruby
    folders 'app/**/*', 'lib/**/*'
  end
end

Yields:

  • Description of block



90
91
92
# File 'lib/syntaxer/reader.rb', line 90

def syntaxer(&block)
  self.instance_eval(&block)
end