Class: Rich::JavaScriptLexer

Inherits:
BaseLexer show all
Defined in:
lib/rich/syntax.rb

Overview

JavaScript lexer

Constant Summary collapse

KEYWORDS =
%w[
  async await break case catch class const continue debugger default
  delete do else export extends finally for function if import in
  instanceof let new return static super switch this throw try typeof
  var void while with yield true false null undefined
].freeze
BUILTINS =
%w[
  Array Boolean Date Error Function JSON Math Number Object Promise
  RegExp String Symbol Map Set WeakMap WeakSet Proxy Reflect
  console window document parseInt parseFloat isNaN isFinite
  decodeURI decodeURIComponent encodeURI encodeURIComponent eval
  setTimeout setInterval clearTimeout clearInterval fetch
].freeze

Instance Method Summary collapse

Instance Method Details

#tokenize(line, theme) ⇒ Object



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
# File 'lib/rich/syntax.rb', line 683

def tokenize(line, theme)
  segments = []
  pos = 0

  while pos < line.length
    if line[pos].match?(/\s/)
      ws_end = pos
      ws_end += 1 while ws_end < line.length && line[ws_end].match?(/\s/)
      segments << Segment.new(line[pos...ws_end])
      pos = ws_end
      next
    end

    # Single-line comment
    if line[pos..pos + 1] == "//"
      segments << Segment.new(line[pos..], style: theme[:comment])
      break
    end

    # Template literal
    if line[pos] == "`"
      str_end = find_string_end(line, pos, "`")
      segments << Segment.new(line[pos..str_end], style: theme[:string])
      pos = str_end + 1
      next
    end

    # String
    if ['"', "'"].include?(line[pos])
      delim = line[pos]
      str_end = find_string_end(line, pos, delim)
      segments << Segment.new(line[pos..str_end], style: theme[:string])
      pos = str_end + 1
      next
    end

    # Regex
    if line[pos] == "/" && (pos == 0 || line[pos - 1].match?(/[\s=({,\[]/)) &&
       pos + 1 < line.length && line[pos + 1] != " "
      regex_end = find_closing_delimiter(line, pos, "/")
      if regex_end
        # Include flags
        regex_end += 1 while regex_end + 1 < line.length && line[regex_end + 1].match?(/[gimsuy]/)
        segments << Segment.new(line[pos..regex_end], style: theme[:string_regex] || theme[:string])
        pos = regex_end + 1
        next
      end
    end

    # Number
    if line[pos].match?(/\d/) || (line[pos] == "." && pos + 1 < line.length && line[pos + 1].match?(/\d/))
      num_end = pos
      num_end += 1 while num_end < line.length && line[num_end].match?(/[\d._xXoObBeEnN]/)
      segments << Segment.new(line[pos...num_end], style: theme[:number])
      pos = num_end
      next
    end

    # Identifier
    if line[pos].match?(/[a-zA-Z_$]/)
      word_end = pos
      word_end += 1 while word_end < line.length && line[word_end].match?(/[\w$]/)
      word = line[pos...word_end]

      style = if KEYWORDS.include?(word)
                theme[:keyword]
              elsif BUILTINS.include?(word)
                theme[:name_builtin] || theme[:name]
              elsif word[0].match?(/[A-Z]/)
                theme[:name_class] || theme[:name]
              else
                theme[:name]
              end

      segments << Segment.new(word, style: style)
      pos = word_end
      next
    end

    # Arrow function
    if line[pos..pos + 1] == "=>"
      segments << Segment.new("=>", style: theme[:operator])
      pos += 2
      next
    end

    # Operators
    if line[pos].match?(/[+\-*\/%&|^~<>=!?:]/)
      op_end = pos + 1
      op_end += 1 while op_end < line.length && line[op_end].match?(/[+\-*\/%&|^~<>=!?:]/)
      segments << Segment.new(line[pos...op_end], style: theme[:operator])
      pos = op_end
      next
    end

    # Punctuation
    if line[pos].match?(/[(){}\[\].,;]/)
      segments << Segment.new(line[pos], style: theme[:punctuation])
      pos += 1
      next
    end

    segments << Segment.new(line[pos])
    pos += 1
  end

  segments
end