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

Let fun names and method calls start with Uppercase #4934

Merged
merged 2 commits into from
Sep 7, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions spec/compiler/parser/parser_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -705,7 +705,10 @@ describe "Parser" do
it_parses "lib LibC\nalias Foo = Bar\nend", LibDef.new("LibC", [Alias.new("Foo", "Bar".path)] of ASTNode)
it_parses "lib LibC; struct Foo; include Bar; end; end", LibDef.new("LibC", [CStructOrUnionDef.new("Foo", Include.new("Bar".path))] of ASTNode)

it_parses "lib LibC\nfun SomeFun\nend", LibDef.new("LibC", [FunDef.new("SomeFun")] of ASTNode)

it_parses "fun foo(x : Int32) : Int64\nx\nend", FunDef.new("foo", [Arg.new("x", restriction: "Int32".path)], "Int64".path, body: "x".var)
assert_syntax_error "fun Foo : Int64\nend"

it_parses "lib LibC; {{ 1 }}; end", LibDef.new("LibC", body: [MacroExpression.new(1.int32)] of ASTNode)
it_parses "lib LibC; {% if 1 %}2{% end %}; end", LibDef.new("LibC", body: [MacroIf.new(1.int32, MacroLiteral.new("2"))] of ASTNode)
Expand Down Expand Up @@ -1297,6 +1300,8 @@ describe "Parser" do

it_parses "1 ** -x", Call.new(1.int32, "**", Call.new("x".call, "-"))

it_parses "foo.Bar", Call.new("foo".call, "Bar")

assert_syntax_error "return do\nend", "unexpected token: do"

%w(def macro class struct module fun alias abstract include extend lib).each do |keyword|
Expand Down
24 changes: 18 additions & 6 deletions src/compiler/crystal/syntax/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ module Crystal
end
end

AtomicWithMethodCheck = [:IDENT, :"+", :"-", :"*", :"/", :"%", :"|", :"&", :"^", :"**", :"<<", :"<", :"<=", :"==", :"!=", :"=~", :"!~", :">>", :">", :">=", :"<=>", :"===", :"[]", :"[]=", :"[]?", :"["]
AtomicWithMethodCheck = [:IDENT, :CONST, :"+", :"-", :"*", :"/", :"%", :"|", :"&", :"^", :"**", :"<<", :"<", :"<=", :"==", :"!=", :"=~", :"!~", :">>", :">", :">=", :"<=>", :"===", :"[]", :"[]=", :"[]?", :"["]

def parse_atomic_with_method
location = @token.location
Expand Down Expand Up @@ -619,7 +619,12 @@ module Crystal
elsif @token.type == :"["
return parse_atomic_method_suffix(atomic, location)
else
name = @token.type == :IDENT ? @token.value.to_s : @token.type.to_s
name = case @token.type
when :IDENT, :CONST
@token.value.to_s
else
@token.type.to_s
end
end_location = token_end_location

@wants_regex = false
Expand Down Expand Up @@ -1026,7 +1031,7 @@ module Crystal
when :fun
check_type_declaration do
check_not_inside_def("can't define fun inside def") do
parse_fun_def require_body: true
parse_fun_def top_level: true, require_body: true
end
end
when :alias
Expand Down Expand Up @@ -4864,7 +4869,7 @@ module Crystal
when :alias
parse_alias
when :fun
parse_fun_def
parse_fun_def(top_level: false)
when :type
parse_type_def
when :struct
Expand Down Expand Up @@ -4918,14 +4923,21 @@ module Crystal

IdentOrConst = [:IDENT, :CONST]

def parse_fun_def(require_body = false)
def parse_fun_def(top_level, require_body = false)
location = @token.location
doc = @token.doc

push_def if require_body

next_token_skip_space_or_newline
name = check_ident

name = if top_level
check_ident
else
check IdentOrConst
@token.value.to_s
end

next_token_skip_space_or_newline

if @token.type == :"="
Expand Down