Skip to content

Commit

Permalink
Merge pull request #3633 from masatake/ruby--handle-curly-bracket
Browse files Browse the repository at this point in the history
Ruby:  handle curly bracket
  • Loading branch information
masatake authored Feb 6, 2023
2 parents 1901123 + ac7ef35 commit 3cd1723
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 4 deletions.
2 changes: 2 additions & 0 deletions Units/parser-ruby.r/ruby-curly-brackets.d/args.ctags
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--sort=no
--fields=+{end}{signature}
11 changes: 11 additions & 0 deletions Units/parser-ruby.r/ruby-curly-brackets.d/expected.tags
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
__anona00f52fc0100 input.rb /^Class.new {$/;" c end:5
f input.rb /^ def f(n)$/;" S class:__anona00f52fc0100 signature:(n) end:4
x input.rb /^def x$/;" f signature:() end:9
__anone0f8ea590100 input-0.rb /^Class.new do$/;" c end:3
f0 input-0.rb /^ def f0(n) n end$/;" S class:__anone0f8ea590100 signature:(n) end:2
y input-0.rb /^def y$/;" f signature:() end:7
C input-1.rb /^class C$/;" c end:5
f1 input-1.rb /^ def f1(n) n end$/;" S class:C signature:(n) end:3
z input-1.rb /^def z$/;" f signature:() end:9
__anone0fa031b0100 input-2.rb /^Class.new {$/;" c end:3
f2 input-2.rb /^ def f2() end$/;" S class:__anone0fa031b0100 signature:() end:2
8 changes: 8 additions & 0 deletions Units/parser-ruby.r/ruby-curly-brackets.d/input-0.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Class.new do
def f0(n) n end
end

def y
y
end

10 changes: 10 additions & 0 deletions Units/parser-ruby.r/ruby-curly-brackets.d/input-1.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class C
class << self
def f1(n) n end
end
end

def z
z
end

3 changes: 3 additions & 0 deletions Units/parser-ruby.r/ruby-curly-brackets.d/input-2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Class.new {
def f2() end
}
9 changes: 9 additions & 0 deletions Units/parser-ruby.r/ruby-curly-brackets.d/input.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Class.new {
def f(n)
n
end
}

def x
x
end
28 changes: 24 additions & 4 deletions parsers/ruby.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ static vString* nestingLevelsToScope (const NestingLevels* nls)
{
NestingLevel *nl = nestingLevelsGetNthFromRoot (nls, i);
tagEntryInfo *e = getEntryOfNestingLevel (nl);
if (e && strlen (e->name) > 0 && (!e->placeholder))
if (e && (*e->name != '\0') && (!e->placeholder))
{
if (chunks_output++ > 0)
vStringPut (result, SCOPE_SEPARATOR);
Expand Down Expand Up @@ -917,7 +917,18 @@ static int readAndEmitDef (const unsigned char **cp)
* end
* end
*/
if (e_scope && e_scope->kindIndex == K_CLASS && strlen (e_scope->name) == 0)
if (e_scope && e_scope->kindIndex == K_CLASS
&& (
/* Class.new do
... in this case, an anonymous class is created and
... pushed. */
isTagExtraBitMarked (e_scope, XTAG_ANONYMOUS)
||
/* class <<
... in this case, a placeholder tag having an empty
... name is created and pushed. */
*(e_scope->name) == '\0'
))
kind = K_SINGLETON;
int corkIndex = readAndEmitTag (cp, kind);
tagEntryInfo *e = getEntryInCorkQueue (corkIndex);
Expand Down Expand Up @@ -1060,7 +1071,10 @@ static void findRubyTags (void)

int r;
if (*(cp - 1) != 's')
{
r = emitRubyTagFull(NULL, K_CLASS, true, false);
expect_separator = true;
}
else
r = readAndEmitTag (&cp, K_CLASS); /* "class" */

Expand Down Expand Up @@ -1183,8 +1197,11 @@ static void findRubyTags (void)
{
enterUnnamedScope ();
}
else if (canMatchKeyword (&cp, "do"))
else if (canMatchKeyword (&cp, "do") || (*cp == '{'))
{
if (*cp == '{')
++cp;

if (! expect_separator)
{
enterUnnamedScope ();
Expand All @@ -1194,8 +1211,11 @@ static void findRubyTags (void)
else
expect_separator = false;
}
else if (canMatchKeyword (&cp, "end") && nesting->n > 0)
else if ((canMatchKeyword (&cp, "end") || (*cp == '}')) && nesting->n > 0)
{
if (*cp == '}')
++cp;

/* Leave the most recent scope. */
nestingLevelsPop (nesting);
}
Expand Down

0 comments on commit 3cd1723

Please sign in to comment.