Class: CodeRay::Scanners::JavaScript

### Home Classes Methods

### In Files

- lib/coderay/scanners/java_script.rb

### Parent

CodeRay::Scanners::Scanner

### Methods

- #reset_instance
- #scan_tokens
- #setup
- #xml_scanner

### Class/Module Index

Quicksearch

- CodeRay
- CodeRay::Duo
- CodeRay::Encoders
- CodeRay::Encoders::CommentFilter
- CodeRay::Encoders::Count
- CodeRay::Encoders::Debug
- CodeRay::Encoders::DebugLint
- CodeRay::Encoders::Div
- CodeRay::Encoders::Encoder
- CodeRay::Encoders::Filter
- CodeRay::Encoders::HTML
- CodeRay::Encoders::HTML::Output
- CodeRay::Encoders::JSON
- CodeRay::Encoders::LinesOfCode
- CodeRay::Encoders::Lint
- CodeRay::Encoders::Null
- CodeRay::Encoders::Page
- CodeRay::Encoders::Span
- CodeRay::Encoders::Statistic
- CodeRay::Encoders::Terminal
- CodeRay::Encoders::Text
- CodeRay::Encoders::TokenKindFilter
- CodeRay::Encoders::XML
- CodeRay::Encoders::YAML
- CodeRay::FileType
- CodeRay::ForRedCloth
- CodeRay::Plugin
- CodeRay::PluginHost
- CodeRay::Scanners
- CodeRay::Scanners::C
- CodeRay::Scanners::CPlusPlus
- CodeRay::Scanners::CSS
- CodeRay::Scanners::Clojure
- CodeRay::Scanners::Debug
- CodeRay::Scanners::Delphi
- CodeRay::Scanners::Diff
- CodeRay::Scanners::ERB
- CodeRay::Scanners::Go
- CodeRay::Scanners::Groovy
- CodeRay::Scanners::HAML
- CodeRay::Scanners::HTML
- CodeRay::Scanners::JSON
- CodeRay::Scanners::Java
- CodeRay::Scanners::JavaScript
- CodeRay::Scanners::Python
- CodeRay::Scanners::Raydebug
- CodeRay::Scanners::Ruby
- CodeRay::Scanners::SQL
- CodeRay::Scanners::Sass
- CodeRay::Scanners::Scanner
- CodeRay::Scanners::Taskpaper
- CodeRay::Scanners::Text
- CodeRay::Scanners::XML
- CodeRay::Scanners::YAML
- CodeRay::Styles
- CodeRay::Styles::Alpha
- CodeRay::Styles::Style
- CodeRay::Tokens
- CodeRay::TokensProxy
- CodeRay::WordList
- CodeRay::WordList::CaseIgnoring

No matching classes.

# CodeRay::Scanners::JavaScript

Scanner for JavaScript.

Aliases: `ecmascript`, `ecma_script`, `javascript`

### Protected Instance Methods

reset_instance() click to toggle source

    # File lib/coderay/scanners/java_script.rb, line 225
    def reset_instance
      super
      @xml_scanner.reset if defined? @xml_scanner
    end

