Sha256: 6893b670e6e79532e619ebcfd577ef6f0c5c750c584cde22d056512cb85904c5

Contents?: true

Size: 1.36 KB

Versions: 6

Compression:

Stored size: 1.36 KB

Contents

require 'set'
require 'strscan'
require 'liquidscript/scanner/base/builder'
require 'liquidscript/scanner/base/context'
require 'liquidscript/scanner/base/dsl'
require 'liquidscript/scanner/base/lexer'

module Liquidscript
  module Scanner
    # TODO: documentation
    class Base

      EXIT = Object.new.freeze

      include Lexer
      include Enumerable

      class << self

        def contexts
          @_contexts ||= Set.new
        end

        attr_accessor :default

      end

      def initialize(source)
        @source  = source
        @scanner = StringScanner.new(@source)
        @tokens  = []
        @_scan   = nil
      end

      def contexts
        self.class.contexts
      end

      def default_context
        self.class.default
      end

      def scan
        lex default_context
        self
      end

      def each
        e = buffer.each

        if block_given?
          e.each(&Proc.new)
        else
          e
        end
      end

      def line
        "(unknown)"
      end

      def column
        "(unkown)"
      end

      def emit(token_name, body = nil)
        @tokens << Token.new(token_name, body, line, column)
      end

      def inspect
        "#<#{self.class}:#{'0x%08x' % object_id}>"
      end

      private

      def buffer
        @_scan ||= begin
          scan
          @tokens
        end
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
liquidscript-0.6.1 lib/liquidscript/scanner/base.rb
liquidscript-0.6.0 lib/liquidscript/scanner/base.rb
liquidscript-0.5.1 lib/liquidscript/scanner/base.rb
liquidscript-0.5.0 lib/liquidscript/scanner/base.rb
liquidscript-0.4.1 lib/liquidscript/scanner/base.rb
liquidscript-0.4.0 lib/liquidscript/scanner/base.rb