Class: Rich::BashLexer

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

Overview

Bash/Shell lexer

Constant Summary collapse

KEYWORDS =
%w[
  if then else elif fi case esac for while until do done in
  function return exit break continue local export readonly
  declare typeset source alias unalias
].freeze
BUILTINS =
%w[
  echo printf read cd pwd pushd popd dirs ls cat grep sed awk
  cut sort uniq wc head tail less more find xargs chmod chown
  mkdir rmdir rm cp mv ln touch date time kill ps top df du
  tar gzip gunzip zip unzip curl wget ssh scp rsync git
  sudo su man which whereis whatis type hash history set unset
  shift eval exec test true false
].freeze

Instance Method Summary collapse

Instance Method Details

#tokenize(line, theme) ⇒ Object



1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
# File 'lib/rich/syntax.rb', line 1111

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

    # Comment
    if line[pos] == "#"
      segments << Segment.new(line[pos..], style: theme[:comment])
      break
    end

    # String
    if ['"', "'"].include?(line[pos])
      delim = line[pos]
      str_end = pos + 1
      str_end += 1 while str_end < line.length && !(line[str_end] == delim && line[str_end - 1] != "\\")
      str_end = [str_end, line.length - 1].min
      segments << Segment.new(line[pos..str_end], style: theme[:string])
      pos = str_end + 1
      next
    end

    # Variable
    if line[pos] == "$"
      var_end = pos + 1
      if var_end < line.length && line[var_end] == "{"
        var_end = line.index("}", var_end) || line.length - 1
      else
        var_end += 1 while var_end < line.length && line[var_end].match?(/\w/)
      end
      segments << Segment.new(line[pos..var_end], style: theme[:name_variable] || theme[:name])
      pos = var_end + 1
      next
    end

    # Number
    if line[pos].match?(/\d/)
      num_end = pos
      num_end += 1 while num_end < line.length && line[num_end].match?(/\d/)
      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]
              else
                theme[:name]
              end

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

    # Operators and special chars
    if line[pos].match?(/[|&;<>(){}]/)
      segments << Segment.new(line[pos], style: theme[:operator])
      pos += 1
      next
    end

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

  segments
end