Sha256: 7696d60cd305b69760789e88d296fd406c1660205e6c3e73f5c7b3503e6fadc9

Contents?: true

Size: 1.76 KB

Versions: 27

Compression:

Stored size: 1.76 KB

Contents

# @Opulent
module Opulent
  # @Parser
  class Parser
    # Check if we have an include node, which will include a new file inside
    # of the current one to be parsed
    #
    # @param parent [Array] Parent node to which we append to
    #
    def include_file(_parent, indent)
      return unless accept :include

      # Process data
      name = accept :line_feed || ''
      name.strip!

      # Check if there is any string after the include input
      Logger.error :parse, @code, @i, @j, :include_end if name.empty?

      # Get the complete file path based on the current file being compiled
      include_path = File.expand_path name, File.dirname(@file[-1][0])

      # Try to see if it has any existing extension, otherwise add .op
      include_path += Settings::FILE_EXTENSION if File.extname(name).empty?

      # Throw an error if the file doesn't exist
      unless Dir[include_path].any?
        Logger.error :parse, @code, @i, @j, :include, name
      end

      # include entire directory tree
      Dir[include_path].each do |file|
        # Skip current file when including from same directory
        next if file == @file[-1][0]

        @file << [include_path, indent]

        # Throw an error if the file doesn't exist
        if File.directory? file
          Logger.error :parse, @code, @i, @j, :include_dir, file
        end

        # Throw an error if the file doesn't exist
        unless File.file? file
          Logger.error :parse, @code, @i, @j, :include, file
        end

        # Indent all lines and prepare them for the parser
        lines = indent_lines File.read(file), ' ' * indent
        lines << ' '

        # Indent all the output lines with the current indentation
        @code.insert @i + 1, *lines.lines
      end

      true
    end
  end
end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
opulent-1.8.5 lib/opulent/parser/include.rb
opulent-1.8.4 lib/opulent/parser/include.rb
opulent-1.8.3 lib/opulent/parser/include.rb
opulent-1.8.2 lib/opulent/parser/include.rb
opulent-1.8.1 lib/opulent/parser/include.rb
opulent-1.8.0 lib/opulent/parser/include.rb
opulent-1.7.11 lib/opulent/parser/include.rb
opulent-1.7.10 lib/opulent/parser/include.rb
opulent-1.7.9 lib/opulent/parser/include.rb
opulent-1.7.8 lib/opulent/parser/include.rb
opulent-1.7.7 lib/opulent/parser/include.rb
opulent-1.7.6 lib/opulent/parser/include.rb
opulent-1.7.5 lib/opulent/parser/include.rb
opulent-1.7.4 lib/opulent/parser/include.rb
opulent-1.7.3 lib/opulent/parser/include.rb
opulent-1.7.2 lib/opulent/parser/include.rb
opulent-1.7.1 lib/opulent/parser/include.rb
opulent-1.7.0 lib/opulent/parser/include.rb
opulent-1.6.9 lib/opulent/parser/include.rb
opulent-1.6.8 lib/opulent/parser/include.rb