Class: Rich::JSONLexer
Overview
JSON Lexer (simple)
Instance Method Summary collapse
Instance Method Details
#tokenize(line, theme) ⇒ Object
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 |
# File 'lib/rich/syntax.rb', line 943 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 # String if line[pos] == '"' str_end = pos + 1 str_end += 1 while str_end < line.length && !(line[str_end] == '"' && line[str_end - 1] != "\\") str_end = [str_end, line.length - 1].min content = line[pos..str_end] # Check if it's a key (followed by :) rest = line[str_end + 1..].lstrip is_key = rest.start_with?(":") segments << Segment.new(content, style: is_key ? theme[:name] : theme[:string]) pos = str_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.eE+\-]/) segments << Segment.new(line[pos...num_end], style: theme[:number]) pos = num_end next end # Boolean/null if line[pos].match?(/[tfn]/) if line[pos..pos + 3] == "true" segments << Segment.new("true", style: theme[:keyword_constant] || theme[:keyword]) pos += 4 next elsif line[pos..pos + 4] == "false" segments << Segment.new("false", style: theme[:keyword_constant] || theme[:keyword]) pos += 5 next elsif line[pos..pos + 3] == "null" segments << Segment.new("null", style: theme[:keyword_constant] || theme[:keyword]) pos += 4 next end 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 |