Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fewer symbols in the compiler source #11625

Merged
14 changes: 10 additions & 4 deletions src/compiler/crystal/semantic/ast.cr
Expand Up @@ -594,21 +594,27 @@ module Crystal
# not when reading it.
property? no_init_flag = false

enum Kind
Class
Instance
Global
end

def kind
case name[0]
when '@'
if name[1] == '@'
:class
Kind::Class
else
:instance
Kind::Instance
end
else
:global
Kind::Global
end
end

def global?
kind == :global
kind.global?
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/bindings.cr
Expand Up @@ -84,7 +84,7 @@ module Crystal
if self.global?
from.raise "global variable '#{self.name}' must be #{freeze_type}, not #{invalid_type}", inner, Crystal::FrozenTypeException
else
from.raise "#{self.kind} variable '#{self.name}' of #{self.owner} must be #{freeze_type}, not #{invalid_type}", inner, Crystal::FrozenTypeException
from.raise "#{self.kind.to_s.downcase} variable '#{self.name}' of #{self.owner} must be #{freeze_type}, not #{invalid_type}", inner, Crystal::FrozenTypeException
straight-shoota marked this conversation as resolved.
Show resolved Hide resolved
end
when Def
(self.return_type || self).raise "method #{self.short_reference} must return #{freeze_type} but it is returning #{invalid_type}", inner, Crystal::FrozenTypeException
Expand Down
21 changes: 11 additions & 10 deletions src/compiler/crystal/syntax/lexer.cr
Expand Up @@ -775,7 +775,8 @@ module Crystal
else
next_char
@token.type = :DELIMITER_START
@token.delimiter_state = Token::DelimiterState.new(delimiter == '`' ? :command : :string, delimiter, delimiter)
delimiter_kind = delimiter == '`' ? Token::DelimiterKind::COMMAND : Token::DelimiterKind::STRING
@token.delimiter_state = Token::DelimiterState.new(delimiter_kind, delimiter, delimiter)
set_token_raw_from_start(start)
end
when '0'..'9'
Expand Down Expand Up @@ -1667,7 +1668,7 @@ module Crystal
string_open_count = delimiter_state.open_count

# For empty heredocs:
if @token.type == :NEWLINE && delimiter_state.kind == :heredoc
if @token.type == :NEWLINE && delimiter_state.kind.heredoc?
if check_heredoc_end delimiter_state
set_token_raw_from_start start
return @token
Expand All @@ -1693,7 +1694,7 @@ module Crystal
@token.delimiter_state = delimiter_state.with_open_count_delta(+1)
when '\\'
if delimiter_state.allow_escapes
if delimiter_state.kind == :regex
if delimiter_state.kind.regex?
char = next_char
raise_unterminated_quoted delimiter_state if char == '\0'
next_char
Expand Down Expand Up @@ -1813,7 +1814,7 @@ module Crystal
@token.line_number = @line_number
@token.column_number = @column_number

if delimiter_state.kind == :heredoc
if delimiter_state.kind.heredoc?
unless check_heredoc_end delimiter_state
next_string_token_noescape delimiter_state
@token.value = string_range(start)
Expand Down Expand Up @@ -1890,11 +1891,11 @@ module Crystal

def raise_unterminated_quoted(delimiter_state)
msg = case delimiter_state.kind
when :command then "Unterminated command literal"
when :regex then "Unterminated regular expression"
when :heredoc
when .command? then "Unterminated command literal"
when .regex? then "Unterminated regular expression"
when .heredoc?
"Unterminated heredoc: can't find \"#{delimiter_state.end}\" anywhere before the end of file"
when :string then "Unterminated string literal"
when .string? then "Unterminated string literal"
else
::raise "unreachable"
end
Expand Down Expand Up @@ -2115,7 +2116,7 @@ module Crystal
delimiter_state = heredocs.shift
end

if delimiter_state && delimiter_state.kind == :heredoc && check_heredoc_end(delimiter_state)
if delimiter_state && delimiter_state.kind.heredoc? && check_heredoc_end(delimiter_state)
char = current_char
delimiter_state = heredocs.try &.shift?
end
Expand Down Expand Up @@ -2528,7 +2529,7 @@ module Crystal
@token.value = value
end

def delimited_pair(kind, string_nest, string_end, start, allow_escapes = true, advance = true)
def delimited_pair(kind : Token::DelimiterKind, string_nest, string_end, start, allow_escapes = true, advance = true)
next_char if advance
@token.type = :DELIMITER_START
@token.delimiter_state = Token::DelimiterState.new(kind, string_nest, string_end, allow_escapes)
Expand Down
24 changes: 12 additions & 12 deletions src/compiler/crystal/syntax/parser.cr
Expand Up @@ -1971,7 +1971,7 @@ module Crystal