scan_tokens(encoder, options) click to toggle source

    # File lib/coderay/scanners/java_script.rb, line 61
    def scan_tokens encoder, options

      state, string_delimiter = options[:state] || @state
      if string_delimiter
        encoder.begin_group state
      end

      value_expected = true
      key_expected = false
      function_expected = false

      until eos?

        case state

        when :initial

          if match = scan(/ \s+ | \\\n /)
            value_expected = true if !value_expected && match.index(\n\)
            encoder.text_token match, :space

          elsif match = scan(% // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .*() ) !x)
            value_expected = true
            encoder.text_token match, :comment
            state = :open_multi_line_comment if self[1]

          elsif check(/\.?\d/)
            key_expected = value_expected = false
            if match = scan(/0[xX][0-9A-Fa-f]+/)
              encoder.text_token match, :hex
            elsif match = scan(/(?>0[0-7]+)(?![89.eEfF])/)
              encoder.text_token match, :octal
            elsif match = scan(/\d+[fF]|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
              encoder.text_token match, :float
            elsif match = scan(/\d+/)
              encoder.text_token match, :integer
            end

          elsif value_expected && match = scan(/<([[:alpha:]]\w*) (?: [^\/>]*\/> | .*?<\/\11>>)/im)
            # TODO: scan over nested tags
            xml_scanner.tokenize match, :tokens => encoder
            value_expected = false
            next

          elsif match = scan(/ [-+*=<>?:;,!&^|(\[{~%]+ | \.(?!\d) /)
            value_expected = true
            last_operator = match[-1]
            key_expected = (last_operator == {{) || (last_operator == ,,)
            function_expected = false
            encoder.text_token match, :operator

          elsif match = scan(/ [)\]}]+ /)
            function_expected = key_expected = value_expected = false
            encoder.text_token match, :operator

          elsif match = scan(/ [$a-zA-Z_][A-Za-z_0-9$]* /)
            kind = IDENT_KIND[match]
            value_expected = (kind == :keyword) && KEYWORDS_EXPECTING_VALUE[match]
            # TODO: labels
            if kind == :ident
              if match.index($$)  # $ allowed inside an identifier
                kind = :predefined
              elsif function_expected
                kind = :function
              elsif check(/\s*[=:]\s*function\b/)
                kind = :function
              elsif key_expected && check(/\s*:/)
                kind = :key
              end
            end
            function_expected = (kind == :keyword) && (match == 'function')
            key_expected = false
            encoder.text_token match, kind

          elsif match = scan(/["']/)
            if key_expected && check(KEY_CHECK_PATTERN[match])
              state = :key
            else
              state = :string
            end
            encoder.begin_group state
            string_delimiter = match
            encoder.text_token match, :delimiter

          elsif value_expected && (match = scan(/\//))
            encoder.begin_group :regexp
            state = :regexp
            string_delimiter = '/'
            encoder.text_token match, :delimiter

          elsif match = scan(/ \/ /)
            value_expected = true
            key_expected = false
            encoder.text_token match, :operator

          else
            encoder.text_token getch, :error

          end

        when :string, :regexp, :key
          if match = scan(STRING_CONTENT_PATTERN[string_delimiter])
            encoder.text_token match, :content
          elsif match = scan(/["'\/]/)
            encoder.text_token match, :delimiter
            if state == :regexp
              modifiers = scan(/[gim]+/)
              encoder.text_token modifiers, :modifier if modifiers && !modifiers.empty?
            end
            encoder.end_group state
            string_delimiter = nil
            key_expected = value_expected = false
            state = :initial
          elsif state != :regexp && (match = scan(/ \\ (?: #{ESCAPE} | #{UNICODE_ESCAPE} ) /ox))
            if string_delimiter == "'" && !(match == "\\\\" || match == "\\'")
              encoder.text_token match, :content
            else
              encoder.text_token match, :char
            end
          elsif state == :regexp && match = scan(/ \\ (?: #{ESCAPE} | #{REGEXP_ESCAPE} | #{UNICODE_ESCAPE} ) /ox)
            encoder.text_token match, :char
          elsif match = scan(/\\./)
            encoder.text_token match, :content
          elsif match = scan(/ \\ | $ /)
            encoder.end_group state
            encoder.text_token match, :error unless match.empty?
            string_delimiter = nil
            key_expected = value_expected = false
            state = :initial
          else
            raise_inspect "else case #{string_delimiter} reached; %p not handled." % peek(1), encoder
          end

        when :open_multi_line_comment
          if match = scan(% .*? \*/ !x)
            state = :initial
          else
            match = scan(% .+ !x)
          end
          value_expected = true
          encoder.text_token match, :comment if match

        else
          #:nocov:
          raise_inspect 'Unknown state: %p' % [state], encoder
          #:nocov:

        end

      end

      if options[:keep_state]
        @state = state, string_delimiter
      end

      if [:string, :regexp].include? state
        encoder.end_group state
      end

      encoder
    end

setup() click to toggle source

    # File lib/coderay/scanners/java_script.rb, line 57
    def setup
      @state = :initial
    end

xml_scanner() click to toggle source

    # File lib/coderay/scanners/java_script.rb, line 230
    def xml_scanner
      @xml_scanner ||= CodeRay.scanner :xml, :tokens => @tokens, :keep_tokens => true, :keep_state => false
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.