Skip to content

Commit

Permalink
Fix Regex reference from in Crystal namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Nov 29, 2022
1 parent eb7694d commit ca016bd
Show file tree
Hide file tree
Showing 8 changed files with 25 additions and 30 deletions.
10 changes: 5 additions & 5 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -1001,7 +1001,7 @@ module Crystal
assert_syntax_error "{% unless 1 %} 2 {% elsif 3 %} 3 {% end %}"

it_parses "{{ 1 // 2 }}", MacroExpression.new(Expressions.from([Call.new(1.int32, "//", 2.int32)] of ASTNode))
it_parses "{{ //.options }}", MacroExpression.new(Expressions.from([Call.new(RegexLiteral.new(StringLiteral.new("")), "options")] of ASTNode))
it_parses "{{ //.options }}", MacroExpression.new(Expressions.from([Call.new(::RegexLiteral.new(StringLiteral.new("")), "options")] of ASTNode))

it_parses "[] of Int", ([] of ASTNode).array_of("Int".path)
it_parses "[1, 2] of Int", ([1.int32, 2.int32] of ASTNode).array_of("Int".path)
Expand Down Expand Up @@ -1163,10 +1163,10 @@ module Crystal
it_parses "1.!(\n)", Not.new(1.int32)

it_parses "/foo/", regex("foo")
it_parses "/foo/i", regex("foo", Regex::Options::IGNORE_CASE)
it_parses "/foo/m", regex("foo", Regex::Options::MULTILINE)
it_parses "/foo/x", regex("foo", Regex::Options::EXTENDED)
it_parses "/foo/imximx", regex("foo", Regex::Options::IGNORE_CASE | Regex::Options::MULTILINE | Regex::Options::EXTENDED)
it_parses "/foo/i", regex("foo", ::Regex::Options::IGNORE_CASE)
it_parses "/foo/m", regex("foo", ::Regex::Options::MULTILINE)
it_parses "/foo/x", regex("foo", ::Regex::Options::EXTENDED)
it_parses "/foo/imximx", regex("foo", ::Regex::Options::IGNORE_CASE | ::Regex::Options::MULTILINE | ::Regex::Options::EXTENDED)
it_parses "/fo\\so/", regex("fo\\so")
it_parses "/fo\#{1}o/", RegexLiteral.new(StringInterpolation.new(["fo".string, 1.int32, "o".string] of ASTNode))
it_parses "/(fo\#{\"bar\"}\#{1}o)/", RegexLiteral.new(StringInterpolation.new(["(fo".string, "bar".string, 1.int32, "o)".string] of ASTNode))
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/codegen/codegen.cr
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ module Crystal
when "1"
dump_all_llvm = true
else
dump_llvm_regex = Regex.new(env_dump)
dump_llvm_regex = ::Regex.new(env_dump)
end

@modules.each do |name, info|
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/macros/methods.cr
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ module Crystal
when RegexLiteral
arg_value = arg.value
if arg_value.is_a?(StringLiteral)
regex = Regex.new(arg_value.value, arg.options)
regex = ::Regex.new(arg_value.value, arg.options)
else
raise "regex interpolations not yet allowed in macros"
end
Expand Down Expand Up @@ -685,7 +685,7 @@ module Crystal

regex_value = first.value
if regex_value.is_a?(StringLiteral)
regex = Regex.new(regex_value.value, first.options)
regex = ::Regex.new(regex_value.value, first.options)
else
raise "regex interpolations not yet allowed in macros"
end
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/semantic/literal_expander.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Crystal
class LiteralExpander
def initialize(@program : Program)
@regexes = [] of {String, Regex::Options}
@regexes = [] of {String, ::Regex::Options}
end

# Converts an array literal to creating an Array and storing the values:
Expand Down
4 changes: 2 additions & 2 deletions src/compiler/crystal/syntax/ast.cr
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,9 @@ module Crystal

class RegexLiteral < ASTNode
property value : ASTNode
property options : Regex::Options
property options : ::Regex::Options

def initialize(@value, @options = Regex::Options::None)
def initialize(@value, @options = ::Regex::Options::None)
end

def accept_children(visitor)
Expand Down
21 changes: 8 additions & 13 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2072,7 +2072,7 @@ module Crystal
when .command?
result = Call.new(nil, "`", result).at(location)
when .regex?
if result.is_a?(StringLiteral) && (regex_error = Regex.error?(result.value))
if result.is_a?(StringLiteral) && (regex_error = ::Regex.error?(result.value))
raise "invalid regex: #{regex_error}", location
end

Expand Down Expand Up @@ -2107,7 +2107,7 @@ module Crystal
end

def consume_delimiter(pieces, delimiter_state, has_interpolation)
options = Regex::Options::None
options = ::Regex::Options::None
token_end_location = nil
while true
case @token.type
Expand Down Expand Up @@ -2167,24 +2167,19 @@ module Crystal
end

def consume_regex_options
options = Regex::Options::None
options = ::Regex::Options::None
while true
case current_char
when 'i'
options |= Regex::Options::IGNORE_CASE
next_char
when 'm'
options |= Regex::Options::MULTILINE
next_char
when 'x'
options |= Regex::Options::EXTENDED
next_char
when 'i' then options |= :ignore_case
when 'm' then options |= :multiline
when 'x' then options |= :extended
else
if 'a' <= current_char.downcase <= 'z'
if current_char.ascii_letter?
raise "unknown regex option: #{current_char}"
end
break
end
next_char
end
options
end
Expand Down
10 changes: 5 additions & 5 deletions src/compiler/crystal/syntax/to_s.cr
Original file line number Diff line number Diff line change
Expand Up @@ -983,18 +983,18 @@ module Crystal
case exp = node.value
when StringLiteral
@str << '\\' if exp.value[0]?.try &.ascii_whitespace?
Regex.append_source exp.value, @str
::Regex.append_source exp.value, @str
when StringInterpolation
@str << '\\' if exp.expressions.first?.as?(StringLiteral).try &.value[0]?.try &.ascii_whitespace?
visit_interpolation(exp) { |s| Regex.append_source s, @str }
visit_interpolation(exp) { |s| ::Regex.append_source s, @str }
else
raise "Bug: shouldn't happen"
end
@str << '/'
end
@str << 'i' if node.options.includes? Regex::Options::IGNORE_CASE
@str << 'm' if node.options.includes? Regex::Options::MULTILINE
@str << 'x' if node.options.includes? Regex::Options::EXTENDED
@str << 'i' if node.options.ignore_case?
@str << 'm' if node.options.multiline?
@str << 'x' if node.options.extended?
false
end

Expand Down
2 changes: 1 addition & 1 deletion src/compiler/crystal/tools/print_hierarchy.cr
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ module Crystal
@llvm_typer : LLVMTyper

def initialize(@program : Program, exp : String?)
@exp = exp ? Regex.new(exp) : nil
@exp = exp ? ::Regex.new(exp) : nil
@targets = Set(Type).new
@llvm_typer = @program.llvm_typer
end
Expand Down

0 comments on commit ca016bd

Please sign in to comment.