Class: Rich::YAMLLexer
Overview
YAML Lexer
Instance Method Summary collapse
Instance Method Details
#tokenize(line, theme) ⇒ Object
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 |
# File 'lib/rich/syntax.rb', line 1015 def tokenize(line, theme) segments = [] pos = 0 while pos < line.length # Comment if line[pos] == "#" segments << Segment.new(line[pos..], style: theme[:comment]) break end # Key (before colon) if pos == 0 || line[0...pos].match?(/^\s*$/) colon_pos = line.index(":") if colon_pos key = line[0...colon_pos] segments << Segment.new(key, style: theme[:name]) segments << Segment.new(":", style: theme[:punctuation]) pos = colon_pos + 1 next end end 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 # String if ['"', "'"].include?(line[pos]) delim = line[pos] str_end = pos + 1 str_end += 1 while str_end < line.length && line[str_end] != delim str_end = [str_end, line.length - 1].min segments << Segment.new(line[pos..str_end], style: theme[:string]) pos = str_end + 1 next end # Boolean/null rest = line[pos..].downcase if rest.start_with?("true") || rest.start_with?("false") || rest.start_with?("null") || rest.start_with?("yes") || rest.start_with?("no") word_end = pos word_end += 1 while word_end < line.length && line[word_end].match?(/\w/) segments << Segment.new(line[pos...word_end], style: theme[:keyword_constant] || theme[:keyword]) pos = word_end 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 # List marker if line[pos] == "-" && (pos + 1 >= line.length || line[pos + 1].match?(/\s/)) segments << Segment.new("-", style: theme[:punctuation]) pos += 1 next end # Default text word_end = pos word_end += 1 while word_end < line.length && !line[word_end].match?(/[\s#]/) segments << Segment.new(line[pos...word_end], style: theme[:string]) pos = word_end end segments end |