Sha256: c610b8ea21ea9a9768eb199044e000b317984edbe6ba81675262347a3fbd92c5

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

module Liquidscript
  module Scanner
    class Base
      module Lexer

        def perform_with_context(context, scanner = @scanner)
          key, value = context.find_matcher(scanner)

          if value.nil? && scanner.rest?
            error scanner
          end

          normalize_action key, value, scanner if scanner.rest?
        end

        def lex(argument)
          context, body =
          if argument.is_a?(Hash)
            argument.to_a.first
          else
            argument
          end

          scanner = if body
            StringScanner.new(body)
          else
            @scanner
          end
          out = []

          context = find_context(context)
          instance_exec &context.init

          while scanner.rest? && out.last != EXIT
            out << perform_with_context(context, scanner)
          end

          out
        end

        def error(scanner = @scanner)
          raise SyntaxError, "Unexpected #{scanner.peek(2).inspect}" \
            " (line: #{line}, column: #{column})"
        end

        private

        def find_context(name)
          context = contexts.dup.keep_if { |c|
            c.name == name
          }.to_a.first

          if context.nil?
            raise NoContextError.new(name)
          end

          context
        end

        def exit
          EXIT
        end

        def normalize_action(key, value, scanner)
          body = scanner.scan(key)

          case value
          when Proc
            instance_exec(*body.match(key), &value)
          when Symbol
            lex value
          when EXIT
            EXIT
          end
        end

      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
liquidscript-0.4.1 lib/liquidscript/scanner/base/lexer.rb
liquidscript-0.4.0 lib/liquidscript/scanner/base/lexer.rb