check :DELIMITER_START

if delimiter_state.kind == :heredoc
if delimiter_state.kind.heredoc?
if @inside_interpolation
raise "heredoc cannot be used inside interpolation", location
end
Expand All @@ -1989,12 +1989,12 @@ module Crystal

delimiter_state, has_interpolation, options, token_end_location = consume_delimiter pieces, delimiter_state, has_interpolation

if want_skip_space && delimiter_state.kind == :string
if want_skip_space && delimiter_state.kind.string?
while true
passed_backslash_newline = @token.passed_backslash_newline
skip_space

if passed_backslash_newline && @token.type == :DELIMITER_START && @token.delimiter_state.kind == :string
if passed_backslash_newline && @token.type == :DELIMITER_START && @token.delimiter_state.kind.string?
next_string_token(delimiter_state)
delimiter_state = @token.delimiter_state
delimiter_state, has_interpolation, options, token_end_location = consume_delimiter pieces, delimiter_state, has_interpolation
Expand All @@ -2013,9 +2013,9 @@ module Crystal
end

case delimiter_state.kind
when :command
when .command?
result = Call.new(nil, "`", result).at(location)
when :regex
when .regex?
if result.is_a?(StringLiteral) && (regex_error = Regex.error?(result.value))
raise "invalid regex: #{regex_error}", location
end
Expand Down Expand Up @@ -2061,19 +2061,19 @@ module Crystal
next_string_token(delimiter_state)
delimiter_state = @token.delimiter_state
when :DELIMITER_END
if delimiter_state.kind == :regex
if delimiter_state.kind.regex?
options = consume_regex_options
end
token_end_location = token_end_location()
next_token
break
when :EOF
case delimiter_state.kind
when :command
when .command?
raise "Unterminated command"
when :regex
when .regex?
raise "Unterminated regular expression"
when :heredoc
when .heredoc?
raise "Unterminated heredoc"
else
raise "Unterminated string literal"
Expand All @@ -2088,7 +2088,7 @@ module Crystal

# We cannot reduce `StringLiteral` of interpolation inside heredoc into `String`
# because heredoc try to remove its indentation.
if exp.is_a?(StringLiteral) && delimiter_state.kind != :heredoc
if exp.is_a?(StringLiteral) && !delimiter_state.kind.heredoc?
pieces << Piece.new(exp.value, line_number)
else
pieces << Piece.new(exp, line_number)
Expand Down Expand Up @@ -2165,7 +2165,7 @@ module Crystal
end

def needs_heredoc_indent_removed?(delimiter_state)
delimiter_state.kind == :heredoc && delimiter_state.heredoc_indent >= 0
delimiter_state.kind.heredoc? && delimiter_state.heredoc_indent >= 0
end

def remove_heredoc_indent(pieces : Array, indent)
Expand Down Expand Up @@ -2493,7 +2493,7 @@ module Crystal
end

def string_literal_start?
@token.type == :DELIMITER_START && @token.delimiter_state.kind == :string
@token.type == :DELIMITER_START && @token.delimiter_state.kind.string?
end

def parse_tuple(first_exp, location)
Expand Down
17 changes: 13 additions & 4 deletions src/compiler/crystal/syntax/token.cr
Expand Up @@ -33,8 +33,17 @@ module Crystal
setter control_nest
end

enum DelimiterKind
STRING
REGEX
STRING_ARRAY
SYMBOL_ARRAY
COMMAND
HEREDOC
end

record DelimiterState,
kind : Symbol,
kind : DelimiterKind,
nest : Char | String,
end : Char | String,
open_count : Int32,
Expand All @@ -47,15 +56,15 @@ module Crystal
DelimiterState.new(:string, '\0', '\0', 0, 0, true)
end

def self.new(kind, nest, the_end)
def self.new(kind : DelimiterKind, nest, the_end)
new kind, nest, the_end, 0, 0, true
end

def self.new(kind, nest, the_end, allow_escapes : Bool)
def self.new(kind : DelimiterKind, nest, the_end, allow_escapes : Bool)
new kind, nest, the_end, 0, 0, allow_escapes
end

def self.new(kind, nest, the_end, open_count : Int32)
def self.new(kind : DelimiterKind, nest, the_end, open_count : Int32)
new kind, nest, the_end, open_count, 0, true
end

Expand Down
15 changes: 7 additions & 8 deletions src/compiler/crystal/tools/doc/markd_doc_renderer.cr
Expand Up @@ -88,22 +88,22 @@ class Crystal::Doc::MarkdDocRenderer < Markd::HTMLRenderer
end

