Class: CodeRay::Scanners::JavaScript

Inherits:
Scanner
  • Object
show all
Includes:
CodeRay::Streamable
Defined in:
lib/coderay/scanners/java_script.rb

Constant Summary collapse

KEYWORDS =

The actual JavaScript keywords.

%w[
  break case catch continue default delete do else
  false finally for function if in instanceof new null
  return switch throw true try typeof var void while with
]
MAGIC_VARIABLES =

arguments was introduced in JavaScript 1.4

%w[ this arguments ]
KEYWORDS_EXPECTING_VALUE =
WordList.new.add %w[
  case delete in instanceof new return throw typeof while with
]
RESERVED_WORDS =

Reserved for future use.

%w[
  abstract boolean byte char class debugger double enum export extends
  final float goto implements import int interface long native package
  private protected public short static super synchronized throws transient
  volatile
]
IDENT_KIND =
WordList.new(:ident).
add(RESERVED_WORDS, :reserved).
add(MAGIC_VARIABLES, :local_variable).
add(KEYWORDS, :keyword)
ESCAPE =
/ [bfnrtv\n\\'"] | x[a-fA-F0-9]{1,2} | [0-7]{1,3} /x
UNICODE_ESCAPE =
/ u[a-fA-F0-9]{4} | U[a-fA-F0-9]{8} /x
REGEXP_ESCAPE =
/ [bBdDsSwW] /x
STRING_CONTENT_PATTERN =
{
  "'" => /[^\\']+/,
  '"' => /[^\\"]+/,
  '/' => /[^\\\/]+/,
}
KEY_CHECK_PATTERN =
{
  "'" => / [^\\']* (?: \\.? [^\\']* )* '? \s* : /x,
  '"' => / [^\\"]* (?: \\.? [^\\"]* )* "? \s* : /x,
}

Constants inherited from Scanner

Scanner::DEFAULT_OPTIONS, Scanner::ScanError

Instance Method Summary collapse

Methods inherited from Scanner

#column, #each, file_extension, #initialize, #line, normify, #reset, streamable?, #streaming?, #string=, #tokenize, #tokens

Methods included from Plugin

#helper, #included, #plugin_host, #plugin_id, #register_for, #title

Constructor Details

This class inherits a constructor from CodeRay::Scanners::Scanner

Instance Method Details

#scan_tokens(tokens, options) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/coderay/scanners/java_script.rb', line 50

def scan_tokens tokens, options

  state = :initial
  string_delimiter = nil
  value_expected = true
  key_expected = false

  until eos?

    kind = nil
    match = nil
    
    case state

    when :initial

      if match = scan(/ \s+ | \\\n /x)
        value_expected = true if !value_expected && match.index(?\n)
        tokens << [match, :space]
        next

      elsif scan(%r! // [^\n\\]* (?: \\. [^\n\\]* )* | /\* (?: .*? \*/ | .* ) !mx)
        value_expected = true
        kind = :comment

      elsif check(/\d/)
        key_expected = value_expected = false
        if scan(/0[xX][0-9A-Fa-f]+/)
          kind = :hex
        elsif scan(/(?>0[0-7]+)(?![89.eEfF])/)
          kind = :oct
        elsif scan(/\d+[fF]|\d*\.\d+(?:[eE][+-]?\d+)?[fF]?|\d+[eE][+-]?\d+[fF]?/)
          kind = :float
        elsif scan(/\d+/)
          kind = :integer
        end
        
      elsif match = scan(/ [-+*=<>?:;,!&^|(\[{~%]+ | \.(?!\d) /x)
        value_expected = true
        last_operator = match[-1]
        key_expected = (last_operator == ?{) || (last_operator == ?,)
        kind = :operator

      elsif scan(/ [)\]}]+ /x)
        key_expected = value_expected = false
        kind = :operator

      elsif match = scan(/ [$a-zA-Z_][A-Za-z_0-9$]* /x)
        kind = IDENT_KIND[match]
        value_expected = (kind == :keyword) && KEYWORDS_EXPECTING_VALUE[match]
        if kind == :ident
          if match.index(?$)  # $ allowed inside an identifier
            kind = :predefined
          elsif key_expected && check(/\s*:/)
            kind = :key
          end
        end
        key_expected = false
      
      elsif match = scan(/["']/)
        if key_expected && check(KEY_CHECK_PATTERN[match])
          state = :key
        else
          state = :string
        end
        tokens << [:open, state]
        string_delimiter = match
        kind = :delimiter

      elsif value_expected && (match = scan(/\/(?=\S)/))
        tokens << [:open, :regexp]
        state = :regexp
        string_delimiter = '/'
        kind = :delimiter

      elsif scan(/ \/ /x)
        value_expected = true
        key_expected = false
        kind = :operator

      else
        getch
        kind = :error

      end

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

    else
      raise_inspect 'Unknown state', tokens

    end

    match ||= matched
    if $DEBUG and not kind
      raise_inspect 'Error token %p in line %d' %
        [[match, kind], line], tokens
    end
    raise_inspect 'Empty token', tokens unless match
    
    tokens << [match, kind]

  end

  if [:string, :regexp].include? state
    tokens << [:close, state]
  end

  tokens
end