type_name = $1.presence
kind = $2 == "#" ? :instance : :class
instance_methods_first = $2 == "#"
method_name = $3
method_args = $4? || ""

if type_name
# Type#method(...)
another_type = @type.lookup_path(type_name)
if another_type && @type.must_be_included?
method = lookup_method another_type, method_name, method_args, kind
method = lookup_method another_type, method_name, method_args, instance_methods_first
if method
next method_link method, match_text
end
end
else
# #method(...)
method = lookup_method @type, method_name, method_args, kind
method = lookup_method @type, method_name, method_args, instance_methods_first
if method && method.must_be_included?
next method_link method, match_text
end
Expand Down Expand Up @@ -138,7 +138,7 @@ class Crystal::Doc::MarkdDocRenderer < Markd::HTMLRenderer
%(<a href="#{method.type.path_from(@type)}#{method.anchor}">#{text}</a>)
end

private def lookup_method(type, name, args, kind = nil)
private def lookup_method(type, name, args, instance_methods_first = true)
case args
when ""
args_count = nil
Expand All @@ -149,11 +149,10 @@ class Crystal::Doc::MarkdDocRenderer < Markd::HTMLRenderer
end

base_match =
case kind
when :class
type.lookup_class_method(name, args_count) || type.lookup_method(name, args_count)
else
if instance_methods_first
type.lookup_method(name, args_count) || type.lookup_class_method(name, args_count)
else
type.lookup_class_method(name, args_count) || type.lookup_method(name, args_count)
end
base_match ||
type.lookup_macro(name, args_count) ||
Expand Down
24 changes: 12 additions & 12 deletions src/compiler/crystal/tools/doc/type.cr
Expand Up @@ -12,23 +12,23 @@ class Crystal::Doc::Type
def kind
case @type
when Const
:const
"const"
when .struct?
:struct
"struct"
when .class?, .metaclass?
:class
"class"
when .module?
:module
"module"
when AliasType
:alias
"alias"
when EnumType
:enum
"enum"
when NoReturnType, VoidType
:struct
"struct"
when AnnotationType
:annotation
"annotation"
when LibType
:module
"module"
else
raise "Unhandled type in `kind`: #{@type}"
end
Expand Down Expand Up @@ -133,15 +133,15 @@ class Crystal::Doc::Type
end

def enum?
kind == :enum
@type.is_a?(EnumType)
end

def alias?
kind == :alias
@type.is_a?(AliasType)
end

def const?
kind == :const
@type.is_a?(Const)
end

def alias_definition
Expand Down
8 changes: 4 additions & 4 deletions src/compiler/crystal/tools/formatter.cr
Expand Up @@ -470,7 +470,7 @@ module Crystal
end

check :DELIMITER_START
is_regex = @token.delimiter_state.kind == :regex
is_regex = @token.delimiter_state.kind.regex?

write @token.raw
next_string_token
Expand Down Expand Up @@ -522,7 +522,7 @@ module Crystal
end

def visit(node : StringInterpolation)
if @token.delimiter_state.kind == :heredoc
if @token.delimiter_state.kind.heredoc?
# For heredoc, only write the start: on a newline will print it
@lexer.heredocs << {@token.delimiter_state, HeredocInfo.new(node, @token.dup, @line, @column, @indent, @string_continuation)}
write @token.raw
Expand All @@ -538,14 +538,14 @@ module Crystal
def visit_string_interpolation(node, token, line, column, old_indent, old_string_continuation, wrote_token = false)
@token = token

is_regex = token.delimiter_state.kind == :regex
is_regex = token.delimiter_state.kind.regex?
indent_difference = token.column_number - (column + 1)

write token.raw unless wrote_token
next_string_token

delimiter_state = token.delimiter_state
is_heredoc = token.delimiter_state.kind == :heredoc
is_heredoc = token.delimiter_state.kind.heredoc?
@last_is_heredoc = is_heredoc

heredoc_line = @line
Expand Down
4 changes: 2 additions & 2 deletions src/crystal/syntax_highlighter.cr
Expand Up @@ -78,7 +78,7 @@ abstract class Crystal::SyntaxHighlighter

case token.type
when :DELIMITER_START
if token.delimiter_state.kind == :heredoc
if token.delimiter_state.kind.heredoc?
heredoc_stack << token.dup
highlight_token token, last_is_def
else
Expand Down Expand Up @@ -210,7 +210,7 @@ abstract class Crystal::SyntaxHighlighter
render :STRING_ARRAY_END, token.raw
break
when :EOF
if token.delimiter_state.kind == :string_array
if token.delimiter_state.kind.string_array?
raise "Unterminated string array literal"
else # == :symbol_array
raise "Unterminated symbol array literal"
Expand Down